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>
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"
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))
35 static GMainLoop *mainloop = NULL;
36 static bool started = false;
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;
44 /* TODO: re-organize activation & deactivation processes */
45 void ctx::Server::initialize()
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");
52 // Start the main loop
53 _I(CYAN("Launching Context-Service"));
57 void ctx::Server::activate()
59 IF_FAIL_VOID(!started);
63 _I("Init Database Manager");
64 IF_FAIL_CATCH(DatabaseManager::__init());
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");
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");
78 _I("Init Policy Manager");
79 __policyMgr = new(std::nothrow) ctx::PolicyManager(__contextMgr);
80 IF_FAIL_CATCH_TAG(__policyMgr, _E, "Memory allocation failed");
83 _I(CYAN("Context-Service Launched"));
87 _E(RED("Launching Failed"));
93 void ctx::Server::release()
95 _I(CYAN("Terminating Context-Service"));
98 _I("Release Policy Manager");
101 _I("Release Context Trigger");
102 if (__contextTrigger)
103 __contextTrigger->release();
105 _I("Release Context Manager");
107 __contextMgr->release();
109 _I("Release Database Manager");
110 DatabaseManager::__release();
112 delete __contextTrigger;
117 static gboolean __postponeRequestAssignment(gpointer data)
119 ctx::Server::sendRequest(static_cast<ctx::RequestInfo*>(data));
123 void ctx::Server::sendRequest(ctx::RequestInfo* request)
126 _W("Service not ready...");
127 g_idle_add(__postponeRequestAssignment, request);
131 if (__contextTrigger->assignRequest(request))
134 __contextMgr->assignRequest(request);
137 static void __signalHandler(int signo)
139 _I("SIGNAL %d received", signo);
141 // Stop the main loop
145 int main(int argc, char* argv[])
147 static struct sigaction signalAction;
148 signalAction.sa_handler = __signalHandler;
149 sigemptyset(&signalAction.sa_mask);
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);
157 #if !defined(GLIB_VERSION_2_36)
162 mainloop = g_main_loop_new(NULL, FALSE);
164 ctx::Server::initialize();
165 ctx::Server::release();
167 g_main_loop_unref(mainloop);