Skip to content

Commit

Permalink
Fix handling of mDNS instances when an IP address changed (#3906)
Browse files Browse the repository at this point in the history
Fix openhab/openhab-addons#16023

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored Dec 9, 2023
1 parent 708a954 commit 152ffe3
Showing 1 changed file with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ private void registerServiceInternal(ServiceDescription description) throws IOEx
@Override
public void unregisterService(ServiceDescription description) {
activeServices.remove(description);
unregisterServiceInternal(description);
}

private void unregisterServiceInternal(ServiceDescription description) {
for (JmDNS instance : jmdnsInstances.values()) {
try {
logger.debug("Unregistering service {} at {}:{} ({})", description.serviceType,
Expand Down Expand Up @@ -277,7 +281,55 @@ private void createJmDNSByAddress(InetAddress address) {
@Override
public void onChanged(List<CidrAddress> added, List<CidrAddress> removed) {
logger.debug("ip address change: added {}, removed {}", added, removed);
close();
start();

// First check if there is really a jmdns instance to remove or add
boolean changeRequired = false;
for (CidrAddress address : removed) {
JmDNS jmdns = jmdnsInstances.get(address.getAddress());
if (jmdns != null) {
changeRequired = true;
break;
}
}
if (!changeRequired) {
for (CidrAddress address : added) {
JmDNS jmdns = jmdnsInstances.get(address.getAddress());
if (jmdns == null) {
changeRequired = true;
break;
}
}
}
if (!changeRequired) {
logger.debug("mDNS services already OK for these ip addresses");
return;
}

for (ServiceDescription description : activeServices) {
unregisterServiceInternal(description);
}
for (CidrAddress address : removed) {
JmDNS jmdns = jmdnsInstances.remove(address.getAddress());
if (jmdns != null) {
closeQuietly(jmdns);
logger.debug("mDNS service has been stopped ({})", jmdns.getName());
}
}
for (CidrAddress address : added) {
JmDNS jmdns = jmdnsInstances.get(address.getAddress());
if (jmdns == null) {
createJmDNSByAddress(address.getAddress());
} else {
logger.debug("mDNS service was already started ({} for IP {})", jmdns.getName(),
address.getAddress().getHostAddress());
}
}
for (ServiceDescription description : activeServices) {
try {
registerServiceInternal(description);
} catch (IOException e) {
logger.warn("Exception while registering service {}", description, e);
}
}
}
}

0 comments on commit 152ffe3

Please sign in to comment.