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 static 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 {0UL} ;
71 unsigned long m_client_removes {0UL} ;
72 unsigned long m_server_peer_adds {0UL} ;
73 unsigned long m_server_peer_removes {0UL} ;
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 & )
177void GNet::MonitorImp::add(
const Connection & connection ,
bool is_client )
179 bool inserted = m_connections.insert(ConnectionMap::value_type(&connection,ConnectionInfo{is_client})).second ;
185 m_server_peer_adds++ ;
189void GNet::MonitorImp::add(
const Listener & server )
191 m_servers.insert( ServerMap::value_type(&server,server.address()) ) ;
194void GNet::MonitorImp::emit( Signal & s ,
const char * a ,
const char * b )
noexcept
198 s.emit( std::string(a) , std::string(b) ) ;
205void GNet::MonitorImp::remove(
const Connection & connection ,
bool is_client )
noexcept
207 bool removed = 0U != m_connections.erase( &connection ) ;
213 m_server_peer_removes++ ;
217void GNet::MonitorImp::remove(
const Listener & server )
noexcept
219 m_servers.erase( &server ) ;
225 for(
const auto & server : m_servers )
227 s << px <<
txt(
"LISTEN: ") << server.second.displayString(
true) << eol ;
230 s << px <<
txt(
"OUT started: ") << m_client_adds << eol ;
231 s << px <<
txt(
"OUT finished: ") << m_client_removes << eol ;
233 for(
const auto & connection : m_connections )
235 if( connection.second.is_client )
239 << connection.first->localAddress().displayString() <<
" -> "
240 << connection.first->connectionState() << eol ;
245 s << px <<
txt(
"IN started: ") << m_server_peer_adds << eol ;
246 s << px <<
txt(
"IN finished: ") << m_server_peer_removes << eol ;
248 for(
const auto & connection : m_connections )
250 if( !connection.second.is_client )
254 << connection.first->localAddress().displayString() <<
" <- "
255 << connection.first->peerAddress().displayString() << eol ;
263 for(
const auto & server : m_servers )
264 add( out ,
"Listening address" , server.second.displayString() ) ;
267 add( out ,
txt(
"Outgoing connections") , m_client_adds ,
txt(
"started") , m_client_removes ,
txt(
"finished") ) ;
268 add( out ,
txt(
"Incoming connections") , m_server_peer_adds ,
txt(
"started") , m_server_peer_removes ,
txt(
"finished") ) ;
269 for(
const auto & connection : m_connections )
271 if( connection.second.is_client )
273 add( out ,
txt(
"Outgoing connection") ,
274 connection.first->localAddress().displayString() ,
275 connection.first->connectionState() ) ;
278 for(
const auto & connection : m_connections )
280 if( !connection.second.is_client )
281 add( out ,
txt(
"Incoming connection") ,
282 connection.first->localAddress().displayString() ,
283 connection.first->peerAddress().displayString() ) ;
287void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
288 unsigned int value_1 ,
const std::string & suffix_1 ,
289 unsigned int value_2 ,
const std::string & suffix_2 )
296void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
const std::string & value )
298 add( out , key , value , std::string() ) ;
301void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
const std::string & value_1 ,
302 const std::string & value_2 )
304 out.push_back( key ) ;
305 out.push_back( value_1 ) ;
306 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().
std::vector< std::string > StringArray
A std::vector of std::strings.
const char * txt(const char *p) noexcept
A briefer alternative to G::gettext().