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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
/*
* request-pipeline.c - Pipeline logic for XMPP requests
*
* Copyright (C) 2007 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "request-pipeline.h"
#include <telepathy-glib/telepathy-glib.h>
#define DEBUG_FLAG GABBLE_DEBUG_PIPELINE
#include "connection.h"
#include "debug.h"
#include "util.h"
#define DEFAULT_REQUEST_TIMEOUT 180
#define REQUEST_PIPELINE_SIZE 10
/* Properties */
enum
{
PROP_CONNECTION = 1,
LAST_PROPERTY
};
G_DEFINE_TYPE (GabbleRequestPipeline, gabble_request_pipeline, G_TYPE_OBJECT);
struct _GabbleRequestPipelineItem
{
GabbleRequestPipeline *pipeline;
WockyStanza *message;
guint timer_id;
guint timeout;
gboolean in_flight;
gboolean zombie;
GabbleRequestPipelineCb callback;
gpointer user_data;
};
struct _GabbleRequestPipelinePrivate
{
GabbleConnection *connection;
GSList *pending_items;
GSList *items_in_flight;
/* Zombie storage (items which were cancelled while the IQ was in flight) */
GSList *crypt_items;
gboolean dispose_has_run;
};
GQuark
gabble_request_pipeline_error_quark (void)
{
static GQuark quark = 0;
if (!quark)
quark = g_quark_from_static_string ("gabble-request-pipeline-error");
return quark;
}
#define GABBLE_REQUEST_PIPELINE_GET_PRIVATE(o) ((o)->priv)
static void
gabble_request_pipeline_init (GabbleRequestPipeline *obj)
{
GabbleRequestPipelinePrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
GABBLE_TYPE_REQUEST_PIPELINE, GabbleRequestPipelinePrivate);
obj->priv = priv;
}
static void gabble_request_pipeline_set_property (GObject *object,
guint property_id, const GValue *value, GParamSpec *pspec);
static void gabble_request_pipeline_get_property (GObject *object,
guint property_id, GValue *value, GParamSpec *pspec);
static void gabble_request_pipeline_dispose (GObject *object);
static void gabble_request_pipeline_finalize (GObject *object);
static void gabble_request_pipeline_go (GabbleRequestPipeline *pipeline);
static void
gabble_request_pipeline_class_init (GabbleRequestPipelineClass *cls)
{
GObjectClass *object_class = G_OBJECT_CLASS (cls);
GParamSpec *param_spec;
g_type_class_add_private (cls, sizeof (GabbleRequestPipelinePrivate));
object_class->get_property = gabble_request_pipeline_get_property;
object_class->set_property = gabble_request_pipeline_set_property;
object_class->dispose = gabble_request_pipeline_dispose;
object_class->finalize = gabble_request_pipeline_finalize;
param_spec = g_param_spec_object ("connection", "GabbleConnection object",
"Gabble connection object that owns this request pipeline helper "
"object.", GABBLE_TYPE_CONNECTION,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_CONNECTION, param_spec);
}
static void
gabble_request_pipeline_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GabbleRequestPipeline *chan = GABBLE_REQUEST_PIPELINE (object);
GabbleRequestPipelinePrivate *priv =
GABBLE_REQUEST_PIPELINE_GET_PRIVATE (chan);
switch (property_id) {
case PROP_CONNECTION:
g_value_set_object (value, priv->connection);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gabble_request_pipeline_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GabbleRequestPipeline *chan = GABBLE_REQUEST_PIPELINE (object);
GabbleRequestPipelinePrivate *priv =
GABBLE_REQUEST_PIPELINE_GET_PRIVATE (chan);
switch (property_id) {
case PROP_CONNECTION:
priv->connection = g_value_get_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
GabbleRequestPipeline *
gabble_request_pipeline_new (GabbleConnection *conn)
{
GabbleRequestPipeline *self;
g_return_val_if_fail (GABBLE_IS_CONNECTION (conn), NULL);
self = GABBLE_REQUEST_PIPELINE (g_object_new (GABBLE_TYPE_REQUEST_PIPELINE,
"connection", conn, NULL));
return self;
}
static void
delete_item (GabbleRequestPipelineItem *item)
{
GabbleRequestPipelinePrivate *priv;
g_assert (GABBLE_IS_REQUEST_PIPELINE (item->pipeline));
priv = GABBLE_REQUEST_PIPELINE_GET_PRIVATE (item->pipeline);
DEBUG ("deleting item %p", item);
if (item->zombie)
{
priv->crypt_items = g_slist_remove (priv->crypt_items, item);
}
else if (item->in_flight)
{
priv->items_in_flight = g_slist_remove (priv->items_in_flight, item);
}
else
{
priv->pending_items = g_slist_remove (priv->pending_items, item);
}
if (item->timer_id)
g_source_remove (item->timer_id);
tp_clear_pointer (&item->message, g_object_unref);
g_slice_free (GabbleRequestPipelineItem, item);
}
static void
gabble_request_pipeline_create_zombie (GabbleRequestPipeline *pipeline,
GabbleRequestPipelineItem *item,
GError *error)
{
GabbleRequestPipelinePrivate *priv = pipeline->priv;
g_assert (!item->zombie);
if (item->timer_id != 0)
{
g_source_remove (item->timer_id);
item->timer_id = 0;
}
(item->callback) (priv->connection, NULL, item->user_data, error);
if (item->in_flight)
{
item->zombie = TRUE;
priv->items_in_flight = g_slist_remove (priv->items_in_flight, item);
priv->crypt_items = g_slist_prepend (priv->crypt_items, item);
gabble_request_pipeline_go (pipeline);
}
else
{
delete_item (item);
}
}
void
gabble_request_pipeline_item_cancel (GabbleRequestPipelineItem *item)
{
GError cancelled = { GABBLE_REQUEST_PIPELINE_ERROR,
GABBLE_REQUEST_PIPELINE_ERROR_CANCELLED,
"Request cancelled" };
gabble_request_pipeline_create_zombie (item->pipeline, item, &cancelled);
}
static void
gabble_request_pipeline_flush (GabbleRequestPipeline *self,
GSList **list)
{
GabbleRequestPipelineItem *item;
GError disconnected = { TP_ERROR, TP_ERROR_DISCONNECTED,
"Request failed because connection became disconnected" };
while (*list != NULL)
{
item = (*list)->data;
if (!item->zombie)
(item->callback) (self->priv->connection, NULL, item->user_data,
&disconnected);
delete_item (item);
}
}
static void
gabble_request_pipeline_dispose (GObject *object)
{
GabbleRequestPipeline *self = GABBLE_REQUEST_PIPELINE (object);
GabbleRequestPipelinePrivate *priv =
GABBLE_REQUEST_PIPELINE_GET_PRIVATE (self);
if (priv->dispose_has_run)
return;
priv->dispose_has_run = TRUE;
DEBUG ("disposing request-pipeline");
gabble_request_pipeline_flush (self, &priv->items_in_flight);
gabble_request_pipeline_flush (self, &priv->pending_items);
gabble_request_pipeline_flush (self, &priv->crypt_items);
g_idle_remove_by_data (self);
if (G_OBJECT_CLASS (gabble_request_pipeline_parent_class)->dispose)
G_OBJECT_CLASS (gabble_request_pipeline_parent_class)->dispose (object);
}
static void
gabble_request_pipeline_finalize (GObject *object)
{
G_OBJECT_CLASS (gabble_request_pipeline_parent_class)->finalize (object);
}
static void
response_cb (GabbleConnection *conn,
WockyStanza *sent,
WockyStanza *reply,
GObject *object,
gpointer user_data)
{
GabbleRequestPipelineItem *item = (GabbleRequestPipelineItem *) user_data;
GabbleRequestPipeline *pipeline = item->pipeline;
GabbleRequestPipelinePrivate *priv;
g_assert (GABBLE_IS_REQUEST_PIPELINE (pipeline));
priv = GABBLE_REQUEST_PIPELINE_GET_PRIVATE (pipeline);
DEBUG ("got reply for request %p", item);
if (NULL == g_slist_find (priv->items_in_flight, item))
return;
g_assert (item->in_flight);
priv->items_in_flight = g_slist_remove (priv->items_in_flight, item);
if (!item->zombie)
{
GError *error = NULL;
wocky_stanza_extract_errors (reply, NULL, &error, NULL, NULL);
item->callback (priv->connection, reply, item->user_data, error);
g_clear_error (&error);
}
else
{
DEBUG ("ignoring zombie connection reply");
}
delete_item (item);
gabble_request_pipeline_go (pipeline);
}
static gboolean
timeout_cb (gpointer data)
{
GabbleRequestPipelineItem *item = (GabbleRequestPipelineItem *) data;
GError timed_out = { GABBLE_REQUEST_PIPELINE_ERROR,
GABBLE_REQUEST_PIPELINE_ERROR_TIMEOUT,
"Request timed out" };
gabble_request_pipeline_create_zombie (item->pipeline, item, &timed_out);
return FALSE;
}
static void
send_next_request (GabbleRequestPipeline *pipeline)
{
GabbleRequestPipelinePrivate *priv =
GABBLE_REQUEST_PIPELINE_GET_PRIVATE (pipeline);
GabbleRequestPipelineItem *item;
GError *error = NULL;
if (priv->pending_items == NULL)
return;
item = priv->pending_items->data;
DEBUG ("processing request %p", item);
g_assert (item->in_flight == FALSE);
priv->pending_items = g_slist_remove (priv->pending_items, item);
if (!_gabble_connection_send_with_reply (priv->connection, item->message,
response_cb, G_OBJECT (pipeline), item, &error))
{
item->callback (priv->connection, NULL, item->user_data, error);
delete_item (item);
send_next_request (pipeline);
}
else
{
priv->items_in_flight = g_slist_prepend (priv->items_in_flight, item);
item->in_flight = TRUE;
item->timer_id = g_timeout_add_seconds (item->timeout, timeout_cb, item);
}
}
static void
gabble_request_pipeline_go (GabbleRequestPipeline *pipeline)
{
GabbleRequestPipelinePrivate *priv =
GABBLE_REQUEST_PIPELINE_GET_PRIVATE (pipeline);
DEBUG ("called; %d pending items, %d items in flight",
g_slist_length (priv->pending_items),
g_slist_length (priv->items_in_flight));
while (priv->pending_items &&
(g_slist_length (priv->items_in_flight) < REQUEST_PIPELINE_SIZE))
{
send_next_request (pipeline);
}
}
static gboolean
delayed_run_pipeline (gpointer user_data)
{
GabbleRequestPipeline *pipeline = (GabbleRequestPipeline *) user_data;
gabble_request_pipeline_go (pipeline);
return FALSE;
}
GabbleRequestPipelineItem *
gabble_request_pipeline_enqueue (GabbleRequestPipeline *pipeline,
WockyStanza *msg,
guint timeout,
GabbleRequestPipelineCb callback,
gpointer user_data)
{
GabbleRequestPipelinePrivate *priv =
GABBLE_REQUEST_PIPELINE_GET_PRIVATE (pipeline);
GabbleRequestPipelineItem *item = g_slice_new0 (GabbleRequestPipelineItem);
g_return_val_if_fail (callback != NULL, NULL);
item->pipeline = pipeline;
item->message = msg;
if (timeout == 0)
timeout = DEFAULT_REQUEST_TIMEOUT;
item->timeout = timeout;
item->in_flight = FALSE;
item->callback = callback;
item->user_data = user_data;
g_object_ref (msg);
priv->pending_items = g_slist_append (priv->pending_items, item);
DEBUG ("enqueued new request as item %p", item);
DEBUG ("number of items in flight: %d", g_slist_length (priv->items_in_flight));
/* If the pipeline isn't full, schedule a run. Run it delayed so that if
* there's an error, the callback will be called after this function returns.
*/
if (g_slist_length (priv->items_in_flight) < REQUEST_PIPELINE_SIZE)
gabble_idle_add_weak (delayed_run_pipeline, G_OBJECT (pipeline));
return item;
}
|