Skip to content

Commit

Permalink
feat: add mDNS service for the API, so it can be discovered by esp32
Browse files Browse the repository at this point in the history
  • Loading branch information
Snootic committed Apr 26, 2024
1 parent c61b7ca commit 94552ae
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
<artifactId>mariadb-java-client</artifactId>
<version>1.5.7</version>
</dependency>
<dependency>
<groupId>org.jmdns</groupId>
<artifactId>jmdns</artifactId>
<version>3.5.9</version>
</dependency>
</dependencies>

<build>
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/com/pedro/sphynx/domain/dnsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.pedro.sphynx.domain;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import javax.jmdns.*;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;

@Component
public class dnsService {

// Getting mdns variables from application.properties

@Value("${mdns.service.type}")
private String serviceType;

@Value("${mdns.service.name}")
private String serviceName;

private JmDNS jmdns;
private int serviceNumber = 0; // If exists more than one interface on PC it concatenates the servicenumber in the serviceName so it won't conflict
private InetAddress ipAddress; // IP address that will start the mDNS service

@PostConstruct
public void registerService() {
try {
// Get all network interfaces available in the host
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement(); // choses an network interface from the list

// If the interface is on and is not loopback (localhost, 127.0.0.1, ::1, etc), continues
if (networkInterface.isUp() && !networkInterface.isLoopback()) {
String nomeServico = serviceName + Integer.toString(serviceNumber); // concatenate the serviceNumber in the serviceName

// get all ip addresses for the network interface chosen before
Enumeration<InetAddress> Addresses = networkInterface.getInetAddresses();
while (Addresses.hasMoreElements()){
ipAddress = Addresses.nextElement(); // assign ipAddress with an ip from the list
String ip = ipAddress.toString();

// check with regex if the ip is an ipv4 (must be ipv4, ipv6 won't work), then leaves
if (ip.matches("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")){
break;
}
}

// create the mDNS service with the desired ipv4 chosen before
jmdns = JmDNS.create(ipAddress);

// set service parameters (http._tcp, name, port and description)
ServiceInfo serviceInfo = ServiceInfo.create(serviceType, nomeServico, 8080, "Sphynx API");
jmdns.registerService(serviceInfo);
serviceNumber++;
}
}
System.out.println("MDNS SERVICE UP");
} catch (Exception e) {
e.printStackTrace();
}
}

@PreDestroy
public void unregisterService() {
// unregister all mDNS services before shutting the api down
if (jmdns != null) {
jmdns.unregisterAllServices();
try {
jmdns.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ spring.datasource.password=${DATABASE_PASSWORD:root}
spring.flyway.enabled=true
spring.flyway.locations=classpath:db/migration
spring.flyway.baseline-on-migrate=true
mdns.service.type=_http._tcp.local.
mdns.service.name=sphynxapi

server.error.include-stacktrace=never

0 comments on commit 94552ae

Please sign in to comment.