modify msg-service to support dpm feature
[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 "MsgDebug.h"
22 #include "MsgException.h"
23 #include "MsgContact.h"
24 #include "MsgMemory.h"
25 #include "MsgGconfWrapper.h"
26 #include "MsgSensorWrapper.h"
27 #include "MsgPluginManager.h"
28 #include "MsgSettingHandler.h"
29 #include "MsgStorageHandler.h"
30 #include "MsgSubmitHandler.h"
31 #include "MsgDeliverHandler.h"
32 #include "MsgTransManager.h"
33 #include "MsgStorageTypes.h"
34 #include "MsgCmdHandler.h"
35 #include "MsgUtilFile.h"
36 #include "MsgUtilFunction.h"
37 #include "MsgUtilStorage.h"
38 #include "MsgNotificationWrapper.h"
39
40 #include <errno.h>
41 #include <glib.h>
42 #include <sys/stat.h>
43 #include <wait.h>
44
45 static GMainLoop* mainloop = NULL;
46
47 /*==================================================================================================
48                                      FUNCTION IMPLEMENTATION
49 ==================================================================================================*/
50
51
52 void* InitMsgServer(void*)
53 {
54         msg_error_t err = MSG_SUCCESS;
55         MSG_DEBUG("Start InitMsgServer.");
56
57         MsgInitCallStatusManager();
58
59         msg_init_dpm_policy();
60
61         try {
62                 /* storage handler initialize */
63                 err = MsgStoInitDB(false);
64                 if (err != MSG_SUCCESS) {
65                         MSG_ERR("FAIL TO INITIALIZE STORAGE HANDLER [%d]", err);
66                 }
67
68                 MsgInitSensor();
69                 MsgInitMsgMgr();
70
71                 /* plugin manager initialize */
72                 MsgPluginManager::instance()->initialize();
73         } catch (MsgException& e) {
74                 MSG_FATAL("%s", e.what());
75         } catch (exception& e) {
76                 MSG_FATAL("%s", e.what());
77         }
78
79         MsgStoDisconnectDB();
80
81         MsgReleaseMemory();
82         MSG_DEBUG("End InitMsgServer.");
83
84         return (void*)0;
85 }
86
87
88 void* StartMsgServer(void*)
89 {
90         try {
91                 if (MsgTransactionManager::instance()->initCynara() == false) {
92                         MSG_ERR("Cynara initialize failed. It will try again when API is called.");
93                 }
94
95                 MsgTransactionManager::instance()->run();
96         } catch (MsgException& e) {
97                 MSG_FATAL("%s", e.what());
98         } catch (exception& e) {
99                 MSG_FATAL("%s", e.what());
100         }
101
102         MsgTransactionManager::instance()->finishCynara();
103
104         if (g_main_loop_is_running(mainloop))
105                 g_main_loop_quit(mainloop);
106
107         return (void*)0;
108 }
109
110
111 int main(void)
112 {
113 #if !GLIB_CHECK_VERSION(2, 31, 0)
114         g_thread_init(NULL);
115 #endif
116         /* set to ignore child process terminated signal */
117         signal(SIGCHLD, SIG_IGN);
118
119         MSG_INFO("===========START MESSAGING FRAMEWORK==========");
120
121 #if !GLIB_CHECK_VERSION(2, 36, 0)
122         g_type_init();
123 #endif
124         /* Reset message server ready flag */
125         if(MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, false) != MSG_SUCCESS)
126                 MSG_DEBUG("MsgSettingSetBool FAIL: VCONFKEY_MSG_SERVER_READY");
127
128         /* init server */
129         InitMsgServer(NULL);
130
131         pthread_t startThreadId;
132
133         /* start transaction manager */
134         if (pthread_create(&startThreadId, NULL, StartMsgServer, NULL) != 0) {
135                 MSG_DEBUG("StartMsgServer not invoked: %s", g_strerror(errno));
136                 return -1;
137         }
138
139
140         mainloop = g_main_loop_new(NULL, FALSE);
141
142         if (mainloop != NULL) {
143                 MSG_DEBUG("Start Messaging Framework!!!");
144
145                 /* Run GMainLoop */
146                 g_main_loop_run(mainloop);
147         } else {
148                 MSG_DEBUG("Fail to start Messaging Framework!!!");
149         }
150
151         /* Disconnect to DB */
152         MsgStoDisconnectDB();
153
154         MsgDeInitCallStatusManager();
155
156         msg_deinit_dpm_policy();
157
158         return 0;
159 }
160