| 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 | |
| 20 | #include <sstream> |
| 21 | #include <stdexcept> |
| 22 | #include <time.h> |
| 23 | |
| 24 | #include <boost/bind.hpp> |
| 25 | |
| 26 | #include "server.hxx" |
| 27 | #include "log.hxx" |
| 28 | |
| 29 | namespace libt2n |
| 30 | { |
| 31 | |
| 32 | server_connection::server_connection(int _timeout) |
| 33 | : connection() |
| 34 | { |
| 35 | set_timeout(_timeout); |
| 36 | reset_timeout(); |
| 37 | connection_id=0; |
| 38 | my_server=0; |
| 39 | } |
| 40 | |
| 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) |
| 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; |
| 53 | } |
| 54 | |
| 55 | /// check if timeout is expired, close connection if so |
| 56 | void server_connection::check_timeout() |
| 57 | { |
| 58 | if (timeout != -1 && last_action_time+timeout < time(NULL)) |
| 59 | { |
| 60 | LOGSTREAM(debug,"timeout on connection " << connection_id << ", closing"); |
| 61 | this->close(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// reset the timeout, e.g. if something is received |
| 66 | void server_connection::reset_timeout() |
| 67 | { |
| 68 | last_action_time=time(NULL); |
| 69 | } |
| 70 | |
| 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 | */ |
| 76 | void server_connection::add_callback(callback_event_type event, const boost::function<void ()>& func) |
| 77 | { |
| 78 | if (event == new_connection) |
| 79 | throw std::logic_error("new_connection callback not allowed for server_connections"); |
| 80 | |
| 81 | connection::add_callback(event,func); |
| 82 | } |
| 83 | |
| 84 | server::server() |
| 85 | : callbacks(__events_end) |
| 86 | { |
| 87 | set_default_timeout(30); |
| 88 | set_logging(NULL,none); |
| 89 | next_id=1; |
| 90 | } |
| 91 | |
| 92 | server::~server() |
| 93 | { |
| 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++) |
| 96 | delete i->second; |
| 97 | } |
| 98 | |
| 99 | /** @brief add a callback |
| 100 | |
| 101 | @param event event the function will be called at |
| 102 | @param func functor (see boost::function) that will be called |
| 103 | |
| 104 | @note use boost::bind to bind to member functions like this: |
| 105 | s.add_callback(new_connection,boost::bind(&my_class::func_to_call_back, boost::ref(*this), _1)); |
| 106 | */ |
| 107 | void server::add_callback(callback_event_type event, const boost::function<void (unsigned int)>& func) |
| 108 | { |
| 109 | callbacks[event].push_back(func); |
| 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++) |
| 116 | i->second->add_callback(event,boost::bind(func, i->first)); |
| 117 | } |
| 118 | } |
| 119 | |
| 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 | */ |
| 126 | void server::do_callbacks(callback_event_type event, unsigned int conn_id) |
| 127 | { |
| 128 | std::list<boost::function<void (unsigned int)> >::iterator i,ie=callbacks[event].end(); |
| 129 | for (i=callbacks[event].begin(); i != ie; i++) |
| 130 | (*i)(conn_id); |
| 131 | } |
| 132 | |
| 133 | /// add a new connection to the server |
| 134 | int server::add_connection(server_connection* newconn) |
| 135 | { |
| 136 | unsigned int cid=next_id++; |
| 137 | newconn->set_id(cid); |
| 138 | newconn->set_server(this); |
| 139 | connections[cid]=newconn; |
| 140 | |
| 141 | // add all callbacks except new_connection |
| 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 | |
| 149 | LOGSTREAM(debug,"new connection accepted, id: " << cid); |
| 150 | |
| 151 | do_callbacks(new_connection,cid); |
| 152 | |
| 153 | return cid; |
| 154 | } |
| 155 | |
| 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 | |
| 163 | /** |
| 164 | @brief Gets a connection by id |
| 165 | |
| 166 | @param conn_id Connection ID |
| 167 | |
| 168 | @retval Pointer to connection object |
| 169 | */ |
| 170 | server_connection* server::get_connection(unsigned int conn_id) |
| 171 | { |
| 172 | std::map<unsigned int, server_connection*>::iterator p=connections.find(conn_id); |
| 173 | if (p==connections.end()) |
| 174 | return NULL; |
| 175 | else |
| 176 | return p->second; |
| 177 | } |
| 178 | |
| 179 | /// check for timeouts, remove closed connections. don't forget to call this from time to time. |
| 180 | void server::cleanup() |
| 181 | { |
| 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++) |
| 184 | i->second->check_timeout(); |
| 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 |
| 191 | LOGSTREAM(debug,"removing connection " << i->first << " because it is closed and no more data waiting"); |
| 192 | |
| 193 | delete i->second; |
| 194 | connections.erase(i); |
| 195 | i=connections.begin(); |
| 196 | ie=connections.end(); |
| 197 | } |
| 198 | else |
| 199 | i++; |
| 200 | } |
| 201 | } |
| 202 | |
| 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 | */ |
| 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 | |
| 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 | { |
| 218 | LOGSTREAM(debug,"got packet (" << data.size() << " bytes) from connection " << i->first); |
| 219 | |
| 220 | conn_id=i->first; |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | /// get pointer to logging stream, returns NULL if no logging needed |
| 228 | std::ostream* server::get_logstream(log_level_values level) |
| 229 | { |
| 230 | if (logstream && log_level >= level) |
| 231 | return logstream; |
| 232 | else |
| 233 | return NULL; |
| 234 | } |
| 235 | }; |