diff --git a/src/pvxs/server.h b/src/pvxs/server.h index f20e841..6f6abce 100644 --- a/src/pvxs/server.h +++ b/src/pvxs/server.h @@ -23,6 +23,7 @@ namespace pvxs { namespace server { +struct SharedPV; struct Source; class Server; @@ -63,13 +64,18 @@ struct Config { * * In order to be useful, a Server will have one or more Source instances added * to it with addSource(). + * + * As a convienence, each Server instance automatically contains a "builtin" StaticSource + * to which SharedPV instances can be directly added. + * The "builtin" has priority zero, and can be accessed or even removed like any Source + * explicitly added with addSource(). */ class PVXS_API Server { public: //! An empty/dummy Server - Server(); + constexpr Server() = default; //! Create/allocate, but do not start, a new server with the provided config. explicit Server(Config&&); ~Server(); @@ -91,6 +97,11 @@ public: //! effective config const Config& config() const; + //! Add a SharedPV to the builtin StaticSource + Server& addPV(const std::string& name, const SharedPV& pv); + //! Remove a SharedPV from the builtin StaticSource + Server& removePV(const std::string& name); + //! Add a Source to this server with an arbitrary source name. //! //! @param name Source name diff --git a/src/server.cpp b/src/server.cpp index 41a4c7d..7093301 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -126,8 +126,6 @@ Server Config::build() return ret; } -Server::Server() {} - Server::Server(Config&& conf) { /* Here be dragons. @@ -231,6 +229,22 @@ const Config& Server::config() const return pvt->effective; } +Server& Server::addPV(const std::string& name, const SharedPV& pv) +{ + if(!pvt) + throw std::logic_error("NULL Server"); + pvt->builtinsrc.add(name, pv); + return *this; +} + +Server& Server::removePV(const std::string& name) +{ + if(!pvt) + throw std::logic_error("NULL Server"); + pvt->builtinsrc.remove(name); + return *this; +} + Server& Server::start() { if(!pvt) @@ -312,6 +326,7 @@ Server::Pvt::Pvt(Config&& conf) ,beaconSender(AF_INET, SOCK_DGRAM, 0) ,beaconTimer(event_new(acceptor_loop.base, -1, EV_TIMEOUT, doBeaconsS, this)) ,searchReply(0x10000) + ,builtinsrc(StaticSource::build()) ,state(Stopped) { // empty interface address list implies the wildcard @@ -453,6 +468,7 @@ Server::Pvt::Pvt(Config&& conf) { auto L = sourcesLock.lockWriter(); sources[std::make_pair(-1, "server")] = std::make_shared(this); + sources[std::make_pair(-1, "builtin")] = builtinsrc.source(); } } diff --git a/src/serverconn.h b/src/serverconn.h index dc1e431..47b2cf8 100644 --- a/src/serverconn.h +++ b/src/serverconn.h @@ -15,6 +15,7 @@ #include #include +#include #include "evhelper.h" #include "utilpvt.h" #include "dataimpl.h" @@ -222,6 +223,8 @@ struct Server::Pvt Source::Search searchOp; + StaticSource builtinsrc; + RWLock sourcesLock; std::map, std::shared_ptr > sources; diff --git a/test/mailbox.cpp b/test/mailbox.cpp index 60a9163..319a93b 100644 --- a/test/mailbox.cpp +++ b/test/mailbox.cpp @@ -36,17 +36,16 @@ int main(int argc, char* argv[]) auto initial = nt::NTScalar{TypeCode::Float64}.create(); initial["value"] = 42.0; - - auto src(StaticSource::build()); + initial["alarm.severity"] = 0; + initial["alarm.status"] = 0; + initial["alarm.message"] = ""; auto pv(SharedPV::buildMailbox()); pv.open(initial); - src.add(argv[1], pv); - auto serv = Config::from_env() .build() - .addSource("box", src.source()); + .addPV(argv[1], pv); auto& conf = serv.config();