3 * \brief Signal (i.e. SIGINT) handling.
4 * Copyright (c) 2013, Ford Motor Company
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
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
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.
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.
35 #include "./life_cycle.h"
36 #include "utils/signals.h"
37 #include "config_profile/profile.h"
38 #include "resumption/last_state.h"
40 using threads::Thread;
42 namespace main_namespace {
44 log4cxx::LoggerPtr LifeCycle::logger_ = log4cxx::LoggerPtr(
45 log4cxx::Logger::getLogger("appMain"));
50 void NameMessageBrokerThread(const System::Thread& thread,
51 const std::string& name) {
52 Thread::SetNameForId(Thread::Id(thread.GetId()), name);
57 LifeCycle::LifeCycle()
58 : transport_manager_(NULL)
59 , protocol_handler_(NULL)
60 , connection_handler_(NULL)
63 , hmi_message_adapter_(NULL)
64 , media_manager_(NULL)
65 #ifdef DBUS_HMIADAPTER
67 , dbus_adapter_thread_(NULL)
68 #endif // DBUS_HMIADAPTER
69 #ifdef MESSAGEBROKER_HMIADAPTER
71 , message_broker_(NULL)
72 , message_broker_server_(NULL)
74 , mb_server_thread_(NULL)
75 , mb_adapter_thread_(NULL)
76 #endif // MESSAGEBROKER_HMIADAPTER
80 bool LifeCycle::StartComponents() {
81 LOG4CXX_INFO(logger_, "LifeCycle::StartComponents()");
83 transport_manager::TransportManagerDefault::instance();
84 DCHECK(transport_manager_ != NULL);
87 new protocol_handler::ProtocolHandlerImpl(transport_manager_);
88 DCHECK(protocol_handler_ != NULL);
91 connection_handler::ConnectionHandlerImpl::instance();
92 DCHECK(connection_handler_ != NULL);
95 application_manager::ApplicationManagerImpl::instance();
96 DCHECK(app_manager_ != NULL);
99 hmi_message_handler::HMIMessageHandlerImpl::instance();
100 DCHECK(hmi_handler_ != NULL)
102 transport_manager_->AddEventListener(protocol_handler_);
103 transport_manager_->AddEventListener(connection_handler_);
105 hmi_handler_->set_message_observer(app_manager_);
107 media_manager_ = media_manager::MediaManagerImpl::instance();
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_);
114 connection_handler_->set_transport_manager(transport_manager_);
115 connection_handler_->set_connection_handler_observer(app_manager_);
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();
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_);
128 #ifdef MESSAGEBROKER_HMIADAPTER
129 bool LifeCycle::InitMessageSystem() {
131 NsMessageBroker::CMessageBroker::getInstance();
132 if (!message_broker_) {
133 LOG4CXX_INFO(logger_, " Wrong pMessageBroker pointer!");
137 message_broker_server_ =
138 new NsMessageBroker::TcpServer(
139 profile::Profile::instance()->server_address(),
140 profile::Profile::instance()->server_port(),
142 if (!message_broker_server_) {
143 LOG4CXX_INFO(logger_, " Wrong pJSONRPC20Server pointer!");
146 message_broker_->startMessageBroker(message_broker_server_);
147 if (!networking::init()) {
148 LOG4CXX_INFO(logger_, " Networking initialization failed!");
152 if (!message_broker_server_->Bind()) {
153 LOG4CXX_FATAL(logger_, "Bind failed!");
156 LOG4CXX_INFO(logger_, "Bind successful!");
159 if (!message_broker_server_->Listen()) {
160 LOG4CXX_FATAL(logger_, "Listen failed!");
163 LOG4CXX_INFO(logger_, " Listen successful!");
167 new hmi_message_handler::MessageBrokerAdapter(
168 hmi_message_handler::HMIMessageHandlerImpl::instance(),
169 profile::Profile::instance()->server_address(),
170 profile::Profile::instance()->server_port());
172 hmi_message_handler::HMIMessageHandlerImpl::instance()->AddHMIMessageAdapter(
174 if (!mb_adapter_->Connect()) {
175 LOG4CXX_INFO(logger_, "Cannot connect to remote peer!");
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,
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");
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,
194 mb_server_thread_->Start(false);
195 NameMessageBrokerThread(*mb_server_thread_, "MessageBrokerTCPServerThread");
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>(
201 &hmi_message_handler::MessageBrokerAdapter::SubscribeAndBeginReceiverThread,
203 mb_adapter_thread_->Start(false);
204 NameMessageBrokerThread(*mb_adapter_thread_, "MessageBrokerAdapterThread");
208 #endif // MESSAGEBROKER_HMIADAPTER
210 #ifdef DBUS_HMIADAPTER
212 * Initialize DBus component
213 * @return true if success otherwise false.
215 bool LifeCycle::InitMessageSystem() {
216 log4cxx::LoggerPtr logger = log4cxx::LoggerPtr(
217 log4cxx::Logger::getLogger("appMain"));
219 dbus_adapter_ = new hmi_message_handler::DBusMessageAdapter(
220 hmi_message_handler::HMIMessageHandlerImpl::instance());
222 hmi_message_handler::HMIMessageHandlerImpl::instance()->AddHMIMessageAdapter(
224 if (!dbus_adapter_->Init()) {
225 LOG4CXX_INFO(logger, "Cannot init DBus service!");
229 dbus_adapter_->SubscribeTo();
231 LOG4CXX_INFO(logger, "Start DBusMessageAdapter thread!");
232 dbus_adapter_thread_ = new System::Thread(
233 new System::ThreadArgImpl<hmi_message_handler::DBusMessageAdapter>(
235 &hmi_message_handler::DBusMessageAdapter::MethodForReceiverThread,
237 dbus_adapter_thread_->Start(false);
241 #endif // DBUS_HMIADAPTER
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_);
251 #endif // MQUEUE_HMIADAPTER
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_);
258 LOG4CXX_INFO(logger_, "Destroying Application Manager.");
259 app_manager_->Stop();
260 application_manager::ApplicationManagerImpl::destroy();
262 LOG4CXX_INFO(logger_, "Destroying Transport Manager.");
263 transport_manager_->Stop();
264 transport_manager::TransportManagerDefault::destroy();
266 LOG4CXX_INFO(logger_, "Destroying Media Manager");
267 protocol_handler_->RemoveProtocolObserver(media_manager_);
268 media_manager_->SetProtocolHandler(NULL);
269 media_manager::MediaManagerImpl::destroy();
271 LOG4CXX_INFO(logger_, "Destroying Connection Handler.");
272 protocol_handler_->set_session_observer(NULL);
273 connection_handler::ConnectionHandlerImpl::destroy();
275 LOG4CXX_INFO(logger_, "Destroying Protocol Handler");
276 delete protocol_handler_;
278 LOG4CXX_INFO(logger_, "Destroying HMI Message Handler and MB adapter.");
279 #ifdef DBUS_HMIADAPTER
282 hmi_handler_->RemoveHMIMessageAdapter(dbus_adapter_);
283 hmi_message_handler::HMIMessageHandlerImpl::destroy();
285 if (dbus_adapter_thread_) {
286 dbus_adapter_thread_->Stop();
287 dbus_adapter_thread_->Join();
288 delete dbus_adapter_thread_;
290 delete dbus_adapter_;
292 #endif // DBUS_HMIADAPTER
293 #ifdef MESSAGEBROKER_HMIADAPTER
294 hmi_handler_->RemoveHMIMessageAdapter(mb_adapter_);
295 hmi_message_handler::HMIMessageHandlerImpl::destroy();
297 mb_adapter_->unregisterController();
298 mb_adapter_->Close();
301 if (mb_adapter_thread_) {
302 mb_adapter_thread_->Stop();
303 mb_adapter_thread_->Join();
304 delete mb_adapter_thread_;
307 #endif // MESSAGEBROKER_HMIADAPTER
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_;
321 message_broker_server_->Close();
322 delete message_broker_server_;
323 message_broker_->stopMessageBroker();
325 networking::cleanup();
326 #endif // MESSAGEBROKER_HMIADAPTER
328 delete hmi_message_adapter_;
329 hmi_message_adapter_ = NULL;
331 LOG4CXX_INFO(logger_, "Destroying Last State");
332 resumption::LastState::destroy();
335 void LifeCycle::StopComponentsOnSignal(int32_t params) {
336 utils::ResetSubscribeToTerminateSignal();
337 instance()->StopComponents();
338 utils::ForwardSignal();
341 } // namespace main_namespace