Skip to content

Commit

Permalink
Merge pull request #519 from yuzawa-san/mdns-advertise
Browse files Browse the repository at this point in the history
Advertise webui on mdns
  • Loading branch information
yuzawa-san authored Jun 9, 2024
2 parents 8547551 + 4c45d71 commit 8d50824
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/dist/conf/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
# retry-interval: 15s
# devices-path: conf/devices.yml
# preferred-interface: eth0
# advertise: true
20 changes: 18 additions & 2 deletions src/main/java/com/jyuzawa/googolplex_theater/ServiceDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
import javax.annotation.PostConstruct;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
import javax.jmdns.ServiceListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
Expand All @@ -30,16 +33,21 @@
@Component
public final class ServiceDiscovery implements Closeable {
public static final String MDNS_SERVICE_NAME = "_googlecast._tcp.local.";
private static final String MDNS_ADVERTISE_NAME = "_http._tcp.local.";
private static final String MDNS_NAME = "googolplex-theater";

private final GoogolplexService service;
private final JmDNS mdns;
private final boolean advertise;

@Autowired
public ServiceDiscovery(
GoogolplexService service,
@Value("${googolplex-theater.preferred-interface:#{null}}") String preferredInterface)
@Value("${googolplex-theater.preferred-interface:#{null}}") String preferredInterface,
@Value("${googolplex-theater.advertise:#{false}}") boolean advertise)
throws IOException {
this.service = service;
this.advertise = advertise;
InetAddress inetAddress = getInterfaceAddress(preferredInterface);
if (inetAddress == null) {
log.warn("No IP address for service discovery found. Falling back to JmDNS library default.");
Expand All @@ -49,10 +57,18 @@ public ServiceDiscovery(
}

@PostConstruct
public void start() {
public void start() throws IOException {
this.mdns.addServiceListener(MDNS_SERVICE_NAME, new ServiceDiscoveryListener());
}

@EventListener
public void onApplicationEvent(WebServerInitializedEvent event) throws IOException {
if (advertise) {
int port = event.getWebServer().getPort();
this.mdns.registerService(ServiceInfo.create(MDNS_ADVERTISE_NAME, MDNS_NAME, port, MDNS_NAME));
}
}

static InetAddress getInterfaceAddress(String preferredInterface) throws SocketException, UnknownHostException {
if (preferredInterface != null) {
return getBestInetAddress(getPreferredInterface(preferredInterface));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.net.NetworkInterface;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.server.WebServer;

class ServiceDiscoveryTest {

Expand All @@ -30,7 +32,12 @@ void preferredInterfaceTest() throws Exception {
@Test
void instantiationTest() throws IOException {
GoogolplexService controller = Mockito.mock(GoogolplexService.class);
ServiceDiscovery sd = new ServiceDiscovery(controller, null);
ServiceDiscovery sd = new ServiceDiscovery(controller, null, true);
WebServerInitializedEvent event = Mockito.mock(WebServerInitializedEvent.class);
WebServer webServer = Mockito.mock(WebServer.class);
Mockito.when(event.getWebServer()).thenReturn(webServer);
Mockito.when(webServer.getPort()).thenReturn(8080);
sd.onApplicationEvent(event);
sd.close();
}
}

0 comments on commit 8d50824

Please sign in to comment.