Rearrange launch order to safely control the case that the DB file is not accessible 26/93726/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 25 Oct 2016 11:44:24 +0000 (20:44 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 25 Oct 2016 11:44:24 +0000 (20:44 +0900)
Change-Id: I7832bcf2455c2129cd1a06925bf730eae8f1759b
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/Server.cpp

index 8c2afe9..34d4fe2 100644 (file)
 #include <glib-object.h>
 
 #include <Types.h>
+#include <DatabaseManager.h>
 #include "DBusServer.h"
 #include "ContextManager.h"
 #include "policy/PolicyManager.h"
 #include "trigger/Trigger.h"
 #include "Server.h"
 
+#define RUN(L) g_main_loop_run((L))
+#define QUIT(L) if (g_main_loop_is_running((L)) == TRUE) g_main_loop_quit((L))
+
 
 static GMainLoop *mainloop = NULL;
 static bool started = false;
@@ -38,9 +42,6 @@ static ctx::trigger::Trigger *__contextTrigger = NULL;
 /* TODO: re-organize activation & deactivation processes */
 void ctx::Server::initialize()
 {
-       _I("Init MainLoop");
-       mainloop = g_main_loop_new(NULL, FALSE);
-
        _I("Init Dbus Connection");
        __dbusHandle = new(std::nothrow) ctx::DBusServer();
        IF_FAIL_VOID_TAG(__dbusHandle, _E, "Memory allocation failed");
@@ -48,7 +49,7 @@ void ctx::Server::initialize()
 
        // Start the main loop
        _I(CYAN("Launching Context-Service"));
-       g_main_loop_run(mainloop);
+       RUN(mainloop);
 }
 
 void ctx::Server::activate()
@@ -57,6 +58,9 @@ void ctx::Server::activate()
 
        bool result = false;
 
+       _I("Init Database Manager");
+       IF_FAIL_CATCH(DatabaseManager::__init());
+
        _I("Init Context Manager");
        __contextMgr = new(std::nothrow) ctx::ContextManager();
        IF_FAIL_CATCH_TAG(__contextMgr, _E, "Memory allocation failed");
@@ -81,12 +85,13 @@ CATCH:
        _E(RED("Launching Failed"));
 
        // Stop the main loop
-       g_main_loop_quit(mainloop);
+       QUIT(mainloop);
 }
 
 void ctx::Server::release()
 {
        _I(CYAN("Terminating Context-Service"));
+       started = false;
 
        _I("Release Policy Manager");
        delete __policyMgr;
@@ -99,11 +104,8 @@ void ctx::Server::release()
        if (__contextMgr)
                __contextMgr->release();
 
-       _I("Release Dbus Connection");
-       if (__dbusHandle)
-               __dbusHandle->__release();
-
-       g_main_loop_unref(mainloop);
+       _I("Release Database Manager");
+       DatabaseManager::__release();
 
        delete __contextTrigger;
        delete __contextMgr;
@@ -135,7 +137,7 @@ static void __signalHandler(int signo)
        _I("SIGNAL %d received", signo);
 
        // Stop the main loop
-       g_main_loop_quit(mainloop);
+       QUIT(mainloop);
 }
 
 int main(int argc, char* argv[])
@@ -154,8 +156,13 @@ int main(int argc, char* argv[])
        g_type_init();
 #endif
 
+       _I("Init MainLoop");
+       mainloop = g_main_loop_new(NULL, FALSE);
+
        ctx::Server::initialize();
        ctx::Server::release();
 
+       g_main_loop_unref(mainloop);
+
        return EXIT_SUCCESS;
 }