| 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 | |
| 22 | #include "client.hxx" |
| 23 | |
| 24 | namespace libt2n |
| 25 | { |
| 26 | |
| 27 | client_connection::client_connection() |
| 28 | : connection(), callbacks(__events_end) |
| 29 | { |
| 30 | set_logging(NULL,none); |
| 31 | } |
| 32 | |
| 33 | client_connection::~client_connection() |
| 34 | { |
| 35 | // we want the connection_closed callbacks to be called before |
| 36 | close(); |
| 37 | |
| 38 | do_callbacks(connection_deleted); |
| 39 | } |
| 40 | |
| 41 | void client_connection::close() |
| 42 | { |
| 43 | if (!is_closed()) |
| 44 | { |
| 45 | connection::close(); |
| 46 | do_callbacks(connection_closed); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// add a callback |
| 51 | /** |
| 52 | @param event event the function will be called at |
| 53 | @param func functor (see boost function) that will be called |
| 54 | @note use boost::bind to bind to member functions and parameters like this: |
| 55 | @verbatim |
| 56 | // in this example 17 is a fixed parameter that is always added to the call |
| 57 | c.add_callback(connection_closed,bind(&my_class::func_to_call_back, boost::ref(*this), 17)); |
| 58 | @endverbatim |
| 59 | */ |
| 60 | void client_connection::add_callback(callback_event_type event, const boost::function<void ()>& func) |
| 61 | { |
| 62 | callbacks[event].push_back(func); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | void client_connection::do_callbacks(callback_event_type event) |
| 68 | { |
| 69 | std::list<boost::function<void ()> >::iterator i,ie=callbacks[event].end(); |
| 70 | for (i=callbacks[event].begin(); i != ie; i++) |
| 71 | (*i)(); |
| 72 | } |
| 73 | |
| 74 | /// get pointer to logging stream, returns NULL if no logging needed |
| 75 | std::ostream* client_connection::get_logstream(log_level_values level) |
| 76 | { |
| 77 | if (logstream && log_level >= level) |
| 78 | return logstream; |
| 79 | else |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | /// activate logging to the given stream. everything above the given level is logged. |
| 84 | void client_connection::set_logging(std::ostream *_logstream, log_level_values _log_level) |
| 85 | { |
| 86 | log_level=_log_level; |
| 87 | logstream=_logstream; |
| 88 | } |
| 89 | |
| 90 | }; |