Commit | Line | Data |
---|---|---|
ac7fdc22 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 | #ifndef __LIBT2N_SERVER | |
20 | #define __LIBT2N_SERVER | |
21 | ||
22 | #include <iostream> | |
23 | #include <string> | |
24 | #include <map> | |
25 | ||
a11e19b7 GE |
26 | #include "connection.hxx" |
27 | #include "types.hxx" | |
28 | ||
ac7fdc22 GE |
29 | namespace libt2n |
30 | { | |
31 | ||
aa499d20 GE |
32 | class server; |
33 | ||
59adb9e2 TJ |
34 | /** |
35 | Basic connection class | |
36 | */ | |
a11e19b7 | 37 | class server_connection : public connection |
ac7fdc22 GE |
38 | { |
39 | private: | |
40 | int timeout; | |
41 | int last_action_time; | |
aa499d20 | 42 | unsigned int connection_id; |
ac7fdc22 GE |
43 | |
44 | protected: | |
a11e19b7 GE |
45 | server_connection(int _timeout) |
46 | : connection() | |
ac7fdc22 GE |
47 | { |
48 | set_timeout(_timeout); | |
49 | reset_timeout(); | |
aa499d20 GE |
50 | connection_id=0; |
51 | my_server=0; | |
ac7fdc22 GE |
52 | } |
53 | ||
aa499d20 | 54 | server *my_server; |
aa499d20 | 55 | |
ac7fdc22 | 56 | public: |
ac7fdc22 GE |
57 | void check_timeout(); |
58 | void reset_timeout(); | |
59 | void set_timeout(int _timeout) | |
60 | { timeout=_timeout; } | |
61 | ||
aa499d20 GE |
62 | void set_server(server* _my_server) |
63 | { my_server=_my_server; } | |
64 | ||
65 | void set_id(unsigned int _connection_id) | |
66 | { connection_id=_connection_id; } | |
67 | unsigned int get_id() | |
68 | { return connection_id; } | |
ac7fdc22 GE |
69 | }; |
70 | ||
59adb9e2 TJ |
71 | /** |
72 | Basic server class | |
73 | */ | |
ac7fdc22 GE |
74 | class server |
75 | { | |
ac7fdc22 GE |
76 | private: |
77 | int default_timeout; | |
78 | log_level_values log_level; | |
79 | std::ostream *logstream; | |
80 | ||
81 | unsigned int next_id; | |
ac7fdc22 GE |
82 | |
83 | protected: | |
a11e19b7 | 84 | std::map<unsigned int, server_connection*> connections; |
aa499d20 | 85 | |
ac7fdc22 GE |
86 | server() |
87 | { | |
88 | set_default_timeout(30); | |
89 | set_logging(NULL,none); | |
aa499d20 | 90 | next_id=1; |
ac7fdc22 GE |
91 | } |
92 | ||
a11e19b7 | 93 | int add_connection(server_connection* newconn); |
04e6b271 | 94 | |
ac7fdc22 GE |
95 | public: |
96 | virtual ~server(); | |
97 | ||
98 | void set_default_timeout(int _default_timeout) | |
99 | { default_timeout=_default_timeout; } | |
04e6b271 GE |
100 | int get_default_timeout(void) |
101 | { return default_timeout; } | |
ac7fdc22 GE |
102 | |
103 | void set_logging(std::ostream *_logstream, log_level_values _log_level) | |
104 | { | |
105 | log_level=_log_level; | |
106 | logstream=_logstream; | |
107 | } | |
108 | ||
a11e19b7 | 109 | server_connection* get_connection(unsigned int conn_id); |
ac7fdc22 GE |
110 | |
111 | virtual void fill_buffer(long long usec_timeout=-1)=0; | |
a11e19b7 | 112 | void cleanup(); |
ac7fdc22 | 113 | |
a11e19b7 GE |
114 | bool get_packet(std::string& data) |
115 | { unsigned int x; return get_packet(data,x); } | |
ac7fdc22 GE |
116 | bool get_packet(std::string& data, unsigned int& conn_id); |
117 | ||
aa499d20 | 118 | virtual void fill_connection_buffers(void)=0; |
0cf4dc9b | 119 | |
0cf4dc9b GE |
120 | void log(log_level_values level, const std::string& message) |
121 | { log(level,message.c_str()); } | |
ac7fdc22 GE |
122 | void log(log_level_values level, const char* message); |
123 | }; | |
124 | ||
125 | } | |
126 | ||
127 | #endif |