servlib/conv_layers/UDPConvergenceLayer.h
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2004-2006 Intel Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifndef _UDP_CONVERGENCE_LAYER_H_
       
    18 #define _UDP_CONVERGENCE_LAYER_H_
       
    19 
       
    20 #include <oasys/io/UDPClient.h>
       
    21 #include <oasys/thread/Thread.h>
       
    22 #include <oasys/io/RateLimitedSocket.h>
       
    23 
       
    24 #include "IPConvergenceLayer.h"
       
    25 
       
    26 namespace dtn {
       
    27 
       
    28 class UDPConvergenceLayer : public IPConvergenceLayer {
       
    29 public:
       
    30     /**
       
    31      * Maximum bundle size
       
    32      */
       
    33     static const u_int MAX_BUNDLE_LEN = 65507;
       
    34 
       
    35     /**
       
    36      * Default port used by the udp cl.
       
    37      */
       
    38     static const u_int16_t UDPCL_DEFAULT_PORT = 4556;
       
    39     
       
    40     /**
       
    41      * Constructor.
       
    42      */
       
    43     UDPConvergenceLayer();
       
    44         
       
    45     /**
       
    46      * Bring up a new interface.
       
    47      */
       
    48     bool interface_up(Interface* iface, int argc, const char* argv[]);
       
    49 
       
    50     /**
       
    51      * Bring down the interface.
       
    52      */
       
    53     bool interface_down(Interface* iface);
       
    54     
       
    55     /**
       
    56      * Dump out CL specific interface information.
       
    57      */
       
    58     void dump_interface(Interface* iface, oasys::StringBuffer* buf);
       
    59 
       
    60     /**
       
    61      * Create any CL-specific components of the Link.
       
    62      */
       
    63     bool init_link(const LinkRef& link, int argc, const char* argv[]);
       
    64 
       
    65     /**
       
    66      * Delete any CL-specific components of the Link.
       
    67      */
       
    68     void delete_link(const LinkRef& link);
       
    69 
       
    70     /**
       
    71      * Dump out CL specific link information.
       
    72      */
       
    73     void dump_link(const LinkRef& link, oasys::StringBuffer* buf);
       
    74     
       
    75     /**
       
    76      * Open the connection to a given contact and send/listen for 
       
    77      * bundles over this contact.
       
    78      */
       
    79     bool open_contact(const ContactRef& contact);
       
    80     
       
    81     /**
       
    82      * Close the connnection to the contact.
       
    83      */
       
    84     bool close_contact(const ContactRef& contact);
       
    85 
       
    86     /**
       
    87      * Send the bundle out the link.
       
    88      */
       
    89     void bundle_queued(const LinkRef& link, const BundleRef& bundle);
       
    90 
       
    91     /**
       
    92      * Tunable parameter structure.
       
    93      *
       
    94      * Per-link and per-interface settings are configurable via
       
    95      * arguments to the 'link add' and 'interface add' commands.
       
    96      *
       
    97      * The parameters are stored in each Link's CLInfo slot, as well
       
    98      * as part of the Receiver helper class.
       
    99      */
       
   100     class Params : public CLInfo {
       
   101     public:
       
   102         /**
       
   103          * Virtual from SerializableObject
       
   104          */
       
   105         virtual void serialize( oasys::SerializeAction *a );
       
   106 
       
   107         in_addr_t local_addr_;		///< Local address to bind to
       
   108         u_int16_t local_port_;		///< Local port to bind to
       
   109         in_addr_t remote_addr_;		///< Peer address to connect to
       
   110         u_int16_t remote_port_;		///< Peer port to connect to
       
   111 
       
   112         u_int32_t rate_;		///< Rate (in bps)
       
   113         u_int32_t bucket_depth_;	///< Token bucket depth (in bits)
       
   114     };
       
   115     
       
   116     /**
       
   117      * Default parameters.
       
   118      */
       
   119     static Params defaults_;
       
   120 
       
   121 protected:
       
   122     bool parse_params(Params* params, int argc, const char** argv,
       
   123                       const char** invalidp);
       
   124     /**
       
   125      * Helper class (and thread) that listens on a registered
       
   126      * interface for incoming data.
       
   127      */
       
   128     class Receiver : public CLInfo,
       
   129                      public oasys::UDPClient,
       
   130                      public oasys::Thread
       
   131     {
       
   132     public:
       
   133         /**
       
   134          * Constructor.
       
   135          */
       
   136         Receiver(UDPConvergenceLayer::Params* params);
       
   137 
       
   138         /**
       
   139          * Destructor.
       
   140          */
       
   141         virtual ~Receiver() {}
       
   142         
       
   143         /**
       
   144          * Loop forever, issuing blocking calls to IPSocket::recvfrom(),
       
   145          * then calling the process_data function when new data does
       
   146          * arrive
       
   147          * 
       
   148          * Note that unlike in the Thread base class, this run() method is
       
   149          * public in case we don't want to actually create a new thread
       
   150          * for this guy, but instead just want to run the main loop.
       
   151          */
       
   152         void run();
       
   153 
       
   154         UDPConvergenceLayer::Params params_;
       
   155         
       
   156     protected:
       
   157         /**
       
   158          * Handler to process an arrived packet.
       
   159          */
       
   160         void process_data(u_char* bp, size_t len);
       
   161     };
       
   162 
       
   163     /*
       
   164      * Helper class that wraps the sender-side per-contact state.
       
   165      */
       
   166     class Sender : public CLInfo, public Logger {
       
   167     public:
       
   168         /**
       
   169          * Destructor.
       
   170          */
       
   171         virtual ~Sender() {}
       
   172 
       
   173         /**
       
   174          * Initialize the sender (the "real" constructor).
       
   175          */
       
   176         bool init(Params* params, in_addr_t addr, u_int16_t port);
       
   177         
       
   178     private:
       
   179         friend class UDPConvergenceLayer;
       
   180         
       
   181         /**
       
   182          * Constructor.
       
   183          */
       
   184         Sender(const ContactRef& contact);
       
   185         
       
   186         /**
       
   187          * Send one bundle.
       
   188          * @return the length of the bundle sent or -1 on error
       
   189          */
       
   190         int send_bundle(const BundleRef& bundle);
       
   191 
       
   192         /**
       
   193          * Pointer to the link parameters.
       
   194          */
       
   195         Params* params_;
       
   196 
       
   197         /**
       
   198          * The udp client socket.
       
   199          */
       
   200         oasys::UDPClient socket_;
       
   201 
       
   202         /**
       
   203          * Rate-limited socket that's optionally enabled.
       
   204          */
       
   205         oasys::RateLimitedSocket rate_socket_;
       
   206         
       
   207         /**
       
   208          * The contact that we're representing.
       
   209          */
       
   210         ContactRef contact_;
       
   211 
       
   212         /**
       
   213          * Temporary buffer for formatting bundles. Note that the
       
   214          * fixed-length buffer is big enough since UDP packets can't
       
   215          * be any bigger than that.
       
   216          */
       
   217         u_char buf_[UDPConvergenceLayer::MAX_BUNDLE_LEN];
       
   218     };   
       
   219 };
       
   220 
       
   221 } // namespace dtn
       
   222 
       
   223 #endif /* _UDP_CONVERGENCE_LAYER_H_ */