tizen 2.4 release
[framework/context/context-service.git] / src / 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_internal.h>
23 #include "dbus_server_impl.h"
24 #include "db_mgr_impl.h"
25 #include "timer_mgr_impl.h"
26 #include "context_mgr_impl.h"
27 #include "context_trigger/trigger.h"
28 #include "server.h"
29
30 #ifdef _USE_ECORE_MAIN_LOOP_
31 #include <Ecore.h>
32 #else
33 static GMainLoop *mainloop = NULL;
34 #endif
35
36 static bool started = false;
37
38 static ctx::context_manager_impl *context_mgr = NULL;
39 static ctx::timer_manager_impl *timer_mgr = NULL;
40 static ctx::db_manager_impl *database_mgr = NULL;
41 static ctx::dbus_server_impl *dbus_handle = NULL;
42 static ctx::context_trigger *trigger = NULL;
43
44 void ctx::server::initialize()
45 {
46         _I("Init MainLoop");
47 #ifdef _USE_ECORE_MAIN_LOOP_
48         ecore_init();
49         ecore_main_loop_glib_integrate();
50 #else
51         mainloop = g_main_loop_new(NULL, FALSE);
52 #endif
53
54         _I("Init Dbus Connection");
55         dbus_handle = new(std::nothrow) ctx::dbus_server_impl();
56         IF_FAIL_VOID_TAG(dbus_handle, _E, "Memory allocation failed");
57
58         dbus_server::set_instance(dbus_handle);
59         IF_FAIL_VOID_TAG(dbus_handle->init(), _E, "Initialization Failed");
60
61         // Start the main loop
62         _I(CYAN("Launching Context-Service"));
63 #ifdef _USE_ECORE_MAIN_LOOP_
64         ecore_main_loop_begin();
65 #else
66         g_main_loop_run(mainloop);
67 #endif
68 }
69
70 void ctx::server::activate()
71 {
72         IF_FAIL_VOID(!started);
73
74         bool result = false;
75
76         _I("Init Timer Manager");
77         timer_mgr = new(std::nothrow) ctx::timer_manager_impl();
78         IF_FAIL_CATCH_TAG(timer_mgr, _E, "Memory allocation failed");
79         timer_manager::set_instance(timer_mgr);
80         result = timer_mgr->init();
81         IF_FAIL_CATCH_TAG(result, _E, "Initialization Failed");
82
83         _I("Init Database Manager");
84         database_mgr = new(std::nothrow) ctx::db_manager_impl();
85         IF_FAIL_CATCH_TAG(database_mgr, _E, "Memory allocation failed");
86         db_manager::set_instance(database_mgr);
87         result = database_mgr->init();
88         IF_FAIL_CATCH_TAG(result, _E, "Initialization Failed");
89
90         _I("Init Context Manager");
91         context_mgr = new(std::nothrow) ctx::context_manager_impl();
92         IF_FAIL_CATCH_TAG(context_mgr, _E, "Memory allocation failed");
93         context_manager::set_instance(context_mgr);
94         result = context_mgr->init();
95         IF_FAIL_CATCH_TAG(result, _E, "Initialization Failed");
96
97         _I("Init Context Trigger");
98         trigger = new(std::nothrow) ctx::context_trigger();
99         IF_FAIL_CATCH_TAG(trigger, _E, "Memory allocation failed");
100         result = trigger->init(context_mgr);
101         IF_FAIL_CATCH_TAG(result, _E, "Initialization Failed");
102
103         started = true;
104         _I(CYAN("Context-Service Launched"));
105         return;
106
107 CATCH:
108         _E(RED("Launching Failed"));
109
110         // Stop the main loop
111 #ifdef _USE_ECORE_MAIN_LOOP_
112         ecore_main_loop_quit();
113 #else
114         g_main_loop_quit(mainloop);
115 #endif
116 }
117
118 void ctx::server::release()
119 {
120         _I(CYAN("Terminating Context-Service"));
121         _I("Release Context Trigger");
122         if (trigger)
123                 trigger->release();
124
125         _I("Release Analyzer Manager");
126         if (context_mgr)
127                 context_mgr->release();
128
129         _I("Release Dbus Connection");
130         if (dbus_handle)
131                 dbus_handle->release();
132
133         _I("Close the Database");
134         if (database_mgr)
135                 database_mgr->release();
136
137         _I("Release Timer Manager");
138         if (timer_mgr)
139                 timer_mgr->release();
140
141 #ifdef _USE_ECORE_MAIN_LOOP_
142         ecore_shutdown();
143 #else
144         g_main_loop_unref(mainloop);
145 #endif
146
147         delete trigger;
148         delete context_mgr;
149         delete dbus_handle;
150         delete database_mgr;
151         delete timer_mgr;
152 }
153
154 void ctx::server::send_request(ctx::request_info* request)
155 {
156         if (!trigger->assign_request(request)) {
157                 context_mgr->assign_request(request);
158         }
159 }
160
161 static void signal_handler(int signo)
162 {
163         _I("SIGNAL %d received", signo);
164
165         // Stop the main loop
166 #ifdef _USE_ECORE_MAIN_LOOP_
167         ecore_main_loop_quit();
168 #else
169         g_main_loop_quit(mainloop);
170 #endif
171 }
172
173 int main(int argc, char* argv[])
174 {
175         static struct sigaction signal_action;
176         signal_action.sa_handler = signal_handler;
177         sigemptyset(&signal_action.sa_mask);
178
179         sigaction(SIGINT, &signal_action, NULL);
180         sigaction(SIGHUP, &signal_action, NULL);
181         sigaction(SIGTERM, &signal_action, NULL);
182         sigaction(SIGQUIT, &signal_action, NULL);
183         sigaction(SIGABRT, &signal_action, NULL);
184
185 #if !defined(GLIB_VERSION_2_36)
186         g_type_init();
187 #endif
188
189         ctx::server::initialize();
190         ctx::server::release();
191
192         return EXIT_SUCCESS;
193 }