generalize Get/Put/RPC handling
This commit is contained in:
+69
-42
@@ -11,6 +11,8 @@
|
||||
#include <atomic>
|
||||
|
||||
#include <epicsEvent.h>
|
||||
#include <epicsMutex.h>
|
||||
#include <epicsGuard.h>
|
||||
#include <epicsTime.h>
|
||||
|
||||
#include <pvxs/server.h>
|
||||
@@ -18,6 +20,7 @@
|
||||
#include <pvxs/nt.h>
|
||||
#include <pvxs/log.h>
|
||||
|
||||
#include "utilpvt.h"
|
||||
|
||||
namespace {
|
||||
using namespace pvxs;
|
||||
@@ -25,50 +28,22 @@ using namespace pvxs::server;
|
||||
|
||||
DEFINE_LOGGER(dummy,"dummyserv");
|
||||
|
||||
struct DummyHandler : public Handler
|
||||
struct DummySource : public Source
|
||||
{
|
||||
static const Value mytype;
|
||||
static std::atomic<unsigned> count;
|
||||
|
||||
const std::string name;
|
||||
epicsMutex lock;
|
||||
Value current;
|
||||
|
||||
DummyHandler()
|
||||
:current(mytype.cloneEmpty())
|
||||
explicit DummySource(const std::string& name)
|
||||
:name(name)
|
||||
,current(nt::NTScalar{TypeCode::Int32}.build().create())
|
||||
{
|
||||
epicsTimeStamp now;
|
||||
epicsTimeGetCurrent(&now);
|
||||
current["value"] = count.fetch_add(1);
|
||||
current["value"] = 0u;
|
||||
current["timeStamp.secondsPastEpoch"] = now.secPastEpoch+POSIX_TIME_AT_EPICS_EPOCH;
|
||||
current["timeStamp.nanoseconds"] = now.nsec;
|
||||
}
|
||||
virtual ~DummyHandler() {}
|
||||
|
||||
virtual void onIntrospect(std::unique_ptr<Introspect> &&op) override final
|
||||
{
|
||||
log_printf(dummy, PLVL_INFO, "GET_FIELD\n");
|
||||
op->reply(mytype);
|
||||
}
|
||||
|
||||
virtual void onGet(std::unique_ptr<Get>&& op) override final
|
||||
{
|
||||
log_printf(dummy, PLVL_INFO, "Create GET\n");
|
||||
std::shared_ptr<Get> sop(std::move(op));
|
||||
sop->connect(current, [this, sop]()
|
||||
{
|
||||
log_printf(dummy, PLVL_INFO, "Execute GET\n");
|
||||
// executing
|
||||
sop->reply(current); // "current" never changes for us, so no locking
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const Value DummyHandler::mytype = nt::NTScalar{TypeCode::Int32}.build().create();
|
||||
std::atomic<unsigned> DummyHandler::count{};
|
||||
|
||||
struct DummySource : public Source
|
||||
{
|
||||
std::set<std::string> names;
|
||||
virtual ~DummySource() {}
|
||||
|
||||
// Source interface
|
||||
@@ -76,7 +51,7 @@ public:
|
||||
virtual void onSearch(Search &search) override final
|
||||
{
|
||||
for(auto& op : search) {
|
||||
if(names.find(op.name())!=names.end()) {
|
||||
if(op.name()==name) {
|
||||
log_printf(dummy, PLVL_INFO, "Claiming '%s'\n", op.name());
|
||||
op.claim();
|
||||
} else {
|
||||
@@ -84,10 +59,63 @@ public:
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual void onCreate(std::unique_ptr<ChannelControl>&& op) override final
|
||||
virtual void onCreate(std::unique_ptr<ChannelControl>&& raw) override final
|
||||
{
|
||||
log_printf(dummy, PLVL_INFO, "Create '%s'\n", op->name.c_str());
|
||||
op->setHandler(std::unique_ptr<Handler>{new DummyHandler});
|
||||
if(raw->name()!=name)
|
||||
return;
|
||||
|
||||
std::shared_ptr<ChannelControl> chan(std::move(raw));
|
||||
|
||||
log_printf(dummy, PLVL_INFO, "Create '%s'\n", chan->name().c_str());
|
||||
|
||||
// callback when client creating Get/Put
|
||||
chan->onOp([this, chan](std::shared_ptr<ConnectOp>&& raw){
|
||||
std::shared_ptr<ConnectOp> conn(std::move(raw));
|
||||
|
||||
log_printf(dummy, PLVL_INFO, "Begin Operation on '%s'\n", chan->name().c_str());
|
||||
|
||||
conn->onGet([this, chan](std::unique_ptr<ExecOp>&& raw) {
|
||||
// client executing Get or Put
|
||||
log_printf(dummy, PLVL_INFO, "Exec Get on '%s'\n", chan->name().c_str());
|
||||
|
||||
{
|
||||
epicsGuard<epicsMutex> G(lock);
|
||||
raw->reply(current);
|
||||
}
|
||||
});
|
||||
|
||||
conn->onPut([this, chan](std::unique_ptr<ExecOp>&& raw, Value&& top) {
|
||||
log_printf(dummy, PLVL_INFO, "Exec Put on '%s'\n", chan->name().c_str());
|
||||
|
||||
{
|
||||
epicsTimeStamp now;
|
||||
epicsTimeGetCurrent(&now);
|
||||
|
||||
epicsGuard<epicsMutex> G(lock);
|
||||
|
||||
current["value"] = top["value"].as<uint32_t>();
|
||||
current["timeStamp.secondsPastEpoch"] = now.secPastEpoch+POSIX_TIME_AT_EPICS_EPOCH;
|
||||
current["timeStamp.nanoseconds"] = now.nsec;
|
||||
}
|
||||
|
||||
raw->reply(); // inform client that Put was successful
|
||||
});
|
||||
|
||||
epicsGuard<epicsMutex> G(lock);
|
||||
conn->connect(current); // only type is used
|
||||
});
|
||||
|
||||
// callback when client executing RPC
|
||||
chan->onRPC([this, chan](std::unique_ptr<ExecOp>&& raw, Value&& top) {
|
||||
log_printf(dummy, PLVL_INFO, "Begin RPC on '%s' with %s\n", chan->name().c_str(),
|
||||
std::string(SB()<<top).c_str());
|
||||
|
||||
auto ret = nt::NTScalar{TypeCode::String}.build().create();
|
||||
|
||||
ret["value"] = "RPC test";
|
||||
|
||||
raw->reply(ret);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -97,11 +125,10 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
int ret = 0;
|
||||
try {
|
||||
pvxs::logger_level_set("dummy", PLVL_INFO);
|
||||
pvxs::logger_level_set("dummyserv", PLVL_INFO);
|
||||
pvxs::logger_config_env();
|
||||
|
||||
std::shared_ptr<DummySource> src(new DummySource);
|
||||
src->names.emplace("blah");
|
||||
auto src = std::make_shared<DummySource>("blah");
|
||||
|
||||
auto serv = Server::Config::from_env()
|
||||
.build()
|
||||
|
||||
Reference in New Issue
Block a user