namespace {
class Server {
public:
- Server();
- ~Server();
- bool start();
- void stop();
+ static bool start();
+ static void stop();
private:
- GMainLoop* __mainloop;
+ Server() {}
+ static GMainLoop* __mainLoop;
};
}
-::Server::Server() :
- __mainloop(NULL)
+static gboolean __stopService(gpointer data)
{
- __mainloop = g_main_loop_new(NULL, FALSE);
- IF_FAIL_VOID_TAG(__mainloop, _E, "Memory allocation failed");
+ _I("Unloading services");
+ ServiceLoader::unload();
+ ::Server::stop();
+ return G_SOURCE_REMOVE;
}
-::Server::~Server()
-{
- if (__mainloop)
- g_main_loop_unref(__mainloop);
-}
+GMainLoop* ::Server::__mainLoop = NULL;
bool ::Server::start()
{
+ __mainLoop = g_main_loop_new(NULL, FALSE);
+ IF_FAIL_RETURN_TAG(__mainLoop, false, _E, "Memory allocation failed");
+
_I(CYAN("Starting..."));
+ g_main_loop_run(__mainLoop);
+
+ g_main_loop_unref(__mainLoop);
- IF_FAIL_RETURN(__mainloop, false);
- g_main_loop_run(__mainloop);
return true;
}
void ::Server::stop()
{
_I(PURPLE("Terminating..."));
-
- if (g_main_loop_is_running(__mainloop))
- g_main_loop_quit(__mainloop);
+ g_main_loop_quit(__mainLoop);
}
-static ::Server __server;
static AlarmInitializer __alarmInit;
static void __signalHandler(int signum)
{
_I(YELLOW("SIGNAL-%d: '%s'"), signum, strsignal(signum));
- _I("Unloading services");
- ServiceLoader::unload();
- __server.stop();
+ static bool terminated = false;
+ if (!terminated) {
+ g_idle_add(__stopService, NULL);
+ terminated = true;
+ }
}
static void __busAcquired(GDBusConnection* conn)
}
_E(RED("No service loaded."));
- // FIXME: __server.stop();
}
static void __busLost(GDBusConnection* conn)
{
- _I("Unloading services");
- ServiceLoader::unload();
- __server.stop();
+ __stopService(NULL);
}
int main(int argc, char* argv[])
DBusConnector dbusConnector(__busAcquired, __busLost);
- __server.start();
+ ::Server::start();
return EXIT_SUCCESS;
}