~ubuntu-branches/ubuntu/vivid/ekiga/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/engine/components/avahi/avahi-heap.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kilian Krause
  • Date: 2011-07-17 00:24:50 UTC
  • mfrom: (5.1.5 upstream) (7.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20110717002450-ytg3wsrc1ptd3153
Tags: 3.3.1-1
* New upstream release.
 - Required libpt-dev 2.10 and libopal-dev 3.10
* Fix debian/watch to catch new version
* Remove libnotify0.7.patch - included upstream
* Add libboost-dev and libboost-signals-dev to Build-Depends
* debian/rules: Don't install *.la files for new internal shared libs
* Fix Vcs URIs to point to correct desktop/experimental/ekiga tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* Ekiga -- A VoIP and Video-Conferencing application
 
3
 * Copyright (C) 2000-2009 Damien Sandras <dsandras@seconix.com>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software Foundation,
 
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
18
 *
 
19
 *
 
20
 * Ekiga is licensed under the GPL license and as a special exception,
 
21
 * you have permission to link or otherwise combine this program with the
 
22
 * programs OPAL, OpenH323 and PWLIB, and distribute the combination,
 
23
 * without applying the requirements of the GNU GPL to the OPAL, OpenH323
 
24
 * and PWLIB programs, as long as you do follow the requirements of the
 
25
 * GNU GPL for all the rest of the software thus combined.
 
26
 */
 
27
 
 
28
 
 
29
/*
 
30
 *                         avahi-heap.cpp  -  description
 
31
 *                         ------------------------------------------
 
32
 *   begin                : written in 2007 by Julien Puydt
 
33
 *   copyright            : (c) 2007 by Julien Puydt
 
34
 *   description          : implementation for the avahi heap
 
35
 *
 
36
 */
 
37
 
 
38
#include <cstdlib>
 
39
#include <glib/gi18n.h>
 
40
 
 
41
#include "avahi-heap.h"
 
42
 
 
43
#define DEBUG 0
 
44
 
 
45
static void
 
46
avahi_client_callback (AvahiClient *client,
 
47
                       AvahiClientState state,
 
48
                       void *data)
 
49
{
 
50
  ((Avahi::Heap *)data)->ClientCallback (client, state);
 
51
}
 
52
 
 
53
static void
 
54
avahi_browser_callback (AvahiServiceBrowser *browser,
 
55
                        AvahiIfIndex interface,
 
56
                        AvahiProtocol protocol,
 
57
                        AvahiBrowserEvent event,
 
58
                        const char *name,
 
59
                        const char *type,
 
60
                        const char *domain,
 
61
                        AvahiLookupResultFlags flags,
 
62
                        void *data)
 
63
{
 
64
  ((Avahi::Heap *)data)->BrowserCallback (browser, interface, protocol,
 
65
                                          event, name, type, domain, flags);
 
66
}
 
67
 
 
68
 
 
69
static void
 
70
avahi_resolver_callback (AvahiServiceResolver *resolver,
 
71
                         AvahiIfIndex interface,
 
72
                         AvahiProtocol protocol,
 
73
                         AvahiResolverEvent event,
 
74
                         const char *name,
 
75
                         const char *type,
 
76
                         const char *domain,
 
77
                         const char *host_name,
 
78
                         const AvahiAddress *address,
 
79
                         uint16_t port,
 
80
                         AvahiStringList *txt,
 
81
                         AvahiLookupResultFlags flags,
 
82
                         void *data)
 
83
{
 
84
  ((Avahi::Heap *)data)->ResolverCallback (resolver, interface, protocol,
 
85
                                           event, name, type, domain,
 
86
                                           host_name, address, port,
 
87
                                           txt, flags);
 
88
}
 
89
 
 
90
 
 
91
Avahi::Heap::Heap (Ekiga::ServiceCore &_core): core(_core)
 
92
{
 
93
  const AvahiPoll *poll_api = NULL;
 
94
  int error;
 
95
 
 
96
  /* let's make sure those are sanely initialized */
 
97
  poll = NULL;
 
98
  client = NULL;
 
99
 
 
100
  avahi_set_allocator (avahi_glib_allocator ());
 
101
  poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);
 
102
  poll_api = avahi_glib_poll_get (poll);
 
103
 
 
104
  /* this may not be the final valid client pointer according to
 
105
   * avahi's documentation... we'll take what our callback gets
 
106
   */
 
107
  client = avahi_client_new (poll_api, (AvahiClientFlags)AVAHI_CLIENT_NO_FAIL,
 
108
                             avahi_client_callback, this,
 
109
                             &error);
 
110
#if DEBUG
 
111
  if (client == NULL)
 
112
    std::cout << __PRETTY_FUNCTION__ << " client is NULL!" << std::endl;
 
113
#endif
 
114
}
 
