Skip to content

Commit e47df13

Browse files
committed
Using JmDNS
1 parent c2606e7 commit e47df13

5 files changed

Lines changed: 22 additions & 24 deletions

File tree

.gitmodules

Lines changed: 0 additions & 4 deletions
This file was deleted.

core/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@
7575

7676
<!-- mDNS -->
7777
<dependency>
78-
<groupId>xyz.gianlu.mdnsjava</groupId>
79-
<artifactId>mdnsjava</artifactId>
80-
<version>2.2.1</version>
78+
<groupId>org.jmdns</groupId>
79+
<artifactId>jmdns</artifactId>
80+
<version>3.5.5</version>
8181
</dependency>
8282
</dependencies>
8383
</project>

core/src/main/java/xyz/gianlu/librespot/core/ZeroconfServer.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package xyz.gianlu.librespot.core;
22

33
import com.google.gson.JsonObject;
4-
import net.posick.mdns.MulticastDNSService;
5-
import net.posick.mdns.ServiceInstance;
6-
import net.posick.mdns.ServiceName;
74
import org.apache.log4j.Logger;
85
import org.jetbrains.annotations.NotNull;
9-
import org.xbill.DNS.Name;
106
import xyz.gianlu.librespot.AbsConfiguration;
117
import xyz.gianlu.librespot.Version;
128
import xyz.gianlu.librespot.common.Utils;
@@ -19,6 +15,8 @@
1915
import javax.crypto.Mac;
2016
import javax.crypto.spec.IvParameterSpec;
2117
import javax.crypto.spec.SecretKeySpec;
18+
import javax.jmdns.JmDNS;
19+
import javax.jmdns.ServiceInfo;
2220
import java.io.Closeable;
2321
import java.io.DataInputStream;
2422
import java.io.IOException;
@@ -77,16 +75,14 @@ public class ZeroconfServer implements Closeable {
7775
}
7876

7977
private final HttpRunner runner;
80-
private final MulticastDNSService mDnsService;
81-
private final ServiceInstance spotifyConnectService;
8278
private final Session.Inner inner;
8379
private final DiffieHellman keys;
8480
private Session session;
81+
private final JmDNS[] instances;
8582

8683
private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOException {
8784
this.inner = inner;
8885
this.keys = new DiffieHellman(inner.random);
89-
this.mDnsService = new MulticastDNSService();
9086

9187
int port = inner.random.nextInt((MAX_PORT - MIN_PORT) + 1) + MIN_PORT;
9288
new Thread(this.runner = new HttpRunner(port), "zeroconf-http-server").start();
@@ -107,10 +103,17 @@ private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOExcepti
107103

108104
LOGGER.debug("Registering service on " + Arrays.toString(bound));
109105

110-
ServiceInstance service = new ServiceInstance(new ServiceName("librespot-java._spotify-connect._tcp.local."), 0, 0, port, Name.fromString("local."), bound, "VERSION=1.0", "CPath=/");
111-
spotifyConnectService = mDnsService.register(service);
112-
if (spotifyConnectService == null)
113-
throw new IOException("Failed registering SpotifyConnect service!");
106+
Map<String, String> txt = new HashMap<>();
107+
txt.put("CPath", "/");
108+
txt.put("VERSION", "1.0");
109+
110+
instances = new JmDNS[bound.length];
111+
for (int i = 0; i < instances.length; i++) {
112+
instances[i] = JmDNS.create(bound[i], bound[i].getHostName());
113+
ServiceInfo serviceInfo = ServiceInfo.create("_spotify-connect._tcp.local.", "librespot-java", port, 0, 0, txt);
114+
instances[i].registerService(serviceInfo);
115+
}
116+
114117
LOGGER.info("SpotifyConnect service registered successfully!");
115118
}
116119

@@ -150,7 +153,8 @@ private static void addAddressOfInterface(List<InetAddress> list, @NotNull Netwo
150153
if (nif.isLoopback()) return;
151154

152155
if (isVirtual(nif)) {
153-
if (checkVirtual) return;
156+
if (checkVirtual)
157+
return;
154158
else
155159
LOGGER.warn(String.format("Interface %s is suspected to be virtual, mac: %s", nif.getName(), Utils.bytesToHex(nif.getHardwareAddress())));
156160
}
@@ -171,10 +175,10 @@ private static InetAddress[] getAllInterfacesAddresses() throws SocketException
171175

172176
@Override
173177
public void close() throws IOException {
174-
mDnsService.unregister(spotifyConnectService);
175-
LOGGER.trace("SpotifyConnect service unregistered successfully.");
178+
for (JmDNS instance : instances)
179+
instance.unregisterAllServices();
176180

177-
mDnsService.close();
181+
LOGGER.trace("SpotifyConnect service unregistered successfully.");
178182
runner.close();
179183
}
180184

mdnsjava

Lines changed: 0 additions & 1 deletion
This file was deleted.

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
<module>core</module>
4747
<module>api</module>
4848
<module>api-client</module>
49-
<module>mdnsjava</module>
5049
</modules>
5150

5251
<dependencies>

0 commit comments

Comments
 (0)