[P161228-01293] change on-boot attribute to true from false
[platform/core/messaging/msg-service.git] / framework / main.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 /*==================================================================================================
18                                          INCLUDE FILES
19 ==================================================================================================*/
20 #include "MsgCallStatusManager.h"
21 #include "MsgDPMUtils.h"
22 #include "MsgDebug.h"
23 #include "MsgException.h"
24 #include "MsgContact.h"
25 #include "MsgMemory.h"
26 #include "MsgGconfWrapper.h"
27 #include "MsgSensorWrapper.h"
28 #include "MsgPluginManager.h"
29 #include "MsgSettingHandler.h"
30 #include "MsgStorageHandler.h"
31 #include "MsgSubmitHandler.h"
32 #include "MsgDeliverHandler.h"
33 #include "MsgTransManager.h"
34 #include "MsgStorageTypes.h"
35 #include "MsgCmdHandler.h"
36 #include "MsgUtilFile.h"
37 #include "MsgUtilStorage.h"
38
39 #include <errno.h>
40 #include <glib.h>
41 #include <sys/stat.h>
42 #include <wait.h>
43
44 static GMainLoop* mainloop = NULL;
45
46 /*==================================================================================================
47                                      FUNCTION IMPLEMENTATION
48 ==================================================================================================*/
49
50
51 void* InitMsgServer(void*)
52 {
53         msg_error_t err = MSG_SUCCESS;
54         MSG_DEBUG("Start InitMsgServer.");
55
56         MsgInitCallStatusManager();
57
58         msg_init_dpm_policy();
59
60         try {
61                 /* storage handler initialize */
62                 err = MsgStoInitDB(false);
63                 if (err != MSG_SUCCESS) {
64                         MSG_ERR("FAIL TO INITIALIZE STORAGE HANDLER [%d]", err);
65                 }
66
67                 MsgInitSensor();
68
69                 /* plugin manager initialize */
70                 MsgPluginManager::instance()->initialize();
71         } catch (MsgException& e) {
72                 MSG_FATAL("%s", e.what());
73         } catch (exception& e) {
74                 MSG_FATAL("%s", e.what());
75         }
76
77         MsgStoDisconnectDB();
78
79         MsgReleaseMemory();
80         MSG_DEBUG("End InitMsgServer.");
81
82         return (void*)0;
83 }
84
85
86 void* StartMsgServer(void*)
87 {
88         try {
89                 if (MsgTransactionManager::instance()->initCynara() == false) {
90                         MSG_ERR("Cynara initialize failed. It will try again when API is called.");
91                 }
92
93                 MsgTransactionManager::instance()->run();
94         } catch (MsgException& e) {
95                 MSG_FATAL("%s", e.what());
96         } catch (exception& e) {
97                 MSG_FATAL("%s", e.what());
98         }
99
100         MsgTransactionManager::instance()->finishCynara();
101
102         if (g_main_loop_is_running(mainloop))
103                 g_main_loop_quit(mainloop);
104
105         return (void*)0;
106 }
107
108
109 int main(void)
110 {
111 #if !GLIB_CHECK_VERSION(2, 31, 0)
112         g_thread_init(NULL);
113 #endif
114         /* set to ignore child process terminated signal */
115         signal(SIGCHLD, SIG_IGN);
116
117         MSG_INFO("===========START MESSAGING FRAMEWORK==========");
118
119 #if !GLIB_CHECK_VERSION(2, 36, 0)
120         g_type_init();
121 #endif
122         /* Reset message server ready flag */
123         if(MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, false) != MSG_SUCCESS)
124                 MSG_DEBUG("MsgSettingSetBool FAIL: VCONFKEY_MSG_SERVER_READY");
125
126         /* init server */
127         InitMsgServer(NULL);
128
129         pthread_t startThreadId;
130
131         /* start transaction manager */
132         if (pthread_create(&startThreadId, NULL, StartMsgServer, NULL) != 0) {
133                 MSG_DEBUG("StartMsgServer not invoked: %s", g_strerror(errno));
134                 return -1;
135         }
136
137
138         mainloop = g_main_loop_new(NULL, FALSE);
139
140         if (mainloop != NULL) {
141                 MSG_DEBUG("Start Messaging Framework!!!");
142
143                 /* Run GMainLoop */
144                 g_main_loop_run(mainloop);
145         } else {
146                 MSG_DEBUG("Fail to start Messaging Framework!!!");
147         }
148
149         /* Disconnect to DB */
150         MsgStoDisconnectDB();
151
152         MsgDeInitCallStatusManager();
153
154         msg_deinit_dpm_policy();
155
156         return 0;
157 }
158