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