Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@
</dependency>

<dependency>
<groupId>javax.jmdns</groupId>
<groupId>org.jmdns</groupId>
<artifactId>jmdns</artifactId>
<version>3.4.1</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... breaking change to bump a minor version of a dependency? May need to wait for 1.2.0 release?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is changing the maven organization from the abandoned name to the maintained name a breaking change (the implemented API did not change in a breaking way)?

<version>3.5.5</version>
</dependency>

<dependency>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/beowulfe/hap/HomekitRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.beowulfe.hap.impl.jmdns.JmdnsHomekitAdvertiser;
import java.io.IOException;
import java.net.InetAddress;
import javax.jmdns.JmDNS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -39,6 +40,11 @@ public class HomekitRoot {
this(label, webHandler, authInfo, new JmdnsHomekitAdvertiser(localhost));
}

HomekitRoot(String label, HomekitWebHandler webHandler, JmDNS jmdns, HomekitAuthInfo authInfo)
throws IOException {
this(label, webHandler, authInfo, new JmdnsHomekitAdvertiser(jmdns));
}

HomekitRoot(
String label,
HomekitWebHandler webHandler,
Expand Down
43 changes: 40 additions & 3 deletions src/main/java/com/beowulfe/hap/HomekitServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.math.BigInteger;
import java.net.InetAddress;
import java.security.InvalidAlgorithmParameterException;
import javax.jmdns.JmDNS;

/**
* The main entry point for hap-java. Creating an instance of this class will listen for Homekit
Expand All @@ -27,6 +28,7 @@ public class HomekitServer {

private final HomekitHttpServer http;
private final InetAddress localAddress;
private final JmDNS jmdns;

/**
* Constructor. Contains an argument indicating the number of threads to use in the http server.
Expand All @@ -40,9 +42,24 @@ public class HomekitServer {
*/
public HomekitServer(InetAddress localAddress, int port, int nThreads) throws IOException {
this.localAddress = localAddress;
this.jmdns = null;
http = new HomekitHttpServer(localAddress, port, nThreads);
}

/**
* Constructor
*
* @param jmdns mdns service to register with
* @param port local port to bind to
* @param nThreads number of threads to use in the http server
* @throws IOException when the server cannot bind to the supplied port
*/
public HomekitServer(JmDNS jmdns, int port, int nThreads) throws IOException {
this.jmdns = jmdns;
this.localAddress = null;
http = new HomekitHttpServer(jmdns.getInetAddress(), port, nThreads);
}

/**
* Constructor
*
Expand All @@ -57,7 +74,18 @@ public HomekitServer(InetAddress localAddress, int port) throws IOException {
/**
* Constructor
*
* @param port local port to bind to.
* @param jmdns mdns service to register with
* @param port local port to bind to
* @throws IOException when the server cannot bind to the supplied port
*/
public HomekitServer(JmDNS jmdns, int port) throws IOException {
this(jmdns, port, Runtime.getRuntime().availableProcessors());
}

/**
* Constructor
*
* @param port local port to bind to
* @throws IOException when the server cannot bind to the supplied port
*/
public HomekitServer(int port) throws IOException {
Expand All @@ -82,7 +110,11 @@ public void stop() {
*/
public HomekitStandaloneAccessoryServer createStandaloneAccessory(
HomekitAuthInfo authInfo, HomekitAccessory accessory) throws IOException {
return new HomekitStandaloneAccessoryServer(accessory, http, localAddress, authInfo);
if (jmdns != null) {
return new HomekitStandaloneAccessoryServer(accessory, http, jmdns, authInfo);
} else {
return new HomekitStandaloneAccessoryServer(accessory, http, localAddress, authInfo);
}
}

/**
Expand All @@ -108,7 +140,12 @@ public HomekitRoot createBridge(
String model,
String serialNumber)
throws IOException {
HomekitRoot root = new HomekitRoot(label, http, localAddress, authInfo);
HomekitRoot root;
if (jmdns != null) {
root = new HomekitRoot(label, http, jmdns, authInfo);
} else {
root = new HomekitRoot(label, http, localAddress, authInfo);
}
root.addAccessory(new HomekitBridge(label, serialNumber, model, manufacturer));
return root;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.jmdns.JmDNS;

/**
* A server for exposing standalone Homekit accessory (as opposed to a Bridge accessory which
Expand All @@ -27,6 +28,16 @@ public class HomekitStandaloneAccessoryServer {
root.addAccessory(accessory);
}

HomekitStandaloneAccessoryServer(
HomekitAccessory accessory,
HomekitWebHandler webHandler,
JmDNS jmdns,
HomekitAuthInfo authInfo)
throws UnknownHostException, IOException {
root = new HomekitRoot(accessory.getLabel(), webHandler, jmdns, authInfo);
root.addAccessory(accessory);
}

/** Begins advertising and handling requests for this accessory. */
public void start() {
root.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class JmdnsHomekitAdvertiser {
private int port;
private int configurationIndex;

public JmdnsHomekitAdvertiser(JmDNS jmdns) {
this.jmdns = jmdns;
}

public JmdnsHomekitAdvertiser(InetAddress localAddress) throws UnknownHostException, IOException {
jmdns = JmDNS.create(localAddress);
}
Expand Down Expand Up @@ -53,33 +57,45 @@ public synchronized void advertise(String label, String mac, int port, int confi
}

public synchronized void stop() {
jmdns.unregisterAllServices();
unregisterService();
}

public synchronized void setDiscoverable(boolean discoverable) throws IOException {
if (this.discoverable != discoverable) {
if (isAdvertising) {
unregisterService();
}
this.discoverable = discoverable;
if (isAdvertising) {
logger.info("Re-creating service due to change in discoverability to " + discoverable);
jmdns.unregisterAllServices();
registerService();
}
}
}

public synchronized void setConfigurationIndex(int revision) throws IOException {
if (this.configurationIndex != revision) {
if (isAdvertising) {
unregisterService();
}
this.configurationIndex = revision;
if (isAdvertising) {
logger.info("Re-creating service due to change in configuration index to " + revision);
jmdns.unregisterAllServices();
registerService();
}
}
}

private void registerService() throws IOException {
logger.info("Registering " + SERVICE_TYPE + " on port " + port);
jmdns.registerService(buildServiceInfo());
}

private void unregisterService() {
jmdns.unregisterService(buildServiceInfo());
}

private ServiceInfo buildServiceInfo() {
Map<String, String> props = new HashMap<>();
props.put("sf", discoverable ? "1" : "0");
props.put("id", mac);
Expand All @@ -88,6 +104,7 @@ private void registerService() throws IOException {
props.put("s#", "1");
props.put("ff", "0");
props.put("ci", "1");
jmdns.registerService(ServiceInfo.create(SERVICE_TYPE, label, port, 1, 1, props));

return ServiceInfo.create(SERVICE_TYPE, label, port, 1, 1, props);
}
}