00001 /*************************************************************************** 00002 * Copyright (C) 2006 by Gerd v. Egidy * 00003 * gve@intra2net.com * 00004 * * 00005 * This library is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU Lesser General Public License version * 00007 * 2.1 as published by the Free Software Foundation. * 00008 * * 00009 * This library is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00012 * GNU Lesser General Public License for more details. * 00013 * * 00014 * You should have received a copy of the GNU Lesser General Public * 00015 * License along with this program; if not, write to the * 00016 * Free Software Foundation, Inc., * 00017 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00018 ***************************************************************************/ 00019 #ifndef __LIBT2N_SERVER 00020 #define __LIBT2N_SERVER 00021 00022 #include <iostream> 00023 #include <string> 00024 #include <map> 00025 #include <vector> 00026 #include <list> 00027 00028 #include <boost/function.hpp> 00029 00030 #include "connection.hxx" 00031 #include "types.hxx" 00032 00033 namespace libt2n 00034 { 00035 00036 class server; 00037 00044 class server_connection : public connection 00045 { 00046 friend class server; 00047 00048 private: 00049 int timeout; 00050 int last_action_time; 00051 unsigned int connection_id; 00052 00053 void set_server(server* _my_server) 00054 { my_server=_my_server; } 00055 00056 void set_id(unsigned int _connection_id) 00057 { connection_id=_connection_id; } 00058 00059 protected: 00060 server *my_server; 00061 00062 server_connection(int _timeout); 00063 00064 std::ostream* get_logstream(log_level_values level); 00065 00066 public: 00067 void check_timeout(); 00068 void reset_timeout(); 00069 void set_timeout(int _timeout) 00070 { timeout=_timeout; } 00071 00073 unsigned int get_id() 00074 { return connection_id; } 00075 00076 void add_callback(callback_event_type event, const boost::function<void ()>& func); 00077 }; 00078 00084 class server 00085 { 00086 private: 00087 int default_timeout; 00088 log_level_values log_level; 00089 std::ostream *logstream; 00090 00092 std::vector<std::list<boost::function<void (unsigned int)> > > callbacks; 00093 00094 unsigned int next_id; 00095 00096 protected: 00097 std::map<unsigned int, server_connection*> connections; 00098 00099 server(); 00100 00101 virtual bool fill_connection_buffers(void)=0; 00102 00103 int add_connection(server_connection* newconn); 00104 00105 void do_callbacks(callback_event_type event, unsigned int conn_id); 00106 00107 public: 00108 virtual ~server(); 00109 00111 void set_default_timeout(int _default_timeout) 00112 { default_timeout=_default_timeout; } 00113 00115 int get_default_timeout(void) 00116 { return default_timeout; } 00117 00118 void set_logging(std::ostream *_logstream, log_level_values _log_level); 00119 00120 server_connection* get_connection(unsigned int conn_id); 00121 00122 void add_callback(callback_event_type event, const boost::function<void (unsigned int)>& func); 00123 00133 virtual bool fill_buffer(long long usec_timeout=-1, long long* usec_timeout_remaining=NULL)=0; 00134 00135 void cleanup(); 00136 00142 bool get_packet(std::string& data) 00143 { unsigned int x; return get_packet(data,x); } 00144 00145 bool get_packet(std::string& data, unsigned int& conn_id); 00146 00147 std::ostream* get_logstream(log_level_values level); 00148 }; 00149 00150 } 00151 00152 #endif
1.5.6