Skip to content

Commit 3429fcd

Browse files
authored
Merge pull request #122 from yfre/JmDNS_pr_port
port of #68 PR on jmDNS to latest code base
2 parents 8232818 + f8e3c47 commit 3429fcd

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

Diff for: pom.xml

+2-3
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>

Diff for: src/main/java/io/github/hapjava/server/impl/HomekitRoot.java

+6
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

Diff for: src/main/java/io/github/hapjava/server/impl/HomekitServer.java

+38-2
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));

Diff for: src/main/java/io/github/hapjava/server/impl/HomekitStandaloneAccessoryServer.java

+11
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();

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public class JmdnsHomekitAdvertiser {
2727
private int port;
2828
private int configurationIndex;
2929

30+
public JmdnsHomekitAdvertiser(JmDNS jmdns) {
31+
this.jmdns = jmdns;
32+
}
33+
3034
public JmdnsHomekitAdvertiser(InetAddress localAddress) throws UnknownHostException, IOException {
3135
jmdns = JmDNS.create(localAddress);
3236
}
@@ -57,15 +61,15 @@ public synchronized void advertise(
5761
}
5862

5963
public synchronized void stop() {
60-
jmdns.unregisterAllServices();
64+
unregisterService();
6165
}
6266

6367
public synchronized void setDiscoverable(boolean discoverable) throws IOException {
6468
if (this.discoverable != discoverable) {
6569
this.discoverable = discoverable;
6670
if (isAdvertising) {
6771
logger.trace("Re-creating service due to change in discoverability to " + discoverable);
68-
jmdns.unregisterAllServices();
72+
unregisterService();
6973
registerService();
7074
}
7175
}
@@ -76,14 +80,22 @@ public synchronized void setConfigurationIndex(int revision) throws IOException
7680
this.configurationIndex = revision;
7781
if (isAdvertising) {
7882
logger.trace("Re-creating service due to change in configuration index to " + revision);
79-
jmdns.unregisterAllServices();
83+
unregisterService();
8084
registerService();
8185
}
8286
}
8387
}
8488

89+
private void unregisterService() {
90+
jmdns.unregisterService(buildServiceInfo());
91+
}
92+
8593
private void registerService() throws IOException {
8694
logger.info("Registering " + SERVICE_TYPE + " on port " + port);
95+
jmdns.registerService(buildServiceInfo());
96+
}
97+
98+
private ServiceInfo buildServiceInfo() {
8799
logger.trace("MAC:" + mac + " Setup Id:" + setupId);
88100
Map<String, String> props = new HashMap<>();
89101
props.put("sf", discoverable ? "1" : "0");
@@ -94,6 +106,6 @@ private void registerService() throws IOException {
94106
props.put("s#", "1");
95107
props.put("ff", "0");
96108
props.put("ci", "1");
97-
jmdns.registerService(ServiceInfo.create(SERVICE_TYPE, label, port, 1, 1, props));
109+
return ServiceInfo.create(SERVICE_TYPE, label, port, 1, 1, props);
98110
}
99111
}

0 commit comments

Comments
 (0)