Switch time() calls to monotonic clock calls (#7597)
[libt2n] / src / command_server.hxx
1 /*
2 Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
3
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
6
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
9
10 As a special exception, if other files instantiate templates or use macros
11 or inline functions from this file, or you compile this file and link it
12 with other works to produce a work based on this file, this file
13 does not by itself cause the resulting work to be covered
14 by the GNU General Public License.
15
16 However the source code for this file must still be made available
17 in accordance with section (3) of the GNU General Public License.
18
19 This exception does not invalidate any other reasons why a work based
20 on this file might be covered by the GNU General Public License.
21 */
22 #ifndef __LIBT2N_COMMAND_SERVER
23 #define __LIBT2N_COMMAND_SERVER
24
25 #include "command.hxx"
26 #include "server.hxx"
27
28 namespace libt2n
29 {
30
31 /// a server handling incoming commands
32 class command_server
33 {
34     private:
35         server& s;
36
37         void handle_packet(const std::string& packet, server_connection* conn);
38
39         int guard_handle;
40
41     protected:
42         virtual command* cast_command(command* input)
43             { return input; }
44
45     public:
46         command_server(server& _s);
47         ~command_server();
48
49         void handle(long long usec_timeout=-1, long long* usec_timeout_remaining=NULL);
50
51         void send_hello(unsigned int conn_id);
52
53         std::ostream* get_logstream(log_level_values level)
54             { return s.get_logstream(level); }
55 };
56
57 template<class T, class B> struct Derived_from {
58         static void constraints(T* p) { B* pb = p; }
59         Derived_from() { void(*/*p*/)(T*) = constraints; }
60 };
61
62 /** @brief server handling group of incoming commands
63
64     the template must be derived from libt2n::command.
65 */
66 template<class COMMAND_GROUP>
67 class group_command_server : public command_server
68 {
69     private:
70         virtual command* cast_command(command* input)
71             { return dynamic_cast<COMMAND_GROUP*>(input); }
72
73     public:
74         group_command_server(server& _s)
75             : command_server(_s)
76         { Derived_from<COMMAND_GROUP,command>(); }
77 };
78
79 }
80 #endif
81