00001 /* 00002 * Copyright 2004-2006 Intel Corporation 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef _APISERVER_H_ 00018 #define _APISERVER_H_ 00019 00020 #include <oasys/debug/Log.h> 00021 #include <oasys/thread/Thread.h> 00022 #include <oasys/io/TCPClient.h> 00023 #include <oasys/io/TCPServer.h> 00024 00025 #include "dtn_api.h" 00026 #include "dtn_ipc.h" 00027 #include "dtn_types.h" 00028 00029 namespace dtn { 00030 00031 class APIRegistration; 00032 class APIRegistrationList; 00033 00038 class APIServer : public oasys::TCPServerThread { 00039 public: 00045 APIServer(); 00046 00047 // Virtual from TCPServerThread 00048 void accepted(int fd, in_addr_t addr, u_int16_t port); 00049 00050 in_addr_t local_addr() const { return local_addr_; } 00051 in_addr_t* local_addr_ptr() { return &local_addr_; } 00052 00053 u_int16_t local_port() const { return local_port_; } 00054 u_int16_t* local_port_ptr() { return &local_port_; } 00055 00056 private: 00057 in_addr_t local_addr_; 00058 u_int16_t local_port_; 00059 }; 00060 00064 class APIClient : public oasys::Thread, public oasys::TCPClient { 00065 public: 00066 APIClient(int fd, in_addr_t remote_host, u_int16_t remote_port); 00067 virtual ~APIClient(); 00068 virtual void run(); 00069 00070 void close_session(); 00071 00072 protected: 00073 int handle_handshake(); 00074 int handle_local_eid(); 00075 int handle_register(); 00076 int handle_unregister(); 00077 int handle_find_registration(); 00078 int handle_bind(); 00079 int handle_unbind(); 00080 int handle_send(); 00081 int handle_recv(); 00082 int handle_begin_poll(); 00083 int handle_cancel_poll(); 00084 int handle_close(); 00085 00086 // wait for a bundle arrival on any bound registration, or for 00087 // traffic on the api socket. 00088 // 00089 // returns the oasys IO error code if there was a timeout or an 00090 // internal error. returns 0 if there is a bundle waiting or 00091 // socket data on the channel, and assigns the reg or sock_ready 00092 // pointers appropriately 00093 int wait_for_bundle(const char* operation, dtn_timeval_t timeout, 00094 APIRegistration** reg, bool* sock_ready); 00095 00096 int send_response(int ret); 00097 00098 bool is_bound(u_int32_t regid); 00099 00100 char buf_[DTN_MAX_API_MSG]; 00101 XDR xdr_encode_; 00102 XDR xdr_decode_; 00103 APIRegistrationList* bindings_; 00104 oasys::Notifier notifier_; 00105 }; 00106 00107 } // namespace dtn 00108 00109 #endif /* _APISERVER_H_ */