Commit e63539b9 authored by Slava Monich's avatar Slava Monich

[mms-engine] Housekeeping

parent 3f72909a
......@@ -21,6 +21,8 @@
#include "mms_settings_dconf.h"
#include "mms_log.h"
#include <gutil_misc.h>
#ifdef SAILFISH
# include "mms_connman_nemo.h"
# define mms_connman_new() mms_connman_nemo_new()
......@@ -32,6 +34,21 @@
/* Generated code */
#include "org.nemomobile.MmsEngine.h"
/* Signals D-Bus proxy */
enum mms_engine_dbus_methods {
MMS_ENGINE_METHOD_SEND_MESSAGE,
MMS_ENGINE_METHOD_PUSH,
MMS_ENGINE_METHOD_PUSH_NOTIFY,
MMS_ENGINE_METHOD_CANCEL,
MMS_ENGINE_METHOD_RECEIVE_MESSAGE,
MMS_ENGINE_METHOD_SEND_READ_REPORT,
MMS_ENGINE_METHOD_SET_LOG_LEVEL,
MMS_ENGINE_METHOD_SET_LOG_TYPE,
MMS_ENGINE_METHOD_GET_VERSION,
MMS_ENGINE_METHOD_MIGRATE_SETTINGS,
MMS_ENGINE_METHOD_COUNT
};
struct mms_engine {
GObject parent;
const MMSConfig* config;
......@@ -48,20 +65,11 @@ struct mms_engine {
gboolean stop_requested;
gboolean keep_running;
guint start_timeout_id;
gulong send_message_id;
gulong push_signal_id;
gulong push_notify_signal_id;
gulong receive_signal_id;
gulong read_report_signal_id;
gulong cancel_signal_id;
gulong set_log_level_signal_id;
gulong set_log_type_signal_id;
gulong get_version_signal_id;
gulong migrate_settings_signal_id;
gulong proxy_signal_id[MMS_ENGINE_METHOD_COUNT];
};
typedef GObjectClass MMSEngineClass;
G_DEFINE_TYPE(MMSEngine, mms_engine, G_TYPE_OBJECT);
G_DEFINE_TYPE(MMSEngine, mms_engine, G_TYPE_OBJECT)
#define MMS_TYPE_ENGINE (mms_engine_get_type())
#define MMS_ENGINE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\
MMS_TYPE_ENGINE, MMSEngine))
......@@ -501,34 +509,34 @@ mms_engine_new(
mms->log_modules = log_modules;
mms->log_count = log_count;
mms->proxy = org_nemomobile_mms_engine_skeleton_new();
mms->send_message_id =
mms->proxy_signal_id[MMS_ENGINE_METHOD_SEND_MESSAGE] =
g_signal_connect(mms->proxy, "handle-send-message",
G_CALLBACK(mms_engine_handle_send_message), mms);
mms->push_signal_id =
mms->proxy_signal_id[MMS_ENGINE_METHOD_PUSH] =
g_signal_connect(mms->proxy, "handle-push",
G_CALLBACK(mms_engine_handle_push), mms);
mms->push_notify_signal_id =
mms->proxy_signal_id[MMS_ENGINE_METHOD_PUSH_NOTIFY] =
g_signal_connect(mms->proxy, "handle-push-notify",
G_CALLBACK(mms_engine_handle_push_notify), mms);
mms->cancel_signal_id =
mms->proxy_signal_id[MMS_ENGINE_METHOD_CANCEL] =
g_signal_connect(mms->proxy, "handle-cancel",
G_CALLBACK(mms_engine_handle_cancel), mms);
mms->receive_signal_id =
mms->proxy_signal_id[MMS_ENGINE_METHOD_RECEIVE_MESSAGE] =
g_signal_connect(mms->proxy, "handle-receive-message",
G_CALLBACK(mms_engine_handle_receive_message), mms);
mms->read_report_signal_id =
mms->proxy_signal_id[MMS_ENGINE_METHOD_SEND_READ_REPORT] =
g_signal_connect(mms->proxy, "handle-send-read-report",
G_CALLBACK(mms_engine_handle_send_read_report), mms);
mms->set_log_level_signal_id =
mms->proxy_signal_id[MMS_ENGINE_METHOD_SET_LOG_LEVEL] =
g_signal_connect(mms->proxy, "handle-set-log-level",
G_CALLBACK(mms_engine_handle_set_log_level), mms);
mms->set_log_type_signal_id =
mms->proxy_signal_id[MMS_ENGINE_METHOD_SET_LOG_TYPE] =
g_signal_connect(mms->proxy, "handle-set-log-type",
G_CALLBACK(mms_engine_handle_set_log_type), mms);
mms->get_version_signal_id =
mms->proxy_signal_id[MMS_ENGINE_METHOD_GET_VERSION] =
g_signal_connect(mms->proxy, "handle-get-version",
G_CALLBACK(mms_engine_handle_get_version), mms);
mms->migrate_settings_signal_id =
mms->proxy_signal_id[MMS_ENGINE_METHOD_MIGRATE_SETTINGS] =
g_signal_connect(mms->proxy, "handle-migrate-settings",
G_CALLBACK(mms_engine_handle_migrate_settings), mms);
......@@ -645,37 +653,29 @@ void
mms_engine_dispose(
GObject* object)
{
MMSEngine* e = MMS_ENGINE(object);
MMS_VERBOSE_("%p", e);
MMS_ASSERT(!e->loop);
mms_engine_unregister(e);
mms_engine_start_timeout_cancel(e);
if (e->proxy) {
g_signal_handler_disconnect(e->proxy, e->send_message_id);
g_signal_handler_disconnect(e->proxy, e->push_signal_id);
g_signal_handler_disconnect(e->proxy, e->push_notify_signal_id);
g_signal_handler_disconnect(e->proxy, e->receive_signal_id);
g_signal_handler_disconnect(e->proxy, e->read_report_signal_id);
g_signal_handler_disconnect(e->proxy, e->cancel_signal_id);
g_signal_handler_disconnect(e->proxy, e->set_log_level_signal_id);
g_signal_handler_disconnect(e->proxy, e->set_log_type_signal_id);
g_signal_handler_disconnect(e->proxy, e->get_version_signal_id);
g_signal_handler_disconnect(e->proxy, e->migrate_settings_signal_id);
g_object_unref(e->proxy);
e->proxy = NULL;
MMSEngine* mms = MMS_ENGINE(object);
MMS_VERBOSE_("%p", mms);
MMS_ASSERT(!mms->loop);
mms_engine_unregister(mms);
mms_engine_start_timeout_cancel(mms);
if (mms->proxy) {
gutil_disconnect_handlers(mms->proxy, mms->proxy_signal_id,
G_N_ELEMENTS(mms->proxy_signal_id));
g_object_unref(mms->proxy);
mms->proxy = NULL;
}
if (e->dispatcher) {
mms_dispatcher_set_delegate(e->dispatcher, NULL);
mms_dispatcher_unref(e->dispatcher);
e->dispatcher = NULL;
if (mms->dispatcher) {
mms_dispatcher_set_delegate(mms->dispatcher, NULL);
mms_dispatcher_unref(mms->dispatcher);
mms->dispatcher = NULL;
}
if (e->settings) {
mms_settings_unref(e->settings);
e->settings = NULL;
if (mms->settings) {
mms_settings_unref(mms->settings);
mms->settings = NULL;
}
if (e->cm) {
mms_connman_unref(e->cm);
e->cm = NULL;
if (mms->cm) {
mms_connman_unref(mms->cm);
mms->cm = NULL;
}
G_OBJECT_CLASS(mms_engine_parent_class)->dispose(object);
}
......
......@@ -28,7 +28,12 @@ MMS_LOG_MODULE_DEFINE("mms-task");
#define MMS_TASK_DEFAULT_LIFETIME (600)
G_DEFINE_TYPE(MMSTask, mms_task, G_TYPE_OBJECT);
struct mms_task_priv {
guint wakeup_id; /* ID of the wakeup source */
time_t wakeup_time; /* Wake up time (if sleeping) */
};
G_DEFINE_TYPE(MMSTask, mms_task, G_TYPE_OBJECT)
#define MMS_TASK(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), MMS_TYPE_TASK, MMSTask))
......@@ -56,8 +61,9 @@ gboolean
mms_task_wakeup_callback(
gpointer data)
{
MMSTask* task = data;
task->wakeup_id = 0;
MMSTask* task = MMS_TASK(data);
MMSTaskPriv* priv = task->priv;
priv->wakeup_id = 0;
MMS_ASSERT(task->state == MMS_TASK_STATE_SLEEP);
mms_task_set_state(task, MMS_TASK_STATE_READY);
return FALSE;
......@@ -68,14 +74,15 @@ mms_task_schedule_wakeup(
MMSTask* task,
unsigned int secs)
{
MMSTaskPriv* priv = task->priv;
const time_t now = time(NULL);
if (!secs) secs = task->settings->config->retry_secs;
/* Cancel the previous sleep */
if (task->wakeup_id) {
if (priv->wakeup_id) {
MMS_ASSERT(task->state == MMS_TASK_STATE_SLEEP);
g_source_remove(task->wakeup_id);
task->wakeup_id = 0;
g_source_remove(priv->wakeup_id);
priv->wakeup_id = 0;
}
if (now < task->deadline) {
......@@ -83,15 +90,15 @@ mms_task_schedule_wakeup(
const unsigned int max_secs = task->deadline - now;
if (secs > max_secs) secs = max_secs;
/* Schedule wakeup */
task->wakeup_time = now + secs;
task->wakeup_id = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT,
priv->wakeup_time = now + secs;
priv->wakeup_id = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT,
secs, mms_task_wakeup_callback, mms_task_ref(task),
mms_task_wakeup_free);
MMS_ASSERT(task->wakeup_id);
MMS_ASSERT(priv->wakeup_id);
MMS_VERBOSE("%s sleeping for %u sec", task->name, secs);
}
return (task->wakeup_id > 0);
return (priv->wakeup_id > 0);
}
gboolean
......@@ -109,10 +116,11 @@ void
mms_task_cancel_cb(
MMSTask* task)
{
if (task->wakeup_id) {
MMSTaskPriv* priv = task->priv;
if (priv->wakeup_id) {
MMS_ASSERT(task->state == MMS_TASK_STATE_SLEEP);
g_source_remove(task->wakeup_id);
task->wakeup_id = 0;
g_source_remove(priv->wakeup_id);
priv->wakeup_id = 0;
}
task->flags |= MMS_TASK_FLAG_CANCELLED;
mms_task_set_state(task, MMS_TASK_STATE_DONE);
......@@ -126,7 +134,7 @@ mms_task_finalize(
MMSTask* task = MMS_TASK(object);
MMS_VERBOSE_("%p", task);
MMS_ASSERT(!task->delegate);
MMS_ASSERT(!task->wakeup_id);
MMS_ASSERT(!task->priv->wakeup_id);
MMS_ASSERT(mms_task_count > 0);
if (!(--mms_task_count)) {
MMS_VERBOSE("Last task is gone");
......@@ -155,6 +163,7 @@ mms_task_class_init(
MMSTaskClass* klass)
{
klass->fn_cancel = mms_task_cancel_cb;
g_type_class_add_private(klass, sizeof(MMSTaskPriv));
G_OBJECT_CLASS(klass)->finalize = mms_task_finalize;
}
......@@ -166,6 +175,7 @@ mms_task_init(
MMS_VERBOSE_("%p", task);
mms_task_count++;
task->order = mms_task_order++;
task->priv = G_TYPE_INSTANCE_GET_PRIVATE(task, MMS_TYPE_TASK, MMSTaskPriv);
}
void*
......@@ -215,7 +225,6 @@ mms_task_run(
{
MMS_ASSERT(task->state == MMS_TASK_STATE_READY);
MMS_TASK_GET_CLASS(task)->fn_run(task);
time(&task->last_run_time);
MMS_ASSERT(task->state != MMS_TASK_STATE_READY);
}
......@@ -227,7 +236,6 @@ mms_task_transmit(
MMS_ASSERT(task->state == MMS_TASK_STATE_NEED_CONNECTION ||
task->state == MMS_TASK_STATE_NEED_USER_CONNECTION);
MMS_TASK_GET_CLASS(task)->fn_transmit(task, connection);
time(&task->last_run_time);
MMS_ASSERT(task->state != MMS_TASK_STATE_NEED_CONNECTION &&
task->state != MMS_TASK_STATE_NEED_USER_CONNECTION);
}
......@@ -245,7 +253,6 @@ mms_task_network_unavailable(
MMS_ASSERT(task->state != MMS_TASK_STATE_NEED_CONNECTION &&
task->state != MMS_TASK_STATE_NEED_USER_CONNECTION &&
task->state != MMS_TASK_STATE_TRANSMITTING);
time(&task->last_run_time);
}
}
......@@ -262,11 +269,12 @@ mms_task_set_state(
MMSTask* task,
MMS_TASK_STATE state)
{
MMSTaskPriv* priv = task->priv;
if (task->state != state) {
MMS_DEBUG("%s %s -> %s", task->name,
mms_task_state_name(task->state),
mms_task_state_name(state));
if (state == MMS_TASK_STATE_SLEEP && !task->wakeup_id) {
if (state == MMS_TASK_STATE_SLEEP && !priv->wakeup_id) {
const unsigned int secs = task_config(task)->retry_secs;
if (!mms_task_schedule_wakeup(task, secs)) {
MMS_DEBUG("%s SLEEP -> DONE (no time left)", task->name);
......
......@@ -45,6 +45,7 @@ typedef enum _MMS_TASK_PRIORITY {
/* Delegate (one per task) */
typedef struct mms_task MMSTask;
typedef struct mms_task_priv MMSTaskPriv;
typedef struct mms_task_delegate MMSTaskDelegate;
struct mms_task_delegate {
/* Submits new task to the queue */
......@@ -60,6 +61,7 @@ struct mms_task_delegate {
/* Task object */
struct mms_task {
GObject parent; /* Parent object */
MMSTaskPriv* priv; /* Private data */
MMS_TASK_PRIORITY priority; /* Task priority */
int order; /* Task creation order */
char* name; /* Task name for debug purposes */
......@@ -69,10 +71,7 @@ struct mms_task {
MMSHandler* handler; /* Message database interface */
MMSTaskDelegate* delegate; /* Observer */
MMS_TASK_STATE state; /* Task state */
time_t last_run_time; /* Last run time */
time_t deadline; /* Task deadline */
time_t wakeup_time; /* Wake up time (if sleeping) */
guint wakeup_id; /* ID of the wakeup source */
int flags; /* Flags: */
#define MMS_TASK_FLAG_CANCELLED (0x01) /* Task has been cancelled */
......
......@@ -54,7 +54,7 @@ typedef struct mms_http_transfer {
} MMSHttpTransfer;
/* Private state */
struct mms_task_http_private {
struct mms_task_http_priv {
MMSHttpTransfer* tx;
char* uri;
char* send_path;
......@@ -69,7 +69,7 @@ struct mms_task_http_private {
MMS_CONNECTION_TYPE connection_type;
};
G_DEFINE_TYPE(MMSTaskHttp, mms_task_http, MMS_TYPE_TASK);
G_DEFINE_TYPE(MMSTaskHttp, mms_task_http, MMS_TYPE_TASK)
#define MMS_TYPE_TASK_HTTP (mms_task_http_get_type())
#define MMS_TASK_HTTP(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\
MMS_TYPE_TASK_HTTP, MMSTaskHttp))
......@@ -228,7 +228,7 @@ mms_task_http_set_state(
MMS_HTTP_STATE new_state,
SoupStatus ss)
{
MMSTaskHttpPrivate* priv = http->priv;
MMSTaskHttpPriv* priv = http->priv;
if (priv->transaction_state != new_state &&
priv->transaction_state != MMS_HTTP_DONE) {
MMSTaskHttpClass* klass = MMS_TASK_HTTP_GET_CLASS(http);
......@@ -254,7 +254,7 @@ void
mms_task_http_finish_transfer(
MMSTaskHttp* http)
{
MMSTaskHttpPrivate* priv = http->priv;
MMSTaskHttpPriv* priv = http->priv;
if (priv->tx) {
SoupMessage* msg = priv->tx->message;
if (priv->wrote_headers_signal_id) {
......@@ -282,7 +282,7 @@ mms_task_http_finished(
gpointer user_data)
{
MMSTaskHttp* http = user_data;
MMSTaskHttpPrivate* priv = http->priv;
MMSTaskHttpPriv* priv = http->priv;
if (priv->tx && priv->tx->session == session) {
MMS_HTTP_STATE next_http_state;
MMSTask* task = &http->task;
......@@ -331,7 +331,7 @@ mms_task_http_write_next_chunk(
SoupMessage* msg,
MMSTaskHttp* http)
{
MMSTaskHttpPrivate* priv = http->priv;
MMSTaskHttpPriv* priv = http->priv;
MMSHttpTransfer* tx = priv->tx;
#if MMS_LOG_VERBOSE
if (priv->bytes_sent) MMS_VERBOSE("%d bytes sent", (int)priv->bytes_sent);
......@@ -357,7 +357,7 @@ mms_task_http_got_chunk(
SoupBuffer* buf,
MMSTaskHttp* http)
{
MMSTaskHttpPrivate* priv = http->priv;
MMSTaskHttpPriv* priv = http->priv;
MMSHttpTransfer* tx = priv->tx;
MMS_ASSERT(tx && tx->message == msg);
if (tx && tx->message == msg) {
......@@ -381,7 +381,7 @@ mms_task_http_start(
int send_fd = -1;
int receive_fd = -1;
guint bytes_to_send = 0;
MMSTaskHttpPrivate* priv = http->priv;
MMSTaskHttpPriv* priv = http->priv;
MMS_ASSERT(mms_connection_is_open(connection));
mms_task_http_finish_transfer(http);
priv->bytes_sent = 0;
......@@ -577,7 +577,6 @@ mms_task_http_finalize(
g_free(http->priv->uri);
g_free(http->priv->send_path);
g_free(http->priv->receive_path);
g_free(http->priv);
G_OBJECT_CLASS(mms_task_http_parent_class)->finalize(object);
}
......@@ -591,6 +590,7 @@ mms_task_http_class_init(
{
GObjectClass* object_class = G_OBJECT_CLASS(klass);
MMSTaskClass* task_class = &klass->task;
g_type_class_add_private(klass, sizeof(MMSTaskHttpPriv));
task_class->fn_run = mms_task_http_run;
task_class->fn_transmit = mms_task_http_transmit;
task_class->fn_network_unavailable = mms_task_http_network_unavailable;
......@@ -605,8 +605,10 @@ mms_task_http_class_init(
static
void
mms_task_http_init(
MMSTaskHttp* up)
MMSTaskHttp* http)
{
http->priv = G_TYPE_INSTANCE_GET_PRIVATE(http, MMS_TYPE_TASK_HTTP,
MMSTaskHttpPriv);
}
/**
......@@ -627,8 +629,7 @@ mms_task_http_alloc(
{
MMSTaskHttp* http = mms_task_alloc(type ? type : MMS_TYPE_TASK_HTTP,
settings, handler, name, id, imsi);
MMSTaskHttpPrivate* priv = g_new0(MMSTaskHttpPrivate, 1);
http->priv = priv;
MMSTaskHttpPriv* priv = http->priv;
priv->uri = g_strdup(uri);
priv->receive_file = receive_file; /* Always static, don't strdup */
priv->connection_type = ct;
......
......@@ -32,10 +32,10 @@
#endif
/* HTTP task object */
typedef struct mms_task_http_private MMSTaskHttpPrivate;
typedef struct mms_task_http_priv MMSTaskHttpPriv;
typedef struct mms_task_http {
MMSTask task; /* Parent object */
MMSTaskHttpPrivate* priv; /* Private state */
MMSTaskHttpPriv* priv; /* Private data */
} MMSTaskHttp;
typedef struct mms_task_http_class {
......
......@@ -10,6 +10,7 @@ Requires: dbus
Requires: ofono
Requires: libgofono >= 1.0.11
Requires: libgofonoext >= 1.0.4
Requires: libglibutil >= 1.0.5
#Requires: ImageMagick
BuildRequires: python
......@@ -24,7 +25,7 @@ BuildRequires: pkgconfig(libsoup-2.4) >= 2.38
BuildRequires: pkgconfig(libwspcodec) >= 2.2
BuildRequires: pkgconfig(libgofono) >= 1.0.11
BuildRequires: pkgconfig(libgofonoext) >= 1.0.4
BuildRequires: pkgconfig(libglibutil)
BuildRequires: pkgconfig(libglibutil) >= 1.0.5
BuildRequires: pkgconfig(Qt5Gui)
#BuildRequires: pkgconfig(ImageMagick)
......
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