libt2n: (reinhard) make command_client class polymoprh by adding virtrual destructor...
[libt2n] / src / server.cpp
CommitLineData
ac7fdc22
GE
1/***************************************************************************
2 * Copyright (C) 2006 by Gerd v. Egidy *
3 * gve@intra2net.com *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License version *
7 * 2.1 as published by the Free Software Foundation. *
8 * *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
aa499d20 20#include <sstream>
04d86ba4 21#include <stdexcept>
aa499d20 22
6cda58a6
GE
23#include <boost/bind.hpp>
24
ac7fdc22 25#include "server.hxx"
a7170401 26#include "log.hxx"
ac7fdc22
GE
27
28namespace libt2n
29{
30
a7170401 31server_connection::server_connection(int _timeout)
6cda58a6 32 : connection(), callbacks(__events_end)
a7170401
GE
33{
34 set_timeout(_timeout);
35 reset_timeout();
36 connection_id=0;
37 my_server=0;
38}
39
6cda58a6
GE
40server_connection::~server_connection()
41{
42 // we want the connection_closed callbacks to be called before
43 close();
44
45 do_callbacks(connection_deleted);
46}
47
48void server_connection::close()
49{
50 if (!is_closed())
51 {
52 connection::close();
53 do_callbacks(connection_closed);
54 }
55}
a7170401
GE
56
57/// get pointer to logging stream, returns NULL if no logging needed
58std::ostream* server_connection::get_logstream(log_level_values level)
59{
60 if (my_server != NULL)
d535333f
GE
61 {
62 std::ostream* ostr=my_server->get_logstream(level);
63 if (ostr != NULL)
64 (*ostr) << "connection " << get_id() << ": ";
65 return ostr;
66 }
67 else
68 return NULL;
a7170401
GE
69}
70
71/// check if timeout is expired, close connection if so
a11e19b7 72void server_connection::check_timeout()
ac7fdc22 73{
a7170401
GE
74 if (timeout != -1 && last_action_time+timeout < time(NULL))
75 {
76 LOGSTREAM(debug,"timeout on connection " << connection_id << ", closing");
ac7fdc22 77 this->close();
a7170401 78 }
ac7fdc22
GE
79}
80
a7170401 81/// reset the timeout, e.g. if something is received
a11e19b7 82void server_connection::reset_timeout()
ac7fdc22
GE
83{
84 last_action_time=time(NULL);
85}
86
6cda58a6
GE
87/** @brief add a callback
88
89 @param event event the function will be called at
90 @param func functor (see boost function) that will be called
91
e1d0794d 92 @note use boost::bind to bind to member functions and parameters like this:
6cda58a6
GE
93 17 is a fixed parameter that is always added to the call
94 c.add_callback(connection_closed,bind(&my_class::func_to_call_back, boost::ref(*this), 17));
95*/
96void server_connection::add_callback(callback_event_type event, const boost::function<void ()>& func)
97{
98 if (event == new_connection)
04d86ba4 99 throw std::logic_error("new_connection callback not allowed for server_connections");
6cda58a6
GE
100
101 callbacks[event].push_back(func);
102}
103
104void server_connection::do_callbacks(callback_event_type event)
105{
106 std::list<boost::function<void ()> >::iterator i,ie=callbacks[event].end();
107 for (i=callbacks[event].begin(); i != ie; i++)
108 (*i)();
109}
110
a7170401 111server::server()
28cb45a5 112 : callbacks(__events_end)
a7170401
GE
113{
114 set_default_timeout(30);
115 set_logging(NULL,none);
116 next_id=1;
117}
118
ac7fdc22
GE
119server::~server()
120{
a11e19b7
GE
121 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
122 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
ac7fdc22
GE
123 delete i->second;
124}
125
28cb45a5
GE
126/** @brief add a callback
127
128 @param event event the function will be called at
129 @param func functor (see boost function) that will be called
6cda58a6 130
e1d0794d 131 @note use boost::bind to bind to member functions like this:
6cda58a6 132 s.add_callback(new_connection,bind(&my_class::func_to_call_back, boost::ref(*this), _1));
28cb45a5 133*/
6cda58a6 134void server::add_callback(callback_event_type event, const boost::function<void (unsigned int)>& func)
28cb45a5
GE
135{
136 callbacks[event].push_back(func);
6cda58a6
GE
137
138 // add callback to all existing connections
139 if (event != new_connection)
140 {
141 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
142 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
143 i->second->add_callback(event,bind(func, i->first));
144 }
28cb45a5
GE
145}
146
6cda58a6 147void server::do_callbacks(callback_event_type event, unsigned int conn_id)
28cb45a5 148{
6cda58a6 149 std::list<boost::function<void (unsigned int)> >::iterator i,ie=callbacks[event].end();
28cb45a5 150 for (i=callbacks[event].begin(); i != ie; i++)
6cda58a6 151 (*i)(conn_id);
28cb45a5
GE
152}
153
a11e19b7 154int server::add_connection(server_connection* newconn)
04e6b271 155{
aa499d20
GE
156 unsigned int cid=next_id++;
157 newconn->set_id(cid);
158 newconn->set_server(this);
159 connections[cid]=newconn;
a7170401 160
6cda58a6
GE
161 // add all callbacks
162 for(int e=connection_closed; e != __events_end; e++)
163 {
164 std::list<boost::function<void (unsigned int)> >::iterator i,ie=callbacks[e].end();
165 for (i=callbacks[e].begin(); i != ie; i++)
166 newconn->add_callback(static_cast<callback_event_type>(e),bind(*i,cid));
167 }
168
a7170401
GE
169 LOGSTREAM(debug,"new connection accepted, id: " << cid);
170
6cda58a6 171 do_callbacks(new_connection,cid);
28cb45a5 172
aa499d20 173 return cid;
04e6b271
GE
174}
175
a7170401
GE
176/// activate logging to the given stream. everything above the given level is logged.
177void server::set_logging(std::ostream *_logstream, log_level_values _log_level)
178{
179 log_level=_log_level;
180 logstream=_logstream;
181}
182
59adb9e2 183/**
94247295 184 @brief Gets a connection by id
59adb9e2 185
94247295 186 @param conn_id Connection ID
59adb9e2 187
94247295 188 @retval Pointer to connection object
59adb9e2 189*/
a11e19b7 190server_connection* server::get_connection(unsigned int conn_id)
ac7fdc22 191{
a11e19b7 192 std::map<unsigned int, server_connection*>::iterator p=connections.find(conn_id);
ac7fdc22
GE
193 if (p==connections.end())
194 return NULL;
195 else
196 return p->second;
197}
198
94247295 199/// check for timeouts, remove closed connections. don't forget to call this from time to time.
a11e19b7 200void server::cleanup()
ac7fdc22 201{
a11e19b7
GE
202 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
203 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
ac7fdc22 204 i->second->check_timeout();
a11e19b7
GE
205
206 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie;)
207 {
208 if (i->second->is_closed() && !i->second->packet_available())
209 {
210 // closed and no usable data in buffer -> remove
38de59c2 211 LOGSTREAM(debug,"removing connection " << i->first << " because it is closed and no more data waiting");
a7170401 212
a11e19b7
GE
213 delete i->second;
214 connections.erase(i);
215 i=connections.begin();
216 ie=connections.end();
217 }
218 else
219 i++;
220 }
ac7fdc22
GE
221}
222
a7170401
GE
223/** @brief get a complete data packet from any client. The packet is removed from the
224 connection buffer.
225 @param[out] data the data package
226 @param[out] conn_id the connection id we got this packet from
227 @retval true if packet found
228*/
ac7fdc22
GE
229bool server::get_packet(std::string& data, unsigned int& conn_id)
230{
231 // todo: this is somehow unfair: the first connections in the map get checked more
232 // often than the others and can thus block them out
233
a11e19b7
GE
234 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
235 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
236 if (i->second->get_packet(data))
237 {
a7170401
GE
238 LOGSTREAM(debug,"got packet (" << data.size() << " bytes) from connection " << i->first);
239
a11e19b7 240 conn_id=i->first;
ac7fdc22 241 return true;
a11e19b7
GE
242 }
243
244 return false;
ac7fdc22
GE
245}
246
a7170401
GE
247/// get pointer to logging stream, returns NULL if no logging needed
248std::ostream* server::get_logstream(log_level_values level)
ac7fdc22 249{
d535333f 250 if (logstream && log_level >= level)
a7170401 251 return logstream;
d535333f
GE
252 else
253 return NULL;
ac7fdc22 254}
ac7fdc22 255};