File: xcap-core.cpp

package info (click to toggle)
ekiga 4.0.1-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 31,612 kB
  • ctags: 14,511
  • sloc: cpp: 72,155; sh: 11,298; xml: 8,904; ansic: 8,572; makefile: 1,738; asm: 453; awk: 16
file content (344 lines) | stat: -rw-r--r-- 9,423 bytes parent folder | download | duplicates (2)
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

/* Ekiga -- A VoIP and Video-Conferencing application
 * Copyright (C) 2000-2009 Damien Sandras <dsandras@seconix.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *
 * Ekiga is licensed under the GPL license and as a special exception,
 * you have permission to link or otherwise combine this program with the
 * programs OPAL, OpenH323 and PWLIB, and distribute the combination,
 * without applying the requirements of the GNU GPL to the OPAL, OpenH323
 * and PWLIB programs, as long as you do follow the requirements of the
 * GNU GPL for all the rest of the software thus combined.
 */


/*
 *                         xcap.cpp  -  description
 *                         ------------------------------------
 *   begin                : Mon 29 September 2008
 *   copyright            : (C) 2008 by Julien Puydt
 *   description          : Implementation of the XCAP support code
 *
 */

#include "config.h"

#include "xcap-core.h"

#include <libsoup/soup.h>
#include <iostream>

/* declaration of XCAP::CoreImpl */

class XCAP::CoreImpl
{
public:

  CoreImpl ();

  ~CoreImpl ();

  void read (boost::shared_ptr<XCAP::Path> path,
	     boost::function2<void,bool,std::string> callback);
  void write (boost::shared_ptr<Path>,
	      const std::string content_type,
	      const std::string content,
	      boost::function1<void,std::string> callback);
  void erase (boost::shared_ptr<Path>,
	      boost::function1<void,std::string> callback);

  /* public to be used by C callbacks */

  /* The memory management is a little complex, here is how it works :
   *
   * There are two kinds of live SOUP sessions :
   * - the old sessions : we have to keep them around because we can't unref
   * a SOUP session from result_callback, so we need to keep it
   * around for later ;
   * - the pending ones : we keep them around to be able to cancel them if
   * we destroy the core (and cancelling calls result_callback with an
   * error -- the sessions get pushed from pending to old) ;
   *
   * So the problem of freeing the SOUP sessions becomes one is reduced to
   * that of freeing the old sessions :
   * - in the read method, we check this old_sessions list and unref any old
   * session we find : ok ;
   * - in the destructor, we check the old_sesssions list and unref any old
   * session we find.
   *
   * That means if you launch a thousand reads fast then nothing,
   * we will end up with a thousand SOUP sessions in old_sessions. But any
   * subsequent read will clear them all.
   *
   * If you launch a thousand reads not fast enough, the last reads will
   * already find the first sessions and get rid of them.
   *
   * In short this code guarantees :
   * - the number of old sessions can't really grow if we have reads from time
   * to time  ;
   * - all the sessions will get freed eventually -- no leak!
   */
  std::list<SoupSession*> pending_sessions;
  std::list<SoupSession*> old_sessions;
  void clear_old_sessions ();
};

/* soup callbacks */

struct cb_read_data
{
  XCAP::CoreImpl* core;
  boost::shared_ptr<XCAP::Path> path;
  boost::function2<void, bool, std::string> callback;
};

struct cb_other_data
{
  XCAP::CoreImpl* core;
  boost::shared_ptr<XCAP::Path> path;
  boost::function1<void, std::string> callback;
};

static void
authenticate_read_callback (G_GNUC_UNUSED SoupSession* session,
			    G_GNUC_UNUSED SoupMessage* message,
			    SoupAuth* auth,
			    gboolean retrying,
			    gpointer data)
{
  cb_read_data* cb = (cb_read_data*)data;

  if ( !retrying) {

    soup_auth_authenticate (auth,
			    cb->path->get_username ().c_str (),
			    cb->path->get_password ().c_str ());
  }
}

static void
authenticate_other_callback (G_GNUC_UNUSED SoupSession* session,
			     G_GNUC_UNUSED SoupMessage* message,
			     SoupAuth* auth,
			     gboolean retrying,
			     gpointer data)
{
  cb_other_data* cb = (cb_other_data*)data;

  if ( !retrying) {

    soup_auth_authenticate (auth,
			    cb->path->get_username ().c_str (),
			    cb->path->get_password ().c_str ());
  }
}

static void
result_read_callback (SoupSession* session,
		      SoupMessage* message,
		      gpointer data)
{
  cb_read_data* cb = (cb_read_data*)data;

  if (message->status_code == SOUP_STATUS_OK) {

    cb->callback (false, message->response_body->data);
  } else {

    cb->callback (true, message->reason_phrase);
  }

  cb->core->pending_sessions.remove (session);
  cb->core->old_sessions.push_back (session);

  delete cb;
}

