38 explicit MonitorImp( Monitor & monitor ) ;
39 void add(
const Connection & ,
bool is_client ) ;
40 void remove(
const Connection & ,
bool is_client ) noexcept ;
41 void add(
const Listener & ) ;
42 void remove(
const Listener & ) noexcept ;
43 void report( std::ostream & s ,
const std::string & px ,
const std::string & eol )
const ;
45 void emit( Signal & ,
const char * ,
const char * ) noexcept ;
52 using ConnectionMap = std::map<const Connection*,ConnectionInfo> ;
53 using ServerMap = std::map<const Listener*,Address> ;
56 ~MonitorImp() = default ;
57 MonitorImp(
const MonitorImp & ) = delete ;
58 MonitorImp( MonitorImp && ) = delete ;
59 MonitorImp & operator=(
const MonitorImp & ) = delete ;
60 MonitorImp & operator=( MonitorImp && ) = delete ;
63 static void add(
G::StringArray & ,
const std::string & ,
unsigned int ,
const std::string & ,
unsigned int ,
const std::string & ) ;
64 static void add(
G::StringArray & ,
const std::string & ,
const std::string & ) ;
65 static void add(
G::StringArray & ,
const std::string & ,
const std::string & ,
const std::string & ) ;
68 ConnectionMap m_connections ;
70 unsigned long m_client_adds ;
71 unsigned long m_client_removes ;
72 unsigned long m_server_peer_adds ;
73 unsigned long m_server_peer_removes ;
83 m_imp(
std::make_unique<MonitorImp>(*this))
85 G_ASSERT( pthis() ==
nullptr ) ;
108 pthis()->m_imp->add( client ,
true ) ;
109 pthis()->m_imp->emit( pthis()->m_signal ,
"out" ,
"start" ) ;
117 pthis()->m_imp->remove( client ,
true ) ;
118 pthis()->m_imp->emit( pthis()->m_signal ,
"out" ,
"stop" ) ;
126 pthis()->m_imp->add( server_peer ,
false ) ;
127 pthis()->m_imp->emit( pthis()->m_signal ,
"in" ,
"start" ) ;
136 pthis()->m_imp->remove( server_peer ,
false ) ;
137 pthis()->m_imp->emit( pthis()->m_signal ,
"in" ,
"stop" ) ;
145 pthis()->m_imp->add( server ) ;
146 pthis()->m_imp->emit( pthis()->m_signal ,
"listen" ,
"start" ) ;
154 pthis()->m_imp->remove( server ) ;
155 pthis()->m_imp->emit( pthis()->m_signal ,
"listen" ,
"stop" ) ;
161 m_imp->report( s , px , eol ) ;
167 m_imp->report( out ) ;
173GNet::MonitorImp::MonitorImp(
Monitor & ) :
175 m_client_removes(0UL) ,
176 m_server_peer_adds(0UL) ,
177 m_server_peer_removes(0UL)
181void GNet::MonitorImp::add(
const Connection & connection ,
bool is_client )
183 bool inserted = m_connections.insert(ConnectionMap::value_type(&connection,ConnectionInfo{is_client})).second ;
189 m_server_peer_adds++ ;
193void GNet::MonitorImp::add(
const Listener & server )
195 m_servers.insert( ServerMap::value_type(&server,server.address()) ) ;
198void GNet::MonitorImp::emit( Signal & s ,
const char * a ,
const char * b )
noexcept
202 s.emit( std::string(a) , std::string(b) ) ;
209void GNet::MonitorImp::remove(
const Connection & connection ,
bool is_client )
noexcept
211 bool removed = 0U != m_connections.erase( &connection ) ;
217 m_server_peer_removes++ ;
221void GNet::MonitorImp::remove(
const Listener & server )
noexcept
223 m_servers.erase( &server ) ;
229 for(
const auto & server : m_servers )
231 s << px <<
txt(
"LISTEN: ") << server.second.displayString(
true) << eol ;
234 s << px <<
txt(
"OUT started: ") << m_client_adds << eol ;
235 s << px <<
txt(
"OUT finished: ") << m_client_removes << eol ;
237 for(
const auto & connection : m_connections )
239 if( connection.second.is_client )
243 << connection.first->localAddress().displayString() <<
" -> "
244 << connection.first->connectionState() << eol ;
249 s << px <<
txt(
"IN started: ") << m_server_peer_adds << eol ;
250 s << px <<
txt(
"IN finished: ") << m_server_peer_removes << eol ;
252 for(
const auto & connection : m_connections )
254 if( !connection.second.is_client )
258 << connection.first->localAddress().displayString() <<
" <- "
259 << connection.first->peerAddress().displayString() << eol ;
267 for(
const auto & server : m_servers )
268 add( out ,
"Listening address" , server.second.displayString() ) ;
271 add( out ,
txt(
"Outgoing connections") , m_client_adds ,
txt(
"started") , m_client_removes ,
txt(
"finished") ) ;
272 add( out ,
txt(
"Incoming connections") , m_server_peer_adds ,
txt(
"started") , m_server_peer_removes ,
txt(
"finished") ) ;
273 for(
const auto & connection : m_connections )
275 if( connection.second.is_client )
277 add( out ,
txt(
"Outgoing connection") ,
278 connection.first->localAddress().displayString() ,
279 connection.first->connectionState() ) ;
282 for(
const auto & connection : m_connections )
284 if( !connection.second.is_client )
285 add( out ,
txt(
"Incoming connection") ,
286 connection.first->localAddress().displayString() ,
287 connection.first->peerAddress().displayString() ) ;
291void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
292 unsigned int value_1 ,
const std::string & suffix_1 ,
293 unsigned int value_2 ,
const std::string & suffix_2 )
300void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
const std::string & value )
302 add( out , key , value , std::string() ) ;
305void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
const std::string & value_1 ,
306 const std::string & value_2 )
308 out.push_back( key ) ;
309 out.push_back( value_1 ) ;
310 out.push_back( value_2 ) ;
An abstract interface which provides information about a network connection.
An interface for a network listener.
A singleton for monitoring GNet::Client and GNet::ServerPeer connections.
static void addServer(const Listener &server)
Adds a server.
static void removeClient(const Connection &client) noexcept
Removes a client connection.
static void addServerPeer(const Connection &server_peer)
Adds a server connection.
static void removeServer(const Listener &server) noexcept
Removes a server.
G::Slot::Signal< const std::string &, const std::string & > & signal()
Provides a callback signal which can be connect()ed to a slot.
void report(std::ostream &stream, const std::string &line_prefix={}, const std::string &eol=std::string("\n")) const
Reports itself onto a stream.
Monitor()
Default constructor.
static Monitor * instance()
Returns the singleton pointer. Returns nullptr if none.
static void removeServerPeer(const Connection &server_peer) noexcept
Removes a server connection.
static void addClient(const Connection &client)
Adds a client connection.
static std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
void report(const Server *, const std::string &group={})
Calls GPop::Server::report().
const char * txt(const char *p)
A briefer alternative to G::gettext().
std::vector< std::string > StringArray
A std::vector of std::strings.