Skip to content

Commit b0f69b1

Browse files
author
Eugen Freiter
committed
port of #68 PR on jmDNS to latest code base
Signed-off-by: Eugen Freiter <freiter@gmx.de>
1 parent a746a02 commit b0f69b1

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<properties>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1414
<netty.version>4.1.42.Final</netty.version>
15-
1615
</properties>
1716

1817
<licenses>
@@ -135,9 +134,9 @@
135134
</dependency>
136135

137136
<dependency>
138-
<groupId>javax.jmdns</groupId>
137+
<groupId>org.jmdns</groupId>
139138
<artifactId>jmdns</artifactId>
140-
<version>3.4.1</version>
139+
<version>3.5.6</version>
141140
</dependency>
142141

143142
<dependency>

src/main/java/io/github/hapjava/server/impl/HomekitRoot.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.github.hapjava.server.impl.jmdns.JmdnsHomekitAdvertiser;
1010
import java.io.IOException;
1111
import java.net.InetAddress;
12+
import javax.jmdns.JmDNS;
1213
import org.slf4j.Logger;
1314
import org.slf4j.LoggerFactory;
1415

@@ -53,6 +54,11 @@ public class HomekitRoot {
5354
this.registry = new HomekitRegistry(label);
5455
}
5556

57+
HomekitRoot(String label, HomekitWebHandler webHandler, JmDNS jmdns, HomekitAuthInfo authInfo)
58+
throws IOException {
59+
this(label, webHandler, authInfo, new JmdnsHomekitAdvertiser(jmdns));
60+
}
61+
5662
/**
5763
* Add an accessory to be handled and advertised by this root. Any existing HomeKit connections
5864
* will be terminated to allow the clients to reconnect and see the updated accessory list. When

src/main/java/io/github/hapjava/server/impl/HomekitServer.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.net.InetAddress;
99
import java.security.InvalidAlgorithmParameterException;
1010
import java.util.concurrent.ExecutionException;
11+
import javax.jmdns.JmDNS;
1112

1213
/**
1314
* The main entry point for hap-java. Creating an instance of this class will listen for HomeKit
@@ -28,6 +29,7 @@ public class HomekitServer {
2829

2930
private final HomekitHttpServer http;
3031
private final InetAddress localAddress;
32+
private final JmDNS jmdns;
3133

3234
/**
3335
* Constructor. Contains an argument indicating the number of threads to use in the http server.
@@ -41,9 +43,24 @@ public class HomekitServer {
4143
*/
4244
public HomekitServer(InetAddress localAddress, int port, int nThreads) throws IOException {
4345
this.localAddress = localAddress;
46+
this.jmdns = null;
4447
http = new HomekitHttpServer(localAddress, port, nThreads);
4548
}
4649

