From 0b6dcde04302bbe0364a22933fb0de1e4a046799 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Tue, 18 Aug 2020 13:00:44 -0700 Subject: [PATCH] add example RPC server/client --- example/Makefile | 6 +++ example/client.cpp | 2 + example/mailbox.cpp | 2 +- example/rpc_client.cpp | 49 +++++++++++++++++++++++ example/rpc_server.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++ example/spam.cpp | 2 +- example/ticker.cpp | 2 +- 7 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 example/rpc_client.cpp create mode 100644 example/rpc_server.cpp diff --git a/example/Makefile b/example/Makefile index 9fa2e74..0b418f1 100644 --- a/example/Makefile +++ b/example/Makefile @@ -23,6 +23,12 @@ ticker_SRCS += ticker.cpp TESTPROD_HOST += client client_SRCS += client.cpp +TESTPROD_HOST += rpc_server +rpc_server_SRCS += rpc_server.cpp + +TESTPROD_HOST += rpc_client +rpc_client_SRCS += rpc_client.cpp + #=========================== include $(TOP)/configure/RULES diff --git a/example/client.cpp b/example/client.cpp index 391fdd3..3949820 100644 --- a/example/client.cpp +++ b/example/client.cpp @@ -109,4 +109,6 @@ int main(int argc, char* argv[]) std::cout<<"Result is:\n"< + +#include +#include + +using namespace pvxs; + +int main(int argc, char* argv[]) +{ + if(argc<4) { + std::cerr<<"Usage: "< \n"; + return 1; + } + + // Read $PVXS_LOG from process environment and update + // logging configuration. eg. + // export PVXS_LOG=*=DEBUG + // makes a lot of noise. + logger_config_env(); + + // Create a client context + client::Context ctxt(client::Config::fromEnv() + .build()); + + auto reply(ctxt.rpc(argv[1]) + .arg("lhs", argv[2]) + .arg("rhs", argv[3]) + .exec() + ->wait(5.0)); + + std::cout<<"Reply\n"< + +#include + +#include +#include +#include +#include + +using namespace pvxs; + +int main(int argc, char* argv[]) +{ + if(argc<=1) { + std::cerr<<"Usage: "<\n"; + return 1; + } + + // Read $PVXS_LOG from process environment and update + // logging configuration. eg. + // export PVXS_LOG=*=DEBUG + // makes a lot of noise. + logger_config_env(); + + // Actually creating the PV. + // buildReadonly() installs a default onPut() handler which + // rejects all Put operations. + server::SharedPV pv(server::SharedPV::buildReadonly()); + + // Provide an RPC handler + pv.onRPC([](server::SharedPV& pv, + std::unique_ptr&& op, + Value&& top) + { + // Callback + + // assume arguments encoded NTURI + auto rhs = top["query.rhs"].as(); + auto lhs = top["query.lhs"].as(); + + auto reply(nt::NTScalar{TypeCode::Float64}.create()); + reply["value"] = lhs + rhs; + + op->reply(reply); + // Scale-able applications may reply outside of this callback, + // and from another thread. + }); + + // (Optional) Provide a data type for the PV. + // Provides a hint to users that Get, Put, or Monitor is not + // meaningful for this PV + Value initial = nt::NTScalar{TypeCode::String}.create(); + initial["value"] = "RPC only"; + pv.open(initial); + + // Build server which will serve this PV + // Configure using process environment. + server::Server serv = server::Config::fromEnv() + .build() + .addPV(argv[1], pv); + + // (optional) Print the configuration this server is using + // with any auto-address list expanded. + std::cout<<"Effective config\n"<(); src->names->insert(argv[1]); - // Build server which will server this PV + // Build server which will serve this PV // Configure using process environment. server::Server serv = server::Config::fromEnv() .build() diff --git a/example/ticker.cpp b/example/ticker.cpp index 3b8f40a..1dd6c17 100644 --- a/example/ticker.cpp +++ b/example/ticker.cpp @@ -70,7 +70,7 @@ int main(int argc, char* argv[]) // Associate a data type (and maybe initial value) with this PV pv.open(initial); - // Build server which will server this PV + // Build server which will serve this PV // Configure using process environment. server::Server serv = server::Config::fromEnv() .build()