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 | #include "StartupWrapper.h"
|
---|
40 | #include "ariba/config.h"
|
---|
41 |
|
---|
42 | #ifdef HAVE_LOG4CXX
|
---|
43 | #include <log4cxx/logger.h>
|
---|
44 | #include <log4cxx/basicconfigurator.h>
|
---|
45 | #endif // HAVE_LOG4CXX
|
---|
46 |
|
---|
47 | namespace ariba {
|
---|
48 | namespace utility {
|
---|
49 |
|
---|
50 | StartupWrapper::ConfigurationList StartupWrapper::configurations;
|
---|
51 | #ifdef UNDERLAY_OMNET
|
---|
52 | StartupWrapper::ModuleList StartupWrapper::modules;
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | SystemEventType StartupWrapperEventStartup("StartupWrapperEventStartup");
|
---|
56 |
|
---|
57 | StartupWrapper::StartupWrapper(StartupInterface* _service) : service( _service ){
|
---|
58 | }
|
---|
59 |
|
---|
60 | StartupWrapper::~StartupWrapper(){
|
---|
61 | }
|
---|
62 |
|
---|
63 | #ifdef UNDERLAY_OMNET
|
---|
64 | void StartupWrapper::insertCurrentModule(AribaOmnetModule* mod){
|
---|
65 | modules.push( mod );
|
---|
66 | }
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | #ifdef UNDERLAY_OMNET
|
---|
70 | AribaOmnetModule* StartupWrapper::getCurrentModule(){
|
---|
71 | assert( modules.size() > 0 );
|
---|
72 |
|
---|
73 | AribaOmnetModule* ret = modules.front();
|
---|
74 | modules.pop();
|
---|
75 |
|
---|
76 | return ret;
|
---|
77 | }
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | void StartupWrapper::startSystem(){
|
---|
81 |
|
---|
82 | //
|
---|
83 | // having seeded the pseudo rng is always good
|
---|
84 | //
|
---|
85 |
|
---|
86 | srand( time(NULL) );
|
---|
87 |
|
---|
88 | //
|
---|
89 | // init the system queue
|
---|
90 | //
|
---|
91 |
|
---|
92 | if( ! SystemQueue::instance().isRunning() )
|
---|
93 | SystemQueue::instance().run();
|
---|
94 |
|
---|
95 | //
|
---|
96 | // init the logging system
|
---|
97 | //
|
---|
98 |
|
---|
99 | #ifdef HAVE_LOG4CXX
|
---|
100 | log4cxx::BasicConfigurator::configure();
|
---|
101 | #endif //HAVE_LOG4CXX
|
---|
102 |
|
---|
103 | //
|
---|
104 | // configure the default logging level to info
|
---|
105 | //
|
---|
106 |
|
---|
107 | logging_rootlevel_info();
|
---|
108 | }
|
---|
109 |
|
---|
110 | void StartupWrapper::stopSystem(){
|
---|
111 | SystemQueue::instance().cancel();
|
---|
112 | }
|
---|
113 |
|
---|
114 | void StartupWrapper::initConfig(string filename){
|
---|
115 | configurations.push( filename );
|
---|
116 | Configuration::setConfigFilename( filename );
|
---|
117 | }
|
---|
118 |
|
---|
119 | void StartupWrapper::handleSystemEvent(const SystemEvent& event){
|
---|
120 |
|
---|
121 | if( event.getType() == StartupWrapperEventStartup ){
|
---|
122 |
|
---|
123 | if(!configurations.empty()){
|
---|
124 | string config = configurations.front();
|
---|
125 | configurations.pop();
|
---|
126 | Configuration::setConfigFilename( config );
|
---|
127 | }
|
---|
128 |
|
---|
129 | //
|
---|
130 | // start the actual application
|
---|
131 | //
|
---|
132 |
|
---|
133 | // TODO: im falle von omnet ist service = null, da von SpoVNetOmnetModule so ÃŒbergeben
|
---|
134 | // wie wird im Falle von omnet die anwendung erstellt?
|
---|
135 |
|
---|
136 | service->startup();
|
---|
137 |
|
---|
138 | }
|
---|
139 |
|
---|
140 | }
|
---|
141 |
|
---|
142 | void StartupWrapper::startup(StartupInterface* service, bool block){
|
---|
143 |
|
---|
144 | StartupWrapper* startup = new StartupWrapper(service);
|
---|
145 | service->wrapper = startup;
|
---|
146 |
|
---|
147 | SystemQueue::instance().scheduleEvent(
|
---|
148 | SystemEvent( startup, StartupWrapperEventStartup, NULL), 0 );
|
---|
149 |
|
---|
150 | #ifndef UNDERLAY_OMNET
|
---|
151 | if( block ) getchar();
|
---|
152 | #endif
|
---|
153 | }
|
---|
154 |
|
---|
155 | void StartupWrapper::shutdown(StartupInterface* service, bool block){
|
---|
156 |
|
---|
157 | if( service == NULL || service->wrapper == NULL ) return;
|
---|
158 |
|
---|
159 | #ifdef UNDERLAY_OMNET
|
---|
160 | //TODO: service->shutdown();
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | if(block){
|
---|
164 | // call directly
|
---|
165 | service->shutdown();
|
---|
166 | }else{
|
---|
167 | // call async, but not using systemqueue! // TODO: mem leak
|
---|
168 | AsyncShutdown* async = new AsyncShutdown(service);
|
---|
169 | async->runBlockingMethod();
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | StartupWrapper::AsyncShutdown::AsyncShutdown(StartupInterface* _service)
|
---|
174 | : service(_service){
|
---|
175 | }
|
---|
176 |
|
---|
177 | void StartupWrapper::AsyncShutdown::blockingFunction(){
|
---|
178 | service->shutdown();
|
---|
179 | }
|
---|
180 |
|
---|
181 | void StartupWrapper::AsyncShutdown::dispatchFunction(){
|
---|
182 | //unused
|
---|
183 | }
|
---|
184 |
|
---|
185 | }} // namespace ariba, utility
|
---|