Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[mms-engine] Close the connection when terminated by signal
  • Loading branch information
monich committed Feb 24, 2015
1 parent 38d5666 commit 7f64aa8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
22 changes: 10 additions & 12 deletions mms-engine/main.c
@@ -1,5 +1,6 @@
/*
* 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
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -299,22 +296,23 @@ 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);

/* Run the main loop */
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);
Expand Down
17 changes: 13 additions & 4 deletions mms-engine/mms_engine.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down

0 comments on commit 7f64aa8

Please sign in to comment.