libt2n: (reinhard) compile fixes.
[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>
f3823a13 22#include <time.h>
aa499d20 23
6cda58a6
GE
24#include <boost/bind.hpp>
25
ac7fdc22 26#include "server.hxx"
a7170401 27#include "log.hxx"
ac7fdc22
GE
28
29namespace libt2n
30{
31
a7170401 32server_connection::server_connection(int _timeout)
a64066eb 33 : connection()
a7170401
GE
34{
35 set_timeout(_timeout);
36 reset_timeout();
37 connection_id=0;
38 my_server=0;
39}
40
a7170401
GE
41/// get pointer to logging stream, returns NULL if no logging needed
42std::ostream* server_connection::get_logstream(log_level_values level)
43{
44 if (my_server != NULL)
d535333f
GE
45 {
46 std::ostream* ostr=my_server->get_logstream(level);
47 if (ostr != NULL)
48 (*ostr) << "connection " << get_id() << ": ";
49 return ostr;
50 }
51 else
52 return NULL;
a7170401
GE
53}
54
55/// check if timeout is expired, close connection if so
a11e19b7 56void server_connection::check_timeout()
ac7fdc22 57{
a7170401
GE
58 if (timeout != -1 && last_action_time+timeout < time(NULL))
59 {
60 LOGSTREAM(debug,"timeout on connection " << connection_id << ", closing");
ac7fdc22 61 this->close();
a7170401 62 }
ac7fdc22
GE
63}
64
a7170401 65/// reset the timeout, e.g. if something is received
a11e19b7 66void server_connection::reset_timeout()
ac7fdc22
GE
67{
68 last_action_time=time(NULL);
69}
70
6cda58a6
GE
71void server_connection::add_callback(callback_event_type event, const boost::function<void ()>& func)
72{
73 if (event == new_connection)
04d86ba4 74 throw std::logic_error("new_connection callback not allowed for server_connections");
6cda58a6 75
a64066eb 76 connection::add_callback(event,func);
6cda58a6
GE
77}
78
a7170401 79server::server()
28cb45a5 80 : callbacks(__events_end)
a7170401
GE
81{
82 set_default_timeout(30);
83 set_logging(NULL,none);
84 next_id=1;
85}
86
ac7fdc22
GE
87server::~server()
88{
a11e19b7
GE
89 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
90 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
ac7fdc22
GE
91 delete i->second;
92}
93
28cb45a5
GE
94/** @brief add a callback
95
96 @param event event the function will be called at
97 @param func functor (see boost function) that will be called
6cda58a6 98
e1d0794d 99 @note use boost::bind to bind to member functions like this:
6cda58a6 100 s.add_callback(new_connection,bind(&my_class::func_to_call_back, boost::ref(*this), _1));
28cb45a5 101*/
6cda58a6 102void server::add_callback(callback_event_type event, const boost::function<void (unsigned int)>& func)
28cb45a5
GE
103{
104 callbacks[event].push_back(func);
6cda58a6
GE
105
106 // add callback to all existing connections
107 if (event != new_connection)
108 {
109 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
110 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
111 i->second->add_callback(event,bind(func, i->first));
112 }
28cb45a5
GE
113}
114
6cda58a6 115void server::do_callbacks(callback_event_type event, unsigned int conn_id)
28cb45a5 116{
6cda58a6 117 std::list<boost::function<void (unsigned int)> >::iterator i,ie=callbacks[event].end();
28cb45a5 118 for (i=callbacks[event].begin(); i != ie; i++)
6cda58a6 119 (*i)(conn_id);
28cb45a5
GE
120}
121
a11e19b7 122int server::add_connection(server_connection* newconn)
04e6b271 123{
aa499d20
GE
124 unsigned int cid=next_id++;
125 newconn->set_id(cid);
126 newconn->set_server(this);
127 connections[cid]=newconn;
a7170401 128
af84dfb5 129 // add all callbacks except new_connection
6cda58a6
GE
130 for(int e=connection_closed; e != __events_end; e++)
131 {
132 std::list<boost::function<void (unsigned int)> >::iterator i,ie=callbacks[e].end();
133 for (i=callbacks[e].begin(); i != ie; i++)
134 newconn->add_callback(static_cast<callback_event_type>(e),bind(*i,cid));
135 }
136
a7170401
GE
137 LOGSTREAM(debug,"new connection accepted, id: " << cid);
138
6cda58a6 139 do_callbacks(new_connection,cid);
28cb45a5 140
aa499d20 141 return cid;
04e6b271
GE
142}
143
a7170401
GE
144/// activate logging to the given stream. everything above the given level is logged.
145void server::set_logging(std::ostream *_logstream, log_level_values _log_level)
146{
147 log_level=_log_level;
148 logstream=_logstream;
149}
150
59adb9e2 151/**
94247295 152 @brief Gets a connection by id
59adb9e2 153
94247295 154 @param conn_id Connection ID
59adb9e2 155
94247295 156 @retval Pointer to connection object
59adb9e2 157*/
a11e19b7 158server_connection* server::get_connection(unsigned int conn_id)
ac7fdc22 159{
a11e19b7 160 std::map<unsigned int, server_connection*>::iterator p=connections.find(conn_id);
ac7fdc22
GE
161 if (p==connections.end())
162 return NULL;
163 else
164 return p->second;
165}
166
94247295 167/// check for timeouts, remove closed connections. don't forget to call this from time to time.
a11e19b7 168void server::cleanup()
ac7fdc22 169{
a11e19b7
GE
170 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
171 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
ac7fdc22 172 i->second->check_timeout();
a11e19b7
GE
173
174 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie;)
175 {
176 if (i->second->is_closed() && !i->second->packet_available())
177 {
178 // closed and no usable data in buffer -> remove
38de59c2 179 LOGSTREAM(debug,"removing connection " << i->first << " because it is closed and no more data waiting");
a7170401 180
a11e19b7
GE
181 delete i->second;
182 connections.erase(i);
183 i=connections.begin();
184 ie=connections.end();
185 }
186 else
187 i++;
188 }
ac7fdc22
GE
189}
190
a7170401
GE
191/** @brief get a complete data packet from any client. The packet is removed from the
192 connection buffer.
193 @param[out] data the data package
194 @param[out] conn_id the connection id we got this packet from
195 @retval true if packet found
196*/
ac7fdc22
GE
197bool server::get_packet(std::string& data, unsigned int& conn_id)
198{
199 // todo: this is somehow unfair: the first connections in the map get checked more
200 // often than the others and can thus block them out
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++)
204 if (i->second->get_packet(data))
205 {
a7170401
GE
206 LOGSTREAM(debug,"got packet (" << data.size() << " bytes) from connection " << i->first);
207
a11e19b7 208 conn_id=i->first;
ac7fdc22 209 return true;
a11e19b7
GE
210 }
211
212 return false;
ac7fdc22
GE
213}
214
a7170401
GE
215/// get pointer to logging stream, returns NULL if no logging needed
216std::ostream* server::get_logstream(log_level_values level)
ac7fdc22 217{
d535333f 218 if (logstream && log_level >= level)
a7170401 219 return logstream;
d535333f
GE
220 else
221 return NULL;
ac7fdc22 222}
ac7fdc22 223};