|
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 _DTN_SIM_EVENT_H_ |
|
18 #define _DTN_SIM_EVENT_H_ |
|
19 |
|
20 #include <oasys/tclcmd/TclCommand.h> |
|
21 #include "bundling/BundleEvent.h" |
|
22 |
|
23 using namespace dtn; |
|
24 |
|
25 namespace dtnsim { |
|
26 |
|
27 class ConnState; |
|
28 class SimEventHandler; |
|
29 |
|
30 /****************************************************************************** |
|
31 * Event Types |
|
32 *****************************************************************************/ |
|
33 typedef enum { |
|
34 SIM_AT_EVENT = 0x1, ///< Generic event for delayed tcl commands |
|
35 SIM_BUNDLE_EVENT, ///< Generic event for delayed bundle events |
|
36 } sim_event_type_t; |
|
37 |
|
38 /** |
|
39 * Pretty printer function simulation events. |
|
40 */ |
|
41 static const char* |
|
42 sim_ev2str(sim_event_type_t event) { |
|
43 switch (event) { |
|
44 case SIM_AT_EVENT: return "SIM_AT_EVENT"; |
|
45 case SIM_BUNDLE_EVENT: return "SIM_BUNDLE_EVENT"; |
|
46 } |
|
47 |
|
48 NOTREACHED; |
|
49 } |
|
50 |
|
51 /****************************************************************************** |
|
52 * Base Event Class |
|
53 *****************************************************************************/ |
|
54 class SimEvent { |
|
55 public: |
|
56 /** |
|
57 * Constructor |
|
58 */ |
|
59 SimEvent(sim_event_type_t type, double time, SimEventHandler *handler) |
|
60 : type_(type), time_(time), handler_(handler), valid_(true) {} |
|
61 |
|
62 SimEventHandler* handler() { return handler_; } |
|
63 double time() { return time_ ; } |
|
64 bool is_valid() { return valid_; } |
|
65 sim_event_type_t type() { return type_ ; } |
|
66 const char* type_str() { return sim_ev2str(type_); } |
|
67 |
|
68 void cancel() { valid_ = false; } |
|
69 |
|
70 private: |
|
71 sim_event_type_t type_; ///< Type of the event |
|
72 double time_; ///< Time when the event will fire |
|
73 SimEventHandler* handler_; ///< Handler that will process the event |
|
74 bool valid_; ///< Indicator if the event was cancelled |
|
75 }; |
|
76 |
|
77 /****************************************************************************** |
|
78 * SimEventCompare |
|
79 *****************************************************************************/ |
|
80 class SimEventCompare { |
|
81 public: |
|
82 /** |
|
83 * The comparator function used in the priority queue. |
|
84 */ |
|
85 bool operator () (SimEvent* a, SimEvent* b) |
|
86 { |
|
87 return a->time() > b->time(); |
|
88 } |
|
89 }; |
|
90 |
|
91 /******************************************************************* |
|
92 * SimAtEvent |
|
93 ******************************************************************/ |
|
94 class SimAtEvent : public SimEvent { |
|
95 public: |
|
96 SimAtEvent(double time, SimEventHandler* handler) |
|
97 : SimEvent(SIM_AT_EVENT, time, handler), objc_(0) {} |
|
98 |
|
99 int objc_; |
|
100 Tcl_Obj* objv_[64]; |
|
101 }; |
|
102 |
|
103 /******************************************************************* |
|
104 * SimBundleEvent |
|
105 ******************************************************************/ |
|
106 class SimBundleEvent : public SimEvent { |
|
107 public: |
|
108 SimBundleEvent(double time, SimEventHandler* handler, |
|
109 dtn::BundleEvent* event) |
|
110 : SimEvent(SIM_BUNDLE_EVENT, time, handler), event_(event) {} |
|
111 |
|
112 dtn::BundleEvent* event_; |
|
113 }; |
|
114 |
|
115 } // namespace dtnsim |
|
116 |
|
117 #endif /* _DTN_SIM_EVENT_H_ */ |