Search in sources :

Example 1 with UPnPRootDeviceImpl

use of com.biglybt.net.upnp.impl.device.UPnPRootDeviceImpl in project BiglyBT by BiglySoftware.

the class UPnPSSWANConnectionImpl method addPortMapping.

@Override
public void addPortMapping(// false -> UDP
boolean tcp, int port, String description) throws UPnPException {
    UPnPAction act = service.getAction("AddPortMapping");
    if (act == null) {
        log("Action 'AddPortMapping' not supported, binding not established");
    } else {
        UPnPActionInvocation add_inv = act.getInvocation();
        // "" = wildcard for hosts, 0 = wildcard for ports
        add_inv.addArgument("NewRemoteHost", "");
        add_inv.addArgument("NewExternalPort", "" + port);
        add_inv.addArgument("NewProtocol", tcp ? "TCP" : "UDP");
        add_inv.addArgument("NewInternalPort", "" + port);
        add_inv.addArgument("NewInternalClient", service.getDevice().getRootDevice().getLocalAddress().getHostAddress());
        add_inv.addArgument("NewEnabled", "1");
        add_inv.addArgument("NewPortMappingDescription", description);
        // 0 -> infinite (?)
        add_inv.addArgument("NewLeaseDuration", "0");
        boolean ok = false;
        try {
            add_inv.invoke();
            ok = true;
        } catch (UPnPException original_error) {
            try {
                log("Problem when adding port mapping - will try to see if an existing mapping is in the way");
                deletePortMapping(tcp, port);
            } catch (Throwable e) {
                throw (original_error);
            }
            add_inv.invoke();
            ok = true;
        } finally {
            ((UPnPRootDeviceImpl) service.getDevice().getRootDevice()).portMappingResult(ok);
            for (int i = 0; i < listeners.size(); i++) {
                UPnPWANConnectionListener listener = (UPnPWANConnectionListener) listeners.get(i);
                try {
                    listener.mappingResult(this, ok);
                } catch (Throwable e) {
                    Debug.printStackTrace(e);
                }
            }
        }
        try {
            class_mon.enter();
            Iterator it = mappings.iterator();
            while (it.hasNext()) {
                portMapping m = (portMapping) it.next();
                if (m.getExternalPort() == port && m.isTCP() == tcp) {
                    it.remove();
                }
            }
            mappings.add(new portMapping(port, tcp, "", description));
        } finally {
            class_mon.exit();
        }
    }
}
Also used : UPnPRootDeviceImpl(com.biglybt.net.upnp.impl.device.UPnPRootDeviceImpl) Iterator(java.util.Iterator) UPnPWANConnectionListener(com.biglybt.net.upnp.services.UPnPWANConnectionListener)

Example 2 with UPnPRootDeviceImpl

use of com.biglybt.net.upnp.impl.device.UPnPRootDeviceImpl in project BiglyBT by BiglySoftware.

the class UPnPImpl method rootDiscovered.

@Override
public void rootDiscovered(final NetworkInterface network_interface, final InetAddress local_address, final String usn, final URL location) {
    try {
        rd_listeners_mon.enter();
        if (device_dispatcher_pending.contains(usn)) {
            return;
        }
        if (device_dispatcher_pending.size() > 512) {
            Debug.out("Device dispatcher queue is full - dropping discovery of " + usn + "/" + location);
        }
        device_dispatcher_pending.add(usn);
    } finally {
        rd_listeners_mon.exit();
    }
    device_dispatcher.run(new AERunnable() {

        @Override
        public void runSupport() {
            final UPnPRootDeviceImpl old_root_device;
            try {
                rd_listeners_mon.enter();
                old_root_device = (UPnPRootDeviceImpl) root_locations.get(usn);
                device_dispatcher_pending.remove(usn);
            } finally {
                rd_listeners_mon.exit();
            }
            if (old_root_device != null) {
                if (!old_root_device.getNetworkInterface().getName().equals(network_interface.getName())) {
                    if (old_root_device.addAlternativeLocation(location)) {
                        log("Adding alternative location " + location + " to " + usn);
                    }
                    return;
                }
                if (old_root_device.getLocation().equals(location)) {
                    return;
                }
            }
            if (old_root_device != null) {
                try {
                    // not the best "atomic" code here but it'll do as the code that adds roots (this)
                    // is single threaded via the dispatcher
                    rd_listeners_mon.enter();
                    root_locations.remove(usn);
                } finally {
                    rd_listeners_mon.exit();
                }
                old_root_device.destroy(true);
            }
            List listeners;
            try {
                rd_listeners_mon.enter();
                listeners = new ArrayList(rd_listeners);
            } finally {
                rd_listeners_mon.exit();
            }
            for (int i = 0; i < listeners.size(); i++) {
                try {
                    if (!((UPnPListener) listeners.get(i)).deviceDiscovered(usn, location)) {
                        return;
                    }
                } catch (Throwable e) {
                    Debug.printStackTrace(e);
                }
            }
            log("UPnP: root discovered: usn=" + usn + ", location=" + location + ", ni=" + network_interface.getName() + ",local=" + local_address.toString());
            try {
                UPnPRootDeviceImpl new_root_device = new UPnPRootDeviceImpl(UPnPImpl.this, network_interface, local_address, usn, location);
                try {
                    rd_listeners_mon.enter();
                    root_locations.put(usn, new_root_device);
                    listeners = new ArrayList(rd_listeners);
                } finally {
                    rd_listeners_mon.exit();
                }
                for (int i = 0; i < listeners.size(); i++) {
                    try {
                        ((UPnPListener) listeners.get(i)).rootDeviceFound(new_root_device);
                    } catch (Throwable e) {
                        Debug.printStackTrace(e);
                    }
                }
            } catch (UPnPException e) {
                String message = e.getMessage();
                String msg = message == null ? Debug.getNestedExceptionMessageAndStack(e) : message;
                adapter.log(msg);
            }
        }
    });
}
Also used : UPnPRootDeviceImpl(com.biglybt.net.upnp.impl.device.UPnPRootDeviceImpl)

Aggregations

UPnPRootDeviceImpl (com.biglybt.net.upnp.impl.device.UPnPRootDeviceImpl)2 UPnPWANConnectionListener (com.biglybt.net.upnp.services.UPnPWANConnectionListener)1 Iterator (java.util.Iterator)1