-
Notifications
You must be signed in to change notification settings - Fork 51
/
androidnsd.cpp
367 lines (320 loc) · 13.7 KB
/
androidnsd.cpp
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
/**************************************************************************************************
---------------------------------------------------------------------------------------------------
Copyright (C) 2015-2021 Jonathan Bagg
This file is part of QtZeroConf.
QtZeroConf 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 3 of the License, or
(at your option) any later version.
QtZeroConf 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 QtZeroConf. If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------------------------------
Project name : QtZeroConf
File name : androidnsd.cpp
Created : 10 Spetember 2021
Author(s) : Michael Zanetti
---------------------------------------------------------------------------------------------------
NsdManager wrapper for use on Android devices
---------------------------------------------------------------------------------------------------
**************************************************************************************************/
#include <QGuiApplication>
#include <QRegularExpression>
#include "androidnsd_p.h"
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
using QAndroidJniEnvironment = QJniEnvironment;
#endif
Q_DECLARE_METATYPE(QHostAddress)
static QMutex s_instancesMutex;
static QList<QZeroConfPrivate*> s_instances;
QZeroConfPrivate::QZeroConfPrivate(QZeroConf *parent)
{
qRegisterMetaType<QHostAddress>();
qRegisterMetaType<TxtRecordMap>("TxtRecordMap");
pub = parent;
QAndroidJniEnvironment env;
JNINativeMethod methods[] {
{ "onServiceResolvedJNI", "(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/util/Map;)V", (void*)QZeroConfPrivate::onServiceResolvedJNI },
{ "onServiceRemovedJNI", "(JLjava/lang/String;)V", (void*)QZeroConfPrivate::onServiceRemovedJNI },
{ "onBrowserStateChangedJNI", "(JZZ)V", (void*)QZeroConfPrivate::onBrowserStateChangedJNI },
{ "onPublisherStateChangedJNI", "(JZZ)V", (void*)QZeroConfPrivate::onPublisherStateChangedJNI },
{ "onServiceNameChangedJNI", "(JLjava/lang/String;)V", (void*)QZeroConfPrivate::onServiceNameChangedJNI }
};
// There seems to be no straight forward way to match the "thiz" pointer from JNI calls to our pointer of the Java class
// Passing "this" as ID down to Java so we can access "this" in callbacks.
// Note: needs to be quint64 as uintptr_t might be 32 or 64 bit depending on the system, while Java expects a jlong which is always 64 bit.
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
nsdManager = QAndroidJniObject("qtzeroconf/QZeroConfNsdManager", "(JLandroid/content/Context;)V", reinterpret_cast<quint64>(this), QtAndroid::androidActivity().object());
#elif (QT_VERSION < QT_VERSION_CHECK(6, 7, 0))
nsdManager = QAndroidJniObject("qtzeroconf/QZeroConfNsdManager", "(JLandroid/content/Context;)V", reinterpret_cast<quint64>(this), QNativeInterface::QAndroidApplication::context());
#else
// QNativeInterface does not provide source compatibility. https://bugreports.qt.io/browse/QTBUG-123900 https://forum.qt.io/topic/157493/binary-and-source-compatibility-breakage-in-6-7
nsdManager = QAndroidJniObject("qtzeroconf/QZeroConfNsdManager", "(JLandroid/content/Context;)V", reinterpret_cast<quint64>(this), QNativeInterface::QAndroidApplication::context().object<jobject>());
#endif
if (nsdManager.isValid()) {
jclass objectClass = env->GetObjectClass(nsdManager.object<jobject>());
env->RegisterNatives(objectClass, methods, sizeof(methods) / sizeof(methods[0]));
env->DeleteLocalRef(objectClass);
}
QMutexLocker locker(&s_instancesMutex);
s_instances.append(this);
}
QZeroConfPrivate::~QZeroConfPrivate()
{
QMutexLocker locker(&s_instancesMutex);
s_instances.removeAll(this);
}
// In order to not having to pay attention to only use thread safe methods on the java side, we're only running
// Java calls on the Android thread.
// To make sure the Java object is not going out of scope and being garbage collected when the QZeroConf object
// is deleted before the worker thread actually starts, keep a new QAndroidJniObject to nsdManager
// which will increase the ref counter in the JVM.
void QZeroConfPrivate::startServicePublish(const char *name, const char *type, quint16 port)
{
QAndroidJniObject ref(nsdManager);
publishName = name;
publishType = type;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtAndroid::runOnAndroidThread([=](){
#else
QNativeInterface::QAndroidApplication::runOnAndroidMainThread([=]() {
#endif
QAndroidJniObject txtMap("java/util/HashMap");
foreach (const QByteArray &key, txtRecords.keys()) {
txtMap.callObjectMethod("put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
QAndroidJniObject::fromString(key).object<jstring>(),
QAndroidJniObject::fromString(txtRecords.value(key)).object<jstring>());
}
ref.callMethod<void>("registerService", "(Ljava/lang/String;Ljava/lang/String;ILjava/util/Map;)V",
QAndroidJniObject::fromString(publishName).object<jstring>(),
QAndroidJniObject::fromString(publishType).object<jstring>(),
port,
txtMap.object());
});
}
void QZeroConfPrivate::stopServicePublish()
{
QAndroidJniObject ref(nsdManager);
// If Android is on it's way to suspend when stopServicePublish() is called, we need to call nsd.unregisterService() synchronously
// to force it to run before the device goes to sleep. If instead it is scheduled to run in the Android thread, it will not run
// until the device is woken back up.
if (qGuiApp->applicationState() == Qt::ApplicationSuspended) {
ref.callMethod<void>("unregisterService");
} else {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtAndroid::runOnAndroidThread([ref]() {
#else
QNativeInterface::QAndroidApplication::runOnAndroidMainThread([ref]() {
#endif
ref.callMethod<void>("unregisterService");
});
}
}
void QZeroConfPrivate::startBrowser(QString type, QAbstractSocket::NetworkLayerProtocol protocol)
{
Q_UNUSED(protocol)
QAndroidJniObject ref(nsdManager);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtAndroid::runOnAndroidThread([ref, type]() {
#else
QNativeInterface::QAndroidApplication::runOnAndroidMainThread([ref, type]() {
#endif
ref.callMethod<void>("discoverServices", "(Ljava/lang/String;)V", QAndroidJniObject::fromString(type).object<jstring>());
});
}
void QZeroConfPrivate::stopBrowser()
{
QAndroidJniObject ref(nsdManager);
// If Android is on it's way to suspend when stopBrowser() is called, we need to call nsd.stopServiceDiscovery() synchronously
// to force it to run before the device goes to sleep.
if (qGuiApp->applicationState() == Qt::ApplicationSuspended) {
ref.callMethod<void>("stopServiceDiscovery");
} else {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtAndroid::runOnAndroidThread([ref]() {
#else
QNativeInterface::QAndroidApplication::runOnAndroidMainThread([ref]() {
#endif
ref.callMethod<void>("stopServiceDiscovery");
});
}
}
// Callbacks will come in from the android thread. So we're never accessing any of our members directly but instead
// propagate callbacks through Qt::QueuedConnection invokes into the Qt thread. Be sure to check if the instance is still
// alive by checking s_instances while holding the mutex before scheduling the invokation.
void QZeroConfPrivate::onServiceResolvedJNI(JNIEnv */*env*/, jobject /*thiz*/, jlong id, jstring name, jstring type, jstring hostname, jstring address, jint port, jobject txtRecords)
{
QMap<QByteArray, QByteArray> txtMap;
QAndroidJniObject txt(txtRecords);
QAndroidJniObject txtKeys = txt.callObjectMethod("keySet", "()Ljava/util/Set;").callObjectMethod("toArray", "()[Ljava/lang/Object;");
QAndroidJniEnvironment env;
for (int i = 0; i < txt.callMethod<jint>("size"); i++) {
QAndroidJniObject key = QAndroidJniObject(env->GetObjectArrayElement(txtKeys.object<jobjectArray>(), i));
QAndroidJniObject valueObj = txt.callObjectMethod("get", "(Ljava/lang/Object;)Ljava/lang/Object;", key.object<jstring>());
if (valueObj.isValid()) {
jboolean isCopy;
jbyte* b = env->GetByteArrayElements(valueObj.object<jbyteArray>(), &isCopy);
QByteArray value((char *)b, env->GetArrayLength(valueObj.object<jbyteArray>()));
env->ReleaseByteArrayElements(valueObj.object<jbyteArray>(), b, JNI_ABORT);
txtMap.insert(key.toString().toUtf8(), value);
} else {
txtMap.insert(key.toString().toUtf8(), QByteArray());
}
}
QZeroConfPrivate *ref = reinterpret_cast<QZeroConfPrivate*>(id);
QMutexLocker locker(&s_instancesMutex);
if (!s_instances.contains(ref)) {
return;
}
QMetaObject::invokeMethod(ref, "onServiceResolved", Qt::QueuedConnection,
Q_ARG(QString, QAndroidJniObject(name).toString()),
Q_ARG(QString, QAndroidJniObject(type).toString()),
Q_ARG(QString, QAndroidJniObject(hostname).toString()),
Q_ARG(QHostAddress, QHostAddress(QAndroidJniObject(address).toString())),
Q_ARG(int, port),
Q_ARG(TxtRecordMap, txtMap)
);
}
void QZeroConfPrivate::onServiceRemovedJNI(JNIEnv */*env*/, jobject /*this*/, jlong id, jstring name)
{
QZeroConfPrivate *ref = reinterpret_cast<QZeroConfPrivate*>(id);
QMutexLocker locker(&s_instancesMutex);
if (!s_instances.contains(ref)) {
return;
}
QMetaObject::invokeMethod(ref, "onServiceRemoved", Qt::QueuedConnection, Q_ARG(QString, QAndroidJniObject(name).toString()));
}
void QZeroConfPrivate::onBrowserStateChangedJNI(JNIEnv */*env*/, jobject /*thiz*/, jlong id, jboolean running, jboolean error)
{
QZeroConfPrivate *ref = reinterpret_cast<QZeroConfPrivate*>(id);
QMutexLocker locker(&s_instancesMutex);
if (!s_instances.contains(ref)) {
return;
}
QMetaObject::invokeMethod(ref, "onBrowserStateChanged", Qt::QueuedConnection, Q_ARG(bool, running), Q_ARG(bool, error));
}
void QZeroConfPrivate::onPublisherStateChangedJNI(JNIEnv */*env*/, jobject /*this*/, jlong id, jboolean running, jboolean error)
{
QZeroConfPrivate *ref = reinterpret_cast<QZeroConfPrivate*>(id);
QMutexLocker locker(&s_instancesMutex);
if (!s_instances.contains(ref)) {
return;
}
QMetaObject::invokeMethod(ref, "onPublisherStateChanged", Qt::QueuedConnection, Q_ARG(bool, running), Q_ARG(bool, error));
}
void QZeroConfPrivate::onServiceNameChangedJNI(JNIEnv */*env*/, jobject /*thiz*/, jlong id, jstring newName)
{
QZeroConfPrivate *ref = reinterpret_cast<QZeroConfPrivate*>(id);
QMutexLocker locker(&s_instancesMutex);
if (!s_instances.contains(ref)) {
return;
}
QMetaObject::invokeMethod(ref, "onServiceNameChanged", Qt::QueuedConnection, Q_ARG(QString, QAndroidJniObject(newName).toString()));
}
void QZeroConfPrivate::onServiceResolved(const QString &name, const QString &type, const QString &hostname, const QHostAddress &address, int port, const TxtRecordMap &txtRecords)
{
QZeroConfService zcs;
bool newRecord = false;
if (pub->services.contains(name)) {
zcs = pub->services.value(name);
} else {
zcs = QZeroConfService(new QZeroConfServiceData);
newRecord = true;
}
zcs->m_name = name;
zcs->m_type = type;
// A previous implementation (based on avahi) returned service type as "_http._tcp" but Android API return "._http._tcp"
// Stripping leading dot for backwards compatibility. FIXME: Still not in line with bonjour, which adds a trailing dot.
zcs->m_type.remove(QRegularExpression("^."));
zcs->m_host = hostname;
zcs->m_port = port;
zcs->m_ip = address;
zcs->m_txt = txtRecords;
// Those are not available on Androids NsdManager
// zcs->m_domain = domain;
// zcs->m_interfaceIndex = interface;
if (newRecord) {
pub->services.insert(name, zcs);
emit pub->serviceAdded(zcs);
} else {
emit pub->serviceUpdated(zcs);
}
}
void QZeroConfPrivate::onServiceRemoved(const QString &name)
{
if (pub->services.contains(name)) {
QZeroConfService service = pub->services.take(name);
emit pub->serviceRemoved(service);
}
}
void QZeroConfPrivate::onBrowserStateChanged(bool running, bool error)
{
browserExists = running;
if (error) {
emit pub->error(QZeroConf::browserFailed);
}
}
void QZeroConfPrivate::onPublisherStateChanged(bool running, bool error)
{
publisherExists = running;
if (running) {
emit pub->servicePublished();
}
if (error) {
emit pub->error(QZeroConf::serviceRegistrationFailed);
}
}
void QZeroConfPrivate::onServiceNameChanged(const QString &newName)
{
emit pub->serviceNameChanged(newName);
}
QZeroConf::QZeroConf(QObject *parent) : QObject(parent)
{
pri = new QZeroConfPrivate(this);
qRegisterMetaType<QZeroConfService>("QZeroConfService");
}
QZeroConf::~QZeroConf()
{
delete pri;
}
void QZeroConf::startServicePublish(const char *name, const char *type, const char *domain, quint16 port, quint32 interface)
{
Q_UNUSED(domain) // Not supported on Android API
Q_UNUSED(interface) // Not supported on Android API
pri->startServicePublish(name, type, port);
}
void QZeroConf::stopServicePublish(void)
{
pri->stopServicePublish();
}
bool QZeroConf::publishExists(void)
{
return pri->publisherExists;
}
void QZeroConf::addServiceTxtRecord(QString nameOnly)
{
pri->txtRecords.insert(nameOnly.toUtf8(), QByteArray());
}
void QZeroConf::addServiceTxtRecord(QString name, QString value)
{
pri->txtRecords.insert(name.toUtf8(), value.toUtf8());
}
void QZeroConf::clearServiceTxtRecords()
{
pri->txtRecords.clear();
}
void QZeroConf::startBrowser(QString type, QAbstractSocket::NetworkLayerProtocol protocol)
{
pri->startBrowser(type, protocol);
}
void QZeroConf::stopBrowser(void)
{
pri->stopBrowser();
}
bool QZeroConf::browserExists(void)
{
return pri->browserExists;
}