738a86978e15ba1dda72c92c3e67f3a8c52d8653
[libt2n] / src / server.hxx
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 #ifndef __LIBT2N_SERVER
20 #define __LIBT2N_SERVER
21
22 #include <iostream>
23 #include <string>
24 #include <map>
25 #include <vector>
26 #include <list>
27
28 #include <boost/function.hpp>
29
30 #include "connection.hxx"
31 #include "types.hxx"
32
33 namespace libt2n
34 {
35
36 class server;
37
38 /**
39     @brief connection on a server
40
41     on a server every connection to a client is represented as server_connection.
42     a server_connection is abstract, derived classes like socket_server_connection are used.
43 */
44 class server_connection : public connection
45 {
46     friend class server;
47
48     private:
49         int timeout;
50         int last_action_time;
51         unsigned int connection_id;
52
53         void set_server(server* _my_server)
54             { my_server=_my_server; }
55
56         void set_id(unsigned int _connection_id)
57             { connection_id=_connection_id; }
58
59     protected:
60         server *my_server;
61
62         server_connection(int _timeout);
63         virtual ~server_connection();
64
65         std::ostream* get_logstream(log_level_values level);
66
67     public:
68         void check_timeout();
69         void reset_timeout();
70         void set_timeout(int _timeout)
71             { timeout=_timeout; }
72
73         /// get the id of this connection within the server object
74         unsigned int get_id()
75             { return connection_id; }
76
77         void add_callback(callback_event_type event, const boost::function<void ()>& func);
78 };
79
80 /**
81     @brief server base class
82
83     constitutes a server. is abstract, use derived classes like socket_server.
84 */
85 class server
86 {
87     private:
88         int default_timeout;
89         log_level_values log_level;
90         std::ostream *logstream;
91
92         /// vector initialized for all callback-types, all elements in each list will be called
93         std::vector<std::list<boost::function<void (unsigned int)> > > callbacks;
94
95         unsigned int next_id;
96
97     protected:
98         std::map<unsigned int, server_connection*> connections;
99
100         server();
101
102         virtual bool fill_connection_buffers(void)=0;
103
104         unsigned int add_connection(server_connection* newconn);
105
106         void do_callbacks(callback_event_type event, unsigned int conn_id);
107
108     public:
109         virtual ~server();
110
111         /// set the default timeout for new client connections
112         void set_default_timeout(int _default_timeout)
113             { default_timeout=_default_timeout; }
114
115         /// get the current default timeout for client connections
116         int get_default_timeout(void)
117             { return default_timeout; }
118
119         void set_logging(std::ostream *_logstream, log_level_values _log_level);
120
121         server_connection* get_connection(unsigned int conn_id);
122
123         void add_callback(callback_event_type event, const boost::function<void (unsigned int)>& func);
124
125         /** @brief look for new data and store it in the local buffer
126             @param usec_timeout wait until new data is found, max timeout usecs.
127                   -1: wait endless
128                    0: return instantly
129             @param usec_timeout_remaining if non-NULL the function will write the
130                   not used time to the given target
131             @retval true if new data was found (does not mean that the received data 
132                     is a complete packet though)
133         */
134         virtual bool fill_buffer(long long usec_timeout=-1, long long* usec_timeout_remaining=NULL)=0;
135
136         void close();
137
138         void cleanup();
139
140         /** @brief get a complete data packet from any client. The packet is removed from the
141                    connection buffer.
142             @param[out] data the data package
143             @retval true if packet found
144         */
145         bool get_packet(std::string& data)
146             { unsigned int x; return get_packet(data,x); }
147
148         bool get_packet(std::string& data, unsigned int& conn_id);
149
150         std::ostream* get_logstream(log_level_values level);
151 };
152
153 }
154
155 #endif