2
/* Ekiga -- A VoIP and Video-Conferencing application
3
* Copyright (C) 2000-2009 Damien Sandras <dsandras@seconix.com>
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.
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.
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.
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.
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
39
#include <glib/gi18n.h>
41
#include "avahi-heap.h"
46
avahi_client_callback (AvahiClient *client,
47
AvahiClientState state,
50
((Avahi::Heap *)data)->ClientCallback (client, state);
54
avahi_browser_callback (AvahiServiceBrowser *browser,
55
AvahiIfIndex interface,
56
AvahiProtocol protocol,
57
AvahiBrowserEvent event,
61
AvahiLookupResultFlags flags,
64
((Avahi::Heap *)data)->BrowserCallback (browser, interface, protocol,
65
event, name, type, domain, flags);
70
avahi_resolver_callback (AvahiServiceResolver *resolver,
71
AvahiIfIndex interface,
72
AvahiProtocol protocol,
73
AvahiResolverEvent event,
77
const char *host_name,
78
const AvahiAddress *address,
81
AvahiLookupResultFlags flags,
84
((Avahi::Heap *)data)->ResolverCallback (resolver, interface, protocol,
85
event, name, type, domain,
86
host_name, address, port,
91
Avahi::Heap::Heap (Ekiga::ServiceCore &_core): core(_core)
93
const AvahiPoll *poll_api = NULL;
96
/* let's make sure those are sanely initialized */
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);
104
/* this may not be the final valid client pointer according to
105
* avahi's documentation... we'll take what our callback gets
107
client = avahi_client_new (poll_api, (AvahiClientFlags)AVAHI_CLIENT_NO_FAIL,
108
avahi_client_callback, this,
112
std::cout << __PRETTY_FUNCTION__ << " client is NULL!" << std::endl;
116
Avahi::Heap::~Heap ()
119
avahi_client_free (client);
122
avahi_glib_poll_free (poll);
127
Avahi::Heap::get_name () const
129
return _("Neighbours");
133
Avahi::Heap::populate_menu (Ekiga::MenuBuilder& /*builder*/)
139
Avahi::Heap::populate_menu_for_group (const std::string /*name*/,
140
Ekiga::MenuBuilder& /*builder*/)
146
Avahi::Heap::ClientCallback (AvahiClient *_client,
147
AvahiClientState state)
149
/* this is the good client pointer */
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?
158
std::cout << __PRETTY_FUNCTION__ << " AVAHI_CLIENT_FAILURE" << std::endl;
161
avahi_client_free (client);
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
168
avahi_service_browser_new (client,
173
avahi_browser_callback,
176
std::cout << __PRETTY_FUNCTION__ << " AVAHI_CLIENT_S_RUNNING" << std::endl;
178
std::cout << "but NULL browser!" << std::endl;
181
case AVAHI_CLIENT_CONNECTING:
182
case AVAHI_CLIENT_S_REGISTERING:
183
case AVAHI_CLIENT_S_COLLISION:
186
std::cout << __PRETTY_FUNCTION__ << " OTHER" << std::endl;
191
std::cout << __PRETTY_FUNCTION__ << " SHOULDN'T HAPPEN" << std::endl;
193
/* shouldn't happen */
199
Avahi::Heap::BrowserCallback (AvahiServiceBrowser *browser,
200
AvahiIfIndex interface,
201
AvahiProtocol protocol,
202
AvahiBrowserEvent event,
206
AvahiLookupResultFlags /*flags*/)
208
AvahiServiceResolver *resolver = NULL;
212
case AVAHI_BROWSER_NEW:
213
/* this may not be the final valid resolver pointer...
214
* we'll take what our callback gets
216
resolver = avahi_service_resolver_new (client, interface, protocol,
220
avahi_resolver_callback, this);
222
std::cout << __PRETTY_FUNCTION__ << " AVAHI_BROWSER_NEW" << std::endl;
224
if (resolver == NULL)
225
std::cout << "resolver is NULL!" << std::endl;
228
case AVAHI_BROWSER_REMOVE:
230
std::cout << __PRETTY_FUNCTION__ << " AVAHI_BROWSER_REMOVE" << std::endl;
232
for (iterator iter = begin ();
235
if ((*iter)->get_name () == name) {
240
case AVAHI_BROWSER_CACHE_EXHAUSTED:
243
std::cout << __PRETTY_FUNCTION__ << " AVAHI_BROWSER_CACHE_EXHAUSTED" << std::endl;
246
case AVAHI_BROWSER_ALL_FOR_NOW:
249
std::cout << __PRETTY_FUNCTION__ << " AVAHI_BROWSER_ALL_FOR_NOW" << std::endl;
252
case AVAHI_BROWSER_FAILURE:
254
std::cout << __PRETTY_FUNCTION__ << " AVAHI_BROWSER_FAILURE" << std::endl;
256
avahi_service_browser_free (browser);
260
/* shouldn't happen */
262
std::cout << __PRETTY_FUNCTION__ << " SHOULDN'T HAPPEN" << std::endl;
268
class resolver_callback_helper
272
resolver_callback_helper (const std::string name_): name(name_)
275
bool operator() (Ekiga::PresentityPtr pres_)
277
boost::shared_ptr<Ekiga::URIPresentity> presentity_ = boost::dynamic_pointer_cast<Ekiga::URIPresentity> (pres_);
280
if (presentity_ && presentity_->get_name () == name) {
282
presentity = presentity_;
288
boost::shared_ptr<Ekiga::URIPresentity> found_presentity () const
289
{ return presentity; }
292
boost::shared_ptr<Ekiga::URIPresentity> presentity;
293
const std::string name;
297
Avahi::Heap::ResolverCallback (AvahiServiceResolver *resolver,
298
AvahiIfIndex /*interface*/,
299
AvahiProtocol /*protocol*/,
300
AvahiResolverEvent event,
303
const char * /*domain*/,
304
const char* host_name,
305
const AvahiAddress */*address*/,
307
AvahiStringList *txt,
308
AvahiLookupResultFlags flags)
311
std::string software;
312
std::string presence;
315
AvahiStringList *txt_tmp = NULL;
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) {
321
avahi_service_resolver_free (resolver);
323
std::cout << __PRETTY_FUNCTION__ << " LOCAL RESULT" << std::endl;
330
case AVAHI_RESOLVER_FOUND: {
332
std::cout << __PRETTY_FUNCTION__ << " AVAHI_RESOLVER_FOUND" << std::endl;
336
for (txt_tmp = txt; txt_tmp != NULL; txt_tmp = txt_tmp->next) {
341
if (avahi_string_list_get_pair (txt_tmp, &ckey, &cvalue, &size) >= 0) {
343
if (ckey != NULL && cvalue != NULL) {
345
std::string key (ckey);
346
std::string value (cvalue);
347
if (key == "presence")
349
else if (key == "status")
351
else if (key == "software")
354
if (ckey != NULL) free (ckey);
355
if (cvalue != NULL) free (cvalue);
359
resolver_callback_helper helper(name);
360
visit_presentities (boost::ref (helper));
361
if (helper.found_presentity ()) {
363
/* known contact has been updated */
364
presence_received (helper.found_presentity ()->get_uri (), presence);
365
status_received (helper.found_presentity ()->get_uri (), status);
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) {
373
std::set<std::string> groups;
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);
385
avahi_service_resolver_free (resolver);
387
case AVAHI_RESOLVER_FAILURE:
390
std::cout << __PRETTY_FUNCTION__ << " AVAHI_RESOLVER_FAILURE" << std::endl;
392
avahi_service_resolver_free (resolver);
395
/* shouldn't happen */
397
std::cout << __PRETTY_FUNCTION__ << " SHOULDN'T HAPPEN" << std::endl;