|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Core |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <string> |
|
19 #include <iostream> |
|
20 #include <algorithm> |
|
21 |
|
22 #ifdef __SYMBIAN32__ |
|
23 #include "e32std.h" |
|
24 #else |
|
25 #include <sys/types.h> |
|
26 #include <sys/wait.h> |
|
27 #include "signalhandlermessages.h" |
|
28 #endif /* __SYMBIAN32__ */ |
|
29 |
|
30 #include "logger.h" |
|
31 #include "comms.h" |
|
32 #include "commsmessage.h" |
|
33 #include "libraryloaderexception.h" |
|
34 #include "javacommonutils.h" |
|
35 #include "monitor.h" |
|
36 |
|
37 #include "core.h" |
|
38 #include "coremessages.h" |
|
39 #include "extensionplugininterface.h" |
|
40 #include "extensionplugindata.h" |
|
41 |
|
42 #include "booteventprovider.h" |
|
43 #include "mmceventprovider.h" |
|
44 |
|
45 #include "timerdata.h" |
|
46 #include "tickerprovider.h" |
|
47 |
|
48 using namespace java::comms; |
|
49 |
|
50 namespace java |
|
51 { |
|
52 namespace captain |
|
53 { |
|
54 |
|
55 //using java::util::ScopedLock; |
|
56 |
|
57 Core::Core(java::util::Monitor* aMonitor) |
|
58 :mTicker(NULL), |
|
59 mMonitor(aMonitor) |
|
60 { |
|
61 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
62 } |
|
63 |
|
64 Core::~Core() |
|
65 { |
|
66 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
67 mMonitor = 0; |
|
68 mTicker = 0; |
|
69 } |
|
70 |
|
71 bool Core::start() |
|
72 { |
|
73 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
74 registerDefaultListener(this); |
|
75 CommsServerEndpoint::start(IPC_ADDRESS_JAVA_CAPTAIN_C); |
|
76 |
|
77 return true; |
|
78 } |
|
79 |
|
80 int Core::stop() |
|
81 { |
|
82 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
83 unregisterDefaultListener(this); |
|
84 return CommsServerEndpoint::stop(); |
|
85 } |
|
86 |
|
87 void Core::onStart() |
|
88 { |
|
89 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
90 mTicker = new TickerProvider(this); |
|
91 mPmc.start(this, this); |
|
92 mAmc.start(this, this); |
|
93 mRtc.start(this, this); |
|
94 loadConfigExtensionPlugin(); |
|
95 } |
|
96 |
|
97 void Core::onExit() |
|
98 { |
|
99 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
100 unloadExtensionPlugins(); |
|
101 mRtc.stop(); |
|
102 mAmc.stop(); |
|
103 mPmc.stop(); |
|
104 delete mTicker; |
|
105 for (timersIterator iter = mTimers.begin() ; iter != mTimers.end() ; ++iter) |
|
106 { |
|
107 delete(*iter); |
|
108 } |
|
109 mTimers.clear(); |
|
110 |
|
111 mMonitor->notify(); |
|
112 } |
|
113 |
|
114 RtcInterface* Core::getRtc() |
|
115 { |
|
116 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
117 return &mRtc; |
|
118 } |
|
119 |
|
120 PmcInterface* Core::getPmc() |
|
121 { |
|
122 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
123 return &mPmc; |
|
124 } |
|
125 |
|
126 AmcInterface* Core::getAmc() |
|
127 { |
|
128 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
129 return &mAmc; |
|
130 } |
|
131 |
|
132 TimerServerInterface* Core::getTimerServer() |
|
133 { |
|
134 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
135 return this; |
|
136 } |
|
137 |
|
138 CommsEndpoint* Core::getComms() |
|
139 { |
|
140 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
141 return this; |
|
142 } |
|
143 |
|
144 ExtensionPluginInterface* Core::loadExtensionPlugin(const std::string& aPluginName) |
|
145 { |
|
146 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
147 LOG1(EJavaCaptain, EInfo, "Core::loadExtensionPlugin(%s)", aPluginName.c_str()); |
|
148 |
|
149 extensionPluginsIterator iter = mExtensionPlugins.begin(); |
|
150 for (; iter != mExtensionPlugins.end() ; ++iter) |
|
151 { |
|
152 if ((*iter)->getName() == aPluginName) |
|
153 { |
|
154 break; |
|
155 } |
|
156 } |
|
157 |
|
158 if (iter != mExtensionPlugins.end()) |
|
159 { |
|
160 (*iter)->addLoadCount(); |
|
161 return (*iter)->mExtensionPlugin; |
|
162 } |
|
163 |
|
164 ExtensionPluginInterface* interface = NULL; |
|
165 |
|
166 if (aPluginName == "boot") |
|
167 { |
|
168 interface = new BootEventProvider(); |
|
169 mExtensionPlugins.push_back(new ExtensionPluginData(interface, aPluginName)); |
|
170 // needs to be pushed in to the collection before starting due to dependencies |
|
171 interface->startPlugin(this); |
|
172 } |
|
173 else if (aPluginName == "mmc") |
|
174 { |
|
175 interface = new MmcEventProvider(); |
|
176 mExtensionPlugins.push_back(new ExtensionPluginData(interface, aPluginName)); |
|
177 // needs to be pushed in to the collection before starting due to dependencies |
|
178 interface->startPlugin(this); |
|
179 } |
|
180 else |
|
181 { |
|
182 try |
|
183 { |
|
184 std::string libraryName = "javacaptain_ext_" + aPluginName; |
|
185 |
|
186 std::auto_ptr<java::util::DynamicLibLoader> |
|
187 loader(new java::util::DynamicLibLoader(libraryName.c_str())); |
|
188 |
|
189 java::captain::ExtensionPluginInterface*(*GetExtensionPluginFunc)() = |
|
190 (java::captain::ExtensionPluginInterface*(*)()) |
|
191 loader->getFunction("getExtensionPlugin"); |
|
192 |
|
193 if (GetExtensionPluginFunc) |
|
194 { |
|
195 interface = GetExtensionPluginFunc(); |
|
196 mExtensionPlugins.push_back(new ExtensionPluginDataDynamic(interface, loader, aPluginName)); |
|
197 // needs to be pushed in to the collection before starting due to dependencies |
|
198 interface->startPlugin(this); |
|
199 } |
|
200 } |
|
201 catch (java::util::LibraryLoaderException& ex) |
|
202 { |
|
203 ELOG2(EJavaCaptain, "Core::loadExtensionPlugin(%s) failed due: %s", |
|
204 aPluginName.c_str(), ex.toString().c_str()); |
|
205 } |
|
206 } |
|
207 |
|
208 return interface; |
|
209 } |
|
210 |
|
211 void Core::unloadExtensionPlugin(const std::string& aPluginName) |
|
212 { |
|
213 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
214 LOG1(EJavaCaptain, EInfo, "Core::unloadExtensionPlugin(%s)", aPluginName.c_str()); |
|
215 |
|
216 extensionPluginsIterator iter = mExtensionPlugins.begin(); |
|
217 for (; iter != mExtensionPlugins.end() ; ++iter) |
|
218 { |
|
219 if ((*iter)->getName() == aPluginName) |
|
220 { |
|
221 break; |
|
222 } |
|
223 } |
|
224 |
|
225 if (iter != mExtensionPlugins.end()) |
|
226 { |
|
227 if (0 == (*iter)->decLoadCount()) |
|
228 { |
|
229 // Stopped via destructor while still in the collection |
|
230 delete(*iter); |
|
231 mExtensionPlugins.erase(iter); |
|
232 } |
|
233 } |
|
234 } |
|
235 |
|
236 EventConsumerInterface* Core::getEventDispatcher() |
|
237 { |
|
238 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
239 return this; |
|
240 } |
|
241 |
|
242 // TimerServerInterface methods |
|
243 int Core::timerCreateSeconds(const unsigned int& aTimeoutInSeconds, |
|
244 TimerServerEventsInterface* aTimerEvents) |
|
245 { |
|
246 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
247 |
|
248 int timerId = timerCreateJavaTime(JavaTime(mTicker->getCurrentJavaTime() + |
|
249 aTimeoutInSeconds * 1000LL), |
|
250 aTimerEvents); |
|
251 LOG2(EJavaCaptain, EInfoHeavyLoad, "timerCreateSeconds(%d) -> timerId %d", |
|
252 aTimeoutInSeconds, timerId); |
|
253 return timerId; |
|
254 } |
|
255 |
|
256 bool compareTimerDatas(TimerData*& a, TimerData*& b) |
|
257 { |
|
258 return a->getTimeout().getTime() < b->getTimeout().getTime(); |
|
259 } |
|
260 |
|
261 int Core::timerCreateJavaTime(const JavaTime& aJavaTime, |
|
262 TimerServerEventsInterface* aTimerEvents) |
|
263 { |
|
264 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
265 |
|
266 TimerData* td = new TimerData(aJavaTime, aTimerEvents); |
|
267 mTimers.push_back(td); |
|
268 mTimers.sort(compareTimerDatas); |
|
269 |
|
270 triggerTicker(); |
|
271 |
|
272 LOG1(EJavaCaptain, EInfo, "timerCreateJavaTime(%#x)", td); |
|
273 |
|
274 return (int)td; |
|
275 } |
|
276 |
|
277 void Core::timerCancel(const int& aTimerId) |
|
278 { |
|
279 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
280 LOG1(EJavaCaptain, EInfo, "timerCancel(%#x)", aTimerId); |
|
281 |
|
282 for (timersIterator iter = mTimers.begin() ; |
|
283 iter != mTimers.end() ; ++iter) |
|
284 { |
|
285 if ((int)(*iter) == aTimerId) |
|
286 { |
|
287 delete(*iter); |
|
288 mTimers.erase(iter); |
|
289 break; |
|
290 } |
|
291 } |
|
292 triggerTicker(); |
|
293 } |
|
294 |
|
295 JavaTime& Core::getCurrentJavaTime(JavaTime& aJt) const |
|
296 { |
|
297 aJt.setTime(mTicker->getCurrentJavaTime()); |
|
298 return aJt; |
|
299 } |
|
300 |
|
301 bool Core::hasExpired(const JavaTime& aJt, const long long& aAccuracyInMs) const |
|
302 { |
|
303 JavaTime currentTime; |
|
304 // Within aAccuracyTime is considered expired to due OS limitations |
|
305 return isMore(getCurrentJavaTime(currentTime) + aAccuracyInMs, aJt); |
|
306 } |
|
307 |
|
308 bool Core::isLess(const JavaTime& a, const JavaTime& b) const |
|
309 { |
|
310 return a.getTime() < b.getTime(); |
|
311 } |
|
312 |
|
313 bool Core::isEqual(const JavaTime& a, const JavaTime& b) const |
|
314 { |
|
315 return a.getTime() == b.getTime(); |
|
316 } |
|
317 |
|
318 bool Core::isMore(const JavaTime& a, const JavaTime& b) const |
|
319 { |
|
320 return !isLess(a, b); |
|
321 } |
|
322 |
|
323 void Core::triggerTicker() |
|
324 { |
|
325 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
326 if (mTimers.size() > 0) |
|
327 { |
|
328 long long nextTickAt = mTicker->getNextTickAt(); |
|
329 if (!nextTickAt || isMore(JavaTime(nextTickAt), (*mTimers.begin())->getTimeout())) |
|
330 { |
|
331 mTicker->nextTickAt((*mTimers.begin())->getTimeout().getTime()); |
|
332 } |
|
333 } |
|
334 else |
|
335 { |
|
336 mTicker->cancel(); |
|
337 } |
|
338 } |
|
339 |
|
340 // TickerEventsInterface methods |
|
341 void Core::tick() |
|
342 { |
|
343 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
344 |
|
345 JavaTime currentTime = JavaTime(mTicker->getCurrentJavaTime()); |
|
346 |
|
347 for (timersIterator iter = mTimers.begin() ; |
|
348 iter != mTimers.end() ; /*iter++*/) |
|
349 { |
|
350 if (hasExpired((*iter)->getTimeout())) |
|
351 { |
|
352 (*iter)->timeoutReached(); |
|
353 delete(*iter); |
|
354 iter = mTimers.erase(iter); |
|
355 } |
|
356 else |
|
357 { |
|
358 ++iter; |
|
359 } |
|
360 } |
|
361 triggerTicker(); |
|
362 } |
|
363 |
|
364 // Helper macro, a template would be better but I prefer macros for 'clarity' |
|
365 #define DISTR_EVENTS(A, B) \ |
|
366 for(extensionPluginsIterator iter = mExtensionPlugins.begin(); \ |
|
367 iter != mExtensionPlugins.end(); ++iter) { \ |
|
368 A##EventsInterface* listener = \ |
|
369 (*iter)->mExtensionPlugin->get##A##Listener(); \ |
|
370 if(listener) listener->B; } |
|
371 |
|
372 // ApplicationRuntimeEventsInterface methods |
|
373 void Core::arLaunched(const Uid& aUID, const int& aRuntimeCommsAddress) |
|
374 { |
|
375 mAmc.arLaunched(aUID, aRuntimeCommsAddress); |
|
376 DISTR_EVENTS(ApplicationRuntime, arLaunched(aUID, aRuntimeCommsAddress)) |
|
377 } |
|
378 void Core::arTerminated(const Uid& aUID, const int& aExitCode) |
|
379 { |
|
380 mAmc.arTerminated(aUID, aExitCode); |
|
381 DISTR_EVENTS(ApplicationRuntime, arTerminated(aUID, aExitCode)) |
|
382 } |
|
383 |
|
384 // ApplicationManagementEventsInterface methods |
|
385 void Core::amAdded(const uids_t& aUIDs) |
|
386 { |
|
387 mRtc.amAdded(aUIDs); |
|
388 LOG1(EJavaCaptain, EInfo, "amAdded event dispatcher %d uids", aUIDs.size()); |
|
389 DISTR_EVENTS(ApplicationManagement, amAdded(aUIDs)) |
|
390 } |
|
391 void Core::amUpdated(const uids_t& aUIDs) |
|
392 { |
|
393 mRtc.amUpdated(aUIDs); |
|
394 LOG1(EJavaCaptain, EInfo, "amUpdated event dispatcher %d uids", aUIDs.size()); |
|
395 DISTR_EVENTS(ApplicationManagement, amUpdated(aUIDs)) |
|
396 } |
|
397 void Core::amDeleted(const uids_t& aUIDs) |
|
398 { |
|
399 mRtc.amDeleted(aUIDs); |
|
400 LOG1(EJavaCaptain, EInfo, "amDeleted event dispatcher %d uids", aUIDs.size()); |
|
401 DISTR_EVENTS(ApplicationManagement, amDeleted(aUIDs)) |
|
402 } |
|
403 |
|
404 // ProcessManagementEventsInterface methods |
|
405 void Core::pmcTerminated(const int& pid, const int& exitCode) |
|
406 { |
|
407 LOG2(EJavaCaptain, EInfo, "pmcTerminated event dispatcher pid=%d, exitCode=%d", pid, exitCode); |
|
408 mRtc.pmcTerminated(pid, exitCode); |
|
409 DISTR_EVENTS(ProcessManagement, pmcTerminated(pid, exitCode)) |
|
410 } |
|
411 |
|
412 // EventConsumerInterface methods |
|
413 void Core::event(const std::string& eventProvider, |
|
414 java::comms::CommsMessage& aMsg) |
|
415 { |
|
416 for (extensionPluginsIterator iter = mExtensionPlugins.begin(); |
|
417 iter != mExtensionPlugins.end(); |
|
418 ++iter) |
|
419 { |
|
420 EventConsumerInterface* consumer = (*iter)->mExtensionPlugin->getEventConsumer(); |
|
421 if (consumer) |
|
422 { |
|
423 consumer->event(eventProvider, aMsg); |
|
424 aMsg.begin(); |
|
425 } |
|
426 } |
|
427 // Rtc needs to listen boot events because of prewarm |
|
428 mRtc.event(eventProvider, aMsg); |
|
429 aMsg.begin(); |
|
430 } |
|
431 |
|
432 // Default messsage handler, means that the receiver module was not loaded yet |
|
433 void Core::processMessage(CommsMessage& aMessage) |
|
434 { |
|
435 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
436 |
|
437 switch (aMessage.getModuleId()) |
|
438 { |
|
439 case PLUGIN_ID_JAVACAPTAIN_CORE_C: |
|
440 switch (aMessage.getMessageId()) |
|
441 { |
|
442 case CORE_MSG_ID_STOP_JAVACAPTAIN: |
|
443 if (aMessage.hasPermission(STOP_APPLICATION)) |
|
444 { |
|
445 WLOG(EJavaCaptain, "STOP message received"); |
|
446 mRtc.terminateRtcRuntimes(); |
|
447 mMonitor->notify(); |
|
448 } |
|
449 break; |
|
450 case CORE_MSG_ID_DO_THREAD_DUMP: |
|
451 if (aMessage.hasPermission(LAUNCH_APPLICATION)) |
|
452 { |
|
453 LOG(EJavaCaptain, EInfo, "DO_THREAD_DUMP message reveived"); |
|
454 mRtc.routeMessageToAll(aMessage); |
|
455 } |
|
456 break; |
|
457 |
|
458 case CORE_MSG_ID_START_PREWARM: |
|
459 if (aMessage.hasPermission(LAUNCH_APPLICATION)) |
|
460 { |
|
461 WLOG(EJavaCaptain, "START prewarm message received"); |
|
462 mRtc.launchPrewarm(); |
|
463 } |
|
464 break; |
|
465 |
|
466 case CORE_MSG_ID_STOP_PREWARM: |
|
467 if (aMessage.hasPermission(STOP_APPLICATION)) |
|
468 { |
|
469 WLOG(EJavaCaptain, "STOP prewarm message received"); |
|
470 mRtc.stopPrewarm(); |
|
471 } |
|
472 break; |
|
473 |
|
474 default: |
|
475 break; |
|
476 } |
|
477 break; |
|
478 |
|
479 #ifndef __SYMBIAN32__ |
|
480 case PLUGIN_ID_SIGNAL_C: |
|
481 LOG(EJavaCaptain, EInfo, "SIGNAL message received"); |
|
482 switch (aMessage.getMessageId()) |
|
483 { |
|
484 case SIG_MSG_ID_SIGCHLD: |
|
485 { |
|
486 int pid = 0; |
|
487 int status = 0; |
|
488 getSignalChildMessage(aMessage, pid, status); |
|
489 pmcTerminated(pid, status); |
|
490 } |
|
491 break; |
|
492 |
|
493 case SIG_MSG_ID_SIGALRM: |
|
494 tick(); |
|
495 break; |
|
496 |
|
497 case SIG_MSG_ID_SIGINT: |
|
498 LOG(EJavaCaptain, EInfo, "STOP (SIGINT)message received"); |
|
499 mRtc.terminateRtcRuntimes(); |
|
500 mMonitor->notify(); |
|
501 break; |
|
502 } |
|
503 |
|
504 break; |
|
505 #endif /* __SYMBIAN32__ */ |
|
506 |
|
507 default: |
|
508 { |
|
509 std::string extensionPluginName = "ondemand_" + java::util::JavaCommonUtils::intToString(aMessage.getModuleId()); |
|
510 ExtensionPluginInterface* interface = loadExtensionPlugin(extensionPluginName.c_str()); |
|
511 if (interface) |
|
512 { |
|
513 java::comms::CommsListener* commsListener = interface->getCommsListener(); |
|
514 if (commsListener) |
|
515 { |
|
516 commsListener->processMessage(aMessage); |
|
517 } |
|
518 } |
|
519 else |
|
520 { |
|
521 WLOG1(EJavaCaptain, "JavaCaptain::ProcessMessage, sender = %d\n", aMessage.getSender()); |
|
522 WLOG1(EJavaCaptain, "JavaCaptain::ProcessMessage, messageId = %d\n", aMessage.getMessageId()); |
|
523 WLOG1(EJavaCaptain, "JavaCaptain::ProcessMessage, moduleId = %d\n", aMessage.getModuleId()); |
|
524 } |
|
525 } |
|
526 } |
|
527 } |
|
528 |
|
529 void Core::loadConfigExtensionPlugin() |
|
530 { |
|
531 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
532 loadExtensionPlugin("config"); |
|
533 // Let's notify the Starter that the boot may proceed |
|
534 #ifdef __SYMBIAN32__ |
|
535 RProcess::Rendezvous(KErrNone); |
|
536 #endif |
|
537 } |
|
538 |
|
539 void Core::unloadExtensionPlugins() |
|
540 { |
|
541 JELOG4(EJavaCaptain, EInfoHeavyLoad); |
|
542 |
|
543 ExtensionPluginData* plugin = NULL; |
|
544 |
|
545 do |
|
546 { |
|
547 plugin = NULL; |
|
548 { |
|
549 extensionPluginsIterator iter = mExtensionPlugins.begin(); |
|
550 if (iter != mExtensionPlugins.end()) |
|
551 { |
|
552 plugin = (*iter); |
|
553 mExtensionPlugins.erase(iter); |
|
554 } |
|
555 } |
|
556 if (plugin) |
|
557 { |
|
558 delete plugin; |
|
559 } |
|
560 } |
|
561 while (mExtensionPlugins.size() > 0); |
|
562 } |
|
563 |
|
564 } // namespace captain |
|
565 } // namespace java |
|
566 |