From 28cb45a5725e9c6054d7048a9bf969b9f2c94d64 Mon Sep 17 00:00:00 2001 From: Gerd v. Egidy Date: Fri, 27 Oct 2006 15:58:17 +0000 Subject: [PATCH] libt2n: (gerd) add protocol version config, add server callbacks (not fully working yet) --- configure.in | 3 +++ src/command_server.cpp | 15 +++++++++++++++ src/command_server.hxx | 6 +++--- src/server.cpp | 20 ++++++++++++++++++++ src/server.hxx | 11 +++++++++++ src/types.hxx | 4 ++++ 6 files changed, 56 insertions(+), 3 deletions(-) diff --git a/configure.in b/configure.in index 6e8b944..2522ab0 100644 --- a/configure.in +++ b/configure.in @@ -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 diff --git a/src/command_server.cpp b/src/command_server.cpp index 3bfc0e3..e7d6d4d 100644 --- a/src/command_server.cpp +++ b/src/command_server.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -27,6 +28,8 @@ #include #include +#include + #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) { diff --git a/src/command_server.hxx b/src/command_server.hxx index 7e475da..2f3df5a 100644 --- a/src/command_server.hxx +++ b/src/command_server.hxx @@ -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); }; } diff --git a/src/server.cpp b/src/server.cpp index 9187974..b1f7962 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -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& func) +{ + callbacks[event].push_back(func); +} + +void server::do_callbacks(callback_event_type event, server_connection* conn) +{ + std::list >::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; } diff --git a/src/server.hxx b/src/server.hxx index 172e98e..aca705b 100644 --- a/src/server.hxx +++ b/src/server.hxx @@ -22,6 +22,10 @@ #include #include #include +#include +#include + +#include #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 > > 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& 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 diff --git a/src/types.hxx b/src/types.hxx index ef42a47..2641485 100644 --- a/src/types.hxx +++ b/src/types.hxx @@ -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 -- 1.7.1