Store the dbus connection while launching the service
[platform/core/context/context-service.git] / src / agent / legacy / Server.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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 #include <stdlib.h>
18 #include <new>
19 #include <glib.h>
20 #include <glib-object.h>
21
22 #include <Types.h>
23 #include <TimerManager.h>
24 #include <DatabaseManager.h>
25 #include "DBusServer.h"
26 #include "ContextManager.h"
27 #include "policy/PolicyManager.h"
28 #include "trigger/Trigger.h"
29 #include "Server.h"
30
31 #define RUN(L) g_main_loop_run((L))
32 #define QUIT(L) if (g_main_loop_is_running((L)) == TRUE) g_main_loop_quit((L))
33
34
35 static GMainLoop *mainloop = NULL;
36 static bool started = false;
37
38 static ctx::ContextManager *__contextMgr = NULL;
39 static ctx::DBusServer *__dbusHandle = NULL;
40 static ctx::PolicyManager *__policyMgr = NULL;
41 static ctx::trigger::Trigger *__contextTrigger = NULL;
42 static ctx::TimerManager __timerManager;
43
44 /* TODO: re-organize activation & deactivation processes */
45 void ctx::Server::initialize()
46 {
47         _I("Init Dbus Connection");
48         __dbusHandle = new(std::nothrow) ctx::DBusServer();
49         IF_FAIL_VOID_TAG(__dbusHandle, _E, "Memory allocation failed");
50         IF_FAIL_VOID_TAG(__dbusHandle->__init(), _E, "Initialization Failed");
51
52         // Start the main loop
53         _I(CYAN("Launching Context-Service"));
54         RUN(mainloop);
55 }
56
57 void ctx::Server::activate()
58 {
59         IF_FAIL_VOID(!started);
60
61         bool result = false;
62
63         _I("Init Database Manager");
64         IF_FAIL_CATCH(DatabaseManager::__init());
65
66         _I("Init Context Manager");
67         __contextMgr = new(std::nothrow) ctx::ContextManager();
68         IF_FAIL_CATCH_TAG(__contextMgr, _E, "Memory allocation failed");
69         result = __contextMgr->init();
70         IF_FAIL_CATCH_TAG(result, _E, "Initialization Failed");
71
72         _I("Init Context Trigger");
73         __contextTrigger = new(std::nothrow) ctx::trigger::Trigger();
74         IF_FAIL_CATCH_TAG(__contextTrigger, _E, "Memory allocation failed");
75         result = __contextTrigger->init(__contextMgr);
76         IF_FAIL_CATCH_TAG(result, _E, "Initialization Failed");
77
78         _I("Init Policy Manager");
79         __policyMgr = new(std::nothrow) ctx::PolicyManager(__contextMgr);
80         IF_FAIL_CATCH_TAG(__policyMgr, _E, "Memory allocation failed");
81
82         started = true;
83         _I(CYAN("Context-Service Launched"));
84         return;
85
86 CATCH:
87         _E(RED("Launching Failed"));
88
89         // Stop the main loop
90         QUIT(mainloop);
91 }
92
93 void ctx::Server::release()
94 {
95         _I(CYAN("Terminating Context-Service"));
96         started = false;
97
98         _I("Release Policy Manager");
99         delete __policyMgr;
100
101         _I("Release Context Trigger");
102         if (__contextTrigger)
103                 __contextTrigger->release();
104
105         _I("Release Context Manager");
106         if (__contextMgr)
107                 __contextMgr->release();
108
109         _I("Release Database Manager");
110         DatabaseManager::__release();
111
112         delete __contextTrigger;
113         delete __contextMgr;
114         delete __dbusHandle;
115 }
116
117 static gboolean __postponeRequestAssignment(gpointer data)
118 {
119         ctx::Server::sendRequest(static_cast<ctx::RequestInfo*>(data));
120         return FALSE;
121 }
122
123 void ctx::Server::sendRequest(ctx::RequestInfo* request)
124 {
125         if (!started) {
126                 _W("Service not ready...");
127                 g_idle_add(__postponeRequestAssignment, request);
128                 return;
129         }
130
131         if (__contextTrigger->assignRequest(request))
132                 return;
133
134         __contextMgr->assignRequest(request);
135 }
136
137 static void __signalHandler(int signo)
138 {
139         _I("SIGNAL %d received", signo);
140
141         // Stop the main loop
142         QUIT(mainloop);
143 }
144
145 int mainLegacy()
146 {
147         static struct sigaction signalAction;
148         signalAction.sa_handler = __signalHandler;
149         sigemptyset(&signalAction.sa_mask);
150
151         sigaction(SIGINT, &signalAction, NULL);
152         sigaction(SIGHUP, &signalAction, NULL);
153         sigaction(SIGTERM, &signalAction, NULL);
154         sigaction(SIGQUIT, &signalAction, NULL);
155         sigaction(SIGABRT, &signalAction, NULL);
156
157 #if !defined(GLIB_VERSION_2_36)
158         g_type_init();
159 #endif
160
161         _I("Init MainLoop");
162         mainloop = g_main_loop_new(NULL, FALSE);
163
164         ctx::Server::initialize();
165         ctx::Server::release();
166
167         g_main_loop_unref(mainloop);
168
169         return EXIT_SUCCESS;
170 }