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 | using std::vector;
|
---|
63 | using boost::posix_time::ptime;
|
---|
64 |
|
---|
65 | namespace ariba {
|
---|
66 | namespace utility {
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * This class implements a simple system event queue to allow
|
---|
70 | * a simulation of cooperative multitasking. It also allows
|
---|
71 | * events to be scheduled from other tasks. This allows
|
---|
72 | * dispatching asynchronous tasks.
|
---|
73 | *
|
---|
74 | * @author Christoph Mayer, Sebastian Mies
|
---|
75 | */
|
---|
76 |
|
---|
77 | #ifndef UNDERLAY_OMNET
|
---|
78 | class SystemQueue : private boost::noncopyable {
|
---|
79 | #else
|
---|
80 | class SystemQueue : public cSimpleModule {
|
---|
81 | #endif
|
---|
82 | use_logging_h(SystemQueue);
|
---|
83 | public:
|
---|
84 | /**
|
---|
85 | * Get the SystemQueue singleton instance.
|
---|
86 | */
|
---|
87 | static SystemQueue& instance() {
|
---|
88 | static SystemQueue _inst;
|
---|
89 | return _inst;
|
---|
90 | }
|
---|
91 |
|
---|
92 | #ifdef UNDERLAY_OMNET
|
---|
93 | /**
|
---|
94 | * Prevent deletion of this module
|
---|
95 | * by implementing the virtual method
|
---|
96 | * and doing nothing in it
|
---|
97 | */
|
---|
98 | virtual void deleteModule(){}
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * This methods schedules a given event.
|
---|
103 | *
|
---|
104 | * @param The event to be scheduled
|
---|
105 | * @param The delay in milli-seconds
|
---|
106 | */
|
---|
107 | void scheduleEvent( const SystemEvent& event, uint32_t delay = 0 );
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Starts the processing and waiting for events.
|
---|
111 | * Use <code>cancel()</code> to end system queue processing and
|
---|
112 | * <code>isEmpty()</code> to check wheter the queue is empty.
|
---|
113 | */
|
---|
114 | void run();
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Cancels the system queue and ends the processing after the
|
---|
118 | * currently processed event is processed.
|
---|
119 | *
|
---|
120 | * This method is thread-safe.
|
---|
121 | */
|
---|
122 | void cancel();
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Check wheter this queue has items or not.
|
---|
126 | *
|
---|
127 | * @return True, if this queue is empty.
|
---|
128 | */
|
---|
129 | bool isEmpty();
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Is the system queue already started and running?
|
---|
133 | *
|
---|
134 | * @return True, if the system queue is running.
|
---|
135 | */
|
---|
136 | bool isRunning();
|
---|
137 |
|
---|
138 | protected:
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Constructs a system queue.
|
---|
142 | */
|
---|
143 | SystemQueue();
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Destroys the system queue. Beware that all events
|
---|
147 | * are canceled
|
---|
148 | */
|
---|
149 | ~SystemQueue();
|
---|
150 |
|
---|
151 | #ifdef UNDERLAY_OMNET
|
---|
152 | virtual void handleMessage( cMessage* msg );
|
---|
153 | #endif
|
---|
154 |
|
---|
155 | private:
|
---|
156 |
|
---|
157 | #ifndef UNDERLAY_OMNET
|
---|
158 | typedef vector<SystemEvent> EventQueue;
|
---|
159 |
|
---|
160 | //********************************************************
|
---|
161 |
|
---|
162 | class QueueThread {
|
---|
163 | public:
|
---|
164 | QueueThread(QueueThread* _transferQueue = NULL);
|
---|
165 | virtual ~QueueThread();
|
---|
166 | void run();
|
---|
167 | void cancel();
|
---|
168 | bool isEmpty();
|
---|
169 | void insert( const SystemEvent& event, uint32_t delay );
|
---|
170 |
|
---|
171 | protected:
|
---|
172 | virtual void onItemInserted( const SystemEvent& event ) = 0;
|
---|
173 | virtual void onNextQueueItem( const SystemEvent& event ) = 0;
|
---|
174 | QueueThread* transferQueue;
|
---|
175 | EventQueue eventsQueue;
|
---|
176 | boost::mutex queueMutex;
|
---|
177 | private:
|
---|
178 | boost::thread* queueThread;
|
---|
179 | static void threadFunc( QueueThread* obj );
|
---|
180 | boost::condition_variable itemsAvailable;
|
---|
181 | volatile bool running;
|
---|
182 | }; // class QueueThread
|
---|
183 |
|
---|
184 | //********************************************************
|
---|
185 |
|
---|
186 | class QueueThreadDirect : public QueueThread {
|
---|
187 | public:
|
---|
188 | QueueThreadDirect();
|
---|
189 | ~QueueThreadDirect();
|
---|
190 | protected:
|
---|
191 | virtual void onItemInserted( const SystemEvent& event );
|
---|
192 | virtual void onNextQueueItem( const SystemEvent& event );
|
---|
193 | }; // class QueueThreadDirect
|
---|
194 |
|
---|
195 | //********************************************************
|
---|
196 |
|
---|
197 | class QueueThreadDelay : public QueueThread {
|
---|
198 | public:
|
---|
199 | QueueThreadDelay(QueueThread* _transferQueue = NULL);
|
---|
200 | ~QueueThreadDelay();
|
---|
201 | protected:
|
---|
202 | virtual void onItemInserted( const SystemEvent& event );
|
---|
203 | virtual void onNextQueueItem( const SystemEvent& event );
|
---|
204 | private:
|
---|
205 | volatile bool isSleeping;
|
---|
206 | ptime sleepStart;
|
---|
207 | boost::mutex sleepMutex;
|
---|
208 | boost::condition_variable sleepCond;
|
---|
209 | }; // class QueueThreadDelay
|
---|
210 |
|
---|
211 | //********************************************************
|
---|
212 |
|
---|
213 | QueueThreadDirect directScheduler;
|
---|
214 | QueueThreadDelay delayScheduler;
|
---|
215 | volatile bool systemQueueRunning;
|
---|
216 | #endif
|
---|
217 |
|
---|
218 | }; // class SystemQueue
|
---|
219 |
|
---|
220 | #ifdef UNDERLAY_OMNET
|
---|
221 |
|
---|
222 | //
|
---|
223 | // the system queue must be a singleton in simulations, too.
|
---|
224 | // and to include it in the simulation the module is defined
|
---|
225 | // as submodule in every SpoVNet host. Therefore we hack the
|
---|
226 | // Define_Module (see omnet/includes/macros.h) the way we need
|
---|
227 | // it with our singleton ...
|
---|
228 | //
|
---|
229 | // this is the macro definition from macros.h
|
---|
230 | //
|
---|
231 | // #define Define_Module(CLASSNAME) \
|
---|
232 | // static cModule *CLASSNAME##__create() {return new CLASSNAME();} \
|
---|
233 | // EXECUTE_ON_STARTUP(CLASSNAME##__mod, modtypes.instance()->add(new cModuleType(#CLASSNAME,#CLASSNAME,(ModuleCreateFunc)CLASSNAME##__create));)
|
---|
234 | //
|
---|
235 | // and this is how we do it :)
|
---|
236 | //
|
---|
237 |
|
---|
238 | static cModule* SystemQueue__create() {
|
---|
239 | return &SystemQueue::instance();
|
---|
240 | }
|
---|
241 |
|
---|
242 | EXECUTE_ON_STARTUP(SystemQueue__mod, modtypes.instance()->add(new cModuleType("SystemQueue","SystemQueue",(ModuleCreateFunc)SystemQueue__create));)
|
---|
243 |
|
---|
244 | #endif
|
---|
245 |
|
---|
246 | }} // spovnet, common
|
---|
247 |
|
---|
248 | #endif /* SYSTEMQUEUE_H_ */
|
---|