40 void add(
const Connection & ,
bool is_client ) ;
41 void remove(
const Connection & ,
bool is_client ) ;
42 void report( std::ostream & s ,
const std::string & px ,
const std::string & eol )
const ;
44 std::string certificateId(
const std::string & certificiate ) ;
45 std::pair<std::string,bool> findCertificate(
const std::string & certificate ) ;
48 static std::string join(
const std::string & ,
const std::string & ) ;
49 static void add(
G::StringArray & ,
const std::string & ,
unsigned int ,
const std::string & ,
50 unsigned int ,
const std::string & ) ;
51 static void add(
G::StringArray & ,
const std::string & ,
const std::string & ,
const std::string & ,
52 const std::string & ,
const std::string & ) ;
58 explicit ConnectionInfo(
bool is_client_ ) : is_client(is_client_) {}
60 struct CertificateInfo
62 std::string certificate ;
64 CertificateInfo(
const std::string & c ,
int id_ ) : certificate(c) , id(id_) {}
65 bool match( std::string s )
const {
return s == certificate ; }
67 struct CertificateMatch
69 const std::string & m_s ;
70 explicit CertificateMatch(
const std::string & s ) : m_s(s) {}
71 bool operator()(
const CertificateInfo & o )
const {
return o.match(m_s) ; }
73 typedef std::map<const Connection*,ConnectionInfo> ConnectionMap ;
74 typedef std::deque<CertificateInfo> Certificates ;
75 ConnectionMap m_connections ;
76 Certificates m_certificates ;
78 unsigned long m_client_adds ;
79 unsigned long m_client_removes ;
80 unsigned long m_server_peer_adds ;
81 unsigned long m_server_peer_removes ;
84 GNet::MonitorImp::MonitorImp(
Monitor & ) :
87 m_client_removes(0UL) ,
88 m_server_peer_adds(0UL) ,
89 m_server_peer_removes(0UL)
104 G_ASSERT( pthis() ==
nullptr ) ;
126 m_imp->add( client ,
true ) ;
127 m_signal.emit(
"out" ,
"start" ) ;
132 m_imp->remove( client ,
true ) ;
133 m_signal.emit(
"out" ,
"end" ) ;
138 m_imp->add( server_peer ,
false ) ;
139 m_signal.emit(
"in" ,
"start" ) ;
145 m_imp->remove( server_peer ,
false ) ;
146 m_signal.emit(
"in" ,
"end" ) ;
151 return m_imp->findCertificate( certificate ) ;
156 m_imp->report( s , px , eol ) ;
161 m_imp->report( out ) ;
166 void GNet::MonitorImp::add(
const Connection & connection ,
bool is_client )
168 bool inserted = m_connections.insert(ConnectionMap::value_type(&connection,ConnectionInfo(is_client))).second ;
174 m_server_peer_adds++ ;
178 void GNet::MonitorImp::remove(
const Connection & connection ,
bool is_client )
180 bool removed = 0U != m_connections.erase( &connection ) ;
186 m_server_peer_removes++ ;
190 void GNet::MonitorImp::report( std::ostream & s ,
const std::string & px ,
const std::string & eol )
const
192 s << px <<
"OUT started: " << m_client_adds << eol ;
193 s << px <<
"OUT finished: " << m_client_removes << eol ;
195 for( ConnectionMap::const_iterator p = m_connections.begin() ; p != m_connections.end() ; ++p )
197 if( (*p).second.is_client )
201 << (*p).first->localAddress().second.displayString() <<
" -> "
202 << (*p).first->peerAddress().second.displayString() << eol ;
207 s << px <<
"IN started: " << m_server_peer_adds << eol ;
208 s << px <<
"IN finished: " << m_server_peer_removes << eol ;
210 for( ConnectionMap::const_iterator p = m_connections.begin() ; p != m_connections.end() ; ++p )
212 if( !(*p).second.is_client )
216 << (*p).first->localAddress().second.displayString() <<
" <- "
217 << (*p).first->peerAddress().second.displayString() << eol ;
225 add( out ,
"Outgoing connections" , m_client_adds ,
"started" , m_client_removes ,
"finished" ) ;
226 add( out ,
"Incoming connections" , m_server_peer_adds ,
"started" , m_server_peer_removes ,
"finished" ) ;
227 for( ConnectionMap::const_iterator p = m_connections.begin() ; p != m_connections.end() ; ++p )
229 if( (*p).second.is_client )
230 add( out ,
"Outgoing connection" , (*p).first->localAddress().second.displayString() ,
"-->" ,
231 (*p).first->peerAddress().second.displayString() ,
"" ) ;
233 for( ConnectionMap::const_iterator p = m_connections.begin() ; p != m_connections.end() ; ++p )
235 if( !(*p).second.is_client )
236 add( out ,
"Incoming connection" , (*p).first->localAddress().second.displayString() ,
"<--" ,
237 (*p).first->peerAddress().second.displayString() ,
"" ) ;
241 void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
242 unsigned int value_1 ,
const std::string & suffix_1 ,
243 unsigned int value_2 ,
const std::string & suffix_2 )
248 std::string GNet::MonitorImp::join(
const std::string & s1 ,
const std::string & s2 )
250 return s2.empty() ? s1 : ( s1 +
" " + s2 ) ;
253 void GNet::MonitorImp::add(
G::StringArray & out ,
const std::string & key ,
254 const std::string & value_1 ,
const std::string & suffix_1 ,
255 const std::string & value_2 ,
const std::string & suffix_2 )
257 out.push_back( key ) ;
258 out.push_back( join(value_1,suffix_1) ) ;
259 out.push_back( join(value_2,suffix_2) ) ;
262 std::pair<std::string,bool> GNet::MonitorImp::findCertificate(
const std::string & certificate )
264 std::pair<std::string,bool> result( std::string() ,
false ) ;
265 if( certificate.empty() )
269 Certificates::iterator
const end = m_certificates.end() ;
270 Certificates::iterator p = std::find_if( m_certificates.begin() , end , CertificateMatch(certificate) ) ;
273 CertificateInfo tmp = *p ;
275 result.second = false ;
276 p = std::remove_if( m_certificates.begin() , end , CertificateMatch(certificate) ) ;
277 G_ASSERT( p != end ) ;
282 const size_t limit = G::limits::net_certificate_cache_size ;
283 if( m_certificates.size() == limit && !m_certificates.empty() )
284 m_certificates.pop_front() ;
285 int id = ++m_id_generator ;
286 m_certificates.push_back( CertificateInfo(certificate,
id) ) ;
288 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 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.