50+
/**
51+
* Constructor
52+
*
53+
* @param jmdns mdns service to register with
54+
* @param port local port to bind to
55+
* @param nThreads number of threads to use in the http server
56+
* @throws IOException when the server cannot bind to the supplied port
57+
*/
58+
public HomekitServer(JmDNS jmdns, int port, int nThreads) throws IOException {
59+
this.jmdns = jmdns;
60+
this.localAddress = null;
61+
http = new HomekitHttpServer(jmdns.getInetAddress(), port, nThreads);
62+
}
63+
4764
/**
4865
* Constructor
4966
*
@@ -55,6 +72,16 @@ public HomekitServer(InetAddress localAddress, int port) throws IOException {
5572
this(localAddress, port, Runtime.getRuntime().availableProcessors());
5673
}
5774

75+
/**
76+
* Constructor
77+
*
78+
* @param jmdns mdns service to register with
79+
* @param port local port to bind to
80+
* @throws IOException when the server cannot bind to the supplied port
81+
*/
82+
public HomekitServer(JmDNS jmdns, int port) throws IOException {
83+
this(jmdns, port, Runtime.getRuntime().availableProcessors());
84+
}
5885
/**
5986
* Constructor
6087
*
@@ -84,7 +111,11 @@ public void stop() {
84111
public HomekitStandaloneAccessoryServer createStandaloneAccessory(
85112
HomekitAuthInfo authInfo, HomekitAccessory accessory)
86113
throws IOException, ExecutionException, InterruptedException {
87-
return new HomekitStandaloneAccessoryServer(accessory, http, localAddress, authInfo);
114+
if (jmdns != null) {
115+
return new HomekitStandaloneAccessoryServer(accessory, http, jmdns, authInfo);
116+
} else {
117+
return new HomekitStandaloneAccessoryServer(accessory, http, localAddress, authInfo);
118+
}
88119
}
89120

90121
/**
@@ -114,7 +145,12 @@ public HomekitRoot createBridge(
114145
String firmwareRevision,
115146
String hardwareRevision)
116147
throws IOException {
117-
HomekitRoot root = new HomekitRoot(label, http, localAddress, authInfo);
148+
HomekitRoot root;
149+
if (jmdns != null) {
150+
root = new HomekitRoot(label, http, jmdns, authInfo);
151+
} else {
152+
root = new HomekitRoot(label, http, localAddress, authInfo);
153+
}
118154
root.addAccessory(
119155
new HomekitBridge(
120156
label, serialNumber, model, manufacturer, firmwareRevision, hardwareRevision));

src/main/java/io/github/hapjava/server/impl/HomekitStandaloneAccessoryServer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.net.InetAddress;
88
import java.net.UnknownHostException;
99
import java.util.concurrent.ExecutionException;
10+
import javax.jmdns.JmDNS;
1011

1112
/**
1213
* A server for exposing standalone HomeKit accessory (as opposed to a Bridge accessory which
@@ -30,6 +31,16 @@ public class HomekitStandaloneAccessoryServer {
3031
root.addAccessory(accessory);
3132
}
3233

34+
HomekitStandaloneAccessoryServer(
35+
HomekitAccessory accessory,
36+
HomekitWebHandler webHandler,
37+
JmDNS jmdns,
38+
HomekitAuthInfo authInfo)
39+
throws UnknownHostException, IOException, ExecutionException, InterruptedException {
40+
root = new HomekitRoot(accessory.getName().get(), webHandler, jmdns, authInfo);
41+
root.addAccessory(accessory);
42+
}
43+
3344
/** Begins advertising and handling requests for this accessory. */
3445
public void start() {
3546
root.start();

src/main/java/io/github/hapjava/server/impl/jmdns/JmdnsHomekitAdvertiser.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public class JmdnsHomekitAdvertiser {
2424
private int port;
2525
private int configurationIndex;
2626

27+
public JmdnsHomekitAdvertiser(JmDNS jmdns) {
28+
this.jmdns = jmdns;
29+
}
30+
2731
public JmdnsHomekitAdvertiser(InetAddress localAddress) throws UnknownHostException, IOException {
2832
jmdns = JmDNS.create(localAddress);
2933
}
@@ -53,15 +57,15 @@ public synchronized void advertise(String label, String mac, int port, int confi
5357
}
5458

5559
public synchronized void stop() {
56-
jmdns.unregisterAllServices();
60+
unregisterService();
5761
}
5862

5963
public synchronized void setDiscoverable(boolean discoverable) throws IOException {
6064
if (this.discoverable != discoverable) {
6165
this.discoverable = discoverable;
6266
if (isAdvertising) {
6367
logger.trace("Re-creating service due to change in discoverability to " + discoverable);
64-
jmdns.unregisterAllServices();
68+
unregisterService();
6569
registerService();
6670
}
6771
}
@@ -72,14 +76,22 @@ public synchronized void setConfigurationIndex(int revision) throws IOException
7276
this.configurationIndex = revision;
7377
if (isAdvertising) {
7478
logger.trace("Re-creating service due to change in configuration index to " + revision);
75-
jmdns.unregisterAllServices();
79+
unregisterService();
7680
registerService();
7781
}
7882
}
7983
}
8084

85+
private void unregisterService() {
86+
jmdns.unregisterService(buildServiceInfo());
87+
}
88+
8189
private void registerService() throws IOException {
8290
logger.info("Registering " + SERVICE_TYPE + " on port " + port);
91+
jmdns.registerService(buildServiceInfo());
92+
}
93+
94+
private ServiceInfo buildServiceInfo() {
8395
Map<String, String> props = new HashMap<>();
8496
props.put("sf", discoverable ? "1" : "0");
8597
props.put("id", mac);
@@ -88,6 +100,6 @@ private void registerService() throws IOException {
88100
props.put("s#", "1");
89101
props.put("ff", "0");
90102
props.put("ci", "1");
91-
jmdns.registerService(ServiceInfo.create(SERVICE_TYPE, label, port, 1, 1, props));
103+
return ServiceInfo.create(SERVICE_TYPE, label, port, 1, 1, props);
92104
}
93105
}

0 commit comments

Comments
 (0)