38 void add(
const Connection & ,
bool is_client ) ;
39 void remove(
const Connection & ,
bool is_client ) noexcept ;
41 void remove(
const Listener & ) noexcept ;
42 void report( std::ostream & s ,
const std::string & px ,
const std::string & eol )
const ;
44 void emit(
Signal & ,
const char * ,
const char * ) noexcept ;
50 explicit ConnectionInfo(
bool is_client_ ) noexcept : is_client(is_client_) {}
52 using ConnectionMap = std::map<const Connection*,ConnectionInfo> ;
53 using ServerMap = std::map<const Listener*,Address> ;
59 void operator=(
const MonitorImp & ) = delete ;
63 static std::string join(
const std::string & ,
const std::string & ) ;
64 static void add(
G::StringArray & ,
const std::string & ,
unsigned int ,
const std::string & ,
65 unsigned int ,
const std::string & ) ;
66 static void add(
G::StringArray & ,
const std::string & ,
const std::string & ,
const std::string & ,
67 const std::string & ,
const std::string & ) ;
68 static void add(
G::StringArray & ,
const std::string & ,
const std::string & ,
const std::string & ) ;
69 static void add(
G::StringArray & ,
const std::string & ,
const std::string & ) ;
72 ConnectionMap m_connections ;
74 unsigned long m_client_adds ;
75 unsigned long m_client_removes ;
76 unsigned long m_server_peer_adds ;
77 unsigned long m_server_peer_removes ;
89 G_ASSERT( pthis() ==
nullptr ) ;
112 pthis()->m_imp->add( client ,
true ) ;
113 pthis()->m_imp->emit( pthis()->m_signal ,
"out" ,
"start" ) ;
121 pthis()->m_imp->remove( client ,
true ) ;
122 pthis()->m_imp->emit( pthis()->m_signal ,
"out" ,
"stop" ) ;
130 pthis()->m_imp->add( server_peer ,
false ) ;
131 pthis()->m_imp->emit( pthis()->m_signal ,
"in" ,
"start" ) ;
140 pthis()->m_imp->remove( server_peer ,
false ) ;
141 pthis()->m_imp->emit( pthis()->m_signal ,
"in" ,
"stop" ) ;
149 pthis()->m_imp->add( server ) ;
150 pthis()->m_imp->emit( pthis()->m_signal ,
"listen" ,
"start" ) ;
158 pthis()->m_imp->remove( server ) ;
159 pthis()->m_imp->emit( pthis()->m_signal ,
"listen" ,
"stop" ) ;
165 m_imp->report( s , px , eol ) ;
170 m_imp->report( out ) ;
175GNet::MonitorImp::MonitorImp(
Monitor & ) :
177 m_client_removes(0UL) ,
178 m_server_peer_adds(0UL) ,
179 m_server_peer_removes(0UL)
183void GNet::MonitorImp::add(
const Connection & connection ,
bool is_client )
185 bool inserted = m_connections.insert(ConnectionMap::value_type(&connection,ConnectionInfo(is_client))).second ;
191 m_server_peer_adds++ ;
195void GNet::MonitorImp::add(
const Listener & server )
197 m_servers.insert( ServerMap::value_type(&server,server.address()) ) ;
200void GNet::MonitorImp::emit( Signal & s ,
const char * a ,
const char * b )
noexcept
204 s.emit( std::string(a) , std::string(b) ) ;
211void GNet::MonitorImp::remove(
const Connection & connection ,
bool is_client )
noexcept
213 bool removed = 0U != m_connections.erase( &connection ) ;
219 m_server_peer_removes++ ;
223void GNet::MonitorImp::remove(
const Listener & server )
noexcept
225 m_servers.erase( &server ) ;
228void GNet::MonitorImp::report( std::ostream & s ,
const std::string & px ,
const std::string & eol )
const
230 for(
const auto & server : m_servers )
232 s << px <<
"LISTEN: " << server.second.displayString(
true) << eol ;
235 s << px <<
"OUT started: " << m_client_adds << eol ;
236 s << px <<
"OUT finished: " << m_client_removes << eol ;
238 for(
const auto & connection : m_connections )
240 if( connection.second.is_client )
244 << connection.first->localAddress().second.displayString() <<
" -> "
245 << connection.first->connectionState() << eol ;
250 s << px <<
"IN started: " << m_server_peer_adds << eol ;
251 s << px <<
"IN finished: " << m_server_peer_removes << eol ;
253 for(
const auto & connection : m_connections )
255 if( !connection.second.is_client )
259 << connection.first->localAddress().second.displayString() <<
" <- "
260 << connection.first->peerAddress().second.displayString() << eol ;
268 for(
const auto & server : m_servers )
269 add( out ,
"Listening address" , server.second.displayString() ) ;
271 add( out ,
"Outgoing connections" , m_client_adds ,
"started" , m_client_removes ,
"finished" ) ;
272 add( out ,
"Incoming connections" , m_server_peer_adds ,
"started" , m_server_peer_removes ,
"finished" ) ;
273 for(
const auto & connection : m_connections )
275 if( connection.second.is_client )
277 add( out ,
"Outgoing connection" , connection.first->localAddress().second.displayString() ,
"-->" ,
278 connection.first->connectionState() ,
"" ) ;
281 for(
const auto & connection : m_connections )
283 if( !connection.second.is_client )
284 add( out ,
"Incoming connection" , connection.first->localAddress().second.displayString() ,
"<--" ,
285 connection.first->peerAddress().second.displayString() ,
"" ) ;
289void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
290 unsigned int value_1 ,
const std::string & suffix_1 ,
291 unsigned int value_2 ,
const std::string & suffix_2 )
296void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
297 const std::string & value_1 ,
const std::string & suffix_1 ,
298 const std::string & value_2 ,
const std::string & suffix_2 )
300 add( out , key , join(value_1,suffix_1) , join(value_2,suffix_2) ) ;
303void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
const std::string & value )
305 add( out , key , value , std::string() ) ;
308void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
const std::string & value_1 ,
309 const std::string & value_2 )
311 out.push_back( key ) ;
312 out.push_back( value_1 ) ;
313 out.push_back( value_2 ) ;
316std::string GNet::MonitorImp::join(
const std::string & s1 ,
const std::string & s2 )
318 return s2.empty() ? s1 : ( s1 +
" " + s2 ) ;
An abstract interface which provides address information for a network connection.
An interface for a network listener.
A pimple-pattern implementation class for GNet::Monitor.
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=std::string(), 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.
std::vector< std::string > StringArray
A std::vector of std::strings.