File: avahidiscovery.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (149 lines) | stat: -rw-r--r-- 4,212 bytes parent folder | download
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
/*
 *Copyright (C) <2017>  Alex B
 *
 *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, see <http://www.gnu.org/licenses/>
 */

#include "avahidiscovery.h"
#include <avahi-common/error.h>

const AvahiPoll* getAvahiPoll(void);

#include <QDebug>
static bool debugIsEnabled = false;
#define DBUG \
	if (debugIsEnabled) qWarning() << "Avahi" << __FUNCTION__

void AvahiDiscovery::enableDebug()
{
	debugIsEnabled = true;
}

static AvahiDiscovery* ptr = nullptr;

static void resolverCallback(
		AvahiServiceResolver* r,
		AVAHI_GCC_UNUSED AvahiIfIndex interface,
		AVAHI_GCC_UNUSED AvahiProtocol protocol,
		AvahiResolverEvent event,
		const char* name,
		AVAHI_GCC_UNUSED const char* type,
		AVAHI_GCC_UNUSED const char* domain,
		AVAHI_GCC_UNUSED const char* hostName,
		const AvahiAddress* address,
		uint16_t port,
		AVAHI_GCC_UNUSED AvahiStringList* txt,
		AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
		void* userdata)
{
	char a[AVAHI_ADDRESS_STR_MAX];

	switch (event) {
	case AVAHI_RESOLVER_FAILURE:
		//avahi resolver not found
		break;
	case AVAHI_RESOLVER_FOUND:
		avahi_address_snprint(a, sizeof(a), address);
		break;
	}

	int eventType = *(static_cast<int*>(userdata));
	switch (eventType) {
	case AVAHI_BROWSER_NEW:
		emit ptr->mpdFound(name, a, port);
		break;
	case AVAHI_BROWSER_REMOVE:
		emit ptr->mpdRemoved(name);
		break;
	}

	avahi_service_resolver_free(r);
	free(userdata);
}

static void browseCallback(
		AvahiServiceBrowser* b,
		AvahiIfIndex interface,
		AvahiProtocol protocol,
		AvahiBrowserEvent event,
		const char* name,
		const char* type,
		const char* domain,
		AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
		AVAHI_GCC_UNUSED void* userdata)
{
	switch (event) {
	case AVAHI_BROWSER_FAILURE:
		DBUG << "(Browser) " << avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b)));
		break;
	case AVAHI_BROWSER_NEW:
		if (!(avahi_service_resolver_new(avahi_service_browser_get_client(b), interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_MULTICAST, resolverCallback, new int(AVAHI_BROWSER_NEW)))) {
			DBUG << "Failed to resolve service " << name;
		}
		break;
	case AVAHI_BROWSER_REMOVE:
		if (!(avahi_service_resolver_new(avahi_service_browser_get_client(b), interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_MULTICAST, resolverCallback, new int(AVAHI_BROWSER_REMOVE)))) {
			DBUG << "Failed to resolve service " << name;
		}
		break;
	case AVAHI_BROWSER_ALL_FOR_NOW:
		DBUG << "Service browser: ALL_FOR_NOW";
		break;
	case AVAHI_BROWSER_CACHE_EXHAUSTED:
		DBUG << "Service browser: CACHE_EXHAUSTED";
		break;
	}
}

static void clientCallback(AvahiClient* c, AvahiClientState state, AVAHI_GCC_UNUSED void* userdata)
{
	assert(c);

	if (AVAHI_CLIENT_FAILURE == state) {
		DBUG << "Server connection failure: " << avahi_strerror(avahi_client_errno(c));
	}
}

AvahiDiscovery::AvahiDiscovery()
{
	ptr = this;

	/* create avahi client */
	int error;
	m_client = avahi_client_new(getAvahiPoll(), AVAHI_CLIENT_IGNORE_USER_CONFIG, clientCallback, nullptr, &error);
	if (!m_client) {
		DBUG << "Failed to create client: " << avahi_strerror(error);
		return;
	}

	/* create avahi browser */
	m_browser = avahi_service_browser_new(m_client, AVAHI_IF_UNSPEC, AVAHI_PROTO_INET, "_mpd._tcp", nullptr, AVAHI_LOOKUP_USE_MULTICAST, browseCallback, m_client);
	if (!m_browser) {
		DBUG << "Failed to create service browser: " << avahi_strerror(avahi_client_errno(m_client));
		return;
	}
}

AvahiDiscovery::~AvahiDiscovery()
{
	if (m_browser) {
		avahi_service_browser_free(m_browser);
	}

	if (m_client) {
		avahi_client_free(m_client);
	}
}

#include "moc_avahidiscovery.cpp"