39 void add(
const Connection & ,
bool is_client ) ;
40 void remove(
const Connection & ,
bool is_client ) ;
41 void report( std::ostream & s ,
const std::string & px ,
const std::string & eol )
const ;
43 std::string certificateId(
const std::string & certificiate ) ;
44 std::pair<std::string,bool> findCertificate(
const std::string & certificate ) ;
47 static std::string join(
const std::string & ,
const std::string & ) ;
48 static void add(
G::StringArray & ,
const std::string & ,
unsigned int ,
const std::string & ,
49 unsigned int ,
const std::string & ) ;
50 static void add(
G::StringArray & ,
const std::string & ,
const std::string & ,
const std::string & ,
51 const std::string & ,
const std::string & ) ;
57 explicit ConnectionInfo(
bool is_client_ ) : is_client(is_client_) {}
59 struct CertificateInfo
61 std::string certificate ;
63 CertificateInfo(
const std::string & c ,
int id_ ) : certificate(c) , id(id_) {}
64 bool match( std::string s )
const {
return s == certificate ; }
66 struct CertificateMatch
68 const std::string & m_s ;
69 explicit CertificateMatch(
const std::string & s ) : m_s(s) {}
70 bool operator()(
const CertificateInfo & o )
const {
return o.match(m_s) ; }
72 typedef std::map<const Connection*,ConnectionInfo> ConnectionMap ;
73 typedef std::deque<CertificateInfo> Certificates ;
74 ConnectionMap m_connections ;
75 Certificates m_certificates ;
77 unsigned long m_client_adds ;
78 unsigned long m_client_removes ;
79 unsigned long m_server_peer_adds ;
80 unsigned long m_server_peer_removes ;
83 GNet::MonitorImp::MonitorImp(
Monitor & ) :
86 m_client_removes(0UL) ,
87 m_server_peer_adds(0UL) ,
88 m_server_peer_removes(0UL)
103 G_ASSERT( pthis() ==
nullptr ) ;
125 m_imp->add( client ,
true ) ;
126 m_signal.emit(
"out" ,
"start" ) ;
131 m_imp->remove( client ,
true ) ;
132 m_signal.emit(
"out" ,
"end" ) ;
137 m_imp->add( server_peer ,
false ) ;
138 m_signal.emit(
"in" ,
"start" ) ;
144 m_imp->remove( server_peer ,
false ) ;
145 m_signal.emit(
"in" ,
"end" ) ;
150 return m_imp->findCertificate( certificate ) ;
155 m_imp->report( s , px , eol ) ;
160 m_imp->report( out ) ;
165 void GNet::MonitorImp::add(
const Connection & connection ,
bool is_client )
167 bool inserted = m_connections.insert(ConnectionMap::value_type(&connection,ConnectionInfo(is_client))).second ;
173 m_server_peer_adds++ ;
177 void GNet::MonitorImp::remove(
const Connection & connection ,
bool is_client )
179 bool removed = 0U != m_connections.erase( &connection ) ;
185 m_server_peer_removes++ ;
189 void GNet::MonitorImp::report( std::ostream & s ,
const std::string & px ,
const std::string & eol )
const
191 s << px <<
"OUT started: " << m_client_adds << eol ;
192 s << px <<
"OUT finished: " << m_client_removes << eol ;
194 for( ConnectionMap::const_iterator p = m_connections.begin() ; p != m_connections.end() ; ++p )
196 if( (*p).second.is_client )
200 << (*p).first->localAddress().second.displayString() <<
" -> "
201 << (*p).first->connectionState() << eol ;
206 s << px <<
"IN started: " << m_server_peer_adds << eol ;
207 s << px <<
"IN finished: " << m_server_peer_removes << eol ;
209 for( ConnectionMap::const_iterator p = m_connections.begin() ; p != m_connections.end() ; ++p )
211 if( !(*p).second.is_client )
215 << (*p).first->localAddress().second.displayString() <<
" <- "
216 << (*p).first->peerAddress().second.displayString() << eol ;
224 add( out ,
"Outgoing connections" , m_client_adds ,
"started" , m_client_removes ,
"finished" ) ;
225 add( out ,
"Incoming connections" , m_server_peer_adds ,
"started" , m_server_peer_removes ,
"finished" ) ;
226 for( ConnectionMap::const_iterator p = m_connections.begin() ; p != m_connections.end() ; ++p )
228 if( (*p).second.is_client )
230 add( out ,
"Outgoing connection" , (*p).first->localAddress().second.displayString() ,
"-->" ,
231 (*p).first->connectionState() ,
"" ) ;
234 for( ConnectionMap::const_iterator p = m_connections.begin() ; p != m_connections.end() ; ++p )
236 if( !(*p).second.is_client )
237 add( out ,
"Incoming connection" , (*p).first->localAddress().second.displayString() ,
"<--" ,
238 (*p).first->peerAddress().second.displayString() ,
"" ) ;
242 void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
243 unsigned int value_1 ,
const std::string & suffix_1 ,
244 unsigned int value_2 ,
const std::string & suffix_2 )
249 std::string GNet::MonitorImp::join(
const std::string & s1 ,
const std::string & s2 )
251 return s2.empty() ? s1 : ( s1 +
" " + s2 ) ;
254 void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
255 const std::string & value_1 ,
const std::string & suffix_1 ,
256 const std::string & value_2 ,
const std::string & suffix_2 )
258 out.push_back( key ) ;
259 out.push_back( join(value_1,suffix_1) ) ;
260 out.push_back( join(value_2,suffix_2) ) ;
263 std::pair<std::string,bool> GNet::MonitorImp::findCertificate(
const std::string & certificate )
265 std::pair<std::string,bool> result( std::string() ,
false ) ;
266 if( certificate.empty() )
270 Certificates::iterator
const end = m_certificates.end() ;
271 Certificates::iterator p = std::find_if( m_certificates.begin() , end , CertificateMatch(certificate) ) ;
274 CertificateInfo tmp = *p ;
276 result.second = false ;
277 p = std::remove_if( m_certificates.begin() , end , CertificateMatch(certificate) ) ;
278 G_ASSERT( p != end ) ;
283 const size_t limit = G::limits::net_certificate_cache_size ;
284 if( m_certificates.size() == limit && !m_certificates.empty() )
285 m_certificates.pop_front() ;
286 int id = ++m_id_generator ;
287 m_certificates.push_back( CertificateInfo(certificate,
id) ) ;
289 result.second = true ;
void removeServerPeer(const Connection &server_peer)
Removes a server connection.
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.
std::vector< std::string > StringArray
A std::vector of std::strings.
void addClient(const Connection &simple_client)
Adds a client connection.
static std::string fromInt(int i)
Converts int 'i' to a string.
G::Slot::Signal2< std::string, std::string > & signal()
Provides a callback signal which can be connect()ed to a slot.
A pimple-pattern implementation class for GNet::Monitor.
static std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
A singleton for monitoring GNet::SimpleClient and GNet::ServerPeer connections and for storing their ...
static Monitor * instance()
Returns the singleton pointer. Returns null if none.
virtual ~Monitor()
Destructor.
void removeClient(const Connection &simple_client)
Removes a client connection.
void addServerPeer(const Connection &server_peer)
Adds a server connection.
An abstract interface which provides address information for a network connection.
std::pair< std::string, bool > findCertificate(const std::string &certificate)
Returns a short id for the given certificate and a boolean flag to indicate if it is a new certificat...
Monitor()
Default constructor.