SimEvent.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) 2004 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 _DTN_SIM_EVENT_H_
00039 #define _DTN_SIM_EVENT_H_
00040 
00041 #include "bundling/BundleEvent.h"
00042 
00043 using namespace dtn;
00044 
00045 namespace dtnsim {
00046 
00047 class ConnState;
00048 class SimEventHandler;
00049 
00050 /******************************************************************************
00051  *
00052  * Event Types
00053  *
00054  *****************************************************************************/
00055 typedef enum {
00056     SIM_ROUTER_EVENT = 0x1,     
00057     SIM_ADD_LINK,               
00058     SIM_DEL_LINK,               
00059     SIM_ADD_ROUTE,              
00060     SIM_DEL_ROUTE,              
00061     SIM_CONTACT_UP,             
00062     SIM_CONTACT_DOWN,           
00063     SIM_CONNECTIVITY,           
00064     SIM_NEXT_SENDTIME,          
00065     
00066 } sim_event_type_t;
00067 
00071 static const char* 
00072 sim_ev2str(sim_event_type_t event) {
00073     switch (event) {
00074     case SIM_ROUTER_EVENT:              return "SIM_ROUTER_EVENT";
00075     case SIM_ADD_LINK:                  return "SIM_ADD_LINK";
00076     case SIM_DEL_LINK:                  return "SIM_DEL_LINK";
00077     case SIM_ADD_ROUTE:                 return "SIM_ADD_ROUTE";
00078     case SIM_DEL_ROUTE:                 return "SIM_DEL_ROUTE";
00079     case SIM_CONTACT_UP:                return "SIM_CONTACT_UP";
00080     case SIM_CONTACT_DOWN:              return "SIM_CONTACT_DOWN";
00081     case SIM_CONNECTIVITY:              return "SIM_CONNECTIVITY";
00082     case SIM_NEXT_SENDTIME:             return "SIM_NEXT_SENDTIME";
00083     }
00084 
00085     NOTREACHED;
00086 }
00087 
00088 /******************************************************************************
00089  *
00090  * Base Event Class
00091  *
00092  *****************************************************************************/
00093 class SimEvent {
00094 public:
00098     SimEvent(sim_event_type_t type, double time, SimEventHandler *handler)
00099         : type_(type), time_(time), handler_(handler), valid_(true) {}
00100 
00101     SimEventHandler* handler()  { return handler_; }
00102     double time()               { return time_ ; }
00103     bool is_valid()          { return valid_; }
00104     sim_event_type_t type()  { return type_ ; }
00105     const char* type_str()   { return sim_ev2str(type_); }
00106 
00107     void cancel()            { valid_ = false; }
00108 
00109 private:
00110     sim_event_type_t type_;     
00111     double time_;               
00112     SimEventHandler* handler_;  
00113     bool valid_;                
00114 };
00115 
00116 
00117 /******************************************************************************
00118  *
00119  * SimEventCompare
00120  *
00121  *****************************************************************************/
00122 class SimEventCompare {
00123 public:
00127     bool operator () (SimEvent* a, SimEvent* b)
00128     {
00129         return a->time() > b->time();
00130     }
00131 };
00132 
00133 /*******************************************************************
00134  *
00135  * SimRouterEvent -- catch all event class to wrap delivering an event
00136  * to the bundle router at a particular time.
00137  *
00138  ******************************************************************/
00139 class SimRouterEvent : public SimEvent {
00140 public:
00141     SimRouterEvent(double time, SimEventHandler* handler, BundleEvent* event)
00142         : SimEvent(SIM_ROUTER_EVENT, time, handler), event_(event) {}
00143     
00144     BundleEvent* event_;
00145 };
00146 
00147 /*******************************************************************
00148  *
00149  * SimAddLinkEvent
00150  *
00151  ******************************************************************/
00152 class SimAddLinkEvent : public SimEvent {
00153 public:
00154     SimAddLinkEvent(double time, SimEventHandler* handler, Link* link)
00155         : SimEvent(SIM_ADD_LINK, time, handler), link_(link) {}
00156     
00157     Link* link_;
00158 };
00159 
00160 /*******************************************************************
00161  *
00162  * SimDelLinkEvent
00163  *
00164  ******************************************************************/
00165 class SimDelLinkEvent : public SimEvent {
00166 public:
00167     SimDelLinkEvent(double time, SimEventHandler* handler, Link* link)
00168         : SimEvent(SIM_DEL_LINK, time, handler), link_(link) {}
00169     
00170     Link* link_;
00171 };
00172 
00173 /*******************************************************************
00174  *
00175  * SimAddRouteEvent
00176  *
00177  ******************************************************************/
00178 class SimAddRouteEvent : public SimEvent {
00179 public:
00180     SimAddRouteEvent(double time, SimEventHandler* handler,
00181                      const EndpointIDPattern& dest, const char* nexthop)
00182         : SimEvent(SIM_ADD_ROUTE, time, handler),
00183           dest_(dest), nexthop_(nexthop) {}
00184     
00185     EndpointIDPattern dest_;
00186     std::string nexthop_;
00187 };
00188 
00189 /*******************************************************************
00190  *
00191  * SimConnectivityEvent
00192  *
00193  ******************************************************************/
00194 class SimConnectivityEvent : public SimEvent {
00195 public:
00196     SimConnectivityEvent(double time, SimEventHandler* handler,
00197                          const char* n1, const char* n2, ConnState* state)
00198     : SimEvent(SIM_CONNECTIVITY, time, handler),
00199       n1_(n1), n2_(n2), state_(state) {}
00200 
00201     std::string n1_, n2_;
00202     ConnState* state_;
00203 };
00204 
00205 } // namespace dtnsim
00206 
00207 #endif /* _DTN_SIM_EVENT_H_ */

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1