libt2n: (gerd) fixes, new logging concept (not working yet)
[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 *my_server;
57
58         server_connection(int _timeout);
59
60         std::ostream* get_logstream(log_level_values level);
61
62     public:
63         void check_timeout();
64         void reset_timeout();
65         void set_timeout(int _timeout)
66             { timeout=_timeout; }
67
68         /// get the id of this connection within the server object
69         unsigned int get_id()
70             { return connection_id; }
71 };
72
73 /**
74     @brief server base class
75
76     constitutes a server. is abstract, use derived classes like socket_server.
77 */
78 class server
79 {
80     private:
81         int default_timeout;
82         log_level_values log_level;
83         std::ostream *logstream;
84
85         unsigned int next_id;
86
87     protected:
88         std::map<unsigned int, server_connection*> connections;
89
90         server();
91
92         virtual bool fill_connection_buffers(void)=0;
93
94         int add_connection(server_connection* newconn);
95
96     public:
97         virtual ~server();
98
99         /// set the default timeout for new client connections
100         void set_default_timeout(int _default_timeout)
101             { default_timeout=_default_timeout; }
102
103         /// get the current default timeout for client connections
104         int get_default_timeout(void)
105             { return default_timeout; }
106
107         void set_logging(std::ostream *_logstream, log_level_values _log_level);
108
109         server_connection* get_connection(unsigned int conn_id);
110
111         /** @brief look for new data on all open connections, accept new connections
112             @param usec_timeout wait until new data is found, max timeout usecs.
113                   -1: wait endless
114                    NULL: no timeout
115             @retval true if new data was found (does not mean that the received data 
116                     is a complete packet though)
117         */
118         virtual bool fill_buffer(long long usec_timeout=-1)=0;
119
120         void cleanup();
121
122         /** @brief get a complete data packet from any client. The packet is removed from the
123                    connection buffer.
124             @param[out] data the data package
125             @retval true if packet found
126         */
127         bool get_packet(std::string& data)
128             { unsigned int x; return get_packet(data,x); }
129
130         bool get_packet(std::string& data, unsigned int& conn_id);
131
132         std::ostream* get_logstream(log_level_values level);
133 };
134
135 }
136
137 #endif