static void
result_other_callback (SoupSession* session,
		       SoupMessage* message,
		       gpointer data)
{
  cb_other_data* cb = (cb_other_data*)data;

  if (message->status_code == SOUP_STATUS_OK) {

    cb->callback ("");
  } else {

    cb->callback (message->reason_phrase);
  }

  cb->core->pending_sessions.remove (session);
  cb->core->old_sessions.push_back (session);

  delete cb;
}

/* implementation of XCAP::CoreImpl */

XCAP::CoreImpl::CoreImpl ()
{
}

XCAP::CoreImpl::~CoreImpl ()
{
  /* we loop like this because aborting calls result_callback, and hence
   * makes the current iterator invalid : it gets pushed to the old sessions
   * list!
   */
  while ( !pending_sessions.empty ())
    soup_session_abort (*pending_sessions.begin ());

  /* now all pending sessions have been made old, so we can do that: */
  clear_old_sessions ();
}

void
XCAP::CoreImpl::clear_old_sessions ()
{
  for (std::list<SoupSession*>::iterator iter = old_sessions.begin ();
       iter != old_sessions.end ();
       ++iter) {

    soup_session_abort (*iter); // needed ?
    g_object_unref (*iter);
  }
  old_sessions.clear ();
}

void
XCAP::CoreImpl::read (boost::shared_ptr<Path> path,
		      boost::function2<void, bool, std::string> callback)
{
  SoupSession* session = NULL;
  SoupMessage* message = NULL;
  cb_read_data* data = NULL;

  clear_old_sessions ();

  /* all of this is freed in the result callback */
  session = soup_session_async_new_with_options ("user-agent", "ekiga", NULL);
  message = soup_message_new ("GET", path->to_uri ().c_str ());
  data = new cb_read_data;
  data->core = this;
  data->path = path;
  data->callback = callback;

  g_signal_connect (session, "authenticate",
		    G_CALLBACK (authenticate_read_callback), data);

  soup_session_queue_message (session, message,
			      result_read_callback, data);

  pending_sessions.push_back (session);
}

void
XCAP::CoreImpl::write (boost::shared_ptr<Path> path,
		       const std::string content_type,
		       const std::string content,
		       boost::function1<void,std::string> callback)
{
  SoupSession* session = NULL;
  SoupMessage* message = NULL;
  cb_other_data* data = NULL;

  clear_old_sessions ();

  /* all of this is freed in the result callback */
  session = soup_session_async_new_with_options ("user-agent", "ekiga", NULL);
  message = soup_message_new ("PUT", path->to_uri ().c_str ());
  soup_message_set_request (message, content_type.c_str (),
			    SOUP_MEMORY_COPY,
			    content.c_str (), content.length ());

  data = new cb_other_data;
  data->core = this;
  data->path = path;
  data->callback = callback;

  g_signal_connect (session, "authenticate",
		    G_CALLBACK (authenticate_other_callback), data);

  soup_session_queue_message (session, message,
			      result_other_callback, data);

  pending_sessions.push_back (session);
}

void
XCAP::CoreImpl::erase (boost::shared_ptr<Path> path,
		       boost::function1<void,std::string> callback)
{
  SoupSession* session = NULL;
  SoupMessage* message = NULL;
  cb_other_data* data = NULL;

  clear_old_sessions ();

  /* all of this is freed in the result callback */
  session = soup_session_async_new_with_options ("user-agent", "ekiga", NULL);
  message = soup_message_new ("DELETE", path->to_uri ().c_str ());
  data = new cb_other_data;
  data->core = this;
  data->path = path;
  data->callback = callback;

  g_signal_connect (session, "authenticate",
		    G_CALLBACK (authenticate_other_callback), data);

  soup_session_queue_message (session, message,
			      result_other_callback, data);

  pending_sessions.push_back (session);
}


/* XCAP::Core just pimples : */
XCAP::Core::Core ()
{
  impl = new CoreImpl ();
}

XCAP::Core::~Core ()
{
  delete impl;
}

void
XCAP::Core::read (boost::shared_ptr<XCAP::Path> path,
		  boost::function2<void, bool,std::string> callback)
{
  impl->read (path, callback);
}

void
XCAP::Core::write (boost::shared_ptr<Path> path,
		   const std::string content_type,
		   const std::string content,
		   boost::function1<void,std::string> callback)
{
  impl->write (path, content_type, content, callback);
}

void
XCAP::Core::erase (boost::shared_ptr<Path> path,
		   boost::function1<void,std::string> callback)
{
  impl->erase (path, callback);
}