BundleDaemon.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-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 _BUNDLE_DAEMON_H_
00018 #define _BUNDLE_DAEMON_H_
00019 
00020 #include <vector>
00021 
00022 #include <oasys/compat/inttypes.h>
00023 #include <oasys/debug/Log.h>
00024 #include <oasys/thread/Thread.h>
00025 #include <oasys/thread/MsgQueue.h>
00026 #include <oasys/util/StringBuffer.h>
00027 
00028 #include "BundleEvent.h"
00029 #include "BundleEventHandler.h"
00030 #include "BundleProtocol.h"
00031 #include "BundleActions.h"
00032 
00033 namespace dtn {
00034 
00035 class AdminRegistration;
00036 class Bundle;
00037 class BundleAction;
00038 class BundleActions;
00039 class BundleList;
00040 class BundleRouter;
00041 class ContactManager;
00042 class FragmentManager;
00043 class PingRegistration;
00044 class RegistrationTable;
00045 
00053 class BundleDaemon : public oasys::Singleton<BundleDaemon, false>,
00054                      public BundleEventHandler,
00055                      public oasys::Thread
00056 {
00057 public:
00061     BundleDaemon();
00062 
00066     virtual ~BundleDaemon();
00067 
00073     virtual void do_init();
00074     
00078     static void init()
00079     {       
00080         if (instance_ != NULL) 
00081         {
00082             PANIC("BundleDaemon already initialized");
00083         }
00084 
00085         instance_ = new BundleDaemon();     
00086         instance_->do_init();
00087     }
00088 
00093     static void post(BundleEvent* event);
00094  
00099     static void post_at_head(BundleEvent* event);
00100     
00105     static bool post_and_wait(BundleEvent* event,
00106                               oasys::Notifier* notifier,
00107                               int timeout = -1, bool at_back = true);
00108     
00113     virtual void post_event(BundleEvent* event, bool at_back = true);
00114 
00118     BundleRouter* router()
00119     {
00120         ASSERT(router_ != NULL);
00121         return router_;
00122     }
00123 
00127     BundleActions* actions() { return actions_; }
00128 
00132     ContactManager* contactmgr() { return contactmgr_; }
00133 
00137     FragmentManager* fragmentmgr() { return fragmentmgr_; }
00138 
00142     const RegistrationTable* reg_table() { return reg_table_; }
00143 
00147     BundleList* pending_bundles() { return pending_bundles_; }
00148     
00152     BundleList* custody_bundles() { return custody_bundles_; }
00153     
00157     void get_routing_state(oasys::StringBuffer* buf);
00158 
00163     void get_bundle_stats(oasys::StringBuffer* buf);
00164 
00169     void get_daemon_stats(oasys::StringBuffer* buf);
00170 
00174     void reset_stats();
00175 
00179     const EndpointID& local_eid() { return local_eid_; }
00180 
00184     void set_local_eid(const char* eid_str) {
00185         local_eid_.assign(eid_str);
00186     }
00187 
00191     struct Params {
00193         Params();
00194         
00197         bool early_deletion_;
00198 
00200         bool accept_custody_;
00201 
00203         bool reactive_frag_enabled_;
00204         
00206         bool retry_reliable_unacked_;
00207 
00209         bool test_permuted_delivery_;
00210     };
00211 
00212     static Params params_;
00213 
00217     typedef void (*ShutdownProc) (void* args);
00218     
00222     void set_app_shutdown(ShutdownProc proc, void* data)
00223     {
00224         app_shutdown_proc_ = proc;
00225         app_shutdown_data_ = data;
00226     }
00227 
00231     void set_rtr_shutdown(ShutdownProc proc, void* data)
00232     {
00233         rtr_shutdown_proc_ = proc;
00234         rtr_shutdown_data_ = data;
00235     }
00236 
00240     static bool shutting_down()
00241     {
00242         return shutting_down_;
00243     }
00244 
00248     static bool is_simulator()
00249     {
00250         return is_simulator_;
00251     }
00252     
00253 protected:
00254     friend class BundleActions;
00255 
00259     void load_registrations();
00260         
00264     void load_bundles();
00265         
00269     void run();
00270 
00274     void handle_event(BundleEvent* event);
00275 
00277 
00280     void handle_bundle_received(BundleReceivedEvent* event);
00281     void handle_bundle_transmitted(BundleTransmittedEvent* event);
00282     void handle_bundle_transmit_failed(BundleTransmitFailedEvent* event);
00283     void handle_bundle_delivered(BundleDeliveredEvent* event);
00284     void handle_bundle_expired(BundleExpiredEvent* event);
00285     void handle_bundle_free(BundleFreeEvent* event);
00286     void handle_bundle_send(BundleSendRequest* event);
00287     void handle_bundle_cancel(BundleCancelRequest* event);
00288     void handle_bundle_inject(BundleInjectRequest* event);
00289     void handle_bundle_accept(BundleAcceptRequest* event);
00290     void handle_bundle_query(BundleQueryRequest* event);
00291     void handle_bundle_report(BundleReportEvent* event);
00292     void handle_registration_added(RegistrationAddedEvent* event);
00293     void handle_registration_removed(RegistrationRemovedEvent* event);
00294     void handle_registration_expired(RegistrationExpiredEvent* event);
00295     void handle_contact_up(ContactUpEvent* event);
00296     void handle_contact_down(ContactDownEvent* event);
00297     void handle_contact_query(ContactQueryRequest* event);
00298     void handle_contact_report(ContactReportEvent* event);
00299     void handle_link_available(LinkAvailableEvent* event);    
00300     void handle_link_unavailable(LinkUnavailableEvent* event);
00301     void handle_link_state_change_request(LinkStateChangeRequest* request);
00302     void handle_link_create(LinkCreateRequest* event);
00303     void handle_link_query(LinkQueryRequest* event);
00304     void handle_link_report(LinkReportEvent* event);
00305     void handle_reassembly_completed(ReassemblyCompletedEvent* event);
00306     void handle_route_add(RouteAddEvent* event);
00307     void handle_route_del(RouteDelEvent* event);
00308     void handle_route_query(RouteQueryRequest* event);
00309     void handle_route_report(RouteReportEvent* event);
00310     void handle_custody_signal(CustodySignalEvent* event);
00311     void handle_custody_timeout(CustodyTimeoutEvent* event);
00312     void handle_shutdown_request(ShutdownRequest* event);
00313     void handle_status_request(StatusRequest* event);
00315 
00316     typedef BundleProtocol::custody_signal_reason_t custody_signal_reason_t;
00317     typedef BundleProtocol::status_report_flag_t status_report_flag_t;
00318     typedef BundleProtocol::status_report_reason_t status_report_reason_t;
00319     
00323     void generate_status_report(Bundle* bundle,
00324                                 status_report_flag_t flag,
00325                                 status_report_reason_t reason =
00326                                 BundleProtocol::REASON_NO_ADDTL_INFO);
00327 
00331     void generate_custody_signal(Bundle* bundle, bool succeeded,
00332                                  custody_signal_reason_t reason);
00333     
00337     void cancel_custody_timers(Bundle* bundle);
00338 
00343     void accept_custody(Bundle* bundle);
00344 
00349     void release_custody(Bundle* bundle);
00350 
00358     bool add_to_pending(Bundle* bundle, bool add_to_store);
00359     
00364     bool delete_from_pending(Bundle* bundle);
00365 
00371     bool try_delete_from_pending(Bundle* bundle);
00372 
00379     bool delete_bundle(Bundle* bundle,
00380                        status_report_reason_t reason =
00381                            BundleProtocol::REASON_NO_ADDTL_INFO);
00382     
00388     Bundle* find_duplicate(Bundle* bundle);
00389 
00393     void deliver_to_registration(Bundle* bundle, Registration* registration);
00394     
00399     void check_registrations(Bundle* bundle);
00400     
00402     BundleRouter* router_;
00403 
00405     BundleActions* actions_;
00406 
00408     AdminRegistration* admin_reg_;
00409 
00411     PingRegistration* ping_reg_;
00412 
00414     ContactManager* contactmgr_;
00415 
00417     FragmentManager* fragmentmgr_;
00418 
00420     RegistrationTable* reg_table_;
00421 
00423     BundleList* pending_bundles_;
00424 
00426     BundleList* custody_bundles_;
00427     
00429     oasys::MsgQueue<BundleEvent*>* eventq_;
00430 
00433     EndpointID local_eid_;
00434 
00436     struct Stats {
00437         u_int32_t bundles_received_;
00438         u_int32_t bundles_delivered_;
00439         u_int32_t bundles_generated_;
00440         u_int32_t bundles_transmitted_;
00441         u_int32_t bundles_expired_;
00442         u_int32_t duplicate_bundles_;
00443         u_int32_t events_processed_;
00444     };
00445 
00447     Stats stats_;
00448 
00450     ShutdownProc app_shutdown_proc_;
00451  
00453     void* app_shutdown_data_;
00454 
00456     ShutdownProc rtr_shutdown_proc_;
00457 
00459     void* rtr_shutdown_data_;
00460 
00461     // indicator that a BundleDaemon shutdown is in progress
00462     static bool shutting_down_;
00463 
00464     // bit that's set when we're running in the simulator
00465     static bool is_simulator_;
00466 };
00467 
00468 } // namespace dtn
00469 
00470 #endif /* _BUNDLE_DAEMON_H_ */

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