115
 
 
116
Avahi::Heap::~Heap ()
 
117
{
 
118
  if (client != NULL)
 
119
    avahi_client_free (client);
 
120
 
 
121
  if (poll != NULL)
 
122
    avahi_glib_poll_free (poll);
 
123
}
 
124
 
 
125
 
 
126
const std::string
 
127
Avahi::Heap::get_name () const
 
128
{
 
129
  return _("Neighbours");
 
130
}
 
131
 
 
132
bool
 
133
Avahi::Heap::populate_menu (Ekiga::MenuBuilder& /*builder*/)
 
134
{
 
135
  return false;
 
136
}
 
137
 
 
138
bool
 
139
Avahi::Heap::populate_menu_for_group (const std::string /*name*/,
 
140
                                      Ekiga::MenuBuilder& /*builder*/)
 
141
{
 
142
  return false;
 
143
}
 
144
 
 
145
void
 
146
Avahi::Heap::ClientCallback (AvahiClient *_client,
 
147
                             AvahiClientState state)
 
148
{
 
149
  /* this is the good client pointer */
 
150
  client = _client;
 
151
 
 
152
  switch (state) {
 
153
  case AVAHI_CLIENT_FAILURE:
 
154
    /* bad, bad: free the client and try to get another one... but
 
155
     * won't I tax the box?
 
156
     */
 
157
#if DEBUG
 
158
    std::cout << __PRETTY_FUNCTION__ << " AVAHI_CLIENT_FAILURE" << std::endl;
 
159
#endif
 
160
    if (client != NULL)
 
161
      avahi_client_free (client);
 
162
    client = NULL;
 
163
    break;
 
164
  case AVAHI_CLIENT_S_RUNNING:
 
165
    /* ignore what we get from the new, as it may not be the final
 
166
     * valid browser pointer... we'll take what our callback gets
 
167
     */
 
168
    avahi_service_browser_new (client,
 
169
                               AVAHI_IF_UNSPEC,
 
170
                               AVAHI_PROTO_UNSPEC,
 
171
                               "_sip._udp", NULL,
 
172
                               (AvahiLookupFlags)0,
 
173
                               avahi_browser_callback,
 
174
                               this);
 
175
#if DEBUG
 
176
    std::cout << __PRETTY_FUNCTION__ << " AVAHI_CLIENT_S_RUNNING" << std::endl;
 
177
    if (browser == NULL)
 
178
      std::cout << "but NULL browser!" << std::endl;
 
179
#endif
 
180
    break;
 
181
  case AVAHI_CLIENT_CONNECTING:
 
182
  case AVAHI_CLIENT_S_REGISTERING:
 
183
  case AVAHI_CLIENT_S_COLLISION:
 
184
    /* do nothing */
 
185
#if DEBUG
 
186
    std::cout << __PRETTY_FUNCTION__ << " OTHER" << std::endl;
 
187
#endif
 
188
    break;
 
189
  default:
 
190
#if DEBUG
 
191
    std::cout << __PRETTY_FUNCTION__ << " SHOULDN'T HAPPEN" << std::endl;
 
192
#endif
 
193
    /* shouldn't happen */
 
194
    break;
 
195
  }
 
196
}
 
197
 
 
198
void
 
199
Avahi::Heap::BrowserCallback (AvahiServiceBrowser *browser,
 
200
                              AvahiIfIndex interface,
 
201
                              AvahiProtocol protocol,
 
202
                              AvahiBrowserEvent event,
 
203
                              const char *name,
 
204
                              const char *type,
 
205
                              const char *domain,
 
206
                              AvahiLookupResultFlags /*flags*/)
 
