started docu
[libt2n] / src / client.cpp
CommitLineData
a11e19b7
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
20#include <sstream>
21
22#include "client.hxx"
23
24namespace libt2n
25{
26
a7170401 27client_connection::client_connection()
91730468 28 : connection(), callbacks(__events_end)
a7170401
GE
29{
30 set_logging(NULL,none);
31}
32
91730468
GE
33client_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
41void client_connection::close()
42{
43 if (!is_closed())
44 {
45 connection::close();
46 do_callbacks(connection_closed);
47 }
48}
49
a930cc99
JT
50/// add a callback
51/**
91730468
GE
52 @param event event the function will be called at
53 @param func functor (see boost function) that will be called
91730468
GE
54*/
55void client_connection::add_callback(callback_event_type event, const boost::function<void ()>& func)
56{
57 callbacks[event].push_back(func);
58}
59
a930cc99
JT
60/**
61 @example callback use boost::bind to bind to member functions and parameters like this:
62 int this example 17 is a fixed parameter that is always added to the call
63 c.add_callback(connection_closed,bind(&my_class::func_to_call_back, boost::ref(*this), 17));
64*/
65
66
91730468
GE
67void 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
a7170401
GE
74/// get pointer to logging stream, returns NULL if no logging needed
75std::ostream* client_connection::get_logstream(log_level_values level)
76{
d535333f 77 if (logstream && log_level >= level)
a7170401 78 return logstream;
d535333f
GE
79 else
80 return NULL;
a7170401
GE
81}
82
83/// activate logging to the given stream. everything above the given level is logged.
84void client_connection::set_logging(std::ostream *_logstream, log_level_values _log_level)
85{
86 log_level=_log_level;
87 logstream=_logstream;
88}
a11e19b7
GE
89
90};