diff --git a/src/client.cpp b/src/client.cpp index 8e3cedb..c2acb87 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -402,11 +402,11 @@ void Context::ignoreServerGUIDs(const std::vector& guids) }); } -Report Context::report() const +Report Context::report(bool zero) const { Report ret; - pvt->impl->tcp_loop.call([this, &ret](){ + pvt->impl->tcp_loop.call([this, &ret, zero](){ for(auto& pair : pvt->impl->connByAddr) { auto conn = pair.second.lock(); @@ -419,6 +419,10 @@ Report Context::report() const sconn.tx = conn->statTx; sconn.rx = conn->statRx; + if(zero) { + conn->statTx = conn->statRx = 0u; + } + // omit stats for transitory conn->creatingByCID for(auto& pair : conn->chanBySID) { @@ -431,6 +435,10 @@ Report Context::report() const schan.name = chan->name; schan.tx = chan->statTx; schan.rx = chan->statRx; + + if(zero) { + chan->statTx = chan->statRx = 0u; + } } } diff --git a/src/pvxs/client.h b/src/pvxs/client.h index 024fef1..20fc178 100644 --- a/src/pvxs/client.h +++ b/src/pvxs/client.h @@ -27,7 +27,6 @@ namespace client { class Context; struct Config; -using pvxs::impl::Report; //! Operation failed because of connection to server was lost struct PVXS_API Disconnect : public std::runtime_error @@ -503,7 +502,7 @@ public: //! Compile report about peers and channels //! @since UNRELEASED - Report report() const; + Report report(bool zero=true) const; #endif explicit operator bool() const { return pvt.operator bool(); } diff --git a/src/pvxs/netcommon.h b/src/pvxs/netcommon.h index ea86435..d0cd978 100644 --- a/src/pvxs/netcommon.h +++ b/src/pvxs/netcommon.h @@ -7,15 +7,31 @@ #include #include +#include #include namespace pvxs { namespace impl { +struct Report; +struct ReportInfo; +} +namespace server { +struct ClientCredentials; +using impl::Report; +using impl::ReportInfo; +} +namespace client { +using impl::Report; +using impl::ReportInfo; +} +namespace impl { + +#ifdef PVXS_EXPERT_API_ENABLED /** Snapshot of information about a client/server * - * cf. pvxs::server::Server::report() + * cf. pvxs::server::Server::report() and pvxs::client::Context::report() * * @since UNRELEASED */ @@ -26,12 +42,16 @@ struct Report { std::string name; //! transmit and receive counters in bytes size_t tx{}, rx{}; + //! Contextual information (maybe) supplied by the Source + std::shared_ptr info; }; //! Info for a single connection to remote peer struct Connection { //! peer endpoint (eg. IPv4 address and port) std::string peer; + //! Credentials presented by peer. Only from Server::report() + std::shared_ptr credentials; //! transmit and receive counters in bytes size_t tx{}, rx{}; //! Channels currently connected through this socket @@ -42,6 +62,12 @@ struct Report { std::list connections; }; +struct PVXS_API ReportInfo { + virtual ~ReportInfo(); +}; + +#endif + } // namespace impl } // namespace pvxs diff --git a/src/pvxs/server.h b/src/pvxs/server.h index d88b0b6..3a02cfc 100644 --- a/src/pvxs/server.h +++ b/src/pvxs/server.h @@ -32,7 +32,6 @@ namespace server { struct SharedPV; struct Source; struct Config; -using pvxs::impl::Report; /** PV Access protocol server instance * @@ -117,8 +116,9 @@ public: #ifdef PVXS_EXPERT_API_ENABLED //! Compile report about peers and channels + //! @param zero If true, zero counters after reading //! @since UNRELEASED - Report report() const; + Report report(bool zero=true) const; #endif explicit operator bool() const { return !!pvt; } diff --git a/src/pvxs/source.h b/src/pvxs/source.h index 70805c5..079dd54 100644 --- a/src/pvxs/source.h +++ b/src/pvxs/source.h @@ -153,6 +153,14 @@ struct PVXS_API ChannelControl : public OpBase { virtual void close() =0; // TODO: signal Rights? + +#ifdef PVXS_EXPERT_API_ENABLED + // Store info struct which will be returned with Report::Channel + inline void updateInfo(const std::shared_ptr& info) + { this->_updateInfo(info); } +#endif +private: + virtual void _updateInfo(const std::shared_ptr& info) =0; }; /** Interface through which a Server discovers Channel names and diff --git a/src/server.cpp b/src/server.cpp index b2829d3..d5b429e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -31,6 +31,9 @@ #include "udp_collector.h" namespace pvxs { +namespace impl { +ReportInfo::~ReportInfo() {} +} namespace server { using namespace impl; @@ -221,14 +224,14 @@ Server& Server::interrupt() return *this; } -Report Server::report() const +Report Server::report(bool zero) const { if(!pvt) throw std::logic_error("NULL Server"); Report ret; - pvt->acceptor_loop.call([this, &ret](){ + pvt->acceptor_loop.call([this, &ret, zero](){ for(auto& pair : pvt->connections) { auto conn = pair.first; @@ -236,9 +239,14 @@ Report Server::report() const ret.connections.emplace_back(); auto& sconn = ret.connections.back(); sconn.peer = conn->peerName; + sconn.credentials = conn->cred; sconn.tx = conn->statTx; sconn.rx = conn->statRx; + if(zero) { + conn->statTx = conn->statRx = 0u; + } + for(auto& pair : conn->chanBySID) { auto& chan = pair.second; @@ -247,6 +255,11 @@ Report Server::report() const schan.name = chan->name; schan.tx = chan->statTx; schan.rx = chan->statRx; + schan.info = chan->reportInfo; + + if(zero) { + chan->statTx = chan->statRx = 0u; + } } } diff --git a/src/serverchan.cpp b/src/serverchan.cpp index 2acfd9c..c548338 100644 --- a/src/serverchan.cpp +++ b/src/serverchan.cpp @@ -174,6 +174,20 @@ void ServerChannelControl::close() }); } +void ServerChannelControl::_updateInfo(const std::shared_ptr& info) +{ + auto serv = server.lock(); + if(!serv) + return; + + serv->acceptor_loop.call([this, &info](){ + auto ch = chan.lock(); + if(!ch) + return; + ch->reportInfo = info; + }); +} + void ServerConn::handle_SEARCH() { EvInBuf M(peerBE, segBuf.get(), 16); diff --git a/src/serverconn.h b/src/serverconn.h index 6bfd66d..3769202 100644 --- a/src/serverconn.h +++ b/src/serverconn.h @@ -66,6 +66,8 @@ struct ServerChannelControl : public server::ChannelControl virtual void onClose(std::function&& fn) override final; virtual void close() override final; + virtual void _updateInfo(const std::shared_ptr& info) override final; + const std::weak_ptr server; const std::weak_ptr chan; @@ -86,6 +88,7 @@ struct ServerChan } state; size_t statTx{}, statRx{}; + std::shared_ptr reportInfo; std::function&&)> onOp; std::function&&, Value&&)> onRPC;