From 39a177649b9c5e3736dcffa69ea82a6ef68c6bf3 Mon Sep 17 00:00:00 2001 From: lotus Date: Sat, 28 Feb 2009 14:36:23 +0000 Subject: [PATCH] * added upnp listener for devices that do not respond to discovery but advertise themselves * moved package git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@5659 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- htroot/ConfigBasic.java | 2 +- source/de/anomic/{tools => net}/UPnP.java | 82 ++++++++++++++++++- .../de/anomic/plasma/plasmaSwitchboard.java | 2 +- 3 files changed, 82 insertions(+), 4 deletions(-) rename source/de/anomic/{tools => net}/UPnP.java (57%) diff --git a/htroot/ConfigBasic.java b/htroot/ConfigBasic.java index 2c1ed09d1..428bbfa0a 100644 --- a/htroot/ConfigBasic.java +++ b/htroot/ConfigBasic.java @@ -34,6 +34,7 @@ import de.anomic.data.translator; import de.anomic.http.httpRequestHeader; import de.anomic.http.httpd; import de.anomic.http.httpdFileHandler; +import de.anomic.net.UPnP; import de.anomic.plasma.plasmaSwitchboard; import de.anomic.plasma.plasmaSwitchboardConstants; import de.anomic.server.serverCore; @@ -41,7 +42,6 @@ import de.anomic.server.serverDomains; import de.anomic.server.serverInstantBusyThread; import de.anomic.server.serverObjects; import de.anomic.server.serverSwitch; -import de.anomic.tools.UPnP; import de.anomic.yacy.yacySeed; public class ConfigBasic { diff --git a/source/de/anomic/tools/UPnP.java b/source/de/anomic/net/UPnP.java similarity index 57% rename from source/de/anomic/tools/UPnP.java rename to source/de/anomic/net/UPnP.java index b50f1cd7f..19010a95e 100644 --- a/source/de/anomic/tools/UPnP.java +++ b/source/de/anomic/net/UPnP.java @@ -24,15 +24,21 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -package de.anomic.tools; +package de.anomic.net; import java.io.IOException; import java.net.InetAddress; +import java.net.MalformedURLException; +import java.net.URL; +import net.sbbi.upnp.DiscoveryAdvertisement; +import net.sbbi.upnp.DiscoveryEventHandler; +import net.sbbi.upnp.devices.UPNPRootDevice; import net.sbbi.upnp.impls.InternetGatewayDevice; import net.sbbi.upnp.messages.UPNPResponseException; import de.anomic.kelondro.util.Log; import de.anomic.plasma.plasmaSwitchboard; +import de.anomic.plasma.plasmaSwitchboardConstants; public class UPnP { @@ -48,14 +54,28 @@ public class UPnP { private static int mappedPort = 0; private static String localHostIP = null;; + /* Discovery message sender IP /10.100.100.2 does not match device description IP /192.168.1.254 skipping message, + set the net.sbbi.upnp.ddos.matchip system property to false to avoid this check + static { + System.setProperty("net.sbbi.upnp.ddos.matchip", "false"); + } */ + public static void setSb(plasmaSwitchboard switchboard) { sb = switchboard; } + public static boolean setIGDs(InternetGatewayDevice[] igds) { + if(IGDs == null) { + IGDs = igds; // set only once to prevent many same devices by advertisement events + return true; + } + return false; + } + private static boolean init() { boolean init = true; try { - IGDs = InternetGatewayDevice.getDevices(discoveryTimeout); + if (IGDs == null) IGDs = InternetGatewayDevice.getDevices(discoveryTimeout); localHostIP = InetAddress.getLocalHost().getHostAddress(); } catch (IOException e) { init = false; @@ -67,6 +87,8 @@ public class UPnP { } else { log.logInfo("no device found"); init = false; + log.logInfo("listening for device"); + Listener.register(); } return init; @@ -132,5 +154,61 @@ public class UPnP { deletePortMapping(); // unmap deletePortMapping(); // nothing } + + /** + * register devices that do not respond to discovery but advertise themselves + */ + public static class Listener { + + private final static Handler handler = new Handler(); + private final static String devicetype = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"; + + public static void register() { + try { + DiscoveryAdvertisement.getInstance().registerEvent(DiscoveryAdvertisement.EVENT_SSDP_ALIVE, devicetype, handler); +// DiscoveryAdvertisement.getInstance().registerEvent(DiscoveryAdvertisement.EVENT_SSDP_BYE_BYE, devicetype, handler); + } catch (IOException e) {} + } + + public static void unregister() { + DiscoveryAdvertisement.getInstance().unRegisterEvent(DiscoveryAdvertisement.EVENT_SSDP_ALIVE, devicetype, handler); +// DiscoveryAdvertisement.getInstance().unRegisterEvent(DiscoveryAdvertisement.EVENT_SSDP_BYE_BYE, devicetype, handler); + } + + private static class Handler implements DiscoveryEventHandler { + + private final Log log = UPnP.log; + + public void eventSSDPAlive(String usn, String udn, String nt, String maxAge, URL location) { + InternetGatewayDevice[] newIGD = { null }; + boolean error = false; + String errorMsg = null; + try { + newIGD[0] = new InternetGatewayDevice(new UPNPRootDevice(location, maxAge, "", usn, udn)); + } catch (UnsupportedOperationException e) { + error = true; + errorMsg = e.getMessage(); + } catch (MalformedURLException e) { + error = true; + errorMsg = e.getMessage(); + } catch (IllegalStateException e) { + error = true; + errorMsg = e.getMessage(); + } + if (error && errorMsg != null) + log.logSevere("eventSSDPAlive: " + errorMsg); + if (newIGD[0] == null) return; + log.logInfo("discovered device: " + newIGD[0].getIGDRootDevice().getFriendlyName()); + if (UPnP.setIGDs(newIGD) && + plasmaSwitchboard.getSwitchboard().getConfigBool(plasmaSwitchboardConstants.UPNP_ENABLED, false)) + UPnP.addPortMapping(); + Listener.unregister(); + } + + public void eventSSDPByeBye(String usn, String udn, String nt) {} + + } + + } } diff --git a/source/de/anomic/plasma/plasmaSwitchboard.java b/source/de/anomic/plasma/plasmaSwitchboard.java index ec3f41757..4866697e4 100644 --- a/source/de/anomic/plasma/plasmaSwitchboard.java +++ b/source/de/anomic/plasma/plasmaSwitchboard.java @@ -147,6 +147,7 @@ import de.anomic.kelondro.util.FileUtils; import de.anomic.kelondro.util.Log; import de.anomic.kelondro.util.MemoryControl; import de.anomic.kelondro.util.SetTools; +import de.anomic.net.UPnP; import de.anomic.plasma.parser.ParserException; import de.anomic.server.serverAbstractSwitch; import de.anomic.server.serverBusyThread; @@ -160,7 +161,6 @@ import de.anomic.server.serverProfiling; import de.anomic.server.serverSemaphore; import de.anomic.server.serverSwitch; import de.anomic.server.serverThread; -import de.anomic.tools.UPnP; import de.anomic.tools.crypt; import de.anomic.yacy.yacyClient; import de.anomic.yacy.yacyCore;