1 | // [License]
|
---|
2 | // The Ariba-Underlay Copyright
|
---|
3 | //
|
---|
4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
5 | //
|
---|
6 | // Institute of Telematics
|
---|
7 | // UniversitÀt Karlsruhe (TH)
|
---|
8 | // Zirkel 2, 76128 Karlsruhe
|
---|
9 | // Germany
|
---|
10 | //
|
---|
11 | // Redistribution and use in source and binary forms, with or without
|
---|
12 | // modification, are permitted provided that the following conditions are
|
---|
13 | // met:
|
---|
14 | //
|
---|
15 | // 1. Redistributions of source code must retain the above copyright
|
---|
16 | // notice, this list of conditions and the following disclaimer.
|
---|
17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
18 | // notice, this list of conditions and the following disclaimer in the
|
---|
19 | // documentation and/or other materials provided with the distribution.
|
---|
20 | //
|
---|
21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
---|
25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
32 | //
|
---|
33 | // The views and conclusions contained in the software and documentation
|
---|
34 | // are those of the authors and should not be interpreted as representing
|
---|
35 | // official policies, either expressed or implied, of the Institute of
|
---|
36 | // Telematics.
|
---|
37 | // [License]
|
---|
38 |
|
---|
39 | #ifndef SYSTEMQUEUE_H_
|
---|
40 | #define SYSTEMQUEUE_H_
|
---|
41 |
|
---|
42 | #include <vector>
|
---|
43 | #include <cassert>
|
---|
44 | #include "SystemEvent.h"
|
---|
45 | #include "SystemEventListener.h"
|
---|
46 | #include "ariba/utility/logging/Logging.h"
|
---|
47 | #include <boost/date_time.hpp>
|
---|
48 | #include <boost/cstdint.hpp>
|
---|
49 |
|
---|
50 | #ifdef UNDERLAY_OMNET
|
---|
51 | #include <csimplemodule.h>
|
---|
52 | #include <cmessage.h>
|
---|
53 | #include <macros.h>
|
---|
54 | #else
|
---|
55 | #include <boost/thread/mutex.hpp>
|
---|
56 | #include <boost/thread/thread.hpp>
|
---|
57 | #include <boost/thread/condition_variable.hpp>
|
---|
58 | #include <boost/utility.hpp>
|
---|
59 | #include <boost/bind.hpp>
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #include <boost/function.hpp>
|
---|
63 |
|
---|
64 |
|
---|
65 | using std::vector;
|
---|
66 | using boost::posix_time::ptime;
|
---|
67 |
|
---|
68 | namespace ariba {
|
---|
69 | namespace utility {
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * This class implements a simple system event queue to allow
|
---|
73 | * a simulation of cooperative multitasking. It also allows
|
---|
74 | * events to be scheduled from other tasks. This allows
|
---|
75 | * dispatching asynchronous tasks.
|
---|
76 | *
|
---|
77 | * @author Christoph Mayer, Sebastian Mies
|
---|
78 | */
|
---|
79 |
|
---|
80 | #ifndef UNDERLAY_OMNET
|
---|
81 | class SystemQueue : private boost::noncopyable {
|
---|
82 | #else
|
---|
83 | class SystemQueue : public cSimpleModule {
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | use_logging_h(SystemQueue);
|
---|
87 | friend class EnterMethod;
|
---|
88 | public:
|
---|
89 | /**
|
---|
90 | * Get the SystemQueue singleton instance.
|
---|
91 | */
|
---|
92 | static SystemQueue& instance() {
|
---|
93 | static SystemQueue _inst;
|
---|
94 | return _inst;
|
---|
95 | }
|
---|
96 |
|
---|
97 | #ifdef UNDERLAY_OMNET
|
---|
98 | /**
|
---|
99 | * Prevent deletion of this module
|
---|
100 | * by implementing the virtual method
|
---|
101 | * and doing nothing in it
|
---|
102 | */
|
---|
103 | virtual void deleteModule(){}
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * This methods schedules a given event.
|
---|
108 | *
|
---|
109 | * @param The event to be scheduled
|
---|
110 | * @param The delay in milli-seconds
|
---|
111 | */
|
---|
112 | void scheduleEvent( const SystemEvent& event, uint32_t delay = 0 );
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * This method schedules a function call in the SystemQueue.
|
---|
116 | * (Like scheduleEvent, but to be used with boost::bind.)
|
---|
117 | *
|
---|
118 | * @param function: The function to be called [void function()]
|
---|
119 | * @param The delay in milli-seconds
|
---|
120 | */
|
---|
121 | void scheduleCall( const boost::function0<void>& function, uint32_t delay = 0 );
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Starts the processing and waiting for events.
|
---|
125 | * Use <code>cancel()</code> to end system queue processing and
|
---|
126 | * <code>isEmpty()</code> to check wheter the queue is empty.
|
---|
127 | */
|
---|
128 | void run();
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Cancels the system queue and ends the processing after the
|
---|
132 | * currently processed event is processed.
|
---|
133 | *
|
---|
134 | * This method is thread-safe.
|
---|
135 | */
|
---|
136 | void cancel();
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Drop all queued events for that listener
|
---|
140 | */
|
---|
141 | void dropAll( const SystemEventListener* mlistener);
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Check wheter this queue has items or not.
|
---|
145 | *
|
---|
146 | * @return True, if this queue is empty.
|
---|
147 | */
|
---|
148 | bool isEmpty();
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Is the system queue already started and running?
|
---|
152 | *
|
---|
153 | * @return True, if the system queue is running.
|
---|
154 | */
|
---|
155 | bool isRunning();
|
---|
156 |
|
---|
157 | protected:
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Aqcuire the mutex
|
---|
161 | */
|
---|
162 | void enterMethod();
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Leave the mutex
|
---|
166 | */
|
---|
167 | void leaveMethod();
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Constructs a system queue.
|
---|
171 | */
|
---|
172 | SystemQueue();
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Destroys the system queue. Beware that all events
|
---|
176 | * are canceled
|
---|
177 | */
|
---|
178 | ~SystemQueue();
|
---|
179 |
|
---|
180 | #ifdef UNDERLAY_OMNET
|
---|
181 | virtual void handleMessage( cMessage* msg );
|
---|
182 | #endif
|
---|
183 |
|
---|
184 |
|
---|
185 |
|
---|
186 | private:
|
---|
187 |
|
---|
188 | #ifndef UNDERLAY_OMNET
|
---|
189 | typedef vector<SystemEvent> EventQueue;
|
---|
190 |
|
---|
191 | //********************************************************
|
---|
192 |
|
---|
193 | class QueueThread {
|
---|
194 | public:
|
---|
195 | QueueThread(QueueThread* _transferQueue = NULL);
|
---|
196 | virtual ~QueueThread();
|
---|
197 | void run();
|
---|
198 | void cancel();
|
---|
199 | bool isEmpty();
|
---|
200 | void insert( const SystemEvent& event, uint32_t delay );
|
---|
201 | void enter();
|
---|
202 | void leave();
|
---|
203 | void dropAll( const SystemEventListener* mlistener);
|
---|
204 |
|
---|
205 | protected:
|
---|
206 | virtual void onItemInserted( const SystemEvent& event ) = 0;
|
---|
207 | virtual void onNextQueueItem( const SystemEvent& event ) = 0;
|
---|
208 | QueueThread* transferQueue;
|
---|
209 | EventQueue eventsQueue;
|
---|
210 | boost::mutex queueMutex;
|
---|
211 | private:
|
---|
212 | boost::thread* queueThread;
|
---|
213 | static void threadFunc( QueueThread* obj );
|
---|
214 | boost::condition_variable itemsAvailable;
|
---|
215 | volatile bool running;
|
---|
216 | }; // class QueueThread
|
---|
217 |
|
---|
218 | //********************************************************
|
---|
219 |
|
---|
220 | class QueueThreadDirect : public QueueThread {
|
---|
221 | public:
|
---|
222 | QueueThreadDirect();
|
---|
223 | ~QueueThreadDirect();
|
---|
224 | protected:
|
---|
225 | virtual void onItemInserted( const SystemEvent& event );
|
---|
226 | virtual void onNextQueueItem( const SystemEvent& event );
|
---|
227 | }; // class QueueThreadDirect
|
---|
228 |
|
---|
229 | //********************************************************
|
---|
230 |
|
---|
231 | class QueueThreadDelay : public QueueThread {
|
---|
232 | public:
|
---|
233 | QueueThreadDelay(QueueThread* _transferQueue = NULL);
|
---|
234 | ~QueueThreadDelay();
|
---|
235 | protected:
|
---|
236 | virtual void onItemInserted( const SystemEvent& event );
|
---|
237 | virtual void onNextQueueItem( const SystemEvent& event );
|
---|
238 | private:
|
---|
239 | volatile bool isSleeping;
|
---|
240 | ptime sleepStart;
|
---|
241 | boost::mutex sleepMutex;
|
---|
242 | boost::condition_variable sleepCond;
|
---|
243 | }; // class QueueThreadDelay
|
---|
244 |
|
---|
245 | //********************************************************
|
---|
246 |
|
---|
247 | QueueThreadDirect directScheduler;
|
---|
248 | QueueThreadDelay delayScheduler;
|
---|
249 | volatile bool systemQueueRunning;
|
---|
250 | #endif
|
---|
251 |
|
---|
252 |
|
---|
253 | private:
|
---|
254 | /**
|
---|
255 | * This inner class handles the function-call events.
|
---|
256 | * @see SystemQueue::scheduleCall
|
---|
257 | */
|
---|
258 | class FunctionCaller : public SystemEventListener
|
---|
259 | {
|
---|
260 | void handleSystemEvent(const SystemEvent& event)
|
---|
261 | {
|
---|
262 | boost::function0<void>* function_ptr = event.getData< boost::function0<void> >();
|
---|
263 | (*function_ptr)();
|
---|
264 | delete function_ptr;
|
---|
265 | }
|
---|
266 | };
|
---|
267 |
|
---|
268 | FunctionCaller internal_function_caller;
|
---|
269 | }; // class SystemQueue
|
---|
270 |
|
---|
271 | #ifdef UNDERLAY_OMNET
|
---|
272 |
|
---|
273 | #if 0
|
---|
274 | //
|
---|
275 | // the system queue must be a singleton in simulations, too.
|
---|
276 | // and to include it in the simulation the module is defined
|
---|
277 | // as submodule in every SpoVNet host. Therefore we hack the
|
---|
278 | // Define_Module (see omnet/includes/macros.h) the way we need
|
---|
279 | // it with our singleton ...
|
---|
280 | //
|
---|
281 | // this is the macro definition from macros.h
|
---|
282 | //
|
---|
283 | // #define Define_Module(CLASSNAME) /backslash
|
---|
284 | // static cModule *CLASSNAME##__create() {return new CLASSNAME();} /backslash
|
---|
285 | // EXECUTE_ON_STARTUP(CLASSNAME##__mod, modtypes.instance()->add(new cModuleType(#CLASSNAME,#CLASSNAME,(ModuleCreateFunc)CLASSNAME##__create));)
|
---|
286 | //
|
---|
287 | // and this is how we do it :)
|
---|
288 | //
|
---|
289 | #endif
|
---|
290 |
|
---|
291 | static cModule* SystemQueue__create() {
|
---|
292 | return &SystemQueue::instance();
|
---|
293 | }
|
---|
294 |
|
---|
295 | EXECUTE_ON_STARTUP(SystemQueue__mod, modtypes.instance()->add(new cModuleType("SystemQueue","SystemQueue",(ModuleCreateFunc)SystemQueue__create));)
|
---|
296 |
|
---|
297 | #endif
|
---|
298 |
|
---|
299 | }} // spovnet, common
|
---|
300 |
|
---|
301 | #endif /* SYSTEMQUEUE_H_ */
|
---|