00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef _CLCONNECTION_H_
00039 #define _CLCONNECTION_H_
00040
00041 #include <list>
00042 #include <oasys/debug/Log.h>
00043 #include <oasys/thread/MsgQueue.h>
00044 #include <oasys/thread/Thread.h>
00045 #include <oasys/util/SparseBitmap.h>
00046 #include <oasys/util/StreamBuffer.h>
00047
00048 #include "ConnectionConvergenceLayer.h"
00049 #include "bundling/Bundle.h"
00050 #include "bundling/BundleEvent.h"
00051
00052 namespace dtn {
00053
00058 class CLConnection : public CLInfo,
00059 public oasys::Thread,
00060 public oasys::Logger {
00061 public:
00062 friend class ConnectionConvergenceLayer;
00063 typedef ConnectionConvergenceLayer::LinkParams LinkParams;
00064
00068 CLConnection(const char* classname,
00069 const char* logpath,
00070 ConvergenceLayer* cl,
00071 LinkParams* params,
00072 bool active_connector);
00073
00077 virtual ~CLConnection();
00078
00082 void set_contact(const ContactRef& contact) { contact_ = contact; }
00083
00084 protected:
00088 void run();
00089
00092 void contact_up();
00093 void break_contact(ContactEvent::reason_t reason);
00094 void close_contact();
00095 void process_command();
00096 void handle_announce_bundle(Bundle* bundle);
00098
00102 void set_nexthop(const std::string& nexthop) {
00103 nexthop_ = nexthop;
00104 }
00105
00109 virtual void connect() = 0;
00110
00114 virtual void accept() = 0;
00115
00119 virtual void disconnect() = 0;
00120
00125 virtual void initialize_pollfds() = 0;
00126
00130 virtual void handle_send_bundle(Bundle* b) = 0;
00131
00135 virtual void handle_cancel_bundle(Bundle* b) = 0;
00136
00147 virtual bool send_pending_data() = 0;
00148
00152 virtual void handle_poll_activity() = 0;
00153
00157 virtual void handle_poll_timeout() = 0;
00158
00163 typedef enum {
00164 CLMSG_INVALID = 0,
00165 CLMSG_SEND_BUNDLE = 1,
00166 CLMSG_CANCEL_BUNDLE = 2,
00167 CLMSG_BREAK_CONTACT = 3,
00168 } clmsg_t;
00169
00173 const char* clmsg_to_str(clmsg_t type) {
00174 switch(type) {
00175 case CLMSG_INVALID: return "CLMSG_INVALID";
00176 case CLMSG_SEND_BUNDLE: return "CLMSG_SEND_BUNDLE";
00177 case CLMSG_CANCEL_BUNDLE: return "CLMSG_CANCEL_BUNDLE";
00178 case CLMSG_BREAK_CONTACT: return "CLMSG_BREAK_CONTACT";
00179 default: PANIC("bogus clmsg_t");
00180 }
00181 }
00182
00187 struct CLMsg {
00188 CLMsg()
00189 : type_(CLMSG_INVALID),
00190 bundle_("ConnectionConvergenceLayer::CLMsg") {}
00191
00192 CLMsg(clmsg_t type, Bundle* bundle = NULL)
00193 : type_(type),
00194 bundle_(bundle, "ConnectedConvergenceLayer::CLMsg") {}
00195
00196 clmsg_t type_;
00197 BundleRef bundle_;
00198 };
00199
00203 typedef oasys::SparseBitmap<size_t> DataBitmap;
00204
00209 class InFlightBundle {
00210 public:
00211 InFlightBundle(Bundle* b)
00212 : bundle_(b, "CLConnection::InFlightBundle"),
00213 formatted_length_(0),
00214 header_block_length_(0),
00215 tail_block_length_(0) {}
00216
00217 BundleRef bundle_;
00218
00219 size_t formatted_length_;
00220 size_t header_block_length_;
00221 size_t tail_block_length_;
00222
00223 DataBitmap sent_data_;
00224 DataBitmap ack_data_;
00225
00226 private:
00227
00228
00229 InFlightBundle(const InFlightBundle& copy);
00230 };
00231
00235 typedef std::list<InFlightBundle*> InFlightList;
00236
00242 class IncomingBundle {
00243 public:
00244 IncomingBundle(Bundle* b)
00245 : bundle_(b, "CLConnection::IncomingBundle"),
00246 total_length_(0),
00247 header_block_length_(0) {}
00248
00249 BundleRef bundle_;
00250
00251 size_t total_length_;
00252 size_t header_block_length_;
00253
00254 DataBitmap rcvd_data_;
00255 DataBitmap ack_data_;
00256
00257 private:
00258
00259
00260 IncomingBundle(const IncomingBundle& copy);
00261 };
00262
00266 typedef std::list<IncomingBundle*> IncomingList;
00267
00268 ContactRef contact_;
00269 bool contact_up_;
00270 oasys::MsgQueue<CLMsg> cmdqueue_;
00271 ConvergenceLayer* cl_;
00272 LinkParams* params_;
00273
00274 bool active_connector_;
00275 std::string nexthop_;
00276 int num_pollfds_;
00277 static const int MAXPOLL = 8;
00278 struct pollfd pollfds_[MAXPOLL];
00279 int poll_timeout_;
00280 oasys::StreamBuffer sendbuf_;
00281 oasys::StreamBuffer recvbuf_;
00282 InFlightList inflight_;
00283 IncomingList incoming_;
00284 volatile bool contact_broken_;
00285 };
00286
00287 }
00288
00289 #endif