00001 /* 00002 Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy 00003 00004 The software in this package is distributed under the GNU General 00005 Public License version 2 (with a special exception described below). 00006 00007 A copy of GNU General Public License (GPL) is included in this distribution, 00008 in the file COPYING.GPL. 00009 00010 As a special exception, if other files instantiate templates or use macros 00011 or inline functions from this file, or you compile this file and link it 00012 with other works to produce a work based on this file, this file 00013 does not by itself cause the resulting work to be covered 00014 by the GNU General Public License. 00015 00016 However the source code for this file must still be made available 00017 in accordance with section (3) of the GNU General Public License. 00018 00019 This exception does not invalidate any other reasons why a work based 00020 on this file might be covered by the GNU General Public License. 00021 */ 00022 #ifndef __LIBT2N_SERVER 00023 #define __LIBT2N_SERVER 00024 00025 #include <iostream> 00026 #include <string> 00027 #include <map> 00028 #include <vector> 00029 #include <list> 00030 00031 #include <boost/function.hpp> 00032 00033 #include "connection.hxx" 00034 #include "types.hxx" 00035 00036 namespace libt2n 00037 { 00038 00039 class server; 00040 00047 class server_connection : public connection 00048 { 00049 friend class server; 00050 00051 private: 00052 int timeout; 00053 int last_action_time; 00054 unsigned int connection_id; 00055 00056 void set_server(server* _my_server) 00057 { my_server=_my_server; } 00058 00059 void set_id(unsigned int _connection_id) 00060 { connection_id=_connection_id; } 00061 00062 protected: 00063 server *my_server; 00064 00065 server_connection(int _timeout); 00066 virtual ~server_connection(); 00067 00068 std::ostream* get_logstream(log_level_values level); 00069 00070 public: 00071 void check_timeout(); 00072 void reset_timeout(); 00073 void set_timeout(int _timeout) 00074 { timeout=_timeout; } 00075 00077 unsigned int get_id() 00078 { return connection_id; } 00079 00080 void add_callback(callback_event_type event, const boost::function<void ()>& func); 00081 }; 00082 00088 class server 00089 { 00090 private: 00091 int default_timeout; 00092 log_level_values log_level; 00093 std::ostream *logstream; 00094 00096 std::vector<std::list<boost::function<void (unsigned int)> > > callbacks; 00097 00098 unsigned int next_id; 00099 00100 protected: 00101 std::map<unsigned int, server_connection*> connections; 00102 00103 server(); 00104 00105 virtual bool fill_connection_buffers(void)=0; 00106 00107 unsigned int add_connection(server_connection* newconn); 00108 00109 void do_callbacks(callback_event_type event, unsigned int conn_id); 00110 00111 public: 00112 virtual ~server(); 00113 00115 void set_default_timeout(int _default_timeout) 00116 { default_timeout=_default_timeout; } 00117 00119 int get_default_timeout(void) 00120 { return default_timeout; } 00121 00122 void set_logging(std::ostream *_logstream, log_level_values _log_level); 00123 00124 server_connection* get_connection(unsigned int conn_id); 00125 00126 void add_callback(callback_event_type event, const boost::function<void (unsigned int)>& func); 00127 00137 virtual bool fill_buffer(long long usec_timeout=-1, long long* usec_timeout_remaining=NULL)=0; 00138 00139 void close(); 00140 00141 void cleanup(); 00142 00148 bool get_packet(std::string& data) 00149 { unsigned int x; return get_packet(data,x); } 00150 00151 bool get_packet(std::string& data, unsigned int& conn_id); 00152 00153 std::ostream* get_logstream(log_level_values level); 00154 }; 00155 00156 } 00157 00158 #endif