APPLINK-6593:
[profile/ivi/smartdevicelink.git] / src / appMain / life_cycle.cc
1 /**
2 * \file signals.cc
3 * \brief Signal (i.e. SIGINT) handling.
4 * Copyright (c) 2013, Ford Motor Company
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of the Ford Motor Company nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include "./life_cycle.h"
36 #include "utils/signals.h"
37 #include "config_profile/profile.h"
38 #include "resumption/last_state.h"
39
40 using threads::Thread;
41
42 namespace main_namespace {
43 #ifdef ENABLE_LOG
44 log4cxx::LoggerPtr LifeCycle::logger_ = log4cxx::LoggerPtr(
45     log4cxx::Logger::getLogger("appMain"));
46 #endif // ENABLE_LOG
47
48 namespace {
49
50 void NameMessageBrokerThread(const System::Thread& thread,
51                              const std::string& name) {
52   Thread::SetNameForId(Thread::Id(thread.GetId()), name);
53 }
54
55 } // namespace
56
57 LifeCycle::LifeCycle()
58   : transport_manager_(NULL)
59   , protocol_handler_(NULL)
60   , connection_handler_(NULL)
61   , app_manager_(NULL)
62   , hmi_handler_(NULL)
63   , hmi_message_adapter_(NULL)
64   , media_manager_(NULL)
65 #ifdef DBUS_HMIADAPTER
66   , dbus_adapter_(NULL)
67   , dbus_adapter_thread_(NULL)
68 #endif  // DBUS_HMIADAPTER
69 #ifdef MESSAGEBROKER_HMIADAPTER
70   , mb_adapter_(NULL)
71   , message_broker_(NULL)
72   , message_broker_server_(NULL)
73   , mb_thread_(NULL)
74   , mb_server_thread_(NULL)
75   , mb_adapter_thread_(NULL)
76 #endif  // MESSAGEBROKER_HMIADAPTER
77 {
78 }
79
80 bool LifeCycle::StartComponents() {
81   LOG4CXX_INFO(logger_, "LifeCycle::StartComponents()");
82   transport_manager_ =
83     transport_manager::TransportManagerDefault::instance();
84   DCHECK(transport_manager_ != NULL);
85
86   protocol_handler_ =
87     new protocol_handler::ProtocolHandlerImpl(transport_manager_);
88   DCHECK(protocol_handler_ != NULL);
89
90   connection_handler_ =
91     connection_handler::ConnectionHandlerImpl::instance();
92   DCHECK(connection_handler_ != NULL);
93
94   app_manager_ =
95     application_manager::ApplicationManagerImpl::instance();
96   DCHECK(app_manager_ != NULL);
97
98   hmi_handler_ =
99     hmi_message_handler::HMIMessageHandlerImpl::instance();
100   DCHECK(hmi_handler_ != NULL)
101
102   transport_manager_->AddEventListener(protocol_handler_);
103   transport_manager_->AddEventListener(connection_handler_);
104
105   hmi_handler_->set_message_observer(app_manager_);
106
107   media_manager_ = media_manager::MediaManagerImpl::instance();
108
109   protocol_handler_->set_session_observer(connection_handler_);
110   protocol_handler_->AddProtocolObserver(media_manager_);
111   protocol_handler_->AddProtocolObserver(app_manager_);
112   media_manager_->SetProtocolHandler(protocol_handler_);
113
114   connection_handler_->set_transport_manager(transport_manager_);
115   connection_handler_->set_connection_handler_observer(app_manager_);
116
117   // It's important to initialise TM after setting up listener chain
118   // [TM -> CH -> AM], otherwise some events from TM could arrive at nowhere
119   transport_manager_->Init();
120
121   app_manager_->set_protocol_handler(protocol_handler_);
122   app_manager_->set_connection_handler(connection_handler_);
123   app_manager_->set_hmi_message_handler(hmi_handler_);
124
125   return true;
126 }
127
128 #ifdef MESSAGEBROKER_HMIADAPTER
129 bool LifeCycle::InitMessageSystem() {
130   message_broker_ =
131     NsMessageBroker::CMessageBroker::getInstance();
132   if (!message_broker_) {
133     LOG4CXX_INFO(logger_, " Wrong pMessageBroker pointer!");
134     return false;
135   }
136
137   message_broker_server_ =
138     new NsMessageBroker::TcpServer(
139     profile::Profile::instance()->server_address(),
140     profile::Profile::instance()->server_port(),
141     message_broker_);
142   if (!message_broker_server_) {
143     LOG4CXX_INFO(logger_, " Wrong pJSONRPC20Server pointer!");
144     return false;
145   }
146   message_broker_->startMessageBroker(message_broker_server_);
147   if (!networking::init()) {
148     LOG4CXX_INFO(logger_, " Networking initialization failed!");
149     return false;
150   }
151
152   if (!message_broker_server_->Bind()) {
153     LOG4CXX_FATAL(logger_, "Bind failed!");
154     return false;
155   } else {
156     LOG4CXX_INFO(logger_, "Bind successful!");
157   }
158
159   if (!message_broker_server_->Listen()) {
160     LOG4CXX_FATAL(logger_, "Listen failed!");
161     return false;
162   } else {
163     LOG4CXX_INFO(logger_, " Listen successful!");
164   }
165
166   mb_adapter_ =
167     new hmi_message_handler::MessageBrokerAdapter(
168     hmi_message_handler::HMIMessageHandlerImpl::instance(),
169     profile::Profile::instance()->server_address(),
170     profile::Profile::instance()->server_port());
171
172   hmi_message_handler::HMIMessageHandlerImpl::instance()->AddHMIMessageAdapter(
173     mb_adapter_);
174   if (!mb_adapter_->Connect()) {
175     LOG4CXX_INFO(logger_, "Cannot connect to remote peer!");
176     return false;
177   }
178
179   LOG4CXX_INFO(logger_, "Start CMessageBroker thread!");
180   mb_thread_ = new System::Thread(
181     new System::ThreadArgImpl<NsMessageBroker::CMessageBroker>(
182       *message_broker_, &NsMessageBroker::CMessageBroker::MethodForThread,
183       NULL));
184   mb_thread_->Start(false);
185   // Thread can be named only when started because before that point
186   // thread doesn't have valid Id to associate name with
187   NameMessageBrokerThread(*mb_thread_, "MessageBrokerThread");
188
189   LOG4CXX_INFO(logger_, "Start MessageBroker TCP server thread!");
190   mb_server_thread_  = new System::Thread(
191     new System::ThreadArgImpl<NsMessageBroker::TcpServer>(
192       *message_broker_server_, &NsMessageBroker::TcpServer::MethodForThread,
193       NULL));
194   mb_server_thread_->Start(false);
195   NameMessageBrokerThread(*mb_server_thread_, "MessageBrokerTCPServerThread");
196
197   LOG4CXX_INFO(logger_, "StartAppMgr JSONRPC 2.0 controller receiver thread!");
198   mb_adapter_thread_  = new System::Thread(
199     new System::ThreadArgImpl<hmi_message_handler::MessageBrokerAdapter>(
200       *mb_adapter_,
201       &hmi_message_handler::MessageBrokerAdapter::SubscribeAndBeginReceiverThread,
202       NULL));
203   mb_adapter_thread_->Start(false);
204   NameMessageBrokerThread(*mb_adapter_thread_, "MessageBrokerAdapterThread");
205
206   return true;
207 }
208 #endif  // MESSAGEBROKER_HMIADAPTER
209
210 #ifdef DBUS_HMIADAPTER
211 /**
212  * Initialize DBus component
213  * @return true if success otherwise false.
214  */
215 bool LifeCycle::InitMessageSystem() {
216   log4cxx::LoggerPtr logger = log4cxx::LoggerPtr(
217                                 log4cxx::Logger::getLogger("appMain"));
218
219   dbus_adapter_ = new hmi_message_handler::DBusMessageAdapter(
220     hmi_message_handler::HMIMessageHandlerImpl::instance());
221
222   hmi_message_handler::HMIMessageHandlerImpl::instance()->AddHMIMessageAdapter(
223     dbus_adapter_);
224   if (!dbus_adapter_->Init()) {
225     LOG4CXX_INFO(logger, "Cannot init DBus service!");
226     return false;
227   }
228
229   dbus_adapter_->SubscribeTo();
230
231   LOG4CXX_INFO(logger, "Start DBusMessageAdapter thread!");
232   dbus_adapter_thread_ = new System::Thread(
233     new System::ThreadArgImpl<hmi_message_handler::DBusMessageAdapter>(
234       *dbus_adapter_,
235       &hmi_message_handler::DBusMessageAdapter::MethodForReceiverThread,
236       NULL));
237   dbus_adapter_thread_->Start(false);
238
239   return true;
240 }
241 #endif  // DBUS_HMIADAPTER
242
243 #ifdef MQUEUE_HMIADAPTER
244 bool LifeCycle::InitMessageSystem() {
245   hmi_message_adapter_ = new hmi_message_handler::MqueueAdapter(
246     hmi_message_handler::HMIMessageHandlerImpl::instance());
247   hmi_message_handler::HMIMessageHandlerImpl::instance()->AddHMIMessageAdapter(
248     hmi_message_adapter_);
249   return true;
250 }
251 #endif  // MQUEUE_HMIADAPTER
252
253 void LifeCycle::StopComponents() {
254   hmi_handler_->set_message_observer(NULL);
255   connection_handler_->set_connection_handler_observer(NULL);
256   protocol_handler_->RemoveProtocolObserver(app_manager_);
257
258   LOG4CXX_INFO(logger_, "Destroying Application Manager.");
259   app_manager_->Stop();
260   application_manager::ApplicationManagerImpl::destroy();
261
262   LOG4CXX_INFO(logger_, "Destroying Transport Manager.");
263   transport_manager_->Stop();
264   transport_manager::TransportManagerDefault::destroy();
265
266   LOG4CXX_INFO(logger_, "Destroying Media Manager");
267   protocol_handler_->RemoveProtocolObserver(media_manager_);
268   media_manager_->SetProtocolHandler(NULL);
269   media_manager::MediaManagerImpl::destroy();
270
271   LOG4CXX_INFO(logger_, "Destroying Connection Handler.");
272   protocol_handler_->set_session_observer(NULL);
273   connection_handler::ConnectionHandlerImpl::destroy();
274
275   LOG4CXX_INFO(logger_, "Destroying Protocol Handler");
276   delete protocol_handler_;
277
278   LOG4CXX_INFO(logger_, "Destroying HMI Message Handler and MB adapter.");
279 #ifdef DBUS_HMIADAPTER
280   if (dbus_adapter_) {
281     if (hmi_handler_) {
282       hmi_handler_->RemoveHMIMessageAdapter(dbus_adapter_);
283       hmi_message_handler::HMIMessageHandlerImpl::destroy();
284     }
285     if (dbus_adapter_thread_) {
286       dbus_adapter_thread_->Stop();
287       dbus_adapter_thread_->Join();
288       delete dbus_adapter_thread_;
289     }
290     delete dbus_adapter_;
291   }
292 #endif  // DBUS_HMIADAPTER
293 #ifdef MESSAGEBROKER_HMIADAPTER
294   hmi_handler_->RemoveHMIMessageAdapter(mb_adapter_);
295   hmi_message_handler::HMIMessageHandlerImpl::destroy();
296   if (mb_adapter_) {
297     mb_adapter_->unregisterController();
298     mb_adapter_->Close();
299     delete mb_adapter_;
300   }
301   if (mb_adapter_thread_) {
302     mb_adapter_thread_->Stop();
303     mb_adapter_thread_->Join();
304     delete mb_adapter_thread_;
305   }
306
307 #endif  // MESSAGEBROKER_HMIADAPTER
308
309 #ifdef MESSAGEBROKER_HMIADAPTER
310   LOG4CXX_INFO(logger_, "Destroying Message Broker");
311   if (mb_server_thread_) {
312     mb_server_thread_->Stop();
313     mb_server_thread_->Join();
314     delete mb_server_thread_;
315   }
316   if (mb_thread_) {
317     mb_thread_->Stop();
318     mb_thread_->Join();
319     delete mb_thread_;
320   }
321   message_broker_server_->Close();
322   delete message_broker_server_;
323   message_broker_->stopMessageBroker();
324
325   networking::cleanup();
326 #endif  // MESSAGEBROKER_HMIADAPTER
327
328   delete hmi_message_adapter_;
329   hmi_message_adapter_ = NULL;
330
331   LOG4CXX_INFO(logger_, "Destroying Last State");
332   resumption::LastState::destroy();
333 }
334
335 void LifeCycle::StopComponentsOnSignal(int32_t params) {
336   utils::ResetSubscribeToTerminateSignal();
337   instance()->StopComponents();
338   utils::ForwardSignal();
339 }
340
341 }  //  namespace main_namespace