(no commit message)
[libt2n] / src / client.cpp
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 /** @brief 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
55     @example use boost::bind to bind to member functions and parameters like this:
56         int 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 */
59 void client_connection::add_callback(callback_event_type event, const boost::function<void ()>& func)
60 {
61     callbacks[event].push_back(func);
62 }
63
64 void client_connection::do_callbacks(callback_event_type event)
65 {
66     std::list<boost::function<void ()> >::iterator i,ie=callbacks[event].end();
67     for (i=callbacks[event].begin(); i != ie; i++)
68         (*i)();
69 }
70
71 /// get pointer to logging stream, returns NULL if no logging needed
72 std::ostream* client_connection::get_logstream(log_level_values level)
73 {
74     if (logstream && log_level >= level)
75         return logstream;
76     else
77         return NULL;
78 }
79
80 /// activate logging to the given stream. everything above the given level is logged.
81 void client_connection::set_logging(std::ostream *_logstream, log_level_values _log_level)
82 {
83     log_level=_log_level;
84     logstream=_logstream;
85 }
86
87 };