207
{
 
208
  AvahiServiceResolver *resolver = NULL;
 
209
 
 
210
  switch (event) {
 
211
 
 
212
  case AVAHI_BROWSER_NEW:
 
213
    /* this may not be the final valid resolver pointer...
 
214
     * we'll take what our callback gets
 
215
     */
 
216
    resolver = avahi_service_resolver_new (client, interface, protocol,
 
217
                                           name, type, domain,
 
218
                                           AVAHI_PROTO_UNSPEC,
 
219
                                           (AvahiLookupFlags)0,
 
220
                                           avahi_resolver_callback, this);
 
221
#if DEBUG
 
222
    std::cout << __PRETTY_FUNCTION__ << " AVAHI_BROWSER_NEW" << std::endl;
 
223
#endif
 
224
    if (resolver == NULL)
 
225
      std::cout << "resolver is NULL!" << std::endl;
 
226
    break;
 
227
 
 
228
  case AVAHI_BROWSER_REMOVE:
 
229
#if DEBUG
 
230
    std::cout << __PRETTY_FUNCTION__ << " AVAHI_BROWSER_REMOVE" << std::endl;
 
231
#endif
 
232
    for (iterator iter = begin ();
 
233
         iter != end ();
 
234
         ++iter)
 
235
      if ((*iter)->get_name () == name) {
 
236
        (*iter)->removed ();
 
237
        break;
 
238
      }
 
239
    break;
 
240
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
 
241
    // FIXME: do I care?
 
242
#if DEBUG
 
243
    std::cout << __PRETTY_FUNCTION__ << " AVAHI_BROWSER_CACHE_EXHAUSTED" << std::endl;
 
244
#endif
 
245
    break;
 
246
  case AVAHI_BROWSER_ALL_FOR_NOW:
 
247
    // FIXME: do I care?
 
248
#if DEBUG
 
249
    std::cout << __PRETTY_FUNCTION__ << " AVAHI_BROWSER_ALL_FOR_NOW" << std::endl;
 
250
#endif
 
251
    break;
 
252
  case AVAHI_BROWSER_FAILURE:
 
253
#if DEBUG
 
254
    std::cout << __PRETTY_FUNCTION__ << " AVAHI_BROWSER_FAILURE" << std::endl;
 
255
#endif
 
256
    avahi_service_browser_free (browser);
 
257
    browser = NULL;
 
258
    break;
 
259
  default:
 
260
    /* shouldn't happen */
 
261
#if DEBUG
 
262
    std::cout << __PRETTY_FUNCTION__ << " SHOULDN'T HAPPEN" << std::endl;
 
263
#endif
 
264
    break;
 
265
  }
 
266
}
 
267
 
 
268
class resolver_callback_helper
 
269
{
 
270
public:
 
271
 
 
272
  resolver_callback_helper (const std::string name_): name(name_)
 
273
  {}
 
274
 
 
275
  bool operator() (Ekiga::PresentityPtr pres_)
 
276
  {
 
277
    boost::shared_ptr<Ekiga::URIPresentity> presentity_ = boost::dynamic_pointer_cast<Ekiga::URIPresentity> (pres_);
 
278
    bool result = true;
 
279
 
 
280
    if (presentity_ && presentity_->get_name () == name) {
 
281
 
 
282
      presentity = presentity_;
 
283
      result = false;
 
284
    }
 
285
    return result;
 
286
  }
 
287
 
 
288
  boost::shared_ptr<Ekiga::URIPresentity> found_presentity () const
 
289
  { return presentity; }
 
290
 
 
291
private:
 
292
  boost::shared_ptr<Ekiga::URIPresentity> presentity;
 
293
  const std::string name;
 
294
};
 
295
 
 
296
void
 
297
Avahi::Heap::ResolverCallback (AvahiServiceResolver *resolver,
 
298
                               AvahiIfIndex /*interface*/,
 
299
                               AvahiProtocol /*protocol*/,
 
300
                               AvahiResolverEvent event,
 
301
                               const char * name_,
 
302
                               const char * typ,
 
303
                               const char * /*domain*/,
 
304
                               const char* host_name,
 
305
                               const AvahiAddress */*address*/,
 
306
                               uint16_t port,
 
307
                               AvahiStringList *txt,
 
308
                               AvahiLookupResultFlags flags)
 
