libt2n: (gerd) add protocol version config, add server callbacks (not fully working...
[libt2n] / src / server.cpp
index 9187974..b1f7962 100644 (file)
@@ -66,6 +66,7 @@ void server_connection::reset_timeout()
 }
 
 server::server()
+    : callbacks(__events_end)
 {
     set_default_timeout(30);
     set_logging(NULL,none);
@@ -79,6 +80,23 @@ server::~server()
         delete i->second;
 }
 
+/** @brief add a callback
+
+    @param event event the function will be called at
+    @param func functor (see boost function) that will be called
+*/
+void server::add_callback(callback_event_type event, const boost::function<void (server_connection*)>& func)
+{
+    callbacks[event].push_back(func);
+}
+
+void server::do_callbacks(callback_event_type event, server_connection* conn)
+{
+    std::list<boost::function<void (server_connection*)> >::iterator i,ie=callbacks[event].end();
+    for (i=callbacks[event].begin(); i != ie; i++)
+        (*i)(conn);
+}
+
 int server::add_connection(server_connection* newconn)
 {
     unsigned int cid=next_id++;
@@ -88,6 +106,8 @@ int server::add_connection(server_connection* newconn)
 
     LOGSTREAM(debug,"new connection accepted, id: " << cid);
 
+    do_callbacks(new_connection,newconn);
+
     return cid;
 }