Search in sources :

Example 1 with UPnPStatus

use of org.cybergarage.upnp.UPnPStatus 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 2 with UPnPStatus

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

the class UPnPActionImpl method invoke.

/* (non-Javadoc)
	 * @see org.osgi.service.upnp.UPnPAction#invoke(java.util.Dictionary)
	 */
public Dictionary invoke(Dictionary args) throws Exception {
    /*TODO 
		check if I have understood wath this method should do
		*/
    /*
		 * I look for argument and value and then I add them to ArgumentList
		 */
    ArgumentList argsList = new ArgumentList();
    argsList = act.getInputArgumentList();
    for (int i = 0; i < argsList.size(); i++) {
        /*			
			 * I assert that .getArgument(i) will return to me an Argument with only the name of the
			 * Argument and not it's value. I'll set the associated value by myself and
			 * Also I assert that the Argument are ordered
			 */
        Argument argument = argsList.getArgument(i);
        String argumentName = argument.getName();
        // String relateVar=argument.getRelatedStateVariableName();
        UPnPStateVariable stateVar = this.getStateVariable(argumentName);
        String upnpType = stateVar.getUPnPDataType();
        /*Class javaClass=stateVar.getJavaDataType();*/
        // setting the value related to the input argument
        argument.setValue(Converter.toString(args.get(argumentName), upnpType));
    }
    act.setInArgumentValues(argsList);
    if (act.postControlAction() == true) {
        // TODO check what happen if I don't have any output argument
        Properties outDic = new Properties();
        ArgumentList outArgs = act.getOutputArgumentList();
        if (outArgs.size() == 0) {
            return null;
        }
        for (int i = 0; i < outArgs.size(); i++) {
            Argument argument = outArgs.getArgument(i);
            String argumentName = outArgs.getArgument(i).getName();
            // String relateVar=argument.getRelatedStateVariableName();
            UPnPStateVariable stateVar = getStateVariable(argumentName);
            // String javaType=stateVar.getJavaDataType().getName();
            // TODO rember to catch number exception
            String upnpType = stateVar.getUPnPDataType();
            outDic.put(argumentName, Converter.parseString(argument.getValue(), upnpType));
        }
        return outDic;
    } else {
        UPnPStatus controlStatus = act.getControlStatus();
        throw new UPnPException(controlStatus.getCode(), controlStatus.getDescription());
    }
}
Also used : Argument(org.cybergarage.upnp.Argument) UPnPStatus(org.cybergarage.upnp.UPnPStatus) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) ArgumentList(org.cybergarage.upnp.ArgumentList) Properties(java.util.Properties) UPnPException(org.osgi.service.upnp.UPnPException)

Aggregations

UPnPStatus (org.cybergarage.upnp.UPnPStatus)2 Properties (java.util.Properties)1 Action (org.cybergarage.upnp.Action)1 Argument (org.cybergarage.upnp.Argument)1 ArgumentList (org.cybergarage.upnp.ArgumentList)1 ControlPoint (org.cybergarage.upnp.ControlPoint)1 Service (org.cybergarage.upnp.Service)1 UPnPException (org.osgi.service.upnp.UPnPException)1 UPnPStateVariable (org.osgi.service.upnp.UPnPStateVariable)1