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

[wemo] Optimize port detection #12651

Merged
merged 2 commits into from
Apr 25, 2022
Merged
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 @@ -14,11 +14,16 @@

import java.net.URL;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -48,6 +53,8 @@ public abstract class WemoBaseThingHandler extends BaseThingHandler implements U

private static final int SUBSCRIPTION_RENEWAL_INITIAL_DELAY_SECONDS = 15;
private static final int SUBSCRIPTION_RENEWAL_INTERVAL_SECONDS = 60;
private static final int PORT_RANGE_START = 49151;
private static final int PORT_RANGE_END = 49157;

private final Logger logger = LoggerFactory.getLogger(WemoBaseThingHandler.class);
private final UpnpIOService service;
Expand Down Expand Up @@ -76,9 +83,9 @@ public void initialize() {

@Override
public void dispose() {
cancelSubscriptionRenewalJob();
removeSubscriptions();
logger.debug("Unregistering UPnP participant for {}", getThing().getUID());
cancelSubscriptionRenewalJob();
service.unregisterParticipant(this);
}

Expand Down Expand Up @@ -223,10 +230,16 @@ private void initializeHost() {
}

private int scanForPort(String host) {
int portCheckStart = 49151;
int portCheckStop = 49157;
Integer portFromService = getPortFromService();
List<Integer> portsToCheck = new ArrayList<Integer>(PORT_RANGE_END - PORT_RANGE_START + 1);
Stream<Integer> portRange = IntStream.rangeClosed(PORT_RANGE_START, PORT_RANGE_END).boxed();
if (portFromService != null) {
portsToCheck.add(portFromService);
portRange = portRange.filter(p -> p.intValue() != portFromService);
}
portsToCheck.addAll(portRange.collect(Collectors.toList()));
int port = 0;
for (int portCheck = portCheckStart; portCheck < portCheckStop; portCheck++) {
for (Integer portCheck : portsToCheck) {
String urlProbe = "http://" + host + ":" + portCheck;
logger.trace("Probing {} to find port", urlProbe);
if (!wemoHttpCaller.probeURL(urlProbe)) {
Expand All @@ -246,4 +259,15 @@ private int scanForPort(String host) {
}
return null;
}

private @Nullable Integer getPortFromService() {
URL descriptorURL = service.getDescriptorURL(this);
if (descriptorURL != null) {
int port = descriptorURL.getPort();
if (port != -1) {
return port;
}
}
return null;
}
}