Quality improvements and JavaDoc
Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>pull/4534/head
parent
fb8b76b815
commit
52cea5e9c2
|
@ -14,7 +14,6 @@ package org.openhab.core.io.transport.upnp;
|
|||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -36,8 +35,16 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Component service that can be used to keep mis-behaving UPnP devices stored in the registry
|
||||
* by sending a targeted M-SEARCH message for their UDN before their maxAge expires.
|
||||
* The {@link UpnpDeviceFinder} is a component service that can be used to keep 'mis-behaving' UPnP devices
|
||||
* 'alive' in the registry by sending a targeted M-SEARCH 'ping' message for their UDN before their maxAge
|
||||
* expires.
|
||||
* <p>
|
||||
* Typically such a 'mis-behaving' device may fail to send regular NOTIFY alive messages, or fail to send
|
||||
* them in time. This component substitutes for such lack by sending a targeted M-SEARCH message 60 seconds
|
||||
* before the device's maxAge would normally expire.
|
||||
* <p>
|
||||
* For a component to use this service it must consume a final @Reference to this class in an @Activate method,
|
||||
* and call 'addUDN()' to start processing the given device, and respectively call 'removeUDN()') to stop it.
|
||||
*
|
||||
* @author Andrew Fiddian-Green - Initial Contribution
|
||||
*/
|
||||
|
@ -45,6 +52,7 @@ import org.slf4j.LoggerFactory;
|
|||
public class UpnpDeviceFinder implements RegistryListener {
|
||||
|
||||
private static final String DEVICE_FINDER_THREADPOOL = "upnpDeviceFinder";
|
||||
|
||||
private static final int SEARCH_SCHEDULE_SECONDS = 60;
|
||||
private static final int SEARCH_RETRY_MAX = 5;
|
||||
private static final int SEARCH_RETRY_INTERVAL = 5000;
|
||||
|
@ -76,10 +84,10 @@ public class UpnpDeviceFinder implements RegistryListener {
|
|||
|
||||
/**
|
||||
* Cancel scheduled search (if any) for the given UDN.
|
||||
* Interrupts the executeSearch() method below.
|
||||
* May interrupt the executeSearch() method below.
|
||||
*/
|
||||
private void cancelSearch(UDN udn) {
|
||||
Future<?> task = subscriptions.get(udn);
|
||||
ScheduledFuture<?> task = subscriptions.get(udn);
|
||||
if (task != null) {
|
||||
task.cancel(true);
|
||||
}
|
||||
|
@ -87,7 +95,7 @@ public class UpnpDeviceFinder implements RegistryListener {
|
|||
|
||||
/**
|
||||
* Execute the search for SEARCH_RETRY_MAX attempts at SEARCH_RETRY_INTERVAL.
|
||||
* Can be interrupted by the cancelSearch() method above.
|
||||
* May be interrupted by the cancelSearch() method above.
|
||||
*/
|
||||
private void executeSearch(UDN udn) {
|
||||
logger.debug("Executing search for {}", udn);
|
||||
|
@ -95,8 +103,8 @@ public class UpnpDeviceFinder implements RegistryListener {
|
|||
upnpService.getControlPoint().search(new UDNHeader(udn));
|
||||
try {
|
||||
Thread.sleep(SEARCH_RETRY_INTERVAL);
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
} catch (InterruptedException cancelled) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -147,21 +155,27 @@ public class UpnpDeviceFinder implements RegistryListener {
|
|||
subscriptions.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void localDeviceAdded(Registry registry, LocalDevice device) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void localDeviceRemoved(Registry registry, LocalDevice device) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
|
||||
remoteDeviceUpdated(registry, device);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remoteDeviceUpdated(Registry registry, RemoteDevice device) {
|
||||
if (subscriptions.containsKey(device.getIdentity().getUdn())) {
|
||||
scheduleSearch(device);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
|
||||
if (subscriptions.containsKey(device.getIdentity().getUdn())) {
|
||||
UDN udn = device.getIdentity().getUdn();
|
||||
logger.warn("Device {} removed unexpectedly from registry", udn);
|
||||
scheduleSearch(udn, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remoteDeviceDiscoveryFailed(Registry registry, RemoteDevice device, Exception e) {
|
||||
if (subscriptions.containsKey(device.getIdentity().getUdn())) {
|
||||
|
@ -174,18 +188,10 @@ public class UpnpDeviceFinder implements RegistryListener {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
|
||||
if (subscriptions.containsKey(device.getIdentity().getUdn())) {
|
||||
UDN udn = device.getIdentity().getUdn();
|
||||
logger.warn("Device {} removed unexpectedly from registry", udn);
|
||||
scheduleSearch(udn, 0);
|
||||
}
|
||||
public void localDeviceAdded(Registry registry, LocalDevice device) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remoteDeviceUpdated(Registry registry, RemoteDevice device) {
|
||||
if (subscriptions.containsKey(device.getIdentity().getUdn())) {
|
||||
scheduleSearch(device);
|
||||
}
|
||||
public void localDeviceRemoved(Registry registry, LocalDevice device) {
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue