Search in sources :

Example 1 with Action

use of org.cybergarage.upnp.Action in project felix by apache.

the class BuildDevice method addServices.

/**
 * Method used to create a new Service in CyberLink world without creating the XML
 *
 * @param id ServiceId
 * @param devUPnP the CyberLink device that where the new Service will be created
 * @param sr ServiceReference to OSGi Device that used as source of the information
 *              for the creation of the device
 */
private static void addServices(String id, Device devUPnP, ServiceReference sr) {
    UPnPDevice devOSGi = (UPnPDevice) Activator.bc.getService(sr);
    if (devOSGi == null) {
        // added by twa to prevent a null pointer exception
        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " is deregistered from the framework while is exported");
        return;
    }
    UPnPService[] services = devOSGi.getServices();
    if (services == null || services.length == 0)
        return;
    for (int i = 0; i < services.length; i++) {
        Service ser = new Service();
        devUPnP.addService(ser);
        ser.setServiceType(services[i].getType());
        ser.setServiceID(services[i].getId());
        ser.setSCPDURL(id + "/service/" + i + "/gen-desc.xml");
        ser.setDescriptionURL(id + "/service/" + i + "/gen-desc.xml");
        ser.setControlURL(id + "/service/" + i + "/ctrl");
        ser.setEventSubURL(id + "/service/" + i + "/event");
        UPnPAction[] actions = services[i].getActions();
        for (int j = 0; j < actions.length; j++) {
            boolean valid = true;
            Action act = new Action(ser.getServiceNode());
            act.setName(actions[j].getName());
            ArgumentList al = new ArgumentList();
            String[] names = actions[j].getInputArgumentNames();
            if (names != null) {
                for (int k = 0; k < names.length; k++) {
                    UPnPStateVariable variable = actions[j].getStateVariable(names[k]);
                    if (variable == null) {
                        /*
                             * //TODO Create a stict and relaxed behavior of the base driver which 
                             * export as much it can or export only 100% complaint UPnPDevice service 
                             */
                        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " contains the action " + actions[j].getName() + " with the Input argument " + names[k] + " not related to any UPnPStateVariable. Thus this action won't be exported");
                        valid = false;
                        break;
                    }
                    Argument a = new Argument();
                    a.setDirection(Argument.IN);
                    a.setName(names[k]);
                    a.setRelatedStateVariableName(variable.getName());
                    al.add(a);
                }
            }
            names = actions[j].getOutputArgumentNames();
            if (names != null && valid) {
                for (int k = 0; k < names.length; k++) {
                    UPnPStateVariable variable = actions[j].getStateVariable(names[k]);
                    if (variable == null) {
                        /*
                             * //TODO Create a stict and relaxed behavior of the base driver which 
                             * export as much it can or export only 100% complaint UPnPDevice service 
                             */
                        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " contains the action " + actions[j].getName() + " with the Output argument " + names[k] + " not related to any UPnPStateVariable. Thus this action won't be exported");
                    }
                    Argument a = new Argument();
                    a.setDirection(Argument.OUT);
                    a.setName(names[k]);
                    a.setRelatedStateVariableName(variable.getName());
                    al.add(a);
                }
            }
            if (valid) {
                act.setArgumentList(al);
                ser.addAction(act);
            }
        }
        UPnPStateVariable[] vars = services[i].getStateVariables();
        for (int j = 0; j < vars.length; j++) {
            StateVariable var = new StateVariable();
            var.setDataType(vars[j].getUPnPDataType());
            var.setName(vars[j].getName());
            var.setSendEvents(vars[j].sendsEvents());
            String[] values = vars[j].getAllowedValues();
            if (values != null) {
                AllowedValueList avl = new AllowedValueList(values);
                var.setAllowedValueList(avl);
            } else if (vars[j].getMaximum() != null) {
                AllowedValueRange avr = new AllowedValueRange(vars[j].getMaximum(), vars[j].getMinimum(), vars[j].getStep());
                var.setAllowedValueRange(avr);
            }
            if (vars[j].getDefaultValue() != null)
                try {
                    var.setDefaultValue(Converter.toString(vars[j].getDefaultValue(), vars[j].getUPnPDataType()));
                } catch (Exception ignored) {
                }
            ser.addStateVariable(var);
        }
        Activator.bc.ungetService(sr);
    }
}
Also used : UPnPAction(org.osgi.service.upnp.UPnPAction) Action(org.cybergarage.upnp.Action) AllowedValueList(org.cybergarage.upnp.AllowedValueList) AllowedValueRange(org.cybergarage.upnp.AllowedValueRange) Argument(org.cybergarage.upnp.Argument) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) StateVariable(org.cybergarage.upnp.StateVariable) UPnPService(org.osgi.service.upnp.UPnPService) Service(org.cybergarage.upnp.Service) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) ArgumentList(org.cybergarage.upnp.ArgumentList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UPnPDevice(org.osgi.service.upnp.UPnPDevice) UPnPService(org.osgi.service.upnp.UPnPService) UPnPAction(org.osgi.service.upnp.UPnPAction)

Example 2 with Action

use of org.cybergarage.upnp.Action in project i2p.i2p by i2p.

the class UPnP method removeMapping.

/**
 * blocking
 */
private boolean removeMapping(String protocol, int port, ForwardPort fp, boolean noLog) {
    Service service;
    synchronized (lock) {
        if (isDisabled || !isNATPresent()) {
            _log.error("Can't removeMapping: " + isDisabled + " " + isNATPresent() + " " + _router);
            return false;
        }
        service = _service;
    }
    Action remove = service.getAction("DeletePortMapping");
    if (remove == null) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Couldn't find DeletePortMapping action!");
        return false;
    }
    // remove.setArgumentValue("NewRemoteHost", "");
    remove.setArgumentValue("NewExternalPort", port);
    remove.setArgumentValue("NewProtocol", protocol);
    boolean retval = remove.postControlAction();
    synchronized (lock) {
        portsForwarded.remove(fp);
    }
    if (_log.shouldLog(Log.WARN) && !noLog)
        _log.warn("UPnP: Removed mapping for " + fp.name + " " + port + " / " + protocol);
    return retval;
}
Also used : Action(org.cybergarage.upnp.Action) Service(org.cybergarage.upnp.Service)

