786770f236298e6fd081986f2677a42f0113abde
[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
26 #include "connection.hxx"
27 #include "types.hxx"
28
29 namespace libt2n
30 {
31
32 class server;
33
34 /**
35     @brief connection on a server
36
37     on a server every connection to a client is represented as server_connection.
38     a server_connection is abstract, derived classes like socket_server_connection are used.
39 */
40 class server_connection : public connection
41 {
42     friend class server;
43
44     private:
45         int timeout;
46         int last_action_time;
47         unsigned int connection_id;
48
49         void set_server(server* _my_server)
50             { my_server=_my_server; }
51
52         void set_id(unsigned int _connection_id)
53             { connection_id=_connection_id; }
54
55     protected:
56         server_connection(int _timeout)
57             : connection()
58         {
59             set_timeout(_timeout);
60             reset_timeout();
61             connection_id=0;
62             my_server=0;
63         }
64
65         server *my_server;
66
67     public:
68         /// check if timeout is expired, close connection if so
69         void check_timeout();
70
71         /// reset the timeout, e.g. if something is received
72         void reset_timeout();
73
74         void set_timeout(int _timeout)
75             { timeout=_timeout; }
76
77         /// get the id of this connection within the server object
78         unsigned int get_id()
79             { return connection_id; }
80 };
81
82 /**
83     @brief server base class
84
85     constitutes a server. is abstract, use derived classes like socket_server.
86 */
87 class server
88 {
89     private:
90         int default_timeout;
91         log_level_values log_level;
92         std::ostream *logstream;
93
94         unsigned int next_id;
95
96     protected:
97         std::map<unsigned int, server_connection*> connections;
98
99         server()
100         {
101             set_default_timeout(30);
102             set_logging(NULL,none);
103             next_id=1;
104         }
105
106         virtual bool fill_connection_buffers(void)=0;
107
108         int add_connection(server_connection* newconn);
109
110     public:
111         virtual ~server();
112
113         /// set the default timeout for new client connections
114         void set_default_timeout(int _default_timeout)
115             { default_timeout=_default_timeout; }
116
117         /// get the current default timeout for client connections
118         int get_default_timeout(void)
119             { return default_timeout; }
120
121         /// activate logging to the given stream. everything above the given level is logged.
122         void set_logging(std::ostream *_logstream, log_level_values _log_level)
123         {
124             log_level=_log_level;
125             logstream=_logstream;
126         }
127
128         server_connection* get_connection(unsigned int conn_id);
129
130         /** @brief look for new data on all open connections, accept new connections
131             @param usec_timeout wait until new data is found, max timeout usecs.
132                   -1: wait endless
133                    NULL: no timeout
134             @retval true if new data was found (does not mean that the received data 
135                     is a complete packet though)
136         */
137         virtual bool fill_buffer(long long usec_timeout=-1)=0;
138
139         void cleanup();
140
141         /** @brief get a complete data packet from any client. The packet is removed from the
142                    connection buffer.
143             @param[out] data the data package
144             @retval true if packet found
145         */
146         bool get_packet(std::string& data)
147             { unsigned int x; return get_packet(data,x); }
148
149         /** @brief get a complete data packet from any client. The packet is removed from the
150                    connection buffer.
151             @param[out] data the data package
152             @param[out] conn_id the connection id we got this packet from
153             @retval true if packet found
154         */
155         bool get_packet(std::string& data, unsigned int& conn_id);
156
157         /// write a message to the log if logging is enabled
158         void log(log_level_values level, const std::string& message)
159             { log(level,message.c_str()); }
160         /// write a message to the log if logging is enabled
161         void log(log_level_values level, const char* message);
162 };
163
164 }
165
166 #endif