CLConnection.h

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2006 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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         // make sure we don't copy the structure by leaving the copy
00228         // constructor undefined
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         // make sure we don't copy the structure by leaving the copy
00259         // constructor undefined
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 } // namespace dtn
00288 
00289 #endif /* _CLCONNECTION_H_ */

Generated on Fri Dec 22 14:47:58 2006 for DTN Reference Implementation by  doxygen 1.5.1