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 GE |
20 | #include <sstream> |
21 | ||
ac7fdc22 | 22 | #include "server.hxx" |
a7170401 | 23 | #include "log.hxx" |
ac7fdc22 GE |
24 | |
25 | namespace libt2n | |
26 | { | |
27 | ||
a7170401 GE |
28 | server_connection::server_connection(int _timeout) |
29 | : connection() | |
30 | { | |
31 | set_timeout(_timeout); | |
32 | reset_timeout(); | |
33 | connection_id=0; | |
34 | my_server=0; | |
35 | } | |
36 | ||
37 | ||
38 | /// get pointer to logging stream, returns NULL if no logging needed | |
39 | std::ostream* server_connection::get_logstream(log_level_values level) | |
40 | { | |
41 | if (my_server != NULL) | |
42 | return my_server->get_logstream(level); | |
43 | } | |
44 | ||
45 | /// check if timeout is expired, close connection if so | |
a11e19b7 | 46 | void server_connection::check_timeout() |
ac7fdc22 | 47 | { |
a7170401 GE |
48 | if (timeout != -1 && last_action_time+timeout < time(NULL)) |
49 | { | |
50 | LOGSTREAM(debug,"timeout on connection " << connection_id << ", closing"); | |
ac7fdc22 | 51 | this->close(); |
a7170401 | 52 | } |
ac7fdc22 GE |
53 | } |
54 | ||
a7170401 | 55 | /// reset the timeout, e.g. if something is received |
a11e19b7 | 56 | void server_connection::reset_timeout() |
ac7fdc22 GE |
57 | { |
58 | last_action_time=time(NULL); | |
59 | } | |
60 | ||
a7170401 GE |
61 | server::server() |
62 | { | |
63 | set_default_timeout(30); | |
64 | set_logging(NULL,none); | |
65 | next_id=1; | |
66 | } | |
67 | ||
ac7fdc22 GE |
68 | server::~server() |
69 | { | |
a11e19b7 GE |
70 | std::map<unsigned int, server_connection*>::iterator ie=connections.end(); |
71 | for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++) | |
ac7fdc22 GE |
72 | delete i->second; |
73 | } | |
74 | ||
a11e19b7 | 75 | int server::add_connection(server_connection* newconn) |
04e6b271 | 76 | { |
aa499d20 GE |
77 | unsigned int cid=next_id++; |
78 | newconn->set_id(cid); | |
79 | newconn->set_server(this); | |
80 | connections[cid]=newconn; | |
a7170401 GE |
81 | |
82 | LOGSTREAM(debug,"new connection accepted, id: " << cid); | |
83 | ||
aa499d20 | 84 | return cid; |
04e6b271 GE |
85 | } |
86 | ||
a7170401 GE |
87 | /// activate logging to the given stream. everything above the given level is logged. |
88 | void server::set_logging(std::ostream *_logstream, log_level_values _log_level) | |
89 | { | |
90 | log_level=_log_level; | |
91 | logstream=_logstream; | |
92 | } | |
93 | ||
59adb9e2 | 94 | /** |
94247295 | 95 | @brief Gets a connection by id |
59adb9e2 | 96 | |
94247295 | 97 | @param conn_id Connection ID |
59adb9e2 | 98 | |
94247295 | 99 | @retval Pointer to connection object |
59adb9e2 | 100 | */ |
a11e19b7 | 101 | server_connection* server::get_connection(unsigned int conn_id) |
ac7fdc22 | 102 | { |
a11e19b7 | 103 | std::map<unsigned int, server_connection*>::iterator p=connections.find(conn_id); |
ac7fdc22 GE |
104 | if (p==connections.end()) |
105 | return NULL; | |
106 | else | |
107 | return p->second; | |
108 | } | |
109 | ||
94247295 | 110 | /// check for timeouts, remove closed connections. don't forget to call this from time to time. |
a11e19b7 | 111 | void server::cleanup() |
ac7fdc22 | 112 | { |
a11e19b7 GE |
113 | std::map<unsigned int, server_connection*>::iterator ie=connections.end(); |
114 | for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++) | |
ac7fdc22 | 115 | i->second->check_timeout(); |
a11e19b7 GE |
116 | |
117 | for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie;) | |
118 | { | |
119 | if (i->second->is_closed() && !i->second->packet_available()) | |
120 | { | |
121 | // closed and no usable data in buffer -> remove | |
a7170401 GE |
122 | LOGSTREAM(debug,"removing conneciton " << i->first << " because it is closed and no more data waiting"); |
123 | ||
a11e19b7 GE |
124 | delete i->second; |
125 | connections.erase(i); | |
126 | i=connections.begin(); | |
127 | ie=connections.end(); | |
128 | } | |
129 | else | |
130 | i++; | |
131 | } | |
ac7fdc22 GE |
132 | } |
133 | ||
a7170401 GE |
134 | /** @brief get a complete data packet from any client. The packet is removed from the |
135 | connection buffer. | |
136 | @param[out] data the data package | |
137 | @param[out] conn_id the connection id we got this packet from | |
138 | @retval true if packet found | |
139 | */ | |
ac7fdc22 GE |
140 | bool server::get_packet(std::string& data, unsigned int& conn_id) |
141 | { | |
142 | // todo: this is somehow unfair: the first connections in the map get checked more | |
143 | // often than the others and can thus block them out | |
144 | ||
a11e19b7 GE |
145 | std::map<unsigned int, server_connection*>::iterator ie=connections.end(); |
146 | for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++) | |
147 | if (i->second->get_packet(data)) | |
148 | { | |
a7170401 GE |
149 | LOGSTREAM(debug,"got packet (" << data.size() << " bytes) from connection " << i->first); |
150 | ||
a11e19b7 | 151 | conn_id=i->first; |
ac7fdc22 | 152 | return true; |
a11e19b7 GE |
153 | } |
154 | ||
155 | return false; | |
ac7fdc22 GE |
156 | } |
157 | ||
a7170401 GE |
158 | /// get pointer to logging stream, returns NULL if no logging needed |
159 | std::ostream* server::get_logstream(log_level_values level) | |
ac7fdc22 GE |
160 | { |
161 | if (logstream && level >= log_level) | |
a7170401 | 162 | return logstream; |
ac7fdc22 | 163 | } |
ac7fdc22 | 164 | }; |