|
1 /* |
|
2 * Copyright 2004-2006 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifndef _APISERVER_H_ |
|
18 #define _APISERVER_H_ |
|
19 |
|
20 #include <list> |
|
21 |
|
22 #include <oasys/compat/rpc.h> |
|
23 #include <oasys/debug/Log.h> |
|
24 #include <oasys/thread/Thread.h> |
|
25 #include <oasys/thread/SpinLock.h> |
|
26 #include <oasys/io/TCPClient.h> |
|
27 #include <oasys/io/TCPServer.h> |
|
28 |
|
29 #include "dtn_api.h" |
|
30 #include "dtn_ipc.h" |
|
31 #include "dtn_types.h" |
|
32 |
|
33 namespace dtn { |
|
34 |
|
35 class APIClient; |
|
36 class APIRegistration; |
|
37 class APIRegistrationList; |
|
38 |
|
39 /** |
|
40 * Class that implements the main server side handling of the DTN |
|
41 * application IPC. |
|
42 */ |
|
43 class APIServer : public oasys::TCPServerThread { |
|
44 public: |
|
45 /** |
|
46 * The constructor checks for environment variable overrides of |
|
47 * the address / port. It is expected that someone else will call |
|
48 * bind_listen_start() on the APIServer instance. |
|
49 */ |
|
50 APIServer(); |
|
51 |
|
52 // shutdown hook, clean up clients |
|
53 virtual void shutdown_hook(); |
|
54 |
|
55 // Virtual from TCPServerThread |
|
56 void accepted(int fd, in_addr_t addr, u_int16_t port); |
|
57 |
|
58 bool enabled() const { return enabled_; } |
|
59 bool* enabled_ptr() { return &enabled_; } |
|
60 |
|
61 in_addr_t local_addr() const { return local_addr_; } |
|
62 in_addr_t* local_addr_ptr() { return &local_addr_; } |
|
63 |
|
64 u_int16_t local_port() const { return local_port_; } |
|
65 u_int16_t* local_port_ptr() { return &local_port_; } |
|
66 |
|
67 void register_client(APIClient *); |
|
68 void unregister_client(APIClient *); |
|
69 |
|
70 protected: |
|
71 bool enabled_; ///< whether or not to enable it |
|
72 in_addr_t local_addr_; ///< local address to bind to |
|
73 u_int16_t local_port_; ///< local port to use for api |
|
74 |
|
75 std::list<APIClient *> client_list; ///< active clients |
|
76 oasys::SpinLock client_list_lock; ///< synchronizer |
|
77 }; |
|
78 |
|
79 /** |
|
80 * Class that implements the API session. |
|
81 */ |
|
82 class APIClient : public oasys::Thread, public oasys::TCPClient { |
|
83 public: |
|
84 APIClient(int fd, in_addr_t remote_host, u_int16_t remote_port, |
|
85 APIServer *parent); |
|
86 virtual ~APIClient(); |
|
87 virtual void run(); |
|
88 |
|
89 void close_client(); |
|
90 |
|
91 protected: |
|
92 int handle_handshake(); |
|
93 int handle_local_eid(); |
|
94 int handle_register(); |
|
95 int handle_unregister(); |
|
96 int handle_find_registration(); |
|
97 int handle_bind(); |
|
98 int handle_unbind(); |
|
99 int handle_send(); |
|
100 int handle_cancel(); |
|
101 int handle_recv(); |
|
102 int handle_begin_poll(); |
|
103 int handle_cancel_poll(); |
|
104 int handle_close(); |
|
105 int handle_session_update(); |
|
106 |
|
107 // block the calling thread, waiting for bundle arrival on a bound |
|
108 // registration, notification that a subscriber has arrived for a |
|
109 // custody session, or for traffic on the api socket. |
|
110 // |
|
111 // returns the oasys IO error code if there was a timeout or an |
|
112 // internal error. returns 0 if there is a bundle waiting or |
|
113 // socket data on the channel, and assigns the reg or sock_ready |
|
114 // pointers appropriately |
|
115 int wait_for_notify(const char* operation, |
|
116 dtn_timeval_t timeout, |
|
117 APIRegistration** recv_ready_reg, |
|
118 APIRegistration** session_ready_reg, |
|
119 bool* sock_ready); |
|
120 |
|
121 int handle_unexpected_data(const char* operation); |
|
122 |
|
123 int send_response(int ret); |
|
124 |
|
125 bool is_bound(u_int32_t regid); |
|
126 |
|
127 char buf_[DTN_MAX_API_MSG]; |
|
128 XDR xdr_encode_; |
|
129 XDR xdr_decode_; |
|
130 APIRegistrationList* bindings_; |
|
131 APIRegistrationList* sessions_; |
|
132 oasys::Notifier notifier_; |
|
133 APIServer* parent_; |
|
134 size_t total_sent_; |
|
135 size_t total_rcvd_; |
|
136 }; |
|
137 |
|
138 } // namespace dtn |
|
139 |
|
140 #endif /* _APISERVER_H_ */ |