servlib/routing/ExternalRouter.h
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2006-2007 The MITRE 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  *    The US Government will not be charged any license fee and/or royalties
       
    17  *    related to this software. Neither name of The MITRE Corporation; nor the
       
    18  *    names of its contributors may be used to endorse or promote products
       
    19  *    derived from this software without specific prior written permission.
       
    20  */
       
    21 
       
    22 #ifndef _EXTERNAL_ROUTER_H_
       
    23 #define _EXTERNAL_ROUTER_H_
       
    24 
       
    25 #ifndef DTN_CONFIG_STATE
       
    26 #error "MUST INCLUDE dtn-config.h before including this file"
       
    27 #endif
       
    28 
       
    29 #if defined(XERCES_C_ENABLED) && defined(EXTERNAL_DP_ENABLED)
       
    30 
       
    31 #include "router-custom.h"
       
    32 #include "BundleRouter.h"
       
    33 #include "RouteTable.h"
       
    34 #include <reg/Registration.h>
       
    35 #include <oasys/serialize/XercesXMLSerialize.h>
       
    36 #include <oasys/io/UDPClient.h>
       
    37 
       
    38 #define EXTERNAL_ROUTER_SERVICE_TAG "/ext.rtr/*"
       
    39 
       
    40 namespace dtn {
       
    41 
       
    42 /**
       
    43  * ExternalRouter provides a plug-in interface for third-party
       
    44  * routing protocol implementations.
       
    45  *
       
    46  * Events received from BundleDaemon are serialized into
       
    47  * XML messages and UDP multicasted to external bundle router processes.
       
    48  * XML actions received on the interface are validated, transformed
       
    49  * into events, and placed on the global event queue.
       
    50  */
       
    51 class ExternalRouter : public BundleRouter {
       
    52 public:
       
    53     /// UDP port for IPC with external routers
       
    54     static u_int16_t server_port;
       
    55 
       
    56     /// Seconds between hello messages
       
    57     static u_int16_t hello_interval;
       
    58 
       
    59     /// Validate incoming XML messages
       
    60     static bool server_validation;
       
    61 
       
    62     /// XML schema required for XML validation
       
    63     static std::string schema;
       
    64 
       
    65     /// Include meta info in xml necessary for client validation 
       
    66     static bool client_validation;
       
    67 
       
    68     /// The static routing table
       
    69     static RouteTable *route_table;
       
    70 
       
    71     ExternalRouter();
       
    72     virtual ~ExternalRouter();
       
    73 
       
    74     /**
       
    75      * Called after all global data structures are set up.
       
    76      */
       
    77     virtual void initialize();
       
    78 
       
    79     /**
       
    80      * External router clean shutdown
       
    81      */
       
    82     virtual void shutdown();
       
    83 
       
    84     /**
       
    85      * Format the given StringBuffer with static routing info.
       
    86      * @param buf buffer to fill with the static routing table
       
    87      */
       
    88     virtual void get_routing_state(oasys::StringBuffer* buf);
       
    89 
       
    90     /**
       
    91      * Serialize events and UDP multicast to external routers.
       
    92      * @param event BundleEvent to process
       
    93      */
       
    94     virtual void handle_event(BundleEvent *event);
       
    95     virtual void handle_bundle_received(BundleReceivedEvent *event);
       
    96     virtual void handle_bundle_transmitted(BundleTransmittedEvent* event);
       
    97     virtual void handle_bundle_delivered(BundleDeliveredEvent* event);
       
    98     virtual void handle_bundle_expired(BundleExpiredEvent* event);
       
    99     virtual void handle_bundle_cancelled(BundleSendCancelledEvent* event);
       
   100     virtual void handle_bundle_injected(BundleInjectedEvent* event);
       
   101     virtual void handle_contact_up(ContactUpEvent* event);
       
   102     virtual void handle_contact_down(ContactDownEvent* event);
       
   103     virtual void handle_link_created(LinkCreatedEvent *event);
       
   104     virtual void handle_link_deleted(LinkDeletedEvent *event);
       
   105     virtual void handle_link_available(LinkAvailableEvent *event);
       
   106     virtual void handle_link_unavailable(LinkUnavailableEvent *event);
       
   107     virtual void handle_link_attribute_changed(LinkAttributeChangedEvent *event);
       
   108     virtual void handle_contact_attribute_changed(ContactAttributeChangedEvent *event);
       
   109 //    virtual void handle_link_busy(LinkBusyEvent *event);
       
   110     virtual void handle_new_eid_reachable(NewEIDReachableEvent* event);
       
   111     virtual void handle_registration_added(RegistrationAddedEvent* event);
       
   112     virtual void handle_registration_removed(RegistrationRemovedEvent* event);
       
   113     virtual void handle_registration_expired(RegistrationExpiredEvent* event);
       
   114     virtual void handle_route_add(RouteAddEvent* event);
       
   115     virtual void handle_route_del(RouteDelEvent* event);
       
   116     virtual void handle_custody_signal(CustodySignalEvent* event);
       
   117     virtual void handle_custody_timeout(CustodyTimeoutEvent* event);
       
   118     virtual void handle_link_report(LinkReportEvent *event);
       
   119     virtual void handle_link_attributes_report(LinkAttributesReportEvent *event);
       
   120     virtual void handle_contact_report(ContactReportEvent* event);
       
   121     virtual void handle_bundle_report(BundleReportEvent *event);
       
   122     virtual void handle_bundle_attributes_report(BundleAttributesReportEvent *event);
       
   123     virtual void handle_route_report(RouteReportEvent* event);
       
   124 
       
   125     virtual void send(rtrmessage::bpa &message);
       
   126 
       
   127 protected:
       
   128     class ModuleServer;
       
   129     class HelloTimer;
       
   130     class ERRegistration;
       
   131 
       
   132     // XXX This function should really go in ContactEvent
       
   133     //     but ExternalRouter needs a less verbose version
       
   134     static const char *reason_to_str(int reason);
       
   135 
       
   136     /// UDP server thread
       
   137     ModuleServer *srv_;
       
   138 
       
   139     /// The route table
       
   140     RouteTable *route_table_;
       
   141 
       
   142     /// ExternalRouter registration with the bundle forwarder
       
   143     ERRegistration *reg_;
       
   144 
       
   145     /// Hello timer
       
   146     HelloTimer *hello_;
       
   147 };
       
   148 
       
   149 /**
       
   150  * Helper class (and thread) that manages communications
       
   151  * with external routers
       
   152  */
       
   153 class ExternalRouter::ModuleServer : public oasys::Thread,
       
   154                                      public oasys::UDPClient {
       
   155 public:
       
   156     ModuleServer();
       
   157     virtual ~ModuleServer();
       
   158 
       
   159     /**
       
   160      * The main thread loop
       
   161      */
       
   162     virtual void run();
       
   163 
       
   164     /**
       
   165      * Parse incoming actions and place them on the
       
   166      * global event queue
       
   167      * @param payload the incoming XML document payload
       
   168      */
       
   169     void process_action(const char *payload);
       
   170 
       
   171     /// Message queue for accepting BundleEvents from ExternalRouter
       
   172     oasys::MsgQueue< std::string * > *eventq;
       
   173 
       
   174     /// Xerces XML validating parser for incoming messages
       
   175     oasys::XercesXMLUnmarshal *parser_;
       
   176 
       
   177     oasys::SpinLock *lock_;
       
   178 
       
   179 private:
       
   180     Link::link_type_t convert_link_type(rtrmessage::linkTypeType type);
       
   181     Bundle::priority_values_t convert_priority(rtrmessage::bundlePriorityType);
       
   182 
       
   183     ForwardingInfo::action_t 
       
   184     convert_fwd_action(rtrmessage::bundleForwardActionType);
       
   185 };
       
   186 
       
   187 /**
       
   188  * Helper class for ExternalRouter hello messages
       
   189  */
       
   190 class ExternalRouter::HelloTimer : public oasys::Timer {
       
   191 public:
       
   192     HelloTimer(ExternalRouter *router);
       
   193     ~HelloTimer();
       
   194         
       
   195     /**
       
   196      * Timer callback function
       
   197      */
       
   198     void timeout(const struct timeval &now);
       
   199 
       
   200     ExternalRouter *router_;
       
   201 };
       
   202 
       
   203 /**
       
   204  * Helper class which registers to receive
       
   205  * bundles from remote peers
       
   206  */
       
   207 class ExternalRouter::ERRegistration : public Registration {
       
   208 public:
       
   209     ERRegistration(ExternalRouter *router);
       
   210 
       
   211     /**
       
   212      * Registration delivery callback function
       
   213      */
       
   214     void deliver_bundle(Bundle *bundle);
       
   215 
       
   216     ExternalRouter *router_;
       
   217 };
       
   218 
       
   219 /**
       
   220  * Global shutdown callback function
       
   221  */
       
   222 void external_rtr_shutdown(void *args);
       
   223 
       
   224 } // namespace dtn
       
   225 
       
   226 #endif // XERCES_C_ENABLED && EXTERNAL_DP_ENABLED
       
   227 #endif //_EXTERNAL_ROUTER_H_