libt2n: (gerd) add protocol version config, add server callbacks (not fully working...
authorGerd v. Egidy <gerd.von.egidy@intra2net.com>
Fri, 27 Oct 2006 15:58:17 +0000 (15:58 +0000)
committerGerd v. Egidy <gerd.von.egidy@intra2net.com>
Fri, 27 Oct 2006 15:58:17 +0000 (15:58 +0000)
configure.in
src/command_server.cpp
src/command_server.hxx
src/server.cpp
src/server.hxx
src/types.hxx

index 6e8b944..2522ab0 100644 (file)
@@ -1,8 +1,11 @@
 AC_INIT(configure.in)
 
 AM_CONFIG_HEADER(config.h)
+dnl AC_CONFIG_HEADERS([config.h:config.h.in])
 AM_INIT_AUTOMAKE(libt2n, 0.1)
 
+AC_DEFINE(PROTOCOL_VERSION, 1, [protocol version used (integers, increase version if incompatible)])
+
 AC_LANG_CPLUSPLUS
 AC_PROG_CXX
 AM_PROG_LIBTOOL
index 3bfc0e3..e7d6d4d 100644 (file)
@@ -20,6 +20,7 @@
 #include <string>
 #include <sstream>
 #include <stdexcept>
+#include <iostream>
 
 #include <boost/archive/binary_oarchive.hpp>
 #include <boost/archive/binary_iarchive.hpp>
@@ -27,6 +28,8 @@
 #include <boost/archive/xml_iarchive.hpp>
 #include <boost/serialization/serialization.hpp>
 
+#include <boost/bind.hpp>
+
 #include "command_server.hxx"
 #include "container.hxx"
 #include "log.hxx"
@@ -36,6 +39,18 @@ using namespace std;
 namespace libt2n
 {
 
+command_server::command_server(server& _s)
+    : s(_s)
+{
+    // register callback
+    s.add_callback(new_connection,bind(&command_server::new_connection_callback, boost::ref(*this), _1));
+}
+
+void command_server::new_connection_callback(server_connection* conn)
+{
+    cerr << "new connection callback: " << conn->get_id() << endl;
+}
+
 /// handle a command including deserialization and answering
 void command_server::handle_packet(const std::string& packet, server_connection* conn)
 {
index 7e475da..2f3df5a 100644 (file)
@@ -33,11 +33,11 @@ class command_server
         void handle_packet(const std::string& packet, server_connection* conn);
 
     public:
-        command_server(server& _s)
-            : s(_s)
-            { }
+        command_server(server& _s);
 
         void handle(long long usec_timeout=-1);
+
+        void new_connection_callback(server_connection* conn);
 };
 
 }
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;
 }
 
index 172e98e..aca705b 100644 (file)
 #include <iostream>
 #include <string>
 #include <map>
+#include <vector>
+#include <list>
+
+#include <boost/function.hpp>
 
 #include "connection.hxx"
 #include "types.hxx"
@@ -82,6 +86,9 @@ class server
         log_level_values log_level;
         std::ostream *logstream;
 
+        /// vector initialized for all callback-types, all elements in each list will be called
+        std::vector<std::list<boost::function<void (server_connection*)> > > callbacks;
+
         unsigned int next_id;
 
     protected:
@@ -93,6 +100,8 @@ class server
 
         int add_connection(server_connection* newconn);
 
+        void do_callbacks(callback_event_type event, server_connection* conn);
+
     public:
         virtual ~server();
 
@@ -108,6 +117,8 @@ class server
 
         server_connection* get_connection(unsigned int conn_id);
 
+        void add_callback(callback_event_type event, const boost::function<void (server_connection*)>& func);
+
         /** @brief look for new data on all open connections, accept new connections
             @param usec_timeout wait until new data is found, max timeout usecs.
                   -1: wait endless
index ef42a47..2641485 100644 (file)
@@ -28,6 +28,10 @@ enum log_level_values { none=0, error=1, debug=2, fulldebug=3 };
 /// possible types of a socket (tcp and unix)
 enum socket_type_value { tcp_s, unix_s };
 
+/// possible events for callback, __events_end must be the last event and must not be used 
+/// for anything else than marking the end
+enum callback_event_type { new_connection=0, connection_closed=1, connection_deleted=2, __events_end };
+
 }
 
 #endif