Example 3 with Action

use of org.cybergarage.upnp.Action in project i2p.i2p by i2p.

the class UPnP method addMapping.

/**
 *  This always requests that the external port == the internal port, for now.
 *  Blocking!
 */
private boolean addMapping(String protocol, int port, String description, ForwardPort fp) {
    Service service;
    synchronized (lock) {
        if (isDisabled || !isNATPresent() || _router == null) {
            _log.error("Can't addMapping: " + isDisabled + " " + isNATPresent() + " " + _router);
            return false;
        }
        service = _service;
    }
    // Just in case...
    // this confuses my linksys? - zzz
    // removeMapping(protocol, port, fp, true);
    Action add = service.getAction("AddPortMapping");
    if (add == null) {
        if (_serviceLacksAPM) {
            if (_log.shouldLog(Log.WARN))
                _log.warn("Couldn't find AddPortMapping action!");
        } else {
            _serviceLacksAPM = true;
            _log.logAlways(Log.WARN, "UPnP device does not support port forwarding");
        }
        return false;
    }
    add.setArgumentValue("NewRemoteHost", "");
    add.setArgumentValue("NewExternalPort", port);
    // bugfix, see below for details
    String intf = _router.getInterfaceAddress();
    String us = getOurAddress(intf);
    if (_log.shouldLog(Log.WARN) && !us.equals(intf))
        _log.warn("Requesting port forward to " + us + ':' + port + " when cybergarage wanted " + intf);
    add.setArgumentValue("NewInternalClient", us);
    add.setArgumentValue("NewInternalPort", port);
    add.setArgumentValue("NewProtocol", protocol);
    add.setArgumentValue("NewPortMappingDescription", description);
    add.setArgumentValue("NewEnabled", "1");
    add.setArgumentValue("NewLeaseDuration", 0);
    boolean rv = add.postControlAction();
    if (rv) {
        synchronized (lock) {
            portsForwarded.add(fp);
        }
    }
    int level = rv ? Log.INFO : Log.WARN;
    if (_log.shouldLog(level)) {
        StringBuilder buf = new StringBuilder();
        buf.append("AddPortMapping result for ").append(protocol).append(" port ").append(port);
        // Not sure which of these has the good info
        UPnPStatus status = add.getStatus();
        if (status != null)
            buf.append(" Status: ").append(status.getCode()).append(' ').append(status.getDescription());
        status = add.getControlStatus();
        if (status != null)
            buf.append(" ControlStatus: ").append(status.getCode()).append(' ').append(status.getDescription());
        _log.log(level, buf.toString());
    }
    return rv;
}
Also used : Action(org.cybergarage.upnp.Action) UPnPStatus(org.cybergarage.upnp.UPnPStatus) Service(org.cybergarage.upnp.Service) ControlPoint(org.cybergarage.upnp.ControlPoint)