309
{
 
310
  std::string name;
 
311
  std::string software;
 
312
  std::string presence;
 
313
  std::string status;
 
314
  gchar *url = NULL;
 
315
  AvahiStringList *txt_tmp = NULL;
 
316
 
 
317
  // filter out seeing ourselves
 
318
  // FIXME: doesn't it hide other people on the same box too?
 
319
  if (flags & AVAHI_LOOKUP_RESULT_LOCAL) {
 
320
 
 
321
    avahi_service_resolver_free (resolver);
 
322
#if DEBUG
 
323
    std::cout << __PRETTY_FUNCTION__ << " LOCAL RESULT" << std::endl;
 
324
#endif
 
325
    return;
 
326
  }
 
327
 
 
328
  switch (event) {
 
329
 
 
330
  case AVAHI_RESOLVER_FOUND: {
 
331
#if DEBUG
 
332
    std::cout << __PRETTY_FUNCTION__ << " AVAHI_RESOLVER_FOUND" << std::endl;
 
333
#endif
 
334
 
 
335
    name = name_;
 
336
    for (txt_tmp = txt;  txt_tmp != NULL; txt_tmp = txt_tmp->next) {
 
337
 
 
338
      char *ckey = NULL;
 
339
      char *cvalue = NULL;
 
340
      size_t size;
 
341
      if (avahi_string_list_get_pair (txt_tmp, &ckey, &cvalue, &size) >= 0) {
 
342
 
 
343
        if (ckey != NULL && cvalue != NULL) {
 
344
 
 
345
          std::string key (ckey);
 
346
          std::string value (cvalue);
 
347
          if (key == "presence")
 
348
            presence = value;
 
349
          else if (key == "status")
 
350
            status = value;
 
351
          else if (key == "software")
 
352
            software = value;
 
353
        }
 
354
        if (ckey != NULL) free (ckey);
 
355
        if (cvalue != NULL) free (cvalue);
 
356
      }
 
357
    }
 
358
 
 
359
    resolver_callback_helper helper(name);
 
360
    visit_presentities (boost::ref (helper));
 
361
    if (helper.found_presentity ()) {
 
362
 
 
363
      /* known contact has been updated */
 
364
      presence_received (helper.found_presentity ()->get_uri (), presence);
 
365
      status_received (helper.found_presentity ()->get_uri (), status);
 
366
    } else {
 
367
 
 
368
      /* ok, this is a new contact */
 
369
      gchar** broken = NULL;
 
370
      broken = g_strsplit_set (typ, "._", 0);
 
371
      if (broken != NULL && broken[0] != NULL && broken[1] != NULL) {
 
372
 
 
373
        std::set<std::string> groups;
 
374
 
 
375
        groups.insert (_("Neighbours"));
 
376
        url = g_strdup_printf ("%s:neighbour@%s:%d", broken[1], host_name, port);
 
377
        boost::shared_ptr<Ekiga::URIPresentity> presentity (new Ekiga::URIPresentity (core, name, url, groups));
 
378
        status_received (url, status);
 
379
        presence_received (url, presence);
 
380
        add_presentity (presentity);
 
381
        g_free (url);
 
382
      }
 
383
      g_strfreev (broken);
 
384
    }
 
385
    avahi_service_resolver_free (resolver);
 
386
    break;}
 
387
  case AVAHI_RESOLVER_FAILURE:
 
388
 
 
389
#if DEBUG
 
390
    std::cout << __PRETTY_FUNCTION__ << " AVAHI_RESOLVER_FAILURE" << std::endl;
 
391
#endif
 
392
    avahi_service_resolver_free (resolver);
 
393
    break;
 
394
  default:
 
395
    /* shouldn't happen */
 
396
#if DEBUG
 
397
    std::cout << __PRETTY_FUNCTION__ << " SHOULDN'T HAPPEN" << std::endl;
 
398
#endif
 
399
    break;
 
400
  }
 
401
}