Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mdns] Add support for discovering multiple Things per MDNS service #3678

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.core.config.discovery.mdns;

import java.util.ArrayList;
import java.util.Set;

import javax.jmdns.ServiceInfo;
Expand All @@ -29,6 +30,7 @@
* mDNS scans.
*
* @author Tobias Bräutigam - Initial contribution
* @author Richard Schleich - Implemented support for getThingUIDs(...) and createResults(...)
*/
@NonNullByDefault
public interface MDNSDiscoveryParticipant {
Expand Down Expand Up @@ -57,6 +59,17 @@ public interface MDNSDiscoveryParticipant {
@Nullable
DiscoveryResult createResult(ServiceInfo service);

/**
* Creates a discovery result list for a mDNS service
*
* @param service the mDNS service found on the network
* @return the according discovery result list, if the device is not
* supported by this participant the list is empty
*/
default ArrayList<DiscoveryResult> createResults(ServiceInfo serviceInfo) {
return new ArrayList<DiscoveryResult>();
}

/**
* Returns the thing UID for a mDNS service
*
Expand All @@ -67,6 +80,17 @@ public interface MDNSDiscoveryParticipant {
@Nullable
ThingUID getThingUID(ServiceInfo service);

/**
* Returns a list of thing UIDs for a mDNS service
*
* @param service the mDNS service on the network
* @return a list of thing UIDs, if the device is not supported
* by this participant the list is empty
*/
default ArrayList<ThingUID> getThingUIDs(ServiceInfo serviceInfo) {
return new ArrayList<ThingUID>();
}

/**
* Some openHAB bindings handle devices that can sometimes be a bit late in updating their mDNS announcements, which
* means that such devices are repeatedly removed from, and (re)added to, the Inbox.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.core.config.discovery.mdns.internal;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -53,7 +54,8 @@
*
* @author Tobias Bräutigam - Initial contribution
* @author Kai Kreuzer - Improved startup behavior and background discovery
* @author Andre Fuechsel - make {@link #startScan()} asynchronous
* @author Andre Fuechsel - make {@link #startScan()} asynchronous
* @author Richard Schleich - Implemented support for getThingUIDs(...) and createResults(...)
*/
@NonNullByDefault
@Component(immediate = true, service = DiscoveryService.class, configurationPid = "discovery.mdns")
Expand Down Expand Up @@ -220,9 +222,13 @@ private void considerService(ServiceEvent serviceEvent) {
private void createDiscoveryResult(MDNSDiscoveryParticipant participant, ServiceInfo serviceInfo) {
try {
DiscoveryResult result = participant.createResult(serviceInfo);
ArrayList<DiscoveryResult> results = participant.createResults(serviceInfo);
if (result != null) {
results.add(result);
}
for (DiscoveryResult thing : results) {
cancelRemovalTask(serviceInfo);
final DiscoveryResult resultNew = getLocalizedDiscoveryResult(result,
final DiscoveryResult resultNew = getLocalizedDiscoveryResult(thing,
FrameworkUtil.getBundle(participant.getClass()));
thingDiscovered(resultNew);
}
Expand All @@ -234,13 +240,17 @@ private void createDiscoveryResult(MDNSDiscoveryParticipant participant, Service
private void removeDiscoveryResult(MDNSDiscoveryParticipant participant, ServiceInfo serviceInfo) {
try {
ThingUID thingUID = participant.getThingUID(serviceInfo);
ArrayList<ThingUID> thingUIDs = participant.getThingUIDs(serviceInfo);
if (thingUID != null) {
thingUIDs.add(thingUID);
}
for (ThingUID thing : thingUIDs) {
long gracePeriod = participant.getRemovalGracePeriodSeconds(serviceInfo);
if (gracePeriod <= 0) {
thingRemoved(thingUID);
thingRemoved(thing);
} else {
cancelRemovalTask(serviceInfo);
scheduleRemovalTask(thingUID, serviceInfo, gracePeriod);
scheduleRemovalTask(thing, serviceInfo, gracePeriod);
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.mockito.Mockito.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Random;
import java.util.Set;
Expand All @@ -43,6 +44,7 @@
* Integration tests for the {@link MDNSDiscoveryService}.
*
* @author Henning Sudbrock - Initial contribution
* @author Richard Schleich - Implemented support for getThingUIDs(...) and createResults(...)
*/
@NonNullByDefault
public class MDNSDiscoveryServiceOSGiTest extends JavaOSGiTest {
Expand All @@ -60,12 +62,20 @@ public void testThingDiscoveredAndRemoved() {
String serviceType = "_http._tcp.local.";
ThingTypeUID thingTypeUID = new ThingTypeUID("myBinding", "myThingType");
ThingUID thingUID = new ThingUID(thingTypeUID, "test" + new Random().nextInt(999999999));
ThingUID thingUID2 = new ThingUID(thingTypeUID, "test2" + new Random().nextInt(999999999));
ArrayList<ThingUID> thingUIDs = new ArrayList<ThingUID>();
thingUIDs.add(thingUID2);
Set<ThingTypeUID> thingTypeUIDs = Set.of(thingTypeUID);
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).build();
DiscoveryResult discoveryResult2 = DiscoveryResultBuilder.create(thingUID2).build();
ArrayList<DiscoveryResult> discoveryResults = new ArrayList<DiscoveryResult>();
discoveryResults.add(discoveryResult2);

MDNSDiscoveryParticipant mockMDNSDiscoveryParticipant = mock(MDNSDiscoveryParticipant.class);
when(mockMDNSDiscoveryParticipant.getSupportedThingTypeUIDs()).thenReturn(thingTypeUIDs);
when(mockMDNSDiscoveryParticipant.getServiceType()).thenReturn(serviceType);
when(mockMDNSDiscoveryParticipant.createResults(any())).thenReturn(discoveryResults);
when(mockMDNSDiscoveryParticipant.getThingUIDs(any())).thenReturn(thingUIDs);
when(mockMDNSDiscoveryParticipant.createResult(any())).thenReturn(discoveryResult);
when(mockMDNSDiscoveryParticipant.getThingUID(any())).thenReturn(thingUID);

Expand All @@ -82,14 +92,17 @@ public void testThingDiscoveredAndRemoved() {

mdnsDiscoveryService.serviceAdded(mockServiceEvent);
verify(mockDiscoveryListener, times(1)).thingDiscovered(mdnsDiscoveryService, discoveryResult);
verify(mockDiscoveryListener, times(1)).thingDiscovered(mdnsDiscoveryService, discoveryResult2);
verifyNoMoreInteractions(mockDiscoveryListener);

mdnsDiscoveryService.serviceResolved(mockServiceEvent);
verify(mockDiscoveryListener, times(2)).thingDiscovered(mdnsDiscoveryService, discoveryResult);
verify(mockDiscoveryListener, times(3)).thingDiscovered(mdnsDiscoveryService, discoveryResult);
verify(mockDiscoveryListener, times(2)).thingDiscovered(mdnsDiscoveryService, discoveryResult2);
verifyNoMoreInteractions(mockDiscoveryListener);

mdnsDiscoveryService.serviceRemoved(mockServiceEvent);
verify(mockDiscoveryListener, times(1)).thingRemoved(mdnsDiscoveryService, thingUID);
verify(mockDiscoveryListener, times(1)).thingRemoved(mdnsDiscoveryService, thingUID2);
verifyNoMoreInteractions(mockDiscoveryListener);
}

Expand Down