Example 4 with Action

use of org.cybergarage.upnp.Action in project i2p.i2p by i2p.

the class UPnP method getDownstreamMaxBitRate.

/**
 * @return the reported downstream bit rate in bits per second. -1 if it's not available. Blocking.
 */
private int getDownstreamMaxBitRate() {
    Service service;
    synchronized (lock) {
        if (!isNATPresent() || thinksWeAreDoubleNatted)
            return -1;
        service = _service;
    }
    Action getIP = service.getAction("GetLinkLayerMaxBitRates");
    if (getIP == null || !getIP.postControlAction())
        return -1;
    Argument a = getIP.getOutputArgumentList().getArgument("NewDownstreamMaxBitRate");
    if (a == null)
        return -1;
    try {
        return Integer.parseInt(a.getValue());
    } catch (NumberFormatException nfe) {
        return -1;
    }
}
Also used : Action(org.cybergarage.upnp.Action) Argument(org.cybergarage.upnp.Argument) Service(org.cybergarage.upnp.Service)

Example 5 with Action

use of org.cybergarage.upnp.Action in project i2p.i2p by i2p.

the class UPnP method listActions.

/**
 * debug only
 */
private static void listActions(Service service, StringBuilder sb) {
    ActionList al = service.getActionList();
    sb.append("<ul>");
    for (int i = 0; i < al.size(); i++) {
        Action action = al.getAction(i);
        if (action == null)
            continue;
        sb.append("<li>").append(DataHelper.escapeHTML(action.getName()));
        listActionsArguments(action, sb);
        sb.append("</li>");
    }
    sb.append("</ul>");
}
Also used : Action(org.cybergarage.upnp.Action) ActionList(org.cybergarage.upnp.ActionList) ControlPoint(org.cybergarage.upnp.ControlPoint)

Aggregations

Action (org.cybergarage.upnp.Action)8 Service (org.cybergarage.upnp.Service)6 Argument (org.cybergarage.upnp.Argument)5 ControlPoint (org.cybergarage.upnp.ControlPoint)2 ActionList (org.cybergarage.upnp.ActionList)1 AllowedValueList (org.cybergarage.upnp.AllowedValueList)1 AllowedValueRange (org.cybergarage.upnp.AllowedValueRange)1 ArgumentList (org.cybergarage.upnp.ArgumentList)1 StateVariable (org.cybergarage.upnp.StateVariable)1 UPnPStatus (org.cybergarage.upnp.UPnPStatus)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 UPnPAction (org.osgi.service.upnp.UPnPAction)1 UPnPDevice (org.osgi.service.upnp.UPnPDevice)1 UPnPService (org.osgi.service.upnp.UPnPService)1 UPnPStateVariable (org.osgi.service.upnp.UPnPStateVariable)1