CLConnection.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 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 _CLCONNECTION_H_
00018 #define _CLCONNECTION_H_
00019 
00020 #include <list>
00021 #include <oasys/debug/Log.h>
00022 #include <oasys/thread/Atomic.h>
00023 #include <oasys/thread/MsgQueue.h>
00024 #include <oasys/thread/Thread.h>
00025 #include <oasys/util/SparseBitmap.h>
00026 #include <oasys/util/StreamBuffer.h>
00027 
00028 #include "ConnectionConvergenceLayer.h"
00029 #include "bundling/Bundle.h"
00030 #include "bundling/BundleEvent.h"
00031 
00032 namespace dtn {
00033 
00038 class CLConnection : public CLInfo,
00039                      public oasys::Thread,
00040                      public oasys::Logger {
00041 public:
00042     friend class ConnectionConvergenceLayer;
00043     typedef ConnectionConvergenceLayer::LinkParams LinkParams;
00044     
00048     CLConnection(const char* classname,
00049                  const char* logpath,
00050                  ConnectionConvergenceLayer* cl,
00051                  LinkParams* params,
00052                  bool active_connector);
00053 
00057     virtual ~CLConnection();
00058 
00062     void set_contact(const ContactRef& contact) { contact_ = contact; }
00063 
00067     void queue_bundle(Bundle* bundle);
00068 
00069 protected:
00073     void run();
00074 
00077     virtual void check_unblock_link();
00078     virtual void contact_up();
00079     virtual void break_contact(ContactEvent::reason_t reason);
00080     virtual void close_contact();
00081     virtual void process_command();
00082     virtual void find_contact(const EndpointID& peer_eid);
00084 
00088     void set_nexthop(const std::string& nexthop) {
00089         nexthop_ = nexthop;
00090     }
00091 
00095     virtual void connect() = 0;
00096 
00100     virtual void accept() = 0;
00101 
00105     virtual void disconnect() = 0;
00106 
00111     virtual void initialize_pollfds() = 0;
00112 
00116     virtual void handle_send_bundle(Bundle* b) = 0;
00117 
00121     virtual void handle_cancel_bundle(Bundle* b) = 0;
00122 
00133     virtual bool send_pending_data() = 0;
00134     
00138     virtual void handle_poll_activity() = 0;
00139 
00143     virtual void handle_poll_timeout() = 0;
00144 
00149     typedef enum {
00150         CLMSG_INVALID       = 0,
00151         CLMSG_SEND_BUNDLE   = 1,
00152         CLMSG_CANCEL_BUNDLE = 2,
00153         CLMSG_BREAK_CONTACT = 3,
00154     } clmsg_t;
00155 
00159     const char* clmsg_to_str(clmsg_t type) {
00160         switch(type) {
00161         case CLMSG_INVALID:             return "CLMSG_INVALID";
00162         case CLMSG_SEND_BUNDLE:         return "CLMSG_SEND_BUNDLE";
00163         case CLMSG_CANCEL_BUNDLE:       return "CLMSG_CANCEL_BUNDLE";
00164         case CLMSG_BREAK_CONTACT:       return "CLMSG_BREAK_CONTACT";
00165         default:                        PANIC("bogus clmsg_t");
00166         }
00167     }
00168     
00173     struct CLMsg {
00174         CLMsg()
00175             : type_(CLMSG_INVALID),
00176               bundle_("ConnectionConvergenceLayer::CLMsg") {}
00177         
00178         CLMsg(clmsg_t type, Bundle* bundle = NULL)
00179             : type_(type),
00180               bundle_(bundle, "ConnectedConvergenceLayer::CLMsg") {}
00181         
00182         clmsg_t   type_;
00183         BundleRef bundle_;
00184     };
00185 
00189     typedef oasys::SparseBitmap<u_int32_t> DataBitmap;
00190    
00195     class InFlightBundle {
00196     public:
00197         InFlightBundle(Bundle* b)
00198             : bundle_(b, "CLConnection::InFlightBundle"),
00199               total_length_(0),
00200               send_complete_(false)
00201         {}
00202         
00203         BundleRef bundle_;
00204         BlockInfoVec* blocks_;
00205 
00206         u_int32_t total_length_;
00207         bool      send_complete_;
00208         
00209         DataBitmap sent_data_;
00210         DataBitmap ack_data_;
00211 
00212     private:
00213         // make sure we don't copy the structure by leaving the copy
00214         // constructor undefined
00215         InFlightBundle(const InFlightBundle& copy);
00216     };
00217 
00221     typedef std::list<InFlightBundle*> InFlightList;
00222 
00228     class IncomingBundle {
00229     public:
00230         IncomingBundle(Bundle* b)
00231             : bundle_(b, "CLConnection::IncomingBundle"),
00232               total_length_(0),
00233               acked_length_(0) {}
00234 
00235         BundleRef bundle_;
00236         
00237         u_int32_t total_length_;
00238         u_int32_t acked_length_;
00239 
00240         DataBitmap rcvd_data_;
00241         DataBitmap ack_data_;
00242     private:
00243         // make sure we don't copy the structure by leaving the copy
00244         // constructor undefined
00245         IncomingBundle(const IncomingBundle& copy);
00246     };
00247 
00251     typedef std::list<IncomingBundle*> IncomingList;
00252     
00253     ContactRef          contact_;       
00254     bool                contact_up_;    
00255     oasys::MsgQueue<CLMsg> cmdqueue_;   
00256     ConnectionConvergenceLayer* cl_;    
00257     LinkParams*         params_;        
00258 
00259     bool                active_connector_; 
00260     std::string         nexthop_;       
00261     int                 num_pollfds_;   
00262     static const int    MAXPOLL = 8;    
00263     struct pollfd       pollfds_[MAXPOLL]; 
00264     int                 poll_timeout_;  
00265     oasys::StreamBuffer sendbuf_;       
00266     oasys::StreamBuffer recvbuf_;       
00267     InFlightList        inflight_;      
00268     IncomingList        incoming_;      
00269     volatile bool       contact_broken_; 
00270     oasys::atomic_t     num_pending_;   
00271 };
00272 
00273 } // namespace dtn
00274 
00275 #endif /* _CLCONNECTION_H_ */

Generated on Thu Jun 7 12:54:26 2007 for DTN Reference Implementation by  doxygen 1.5.1