2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <glib-object.h>
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"
30 #ifdef _USE_ECORE_MAIN_LOOP_
33 static GMainLoop *mainloop = NULL;
36 static bool started = false;
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;
44 void ctx::server::initialize()
47 #ifdef _USE_ECORE_MAIN_LOOP_
49 ecore_main_loop_glib_integrate();
51 mainloop = g_main_loop_new(NULL, FALSE);
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");
58 dbus_server::set_instance(dbus_handle);
59 IF_FAIL_VOID_TAG(dbus_handle->init(), _E, "Initialization Failed");
61 // Start the main loop
62 _I(CYAN("Launching Context-Service"));
63 #ifdef _USE_ECORE_MAIN_LOOP_
64 ecore_main_loop_begin();
66 g_main_loop_run(mainloop);
70 void ctx::server::activate()
72 IF_FAIL_VOID(!started);
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");
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");
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");
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");
104 _I(CYAN("Context-Service Launched"));
108 _E(RED("Launching Failed"));
110 // Stop the main loop
111 #ifdef _USE_ECORE_MAIN_LOOP_
112 ecore_main_loop_quit();
114 g_main_loop_quit(mainloop);
118 void ctx::server::release()
120 _I(CYAN("Terminating Context-Service"));
121 _I("Release Context Trigger");
125 _I("Release Analyzer Manager");
127 context_mgr->release();
129 _I("Release Dbus Connection");
131 dbus_handle->release();
133 _I("Close the Database");
135 database_mgr->release();
137 _I("Release Timer Manager");
139 timer_mgr->release();
141 #ifdef _USE_ECORE_MAIN_LOOP_
144 g_main_loop_unref(mainloop);
154 void ctx::server::send_request(ctx::request_info* request)
156 if (!trigger->assign_request(request)) {
157 context_mgr->assign_request(request);
161 static void signal_handler(int signo)
163 _I("SIGNAL %d received", signo);
165 // Stop the main loop
166 #ifdef _USE_ECORE_MAIN_LOOP_
167 ecore_main_loop_quit();
169 g_main_loop_quit(mainloop);
173 int main(int argc, char* argv[])
175 static struct sigaction signal_action;
176 signal_action.sa_handler = signal_handler;
177 sigemptyset(&signal_action.sa_mask);
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);
185 #if !defined(GLIB_VERSION_2_36)
189 ctx::server::initialize();
190 ctx::server::release();