Skip to content

Latest commit

 

History

History
354 lines (322 loc) · 8.66 KB

mms_task.c

File metadata and controls

354 lines (322 loc) · 8.66 KB
 
Feb 17, 2014
Feb 17, 2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright (C) 2013-2014 Jolla Ltd.
*
* 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
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "mms_task.h"
#include "mms_handler.h"
#include "mms_file_util.h"
#ifdef _WIN32
# define snprintf _snprintf
#endif
/* Logging */
#define MMS_LOG_MODULE_NAME mms_task_log
#include "mms_lib_log.h"
MMS_LOG_MODULE_DEFINE("mms-task");
#define MMS_TASK_DEFAULT_LIFETIME (600)
G_DEFINE_TYPE(MMSTask, mms_task, G_TYPE_OBJECT);
#define MMS_TASK(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), MMS_TYPE_TASK, MMSTask))
#define MMS_TASK_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), MMS_TYPE_TASK, MMSTaskClass))
static
void
mms_task_wakeup_free(
gpointer data)
{
mms_task_unref(data);
}
static
gboolean
mms_task_wakeup_callback(
gpointer data)
{
MMSTask* task = data;
task->wakeup_id = 0;
MMS_ASSERT(task->state == MMS_TASK_STATE_SLEEP);
mms_task_set_state(task, MMS_TASK_STATE_READY);
return FALSE;
}
gboolean
mms_task_schedule_wakeup(
MMSTask* task,
unsigned int secs)
{
const time_t now = time(NULL);
if (!secs) secs = task->config->retry_secs;
/* Cancel the previous sleep */
if (task->wakeup_id) {
MMS_ASSERT(task->state == MMS_TASK_STATE_SLEEP);
g_source_remove(task->wakeup_id);
task->wakeup_id = 0;
}
if (now < task->deadline) {
/* Don't sleep past deadline */
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,
secs, mms_task_wakeup_callback, mms_task_ref(task),
mms_task_wakeup_free);
MMS_ASSERT(task->wakeup_id);
MMS_VERBOSE("%s sleeping for %u sec", task->name, secs);
}
return (task->wakeup_id > 0);
}
gboolean
mms_task_sleep(
MMSTask* task,
unsigned int secs)
{
gboolean ok = mms_task_schedule_wakeup(task, secs);
mms_task_set_state(task, ok ? MMS_TASK_STATE_SLEEP : MMS_TASK_STATE_DONE);
return ok;
}
static
void
mms_task_cancel_cb(
MMSTask* task)
{
if (task->wakeup_id) {
MMS_ASSERT(task->state == MMS_TASK_STATE_SLEEP);
g_source_remove(task->wakeup_id);
task->wakeup_id = 0;
}
task->flags |= MMS_TASK_FLAG_CANCELLED;
mms_task_set_state(task, MMS_TASK_STATE_DONE);
}
static
void
mms_task_finalize(
GObject* object)
{
MMSTask* task = MMS_TASK(object);
MMS_VERBOSE_("%p", task);
MMS_ASSERT(!task->delegate);
MMS_ASSERT(!task->wakeup_id);
Feb 19, 2014
Feb 19, 2014
121
122
123
124
125
126
127
128
129
130
if (task->id) {
if (!task->config->keep_temp_files) {
char* dir = mms_task_dir(task);
if (rmdir(dir) == 0) {
MMS_VERBOSE("Deleted %s", dir);
}
g_free(dir);
}
g_free(task->id);
}
Feb 17, 2014
Feb 17, 2014
131
132
133
134
135
136
g_free(task->name);
g_free(task->imsi);
mms_handler_unref(task->handler);
G_OBJECT_CLASS(mms_task_parent_class)->finalize(object);
}
Feb 24, 2014
Feb 24, 2014
137
138
139
140
141
static
void
mms_task_class_init(
MMSTaskClass* klass)
{
Feb 24, 2014
Feb 24, 2014
142
143
klass->fn_cancel = mms_task_cancel_cb;
G_OBJECT_CLASS(klass)->finalize = mms_task_finalize;
Feb 24, 2014
Feb 24, 2014
144
145
}
Feb 17, 2014
Feb 17, 2014
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
static
void
mms_task_init(
MMSTask* task)
{
MMS_VERBOSE_("%p", task);
}
void*
mms_task_alloc(
GType type,
const MMSConfig* config,
MMSHandler* handler,
const char* name,
const char* id,
const char* imsi)
{
MMSTask* task = g_object_new(type, NULL);
const time_t now = time(NULL);
time_t max_lifetime = MMS_TASK_GET_CLASS(task)->max_lifetime;
if (!max_lifetime) max_lifetime = MMS_TASK_DEFAULT_LIFETIME;
task->config = config;
task->handler = mms_handler_ref(handler);
if (name) {
task->name = id ?
g_strdup_printf("%s[%.08s]", name, id) :
g_strdup(name);
}
task->id = g_strdup(id);
task->imsi = g_strdup(imsi);
task->deadline = now + max_lifetime;
return task;
}
MMSTask*
mms_task_ref(
MMSTask* task)
{
if (task) g_object_ref(MMS_TASK(task));
return task;
}
void
mms_task_unref(
MMSTask* task)
{
if (task) g_object_unref(MMS_TASK(task));
}
void
mms_task_run(
MMSTask* task)
{
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);
}
void
mms_task_transmit(
MMSTask* task,
MMSConnection* connection)
{
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);
}
void
mms_task_network_unavailable(
MMSTask* task)
{
if (task->state != MMS_TASK_STATE_DONE) {
MMS_ASSERT(task->state == MMS_TASK_STATE_NEED_CONNECTION ||
task->state == MMS_TASK_STATE_NEED_USER_CONNECTION ||
task->state == MMS_TASK_STATE_TRANSMITTING);
MMS_TASK_GET_CLASS(task)->fn_network_unavailable(task);
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);
}
}
void
mms_task_cancel(
MMSTask* task)
{
MMS_DEBUG_("%s", task->name);
MMS_TASK_GET_CLASS(task)->fn_cancel(task);
}
void
mms_task_set_state(
MMSTask* task,
MMS_TASK_STATE state)
{
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 (!mms_task_schedule_wakeup(task, task->config->retry_secs)) {
MMS_DEBUG("%s SLEEP -> DONE (no time left)", task->name);
state = MMS_TASK_STATE_DONE;
}
}
task->state = state;
if (task->delegate && task->delegate->fn_task_state_changed) {
task->delegate->fn_task_state_changed(task->delegate, task);
}
}
}
/* Utilities */
static const char* mms_task_names[] = {"READY", "NEED_CONNECTION",
"NEED_USER_CONNECTION", "TRANSMITTING", "WORKING", "SLEEP", "DONE"
};
G_STATIC_ASSERT(G_N_ELEMENTS(mms_task_names) == MMS_TASK_STATE_COUNT);
const char*
mms_task_state_name(
MMS_TASK_STATE state)
{
if (state >= 0 && state < G_N_ELEMENTS(mms_task_names)) {
return mms_task_names[state];
} else {
/* This shouldn't happen */
static char unknown[32];
snprintf(unknown, sizeof(unknown), "%d ????", state);
return unknown;
}
}
gboolean
mms_task_queue_and_unref(
MMSTaskDelegate* delegate,
MMSTask* task)
{
gboolean ok = FALSE;
if (task) {
if (delegate && delegate->fn_task_queue) {
delegate->fn_task_queue(delegate, task);
ok = TRUE;
}
mms_task_unref(task);
}
return ok;
}
Mar 21, 2014
Mar 21, 2014
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
gboolean
mms_task_match_id(
MMSTask* task,
const char* id)
{
if (!task) {
/* No task - no match */
return FALSE;
} else if (!id || !id[0]) {
/* A wildcard matching any task */
return TRUE;
} else if (!task->id || !task->id[0]) {
/* Only wildcard will match that */
return FALSE;
} else {
return !strcmp(task->id, id);
}
}
Feb 20, 2014
Feb 20, 2014
320
321
322
323
324
325
326
/**
* Generates dummy task id if necessary.
*/
const char*
mms_task_make_id(
MMSTask* task)
{
Feb 27, 2014
Feb 27, 2014
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
if (!task->id || !task->id[0]) {
char* msgdir = g_strconcat(task->config->root_dir, "/",
MMS_MESSAGE_DIR, NULL);
int err = g_mkdir_with_parents(msgdir, MMS_DIR_PERM);
if (!err || errno == EEXIST) {
char* tmpl = g_strconcat(msgdir, "/XXXXXX" , NULL);
char* taskdir = g_mkdtemp_full(tmpl, MMS_DIR_PERM);
if (taskdir) {
g_free(task->id);
task->id = g_path_get_basename(taskdir);
}
g_free(tmpl);
} else {
MMS_ERR("Failed to create %s: %s", task->config->root_dir,
strerror(errno));
}
g_free(msgdir);
Feb 20, 2014
Feb 20, 2014
344
345
346
347
}
return task->id;
}
Feb 17, 2014
Feb 17, 2014
348
349
350
351
352
353
354
/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/