servlib/routing/TcaRouter.h
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2005-2006 University of Waterloo
       
     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 
       
    18 #ifndef _TCA_ROUTER_H_
       
    19 #define _TCA_ROUTER_H_
       
    20 
       
    21 #include "naming/EndpointID.h"
       
    22 #include "TableBasedRouter.h"
       
    23 
       
    24 #define SERVLIB 1
       
    25 //#include "TcaTypes.h"
       
    26 #include "TcaEndpointID.h"
       
    27 #include "TcaControlBundle.h"
       
    28 
       
    29 
       
    30 namespace dtn {
       
    31 
       
    32 
       
    33 /**
       
    34  * This is the implementation of the TCA bundle routing algorithm.
       
    35  *
       
    36  * A TCARouter is a specialized TableBasedRouter where the route
       
    37  * table is manipulated in response to certain control bundles
       
    38  * (for example, a "register" bundle, or a "change-of-address" bundle).
       
    39  * Specialized routing logic is then applied in order to route late-bound
       
    40  * bundles addressed to a mobile node, to the mobile's current location
       
    41  * in the network.
       
    42  * 
       
    43  * The main interface point is the overridden handle_bundle_received
       
    44  * function which tests for the special TCA bundles (control bundles and
       
    45  * late-bound data bundles).
       
    46  *
       
    47  */
       
    48  
       
    49 class TcaRouter : public TableBasedRouter {
       
    50 
       
    51 public:
       
    52 
       
    53     enum Role { TCA_MOBILE, TCA_ROUTER, TCA_GATEWAY };
       
    54 
       
    55     // Internal bundle-forwarding rule.
       
    56     // This mostly has to do with how to treat the default route
       
    57     // (UDR = "Use Default Route").
       
    58     enum ForwardingRule {
       
    59         FWD_NEVER,              // do not forward, ever
       
    60         FWD_UDR_EXCLUSIVELY,    // forward (only) to the default route
       
    61         FWD_UDR_NEVER,          // fwd to matching, except default route
       
    62         FWD_UDR_IFNECESSARY,    // fwd to matching, using default route iff
       
    63                                 // no other matches
       
    64         FWD_UDR_ALWAYS          // forward to matching, including default route
       
    65         };  
       
    66 
       
    67     TcaRouter(Role role);
       
    68 
       
    69 protected:
       
    70 
       
    71     Role role_;
       
    72 
       
    73     TcaEndpointID admin_app_;   // eid of local admin application
       
    74     
       
    75     std::string get_role_str() const;
       
    76 
       
    77     // BundleEventHandler functions to handle events important to TCA.
       
    78     virtual void handle_bundle_received(BundleReceivedEvent* event);
       
    79     virtual void handle_bundle_transmitted(BundleTransmittedEvent* event);
       
    80     virtual void handle_contact_up(ContactUpEvent* event);
       
    81     virtual void handle_contact_down(ContactDownEvent* event);
       
    82     virtual void handle_link_available(LinkAvailableEvent* event);
       
    83     virtual void handle_link_unavailable(LinkUnavailableEvent* event);
       
    84     virtual void handle_shutdown_request(ShutdownRequest* event);
       
    85 
       
    86     // fwd function to broadcast a bundle to everybody in the route table
       
    87     virtual int fwd_to_all(Bundle* bundle);
       
    88 
       
    89     virtual int fwd_to_matching(Bundle* bundle, const LinkRef& next_hop);
       
    90     virtual int fwd_to_matching(Bundle* bundle) {
       
    91         LinkRef link("TcaRouter::fwd_to_matching: null");
       
    92         return fwd_to_matching(bundle, link);
       
    93     }
       
    94     
       
    95     // fwd function with special forwarding rules for default route
       
    96     // used for forwarding unbound tca bundles and some tca control bundles
       
    97     virtual int fwd_to_matching_r(Bundle* bundle, const LinkRef& next_hop,
       
    98                                   ForwardingRule fwd_rule);
       
    99 
       
   100     bool on_coa_transmitted(Bundle* b, const TcaControlBundle& cb);
       
   101     bool on_ask_transmitted(Bundle* b, const TcaControlBundle& cb);
       
   102     bool on_adv_transmitted(Bundle* b, const TcaControlBundle& cb);
       
   103 
       
   104     // special control bundle handlers
       
   105     bool handle_register(Bundle* b);
       
   106     bool handle_coa(Bundle* b);
       
   107 
       
   108     // handle bundle sent to anonymous address
       
   109     bool handle_anonymous_bundle(Bundle* b);
       
   110 
       
   111     bool handle_ask(Bundle* b, const TcaControlBundle& cb);
       
   112 
       
   113     // handle control bundles addressed to bundlelayer
       
   114     bool handle_bl_control_bundle(Bundle* b);
       
   115 
       
   116     bool handle_bl_ask(Bundle* b, const TcaControlBundle& cb);
       
   117     bool handle_get_routes(Bundle* b, const TcaControlBundle& cb);
       
   118     bool handle_add_route(Bundle* b, const TcaControlBundle& cb);
       
   119     bool handle_del_route(Bundle* b, const TcaControlBundle& cb);
       
   120 
       
   121     // handle regular late-bound tca data bundle
       
   122     bool handle_tca_unbound_bundle(Bundle* bundle);
       
   123 
       
   124     bool on_route_unbound_bundle(Bundle* bundle);
       
   125     bool on_gate_unbound_bundle(Bundle* bundle);
       
   126 
       
   127     // did the bundle originate at this node?
       
   128     bool is_local_source(Bundle* b);
       
   129 
       
   130     ForwardingRule get_forwarding_rule(Bundle* b);
       
   131 
       
   132     // create a link entry for the given address
       
   133     LinkRef create_link(const std::string& link_addr);
       
   134 
       
   135     // create a route entry for the given endpoint pattern, specified link
       
   136     RouteEntry* create_route(const std::string& pattern, const LinkRef& p_link);
       
   137 
       
   138     // create a route *and link* if necessary, for the given endpoint pattern,
       
   139     // given link address
       
   140     bool create_route(const std::string& pattern,
       
   141                       const std::string& link_addr);
       
   142 
       
   143     // Ultra-simplified helper function to inject a new bundle into
       
   144     // the works, using defaults for most fields.
       
   145     // Specify empty src for bundlelayer
       
   146     bool post_bundle(const EndpointID& src, const EndpointID& dest,
       
   147                      const std::string& payload);
       
   148 
       
   149     // Ultra-simplified helper function to post a wrapped bundle to the
       
   150     // admin app. This is in lieu of a WrappedBundle class.
       
   151     bool push_wrapped_bundle(const std::string& code,
       
   152                              const EndpointID& src,
       
   153                              const EndpointID& dest,
       
   154                              const std::string& bsp);
       
   155 
       
   156 };
       
   157 
       
   158 } // namespace dtn
       
   159 
       
   160 #endif /* _TCA_ROUTER_H_ */