IBR-DTNSuite  0.8
ibrdtn/ibrdtn/api/APIClient.h
Go to the documentation of this file.
00001 /*
00002  * APIClient.h
00003  *
00004  *  Created on: 01.07.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef APICLIENT_H_
00009 #define APICLIENT_H_
00010 
00011 #include "ibrdtn/api/Bundle.h"
00012 #include "ibrdtn/data/BundleID.h"
00013 #include <ibrcommon/net/tcpstream.h>
00014 #include <ibrcommon/Exceptions.h>
00015 
00016 namespace dtn
00017 {
00018         namespace api
00019         {
00029                 class APIClient
00030                 {
00031                 public:
00032                         enum STATUS_CODES
00033                         {
00034                                 API_STATUS_CONTINUE = 100,
00035                                 API_STATUS_OK = 200,
00036                                 API_STATUS_CREATED = 201,
00037                                 API_STATUS_ACCEPTED = 202,
00038                                 API_STATUS_FOUND = 302,
00039                                 API_STATUS_BAD_REQUEST = 400,
00040                                 API_STATUS_UNAUTHORIZED = 401,
00041                                 API_STATUS_FORBIDDEN = 403,
00042                                 API_STATUS_NOT_FOUND = 404,
00043                                 API_STATUS_NOT_ALLOWED = 405,
00044                                 API_STATUS_NOT_ACCEPTABLE = 406,
00045                                 API_STATUS_CONFLICT = 409,
00046                                 API_STATUS_INTERNAL_ERROR = 500,
00047                                 API_STATUS_NOT_IMPLEMENTED = 501,
00048                                 API_STATUS_SERVICE_UNAVAILABLE = 503,
00049                                 API_STATUS_VERSION_NOT_SUPPORTED = 505,
00050                                 API_STATUS_NOTIFY_COMMON = 600,
00051                                 API_STATUS_NOTIFY_NEIGHBOR = 601,
00052                                 API_STATUS_NOTIFY_BUNDLE = 602,
00053                         };
00054 
00055                         class Message
00056                         {
00057                         public:
00058                                 Message(STATUS_CODES code, const std::string &msg);
00059                                 virtual ~Message();
00060 
00061                                 const STATUS_CODES code;
00062                                 const std::string msg;
00063                         };
00064 
00065                         APIClient(ibrcommon::tcpstream &stream);
00066 
00070                         virtual ~APIClient();
00071 
00072                         void connect();
00073 
00078                         void setEndpoint(const std::string &endpoint);
00079 
00084                         void subscribe(const dtn::data::EID &eid);
00085 
00090                         void unsubscribe(const dtn::data::EID &eid);
00091 
00095                         std::list<dtn::data::EID> getSubscriptions();
00096 
00101                         virtual dtn::api::Bundle get(dtn::data::BundleID &id);
00102 
00107                         virtual dtn::api::Bundle get();
00108 
00113                         virtual void send(const dtn::api::Bundle &bundle);
00114 
00118                         Message wait();
00119 
00123                         void unblock_wait();
00124 
00129                         void notify_delivered(const dtn::api::Bundle &b);
00130                         void notify_delivered(const dtn::data::BundleID &id);
00131 
00132                 protected:
00137                         virtual void register_put(const dtn::data::Bundle &bundle);
00138 
00139                         virtual void register_clear();
00140 
00141                         virtual void register_free();
00142 
00143                         virtual void register_store();
00144 
00145                         virtual void register_send();
00146 
00147                         virtual dtn::data::Bundle register_get();
00148 
00149                         // receive the response (queue notifies into the queue)
00150                         virtual int __get_return();
00151 
00152                         // receive the next notify
00153                         Message __get_message();
00154 
00155                         static dtn::data::BundleID readBundleID(const std::string &data);
00156 
00157                         // tcp stream reference to send/receive data to the daemon
00158                         ibrcommon::tcpstream &_stream;
00159 
00160                         // notify queue
00161                         ibrcommon::Conditional _queue_cond;
00162                         bool _get_busy;
00163                         std::queue<Message> _notify_queue;
00164                         std::queue<Message> _status_queue;
00165                 };
00166         }
00167 }
00168 
00169 #endif /* APICLIENT_H_ */