Commit 7f64aa83 authored by Slava Monich's avatar Slava Monich

[mms-engine] Close the connection when terminated by signal

parent 38d56668
/*
* Copyright (C) 2013-2014 Jolla Ltd.
* Copyright (C) 2013-2015 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
......@@ -50,14 +51,10 @@ gboolean
mms_app_signal(
gpointer arg)
{
GMainLoop* loop = arg;
MMSEngine* engine = arg;
MMS_INFO("Caught signal, shutting down...");
if (loop) {
g_idle_add((GSourceFunc)g_main_loop_quit, loop);
} else {
exit(0);
}
return FALSE;
mms_engine_stop(engine);
return TRUE;
}
/* D-Bus event handlers */
......@@ -299,15 +296,14 @@ int main(int argc, char* argv[])
engine = mms_engine_new(&opt.config, &opt.settings, opt.flags,
mms_app_log_modules, G_N_ELEMENTS(mms_app_log_modules));
if (engine) {
guint name_id;
/* Setup main loop */
GMainLoop* loop = g_main_loop_new(NULL, FALSE);
g_unix_signal_add(SIGTERM, mms_app_signal, loop);
g_unix_signal_add(SIGINT, mms_app_signal, loop);
guint sigtrm = g_unix_signal_add(SIGTERM, mms_app_signal, engine);
guint sigint = g_unix_signal_add(SIGINT, mms_app_signal, engine);
/* Acquire name, don't allow replacement */
name_id = g_bus_own_name(opt.bus_type, MMS_ENGINE_SERVICE,
guint name_id = g_bus_own_name(opt.bus_type, MMS_ENGINE_SERVICE,
G_BUS_NAME_OWNER_FLAGS_REPLACE, mms_app_bus_acquired,
mms_app_name_acquired, mms_app_name_lost, engine, NULL);
......@@ -315,6 +311,8 @@ int main(int argc, char* argv[])
mms_engine_run(engine, loop);
/* Cleanup and exit */
if (sigtrm) g_source_remove(sigtrm);
if (sigint) g_source_remove(sigint);
g_bus_unown_name(name_id);
g_main_loop_unref(loop);
mms_engine_unref(engine);
......
......@@ -38,6 +38,7 @@ struct mms_engine {
OrgNemomobileMmsEngine* proxy;
GMainLoop* loop;
gboolean stopped;
gboolean stop_requested;
gboolean keep_running;
guint start_timeout_id;
gulong send_message_id;
......@@ -68,7 +69,8 @@ mms_engine_stop_callback(
gpointer data)
{
MMSEngine* engine = data;
mms_engine_stop(engine);
engine->stopped = TRUE;
if (engine->loop) g_main_loop_quit(engine->loop);
mms_engine_unref(engine);
return FALSE;
}
......@@ -551,6 +553,7 @@ mms_engine_run(
MMS_ASSERT(!engine->loop);
engine->loop = loop;
engine->stopped = FALSE;
engine->stop_requested = FALSE;
if (!mms_dispatcher_start(engine->dispatcher) && !engine->keep_running) {
mms_engine_start_timeout_schedule(engine);
}
......@@ -563,8 +566,12 @@ void
mms_engine_stop(
MMSEngine* engine)
{
engine->stopped = TRUE;
if (engine->loop) g_main_loop_quit(engine->loop);
if (mms_dispatcher_is_active(engine->dispatcher)) {
engine->stop_requested = TRUE;
mms_dispatcher_cancel(engine->dispatcher, NULL);
} else {
mms_engine_stop_schedule(engine);
}
}
void
......@@ -604,7 +611,9 @@ mms_engine_delegate_dispatcher_done(
{
MMSEngine* engine = mms_engine_from_dispatcher_delegate(delegate);
MMS_DEBUG("All done");
if (!engine->keep_running) mms_engine_stop_schedule(engine);
if (!engine->keep_running || engine->stop_requested) {
mms_engine_stop_schedule(engine);
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment