88import java .net .InetAddress ;
99import java .security .InvalidAlgorithmParameterException ;
1010import 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 ));
0 commit comments