upnp first cut

This commit is contained in:
zzz
2009-02-19 21:42:59 +00:00
parent fd32d77976
commit 312e6071d7
123 changed files with 17920 additions and 1 deletions

View File

@ -67,7 +67,17 @@ Public domain except as listed below:
Router:
Public domain
Public domain except as listed below:
UPnP.java:
From freenet
See licenses/LICENSE-GPLv2.txt
UPnP subsystem:
Copyright (C) 2003-2006 Satoshi Konno
See licenses/LICENSE-UPnP.txt
XMLPull library used by UPnP:
See licenses/LICENSE-Apache2.0.txt

12
licenses/LICENSE-UPnP.txt Normal file
View File

@ -0,0 +1,12 @@
Copyright (C) 2003-2006 Satoshi Konno
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -14,4 +14,5 @@ import net.i2p.data.i2np.I2NPMessage;
public interface TransportEventListener {
public void messageReceived(I2NPMessage message, RouterIdentity fromRouter, Hash fromRouterHash);
public void transportAddressChanged();
}

View File

@ -410,6 +410,8 @@ public abstract class TransportImpl implements Transport {
protected void replaceAddress(RouterAddress address) {
// _log.error("Replacing address for " + getStyle() + " was " + _currentAddress + " now " + address);
_currentAddress = address;
if (_listener != null)
_listener.transportAddressChanged();
if ("SSU".equals(getStyle()))
_context.commSystem().notifyReplaceAddress(address);
}

View File

@ -35,6 +35,7 @@ public class TransportManager implements TransportEventListener {
private Log _log;
private List _transports;
private RouterContext _context;
private UPnPManager _upnpManager;
private final static String PROP_ENABLE_UDP = "i2np.udp.enable";
private final static String PROP_ENABLE_NTCP = "i2np.ntcp.enable";
@ -51,6 +52,7 @@ public class TransportManager implements TransportEventListener {
_context.statManager().createRateStat("transport.bidFailNoTransports", "Could not attempt to bid on message, as none of the transports could attempt it", "Transport", new long[] { 60*1000, 10*60*1000, 60*60*1000 });
_context.statManager().createRateStat("transport.bidFailAllTransports", "Could not attempt to bid on message, as all of the transports had failed", "Transport", new long[] { 60*1000, 10*60*1000, 60*60*1000 });
_transports = new ArrayList();
_upnpManager = new UPnPManager(context);
}
public void addTransport(Transport transport) {
@ -91,6 +93,7 @@ public class TransportManager implements TransportEventListener {
}
public void startListening() {
_upnpManager.start();
configTransports();
_log.debug("Starting up the transport manager");
for (int i = 0; i < _transports.size(); i++) {
@ -109,6 +112,7 @@ public class TransportManager implements TransportEventListener {
}
public void stopListening() {
_upnpManager.stop();
for (int i = 0; i < _transports.size(); i++) {
((Transport)_transports.get(i)).stopListening();
}
@ -341,6 +345,10 @@ public class TransportManager implements TransportEventListener {
}
}
public void transportAddressChanged() {
_upnpManager.update(getAddresses());
}
public List getMostRecentErrorMessages() {
List rv = new ArrayList(16);
for (int i = 0; i < _transports.size(); i++) {

View File

@ -0,0 +1,624 @@
/* This code is part of Freenet. It is distributed under the GNU General
* Public License, version 2 (or at your option any later version). See
* http://www.gnu.org/ for further details of the GPL. */
package net.i2p.router.transport;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import net.i2p.util.Log;
import net.i2p.I2PAppContext;
import net.i2p.router.RouterContext;
import org.cybergarage.upnp.Action;
import org.cybergarage.upnp.ActionList;
import org.cybergarage.upnp.Argument;
import org.cybergarage.upnp.ArgumentList;
import org.cybergarage.upnp.ControlPoint;
import org.cybergarage.upnp.Device;
import org.cybergarage.upnp.DeviceList;
import org.cybergarage.upnp.Service;
import org.cybergarage.upnp.ServiceList;
import org.cybergarage.upnp.ServiceStateTable;
import org.cybergarage.upnp.StateVariable;
import org.cybergarage.upnp.device.DeviceChangeListener;
import org.freenetproject.DetectedIP;
import org.freenetproject.ForwardPort;
import org.freenetproject.ForwardPortCallback;
import org.freenetproject.ForwardPortStatus;
/**
* This (and all in org/freenet, org/cybergarage, org/xmlpull)
* grabbed from freenet SVN, mid-February 2009 by zzz.
* This file modded somewhat to remove freenet-specific stuff,
* but most of the glue to I2P is in UPnPManager (which was written
* from scratch and is not the Limewire one referred to below).
*
* ==================
*
* This plugin implements UP&P support on a Freenet node.
*
* @author Florent Daigni&egrave;re &lt;nextgens@freenetproject.org&gt;
*
*
* some code has been borrowed from Limewire : @see com.limegroup.gnutella.UPnPManager
*
* @see http://www.upnp.org/
* @see http://en.wikipedia.org/wiki/Universal_Plug_and_Play
*
* TODO: Support multiple IGDs ?
* TODO: Advertise the node like the MDNS plugin does
* TODO: Implement EventListener and react on ip-change
*/
public class UPnP extends ControlPoint implements DeviceChangeListener {
private Log _log;
private I2PAppContext _context;
/** some schemas */
private static final String ROUTER_DEVICE = "urn:schemas-upnp-org:device:InternetGatewayDevice:1";
private static final String WAN_DEVICE = "urn:schemas-upnp-org:device:WANDevice:1";
private static final String WANCON_DEVICE = "urn:schemas-upnp-org:device:WANConnectionDevice:1";
private static final String WAN_IP_CONNECTION = "urn:schemas-upnp-org:service:WANIPConnection:1";
private static final String WAN_PPP_CONNECTION = "urn:schemas-upnp-org:service:WANPPPConnection:1";
private Device _router;
private Service _service;
private boolean isDisabled = false; // We disable the plugin if more than one IGD is found
private final Object lock = new Object();
// FIXME: detect it for real and deal with it! @see #2524
private volatile boolean thinksWeAreDoubleNatted = false;
/** List of ports we want to forward */
private Set<ForwardPort> portsToForward;
/** List of ports we have actually forwarded */
private Set<ForwardPort> portsForwarded;
/** Callback to call when a forward fails or succeeds */
private ForwardPortCallback forwardCallback;
public UPnP(I2PAppContext context) {
super();
_context = context;
_log = _context.logManager().getLog(UPnP.class);
portsForwarded = new HashSet<ForwardPort>();
addDeviceChangeListener(this);
}
public void runPlugin() {
super.start();
}
public void terminate() {
unregisterPortMappings();
super.stop();
}
public DetectedIP[] getAddress() {
_log.info("UP&P.getAddress() is called \\o/");
if(isDisabled) {
_log.warn("Plugin has been disabled previously, ignoring request.");
return null;
} else if(!isNATPresent()) {
_log.warn("No UP&P device found, detection of the external ip address using the plugin has failed");
return null;
}
DetectedIP result = null;
final String natAddress = getNATAddress();
try {
InetAddress detectedIP = InetAddress.getByName(natAddress);
short status = DetectedIP.NOT_SUPPORTED;
thinksWeAreDoubleNatted = !TransportImpl.isPubliclyRoutable(detectedIP.getAddress());
// If we have forwarded a port AND we don't have a private address
if((portsForwarded.size() > 1) && (!thinksWeAreDoubleNatted))
status = DetectedIP.FULL_INTERNET;
result = new DetectedIP(detectedIP, status);
_log.warn("Successful UP&P discovery :" + result);
return new DetectedIP[] { result };
} catch (UnknownHostException e) {
_log.error("Caught an UnknownHostException resolving " + natAddress, e);
return null;
}
}
public void deviceAdded(Device dev) {
synchronized (lock) {
if(isDisabled) {
_log.warn("Plugin has been disabled previously, ignoring new device.");
return;
}
}
if(!ROUTER_DEVICE.equals(dev.getDeviceType()) || !dev.isRootDevice())
return; // Silently ignore non-IGD devices
else if(isNATPresent()) {
_log.error("We got a second IGD on the network! the plugin doesn't handle that: let's disable it.");
isDisabled = true;
synchronized(lock) {
_router = null;
_service = null;
}
stop();
return;
}
_log.warn("UP&P IGD found : " + dev.getFriendlyName());
synchronized(lock) {
_router = dev;
}
discoverService();
// We have found the device we need: stop the listener thread
stop();
synchronized(lock) {
if(_service == null) {
_log.error("The IGD device we got isn't suiting our needs, let's disable the plugin");
isDisabled = true;
_router = null;
return;
}
}
registerPortMappings();
}
private void registerPortMappings() {
Set ports;
synchronized(lock) {
ports = portsToForward;
}
if(ports == null) return;
registerPorts(ports);
}
/**
* Traverses the structure of the router device looking for the port mapping service.
*/
private void discoverService() {
synchronized (lock) {
for (Iterator iter = _router.getDeviceList().iterator();iter.hasNext();) {
Device current = (Device)iter.next();
if (!current.getDeviceType().equals(WAN_DEVICE))
continue;
DeviceList l = current.getDeviceList();
for (int i=0;i<current.getDeviceList().size();i++) {
Device current2 = l.getDevice(i);
if (!current2.getDeviceType().equals(WANCON_DEVICE))
continue;
_service = current2.getService(WAN_PPP_CONNECTION);
if(_service == null) {
_log.warn(_router.getFriendlyName()+ " doesn't seems to be using PPP; we won't be able to extract bandwidth-related informations out of it.");
_service = current2.getService(WAN_IP_CONNECTION);
if(_service == null)
_log.error(_router.getFriendlyName()+ " doesn't export WAN_IP_CONNECTION either: we won't be able to use it!");
}
return;
}
}
}
}
public boolean tryAddMapping(String protocol, int port, String description, ForwardPort fp) {
_log.warn("Registering a port mapping for " + port + "/" + protocol);
int nbOfTries = 0;
boolean isPortForwarded = false;
while(nbOfTries++ < 5) {
isPortForwarded = addMapping(protocol, port, "I2P " + description, fp);
if(isPortForwarded)
break;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {}
}
_log.warn((isPortForwarded ? "Mapping is successful!" : "Mapping has failed!") + " ("+ nbOfTries + " tries)");
return isPortForwarded;
}
public void unregisterPortMappings() {
Set ports;
synchronized(lock) {
ports = portsForwarded;
}
this.unregisterPorts(ports);
}
public void deviceRemoved(Device dev ){
synchronized (lock) {
if(_router == null) return;
if(_router.equals(dev)) {
_router = null;
_service = null;
}
}
}
/**
* @return whether we are behind an UPnP-enabled NAT/router
*/
public boolean isNATPresent() {
return _router != null && _service != null;
}
/**
* @return the external address the NAT thinks we have. Blocking.
* null if we can't find it.
*/
public String getNATAddress() {
if(!isNATPresent())
return null;
Action getIP = _service.getAction("GetExternalIPAddress");
if(getIP == null || !getIP.postControlAction())
return null;
return (getIP.getOutputArgumentList().getArgument("NewExternalIPAddress")).getValue();
}
/**
* @return the reported upstream bit rate in bits per second. -1 if it's not available. Blocking.
*/
public int getUpstramMaxBitRate() {
if(!isNATPresent() || thinksWeAreDoubleNatted)
return -1;
Action getIP = _service.getAction("GetLinkLayerMaxBitRates");
if(getIP == null || !getIP.postControlAction())
return -1;
return Integer.valueOf(getIP.getOutputArgumentList().getArgument("NewUpstreamMaxBitRate").getValue());
}
/**
* @return the reported downstream bit rate in bits per second. -1 if it's not available. Blocking.
*/
public int getDownstreamMaxBitRate() {
if(!isNATPresent() || thinksWeAreDoubleNatted)
return -1;
Action getIP = _service.getAction("GetLinkLayerMaxBitRates");
if(getIP == null || !getIP.postControlAction())
return -1;
return Integer.valueOf(getIP.getOutputArgumentList().getArgument("NewDownstreamMaxBitRate").getValue());
}
private void listStateTable(Service serv, StringBuilder sb) {
ServiceStateTable table = serv.getServiceStateTable();
sb.append("<div><small>");
for(int i=0; i<table.size(); i++) {
StateVariable current = table.getStateVariable(i);
sb.append(current.getName() + " : " + current.getValue() + "<br>");
}
sb.append("</small></div>");
}
private void listActionsArguments(Action action, StringBuilder sb) {
ArgumentList ar = action.getArgumentList();
for(int i=0; i<ar.size(); i++) {
Argument argument = ar.getArgument(i);
if(argument == null ) continue;
sb.append("<div><small>argument ("+i+") :" + argument.getName()+"</small></div>");
}
}
private void listActions(Service service, StringBuilder sb) {
ActionList al = service.getActionList();
for(int i=0; i<al.size(); i++) {
Action action = al.getAction(i);
if(action == null ) continue;
sb.append("<div>action ("+i+") :" + action.getName());
listActionsArguments(action, sb);
sb.append("</div>");
}
}
private String toString(String action, String Argument, Service serv) {
Action getIP = serv.getAction(action);
if(getIP == null || !getIP.postControlAction())
return null;
Argument ret = getIP.getOutputArgumentList().getArgument(Argument);
return ret.getValue();
}
// TODO: extend it! RTFM
private void listSubServices(Device dev, StringBuilder sb) {
ServiceList sl = dev.getServiceList();
for(int i=0; i<sl.size(); i++) {
Service serv = sl.getService(i);
if(serv == null) continue;
sb.append("<div>service ("+i+") : "+serv.getServiceType()+"<br>");
if("urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1".equals(serv.getServiceType())){
sb.append("WANCommonInterfaceConfig");
sb.append(" status: " + toString("GetCommonLinkProperties", "NewPhysicalLinkStatus", serv));
sb.append(" type: " + toString("GetCommonLinkProperties", "NewWANAccessType", serv));
sb.append(" upstream: " + toString("GetCommonLinkProperties", "NewLayer1UpstreamMaxBitRate", serv));
sb.append(" downstream: " + toString("GetCommonLinkProperties", "NewLayer1DownstreamMaxBitRate", serv) + "<br>");
}else if("urn:schemas-upnp-org:service:WANPPPConnection:1".equals(serv.getServiceType())){
sb.append("WANPPPConnection");
sb.append(" status: " + toString("GetStatusInfo", "NewConnectionStatus", serv));
sb.append(" type: " + toString("GetConnectionTypeInfo", "NewConnectionType", serv));
sb.append(" upstream: " + toString("GetLinkLayerMaxBitRates", "NewUpstreamMaxBitRate", serv));
sb.append(" downstream: " + toString("GetLinkLayerMaxBitRates", "NewDownstreamMaxBitRate", serv) + "<br>");
sb.append(" external IP: " + toString("GetExternalIPAddress", "NewExternalIPAddress", serv) + "<br>");
}else if("urn:schemas-upnp-org:service:Layer3Forwarding:1".equals(serv.getServiceType())){
sb.append("Layer3Forwarding");
sb.append("DefaultConnectionService: " + toString("GetDefaultConnectionService", "NewDefaultConnectionService", serv));
}else if(WAN_IP_CONNECTION.equals(serv.getServiceType())){
sb.append("WANIPConnection");
sb.append(" status: " + toString("GetStatusInfo", "NewConnectionStatus", serv));
sb.append(" type: " + toString("GetConnectionTypeInfo", "NewConnectionType", serv));
sb.append(" external IP: " + toString("GetExternalIPAddress", "NewExternalIPAddress", serv) + "<br>");
}else if("urn:schemas-upnp-org:service:WANEthernetLinkConfig:1".equals(serv.getServiceType())){
sb.append("WANEthernetLinkConfig");
sb.append(" status: " + toString("GetEthernetLinkStatus", "NewEthernetLinkStatus", serv) + "<br>");
}else
sb.append("~~~~~~~ "+serv.getServiceType());
listActions(serv, sb);
listStateTable(serv, sb);
sb.append("</div>");
}
}
private void listSubDev(String prefix, Device dev, StringBuilder sb){
sb.append("<div><p>Device : "+dev.getFriendlyName()+" - "+ dev.getDeviceType()+"<br>");
listSubServices(dev, sb);
DeviceList dl = dev.getDeviceList();
for(int j=0; j<dl.size(); j++) {
Device subDev = dl.getDevice(j);
if(subDev == null) continue;
sb.append("<div>");
listSubDev(dev.getFriendlyName(), subDev, sb);
sb.append("</div></div>");
}
sb.append("</p></div>");
}
/*****
public String handleHTTPGet(HTTPRequest request) throws PluginHTTPException {
if(request.isParameterSet("getDeviceCapabilities")) {
final StringBuilder sb = new StringBuilder();
sb.append("<html><head><title>UPnP report</title></head><body>");
listSubDev("WANDevice", _router, sb);
sb.append("</body></html>");
return sb.toString();
}
HTMLNode pageNode = pr.getPageMaker().getPageNode("UP&P plugin configuration page", false, null);
HTMLNode contentNode = pr.getPageMaker().getContentNode(pageNode);
if(isDisabled) {
HTMLNode disabledInfobox = contentNode.addChild("div", "class", "infobox infobox-error");
HTMLNode disabledInfoboxHeader = disabledInfobox.addChild("div", "class", "infobox-header");
HTMLNode disabledInfoboxContent = disabledInfobox.addChild("div", "class", "infobox-content");
disabledInfoboxHeader.addChild("#", "UP&P plugin report");
disabledInfoboxContent.addChild("#", "The plugin has been disabled; Do you have more than one UP&P IGD on your LAN ?");
return pageNode.generate();
} else if(!isNATPresent()) {
HTMLNode notFoundInfobox = contentNode.addChild("div", "class", "infobox infobox-warning");
HTMLNode notFoundInfoboxHeader = notFoundInfobox.addChild("div", "class", "infobox-header");
HTMLNode notFoundInfoboxContent = notFoundInfobox.addChild("div", "class", "infobox-content");
notFoundInfoboxHeader.addChild("#", "UP&P plugin report");
notFoundInfoboxContent.addChild("#", "The plugin hasn't found any UP&P aware, compatible device on your LAN.");
return pageNode.generate();
}
HTMLNode foundInfobox = contentNode.addChild("div", "class", "infobox infobox-normal");
HTMLNode foundInfoboxHeader = foundInfobox.addChild("div", "class", "infobox-header");
HTMLNode foundInfoboxContent = foundInfobox.addChild("div", "class", "infobox-content");
// FIXME L10n!
foundInfoboxHeader.addChild("#", "UP&P plugin report");
foundInfoboxContent.addChild("p", "The following device has been found : ").addChild("a", "href", "?getDeviceCapabilities").addChild("#", _router.getFriendlyName());
foundInfoboxContent.addChild("p", "Our current external ip address is : " + getNATAddress());
int downstreamMaxBitRate = getDownstreamMaxBitRate();
int upstreamMaxBitRate = getUpstramMaxBitRate();
if(downstreamMaxBitRate > 0)
foundInfoboxContent.addChild("p", "Our reported max downstream bit rate is : " + getDownstreamMaxBitRate()+ " bits/sec");
if(upstreamMaxBitRate > 0)
foundInfoboxContent.addChild("p", "Our reported max upstream bit rate is : " + getUpstramMaxBitRate()+ " bits/sec");
synchronized(lock) {
if(portsToForward != null) {
for(ForwardPort port : portsToForward) {
if(portsForwarded.contains(port)) {
foundInfoboxContent.addChild("p", "The "+port.name+" port "+port.portNumber+" / "+port.protocol+" has been forwarded successfully.");
} else {
foundInfoboxContent.addChild("p", "The "+port.name+" port "+port.portNumber+" / "+port.protocol+" has not been forwarded.");
}
}
}
}
return pageNode.generate();
}
public String handleHTTPPost(HTTPRequest request) throws PluginHTTPException {
return null;
}
***/
private boolean addMapping(String protocol, int port, String description, ForwardPort fp) {
if(isDisabled || !isNATPresent() || _router == null) {
_log.error("Can't addMapping: " + isDisabled + " " + isNATPresent() + " " + _router);
return false;
}
// Just in case...
// this confuses my linksys - zzz
// removeMapping(protocol, port, fp, true);
Action add = _service.getAction("AddPortMapping");
if(add == null) {
_log.error("Couldn't find AddPortMapping action!");
return false;
}
add.setArgumentValue("NewRemoteHost", "");
add.setArgumentValue("NewExternalPort", port);
add.setArgumentValue("NewInternalClient", _router.getInterfaceAddress());
add.setArgumentValue("NewInternalPort", port);
add.setArgumentValue("NewProtocol", protocol);
add.setArgumentValue("NewPortMappingDescription", description);
add.setArgumentValue("NewEnabled","1");
add.setArgumentValue("NewLeaseDuration", 0);
if(add.postControlAction()) {
synchronized(lock) {
portsForwarded.add(fp);
}
return true;
} else return false;
}
private boolean removeMapping(String protocol, int port, ForwardPort fp, boolean noLog) {
if(isDisabled || !isNATPresent())
return false;
Action remove = _service.getAction("DeletePortMapping");
if(remove == null) {
_log.error("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(!noLog)
_log.warn("UPnP: Removed mapping for "+fp.name+" "+port+" / "+protocol);
return retval;
}
public void onChangePublicPorts(Set<ForwardPort> ports, ForwardPortCallback cb) {
Set<ForwardPort> portsToDumpNow = null;
Set<ForwardPort> portsToForwardNow = null;
_log.warn("UP&P Forwarding "+ports.size()+" ports...");
synchronized(lock) {
if(forwardCallback != null && forwardCallback != cb && cb != null) {
_log.error("ForwardPortCallback changed from "+forwardCallback+" to "+cb+" - using new value, but this is very strange!");
}
forwardCallback = cb;
if(portsToForward == null || portsToForward.isEmpty()) {
portsToForward = ports;
portsToForwardNow = ports;
portsToDumpNow = null;
} else if(ports == null || ports.isEmpty()) {
portsToDumpNow = portsToForward;
portsToForward = ports;
portsToForwardNow = null;
} else {
// Some ports to keep, some ports to dump
// Ports in ports but not in portsToForwardNow we must forward
// Ports in portsToForwardNow but not in ports we must dump
for(ForwardPort port: ports) {
if(portsToForward.contains(port)) {
// We have forwarded it, and it should be forwarded, cool.
} else {
// Needs forwarding
if(portsToForwardNow == null) portsToForwardNow = new HashSet<ForwardPort>();
portsToForwardNow.add(port);
}
}
for(ForwardPort port : portsToForward) {
if(ports.contains(port)) {
// Should be forwarded, has been forwarded, cool.
} else {
// Needs dropping
if(portsToDumpNow == null) portsToDumpNow = new HashSet<ForwardPort>();
portsToDumpNow.add(port);
}
}
portsToForward = ports;
}
if(_router == null) return; // When one is found, we will do the forwards
}
if(portsToDumpNow != null)
unregisterPorts(portsToDumpNow);
if(portsToForwardNow != null)
registerPorts(portsToForwardNow);
}
private void registerPorts(Set<ForwardPort> portsToForwardNow) {
for(ForwardPort port : portsToForwardNow) {
String proto;
if(port.protocol == ForwardPort.PROTOCOL_UDP_IPV4)
proto = "UDP";
else if(port.protocol == ForwardPort.PROTOCOL_TCP_IPV4)
proto = "TCP";
else {
HashMap<ForwardPort, ForwardPortStatus> map = new HashMap<ForwardPort, ForwardPortStatus>();
map.put(port, new ForwardPortStatus(ForwardPortStatus.DEFINITE_FAILURE, "Protocol not supported", port.portNumber));
forwardCallback.portForwardStatus(map);
continue;
}
if(tryAddMapping(proto, port.portNumber, port.name, port)) {
HashMap<ForwardPort, ForwardPortStatus> map = new HashMap<ForwardPort, ForwardPortStatus>();
map.put(port, new ForwardPortStatus(ForwardPortStatus.MAYBE_SUCCESS, "Port apparently forwarded by UPnP", port.portNumber));
forwardCallback.portForwardStatus(map);
continue;
} else {
HashMap<ForwardPort, ForwardPortStatus> map = new HashMap<ForwardPort, ForwardPortStatus>();
map.put(port, new ForwardPortStatus(ForwardPortStatus.PROBABLE_FAILURE, "UPnP port forwarding apparently failed", port.portNumber));
forwardCallback.portForwardStatus(map);
continue;
}
}
}
private void unregisterPorts(Set<ForwardPort> portsToForwardNow) {
for(ForwardPort port : portsToForwardNow) {
String proto;
if(port.protocol == ForwardPort.PROTOCOL_UDP_IPV4)
proto = "UDP";
else if(port.protocol == ForwardPort.PROTOCOL_TCP_IPV4)
proto = "TCP";
else {
// Ignore, we've already complained about it
continue;
}
removeMapping(proto, port.portNumber, port, false);
}
}
public static void main(String[] args) throws Exception {
UPnP upnp = new UPnP(I2PAppContext.getGlobalContext());
ControlPoint cp = new ControlPoint();
System.out.println("Searching for up&p devices:");
cp.start();
cp.search();
while(true) {
DeviceList list = cp.getDeviceList();
System.out.println("Found " + list.size() + " devices!");
StringBuilder sb = new StringBuilder();
Iterator<Device> it = list.iterator();
while(it.hasNext()) {
Device device = it.next();
upnp.listSubDev(device.toString(), device, sb);
System.out.println("Here is the listing for " + device.toString() + " :");
System.out.println(sb.toString());
sb = new StringBuilder();
}
System.out.println("End");
Thread.sleep(2000);
}
}
}

View File

@ -0,0 +1,115 @@
package net.i2p.router.transport;
/*
* public domain
*/
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import net.i2p.data.RouterAddress;
import net.i2p.router.RouterContext;
import net.i2p.util.Log;
import org.cybergarage.util.Debug;
import org.freenetproject.DetectedIP;
import org.freenetproject.ForwardPort;
import org.freenetproject.ForwardPortCallback;
import org.freenetproject.ForwardPortStatus;
/**
* Bridge from the I2P RouterAddress data structure to
* the freenet data structures
*
* No disable option yet.
* UPnP listens on ports 1900, 8008, and 8058 - no config option yet.
* No routerconsole support yet.
*
* @author zzz
*/
public class UPnPManager {
private Log _log;
private RouterContext _context;
private UPnP _upnp;
private UPnPCallback _upnpCallback;
private boolean _isRunning;
public UPnPManager(RouterContext context) {
_context = context;
_log = _context.logManager().getLog(UPnPManager.class);
_upnp = new UPnP(context);
_upnpCallback = new UPnPCallback();
_isRunning = false;
}
public synchronized void start() {
_log.error("UPnP Start");
Debug.on(); // UPnP stuff -> wrapper log
if (!_isRunning)
_upnp.runPlugin();
_isRunning = true;
}
public synchronized void stop() {
_log.error("UPnP Stop");
if (_isRunning)
_upnp.terminate();
_isRunning = false;
}
/** call when the ports might have changed */
public void update(Map<String, RouterAddress> addresses) {
_log.error("UPnP Update:");
if (!_isRunning)
return;
Set<ForwardPort> forwards = new HashSet(addresses.size());
for (String style : addresses.keySet()) {
RouterAddress ra = addresses.get(style);
if (ra == null)
continue;
Properties opts = ra.getOptions();
if (opts == null)
continue;
String s = opts.getProperty("port");
if (s == null)
continue;
int port = -1;
try {
port = Integer.parseInt(s);
} catch (NumberFormatException nfe) { continue; }
int protocol = -1;
if ("SSU".equals(style))
protocol = ForwardPort.PROTOCOL_UDP_IPV4;
else if ("NTCP".equals(style))
protocol = ForwardPort.PROTOCOL_TCP_IPV4;
else
continue;
_log.error("Adding: " + style + " " + port);
ForwardPort fp = new ForwardPort(style, false, protocol, port);
forwards.add(fp);
}
_upnp.onChangePublicPorts(forwards, _upnpCallback);
}
/** just logs for now */
private class UPnPCallback implements ForwardPortCallback {
/** Called to indicate status on one or more forwarded ports. */
public void portForwardStatus(Map<ForwardPort,ForwardPortStatus> statuses) {
_log.error("UPnP Callback:");
DetectedIP[] ips = _upnp.getAddress();
for (DetectedIP ip : ips) {
_log.error("External address: " + ip.publicAddress + " type: " + ip.natType);
}
for (ForwardPort fp : statuses.keySet()) {
ForwardPortStatus fps = statuses.get(fp);
_log.error(fp.name + " " + fp.protocol + " " + fp.portNumber +
" status: " + fps.status + " reason: " + fps.reasonString + " ext port: " + fps.externalPort);
}
}
}
}

View File

@ -543,6 +543,8 @@ public class NTCPTransport extends TransportImpl {
NTCPConnection con = (NTCPConnection)iter.next();
con.close();
}
// will this work?
replaceAddress(null);
}
public static final String STYLE = "NTCP";

View File

@ -589,11 +589,13 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
return true;
}
/*** infinite loop
public RouterAddress getCurrentAddress() {
if (needsRebuild())
rebuildExternalAddress(false);
return super.getCurrentAddress();
}
***/
public void messageReceived(I2NPMessage inMsg, RouterIdentity remoteIdent, Hash remoteIdentHash, long msToReceive, int bytesReceived) {
if (inMsg.getType() == DatabaseStoreMessage.MESSAGE_TYPE) {
@ -996,6 +998,9 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
public void stopListening() {
shutdown();
// will this work?
_externalAddress = null;
replaceAddress(null);
}
private boolean explicitAddressSpecified() {

View File

@ -0,0 +1,165 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File : Date.java
*
* Revision;
*
* 01/05/03
* - first revision
* 10/20/04
* - Theo Beisch <theo.beisch@gmx.de>
* - Fixed the following methods to use HOUR_OF_DAY instead of HOUR.
* getHour(), getDateString() getTimeString()
* - Fixed getInstance() to return GMT instance.
*
******************************************************************/
package org.cybergarage.http;
import java.util.Calendar;
import java.util.TimeZone;
public class Date
{
private Calendar cal;
public Date(Calendar cal)
{
this.cal = cal;
}
public Calendar getCalendar()
{
return cal;
}
////////////////////////////////////////////////
// Time
////////////////////////////////////////////////
public int getHour()
{
// Thanks for Theo Beisch (10/20/04)
return getCalendar().get(Calendar.HOUR_OF_DAY);
}
public int getMinute()
{
return getCalendar().get(Calendar.MINUTE);
}
public int getSecond()
{
return getCalendar().get(Calendar.SECOND);
}
////////////////////////////////////////////////
// paint
////////////////////////////////////////////////
public final static Date getLocalInstance()
{
return new Date(Calendar.getInstance());
}
public final static Date getInstance()
{
// Thanks for Theo Beisch (10/20/04)
return new Date(Calendar.getInstance(TimeZone.getTimeZone("GMT")));
}
////////////////////////////////////////////////
// getDateString
////////////////////////////////////////////////
public final static String toDateString(int value)
{
if (value < 10)
return "0" + Integer.toString(value);
return Integer.toString(value);
}
private final static String MONTH_STRING[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
};
public final static String toMonthString(int value)
{
value -= Calendar.JANUARY;
if (0 <= value && value < 12)
return MONTH_STRING[value];
return "";
}
private final static String WEEK_STRING[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
};
public final static String toWeekString(int value)
{
value -= Calendar.SUNDAY;
if (0 <= value && value < 7)
return WEEK_STRING[value];
return "";
}
public final static String toTimeString(int value)
{
String str = "";
if (value < 10)
str += "0";
str += Integer.toString(value);
return str;
}
public String getDateString()
{
// Thanks for Theo Beisch (10/20/04)
Calendar cal = getCalendar();
return
toWeekString(cal.get(Calendar.DAY_OF_WEEK)) +", " +
toTimeString(cal.get(Calendar.DATE)) + " " +
toMonthString(cal.get(Calendar.MONTH)) + " " +
Integer.toString(cal.get(Calendar.YEAR)) + " " +
toTimeString(cal.get(Calendar.HOUR_OF_DAY)) + ":" +
toTimeString(cal.get(Calendar.MINUTE)) + ":" +
toTimeString(cal.get(Calendar.SECOND)) + " GMT";
}
////////////////////////////////////////////////
// getTimeString
////////////////////////////////////////////////
public String getTimeString()
{
// Thanks for Theo Beisch (10/20/04)
Calendar cal = getCalendar();
return
toDateString(cal.get(Calendar.HOUR_OF_DAY)) +
(((cal.get(Calendar.SECOND) % 2) == 0) ? ":" : " ") +
toDateString(cal.get(Calendar.MINUTE));
}
}

View File

@ -0,0 +1,22 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: HTML.java
*
* Revision;
*
* 01/05/03
* - first revision.
*
******************************************************************/
package org.cybergarage.http;
public class HTML
{
public static final String CONTENT_TYPE = "text/html; charset=\"utf-8\"";
}

View File

@ -0,0 +1,210 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: HTTP.java
*
* Revision:
*
* 11/18/02
* - first revision.
* 08/30/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Problem : the method getPort should return the default http port 80 when a port is not specified
* - Description : the method is used in ControlRequest.setRequestHost() and in SubscriptionRequest.setService(). maybe the default port check could be done in these methods.
* 09/03/02
* - Added getRequestHostURL().
* 03/11/04
* - Added the following methods to send big content stream.
* post(HTTPResponse, byte[])
* post(HTTPResponse, InputStream)
* 05/26/04
* - Added NO_CATCH and MAX_AGE.
* 10/20/04
* - Brent Hills <bhills@openshores.com>
* - Added Range and MYNAME;
*
******************************************************************/
package org.cybergarage.http;
import java.net.*;
public class HTTP
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public static final String HOST = "HOST";
public static final String VERSION = "1.1";
public static final String VERSION_10 = "1.0";
public static final String VERSION_11 = "1.1";
public static final String CRLF = "\r\n";
public static final String TAB = "\t";
public static final String SOAP_ACTION = "SOAPACTION";
public static final String M_SEARCH = "M-SEARCH";
public static final String NOTIFY = "NOTIFY";
public static final String POST = "POST";
public static final String GET = "GET";
public static final String HEAD = "HEAD";
public static final String SUBSCRIBE = "SUBSCRIBE";
public static final String UNSUBSCRIBE = "UNSUBSCRIBE";
public static final String DATE = "Date";
public static final String CACHE_CONTROL = "Cache-Control";
public static final String NO_CACHE = "no-cache";
public static final String MAX_AGE = "max-age";
public static final String CONNECTION = "Connection";
public static final String CLOSE = "close";
public static final String KEEP_ALIVE = "Keep-Alive";
public static final String CONTENT_TYPE = "Content-Type";
public static final String CONTENT_LENGTH = "Content-Length";
public static final String CONTENT_RANGE = "Content-Range";
public static final String CONTENT_RANGE_BYTES = "bytes";
// Thanks for Brent Hills (10/20/04)
public static final String RANGE = "Range";
public static final String TRANSFER_ENCODING = "Transfer-Encoding";
public static final String CHUNKED = "Chunked";
public static final String LOCATION = "Location";
public static final String SERVER = "Server";
public static final String ST = "ST";
public static final String MX = "MX";
public static final String MAN = "MAN";
public static final String NT = "NT";
public static final String NTS = "NTS";
public static final String USN = "USN";
public static final String EXT = "EXT";
public static final String SID = "SID";
public static final String SEQ = "SEQ";
public final static String CALLBACK = "CALLBACK";
public final static String TIMEOUT = "TIMEOUT";
// Thanks for Brent Hills (10/20/04)
public final static String MYNAME = "MYNAME";
public static final String REQEST_LINE_DELIM = " ";
public static final String HEADER_LINE_DELIM = " :";
public static final String STATUS_LINE_DELIM = " ";
public static final int DEFAULT_PORT = 80;
public static final int DEFAULT_CHUNK_SIZE = 512 * 1024;
public static final int DEFAULT_TIMEOUT = 30;
////////////////////////////////////////////////
// URL
////////////////////////////////////////////////
public static final boolean isAbsoluteURL(String urlStr)
{
try {
new URL(urlStr);
return true;
}
catch (Exception e) {
return false;
}
}
public static final String getHost(String urlStr)
{
try {
URL url = new URL(urlStr);
return url.getHost();
}
catch (Exception e) {
return "";
}
}
public static final int getPort(String urlStr)
{
try {
URL url = new URL(urlStr);
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (08/30/03)
int port = url.getPort();
if (port <= 0)
port = DEFAULT_PORT;
return port;
}
catch (Exception e) {
return DEFAULT_PORT;
}
}
public static final String getRequestHostURL(String host, int port)
{
String reqHost = "http://" + host + ":" + port;
return reqHost;
}
public static final String toRelativeURL(String urlStr, boolean withParam)
{
String uri = urlStr;
if (isAbsoluteURL(urlStr) == false) {
if (0 < urlStr.length() && urlStr.charAt(0) != '/')
uri = "/" + urlStr;
}
else {
try{
URL url = new URL(urlStr);
uri = url.getPath();
if (withParam == true) {
String queryStr = url.getQuery();
if (!queryStr.equals("")){
uri += "?" + queryStr;
}
}
if (uri.endsWith("/"))
uri = uri.substring(0,uri.length()-1);
}catch(Exception e){}
}
return uri;
}
public static final String toRelativeURL(String urlStr)
{
return toRelativeURL(urlStr, true);
}
public static final String getAbsoluteURL(String baseURLStr, String relURlStr)
{
try {
URL baseURL = new URL(baseURLStr);
String url =
baseURL.getProtocol() + "://" +
baseURL.getHost() + ":" +
baseURL.getPort() +
toRelativeURL(relURlStr);
return url;
}
catch (Exception e) {
return "";
}
}
////////////////////////////////////////////////
// Chunk Size
////////////////////////////////////////////////
private static int chunkSize = DEFAULT_CHUNK_SIZE;
public static final void setChunkSize(int size)
{
chunkSize = size;
}
public static final int getChunkSize()
{
return chunkSize;
}
}

View File

@ -0,0 +1,144 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: HTTPHeader.java
*
* Revision;
*
* 11/19/02
* - first revision.
* 05/26/04
* - Jan Newmarch <jan.newmarch@infotech.monash.edu.au> (05/26/04)
* - Fixed getValue() to compare using String::equals() instead of String::startWidth().
*
******************************************************************/
package org.cybergarage.http;
import java.io.*;
import org.cybergarage.util.*;
public class HTTPHeader
{
private String name;
private String value;
public HTTPHeader(String name, String value)
{
setName(name);
setValue(value);
}
public HTTPHeader(String lineStr)
{
setName("");
setValue("");
if (lineStr == null)
return;
int colonIdx = lineStr.indexOf(':');
if (colonIdx < 0)
return;
String name = new String(lineStr.getBytes(), 0, colonIdx);
String value = new String(lineStr.getBytes(), colonIdx+1, lineStr.length()-colonIdx-1);
setName(name.trim());
setValue(value.trim());
}
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
public void setName(String name)
{
this.name = name;
}
public void setValue(String value)
{
this.value = value;
}
public String getName()
{
return name;
}
public String getValue()
{
return value;
}
public boolean hasName()
{
if (name == null || name.length() <= 0)
return false;
return true;
}
////////////////////////////////////////////////
// static methods
////////////////////////////////////////////////
public final static String getValue(LineNumberReader reader, String name)
{
String bigName = name.toUpperCase();
try {
String lineStr = reader.readLine();
while (lineStr != null && 0 < lineStr.length()) {
HTTPHeader header = new HTTPHeader(lineStr);
if (header.hasName() == false) {
lineStr = reader.readLine();
continue;
}
String bigLineHeaderName = header.getName().toUpperCase();
// Thanks for Jan Newmarch <jan.newmarch@infotech.monash.edu.au> (05/26/04)
if (bigLineHeaderName.equals(bigName) == false) {
lineStr = reader.readLine();
continue;
}
return header.getValue();
}
}
catch (IOException e) {
Debug.warning(e);
return "";
}
return "";
}
public final static String getValue(String data, String name)
{
StringReader strReader = new StringReader(data);
LineNumberReader lineReader = new LineNumberReader(strReader);
return getValue(lineReader, name);
}
public final static String getValue(byte[] data, String name)
{
return getValue(new String(data), name);
}
public final static int getIntegerValue(String data, String name)
{
try {
return Integer.parseInt(getValue(data, name));
}
catch (Exception e) {
return 0;
}
}
public final static int getIntegerValue(byte[] data, String name)
{
try {
return Integer.parseInt(getValue(data, name));
}
catch (Exception e) {
return 0;
}
}
}

View File

@ -0,0 +1,808 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: HTTPConnection.java
*
* Revision;
*
* 11/18/02
* - first revision.
* 09/02/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Problem : The API is unable to receive responses from the Microsoft UPnP stack
* - Error : the Microsoft UPnP stack is based on ISAPI on IIS, and whenever IIS
* receives a post request, it answers with two responses: the first one has no
* body and it is a code 100 (continue) response, which has to be ignored. The
* second response is the actual one and should be parsed as the response.
* 02/09/04
* - Ralf G. R. Bergs" <Ralf@Ber.gs>
* - Why do you strip leading and trailing white space from the response body?
* - Disabled to trim the content string.
* 03/11/04
* - Added some methods about InputStream content.
* setContentInputStream(), getContentInputStream() and hasContentInputStream().
* 03/16/04
* - Thanks for Darrell Young
* - Added setVersion() and getVersion();
* 03/17/04
* - Added hasFirstLine();
* 05/26/04
* - Jan Newmarch <jan.newmarch@infotech.monash.edu.au> (05/26/04)
* - Changed setCacheControl() and getChcheControl();
* 08/25/04
* - Added the following methods.
* hasContentRange(), setContentRange(), getContentRange(),
* getContentRangeFirstPosition(), getContentRangeLastPosition() and getContentRangeInstanceLength()
* 08/26/04
* - Added the following methods.
* hasConnection(), setConnection(), getConnection(),
* isCloseConnection() and isKeepAliveConnection()
* 08/27/04
* - Added a updateWithContentLength paramger to setContent().
* - Changed to HTTPPacket::set() not to change the header of Content-Length.
* 08/28/04
* - Added init() and read().
* 09/19/04
* - Added a onlyHeaders parameter to set().
* 10/20/04
* - Brent Hills <bhills@openshores.com>
* - Changed hasContentRange() to check Content-Range and Range header.
* - Added support for Range header to getContentRange().
* 02/02/05
* - Mark Retallack <mretallack@users.sourceforge.net>
* - Fixed set() not to read over the content length when the stream is keep alive.
* 02/28/05
* - Added the following methods for chunked stream support.
* hasTransferEncoding(), setTransferEncoding(), getTransferEncoding(), isChunked().
* 03/02/05
* - Changed post() to suppot chunked stream.
*
*******************************************************************/
package org.cybergarage.http;
import java.io.*;
import java.util.*;
import org.cybergarage.net.*;
import org.cybergarage.util.*;
import java.util.Calendar;
public class HTTPPacket
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public HTTPPacket()
{
setVersion(HTTP.VERSION);
setContentInputStream(null);
}
public HTTPPacket(HTTPPacket httpPacket)
{
setVersion(HTTP.VERSION);
set(httpPacket);
setContentInputStream(null);
}
public HTTPPacket(InputStream in)
{
setVersion(HTTP.VERSION);
set(in);
setContentInputStream(null);
}
////////////////////////////////////////////////
// init
////////////////////////////////////////////////
public void init()
{
setFirstLine("");
clearHeaders();
setContent(new byte[0], false);
setContentInputStream(null);
}
////////////////////////////////////////////////
// Version
////////////////////////////////////////////////
private String version;
public void setVersion(String ver)
{
version = ver;
}
public String getVersion()
{
return version;
}
////////////////////////////////////////////////
// set
////////////////////////////////////////////////
protected boolean set(InputStream in, boolean onlyHeaders)
{
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String firstLine = reader.readLine();
if (firstLine == null || firstLine.length() <= 0)
return false;
setFirstLine(firstLine);
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (09/03/03)
HTTPStatus httpStatus = new HTTPStatus(firstLine);
int statCode = httpStatus.getStatusCode();
if (statCode == HTTPStatus.CONTINUE){
//ad hoc code for managing iis non-standard behaviour
//iis sends 100 code response and a 200 code response in the same
//stream, so the code should check the presence of the actual
//response in the stream.
//skip all header lines
String headerLine = reader.readLine();
while ((headerLine != null) && (0 < headerLine.length()) ) {
HTTPHeader header = new HTTPHeader(headerLine);
if (header.hasName() == true)
setHeader(header);
headerLine = reader.readLine();
}
//look forward another first line
String actualFirstLine = reader.readLine();
if ((actualFirstLine != null) && (0 < actualFirstLine.length()) ) {
//this is the actual first line
setFirstLine(actualFirstLine);
}else{
return true;
}
}
String headerLine = reader.readLine();
while ((headerLine != null) && (0 < headerLine.length()) ) {
HTTPHeader header = new HTTPHeader(headerLine);
if (header.hasName() == true)
setHeader(header);
headerLine = reader.readLine();
}
if (onlyHeaders == true) {
setContent("", false);
return true;
}
boolean isChunkedRequest = isChunked();
long contentLen = 0;
if (isChunkedRequest == true) {
try {
String chunkSizeLine = reader.readLine();
contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length()-2));
}
catch (Exception e) {};
}
else
contentLen = getContentLength();
StringBuilder contentBuf = new StringBuilder();
while (0 < contentLen) {
int chunkSize = HTTP.getChunkSize();
char readBuf[] = new char[chunkSize];
long readCnt = 0;
while (readCnt < contentLen) {
try {
// Thanks for Mark Retallack (02/02/05)
long bufReadLen = contentLen - readCnt;
if (chunkSize < bufReadLen)
bufReadLen = chunkSize;
int readLen = reader.read(readBuf, 0, (int)bufReadLen);
if (readLen < 0)
break;
contentBuf.append(new String(readBuf, 0, readLen));
readCnt += readLen;
}
catch (Exception e)
{
Debug.warning(e);
break;
}
}
if (isChunkedRequest == true) {
// skip CRLF
long skipLen = 0;
do {
long skipCnt = reader.skip(HTTP.CRLF.length() - skipLen);
if (skipCnt < 0)
break;
skipLen += skipCnt;
} while (skipLen < HTTP.CRLF.length());
// read next chunk size
try {
String chunkSizeLine = reader.readLine();
contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length()-2));
}
catch (Exception e) {
contentLen = 0;
};
}
else
contentLen = 0;
}
// Thanks for Ralf G. R. Bergs (02/09/04)
String contentStr = contentBuf.toString();
setContent(contentStr.getBytes(), false);
}
catch (Exception e) {
Debug.warning(e);
return false;
}
return true;
}
protected boolean set(InputStream in)
{
return set(in, false);
}
protected boolean set(HTTPSocket httpSock)
{
return set(httpSock.getInputStream());
}
protected void set(HTTPPacket httpPacket)
{
setFirstLine(httpPacket.getFirstLine());
clearHeaders();
int nHeaders = httpPacket.getNHeaders();
for (int n=0; n<nHeaders; n++) {
HTTPHeader header = httpPacket.getHeader(n);
addHeader(header);
}
setContent(httpPacket.getContent());
}
////////////////////////////////////////////////
// read
////////////////////////////////////////////////
public boolean read(HTTPSocket httpSock)
{
init();
return set(httpSock);
}
////////////////////////////////////////////////
// String
////////////////////////////////////////////////
private String firstLine = "";
private void setFirstLine(String value)
{
firstLine = value;
}
protected String getFirstLine()
{
return firstLine;
}
protected String getFirstLineToken(int num)
{
StringTokenizer st = new StringTokenizer(firstLine, HTTP.REQEST_LINE_DELIM);
String lastToken = "";
for (int n=0; n<=num; n++) {
if (st.hasMoreTokens() == false)
return "";
lastToken = st.nextToken();
}
return lastToken;
}
public boolean hasFirstLine()
{
return (0 < firstLine.length()) ? true : false;
}
////////////////////////////////////////////////
// Header
////////////////////////////////////////////////
private Vector httpHeaderList = new Vector();
public int getNHeaders()
{
return httpHeaderList.size();
}
public void addHeader(HTTPHeader header)
{
httpHeaderList.add(header);
}
public void addHeader(String name, String value)
{
HTTPHeader header = new HTTPHeader(name, value);
httpHeaderList.add(header);
}
public HTTPHeader getHeader(int n)
{
return (HTTPHeader)httpHeaderList.get(n);
}
public HTTPHeader getHeader(String name)
{
int nHeaders = getNHeaders();
for (int n=0; n<nHeaders; n++) {
HTTPHeader header = getHeader(n);
String headerName = header.getName();
if (headerName.equalsIgnoreCase(name) == true)
return header;
}
return null;
}
public void clearHeaders()
{
httpHeaderList.clear();
httpHeaderList = new Vector();
}
public boolean hasHeader(String name)
{
return (getHeader(name) != null) ? true : false;
}
public void setHeader(String name, String value)
{
HTTPHeader header = getHeader(name);
if (header != null) {
header.setValue(value);
return;
}
addHeader(name, value);
}
public void setHeader(String name, int value)
{
setHeader(name, Integer.toString(value));
}
public void setHeader(String name, long value)
{
setHeader(name, Long.toString(value));
}
public void setHeader(HTTPHeader header)
{
setHeader(header.getName(), header.getValue());
}
public String getHeaderValue(String name)
{
HTTPHeader header = getHeader(name);
if (header == null)
return "";
return header.getValue();
}
////////////////////////////////////////////////
// set*Value
////////////////////////////////////////////////
public void setStringHeader(String name, String value, String startWidth, String endWidth)
{
String headerValue = value;
if (headerValue.startsWith(startWidth) == false)
headerValue = startWidth + headerValue;
if (headerValue.endsWith(endWidth) == false)
headerValue = headerValue + endWidth;
setHeader(name, headerValue);
}
public void setStringHeader(String name, String value)
{
setStringHeader(name, value, "\"", "\"");
}
public String getStringHeaderValue(String name, String startWidth, String endWidth)
{
String headerValue = getHeaderValue(name);
if (headerValue.startsWith(startWidth) == true)
headerValue = headerValue.substring(1, headerValue.length());
if (headerValue.endsWith(endWidth) == true)
headerValue = headerValue.substring(0, headerValue.length()-1);
return headerValue;
}
public String getStringHeaderValue(String name)
{
return getStringHeaderValue(name, "\"", "\"");
}
public void setIntegerHeader(String name, int value)
{
setHeader(name, Integer.toString(value));
}
public void setLongHeader(String name, long value)
{
setHeader(name, Long.toString(value));
}
public int getIntegerHeaderValue(String name)
{
HTTPHeader header = getHeader(name);
if (header == null)
return 0;
return StringUtil.toInteger(header.getValue());
}
public long getLongHeaderValue(String name)
{
HTTPHeader header = getHeader(name);
if (header == null)
return 0;
return StringUtil.toLong(header.getValue());
}
////////////////////////////////////////////////
// getHeader
////////////////////////////////////////////////
public String getHeaderString()
{
StringBuilder str = new StringBuilder();
int nHeaders = getNHeaders();
for (int n=0; n<nHeaders; n++) {
HTTPHeader header = getHeader(n);
str.append(header.getName() + ": " + header.getValue() + HTTP.CRLF);
}
return str.toString();
}
////////////////////////////////////////////////
// Contents
////////////////////////////////////////////////
private byte content[] = new byte[0];
public void setContent(byte data[], boolean updateWithContentLength)
{
content = data;
if (updateWithContentLength == true)
setContentLength(data.length);
}
public void setContent(byte data[])
{
setContent(data, true);
}
public void setContent(String data, boolean updateWithContentLength)
{
setContent(data.getBytes(), updateWithContentLength);
}
public void setContent(String data)
{
setContent(data, true);
}
public byte []getContent()
{
return content;
}
public String getContentString()
{
return new String(content);
}
public boolean hasContent()
{
return (content.length > 0) ? true : false;
}
////////////////////////////////////////////////
// Contents (InputStream)
////////////////////////////////////////////////
private InputStream contentInput = null;
public void setContentInputStream(InputStream in)
{
contentInput = in;
}
public InputStream getContentInputStream()
{
return contentInput;
}
public boolean hasContentInputStream()
{
return (contentInput != null) ? true : false;
}
////////////////////////////////////////////////
// ContentType
////////////////////////////////////////////////
public void setContentType(String type)
{
setHeader(HTTP.CONTENT_TYPE, type);
}
public String getContentType()
{
return getHeaderValue(HTTP.CONTENT_TYPE);
}
////////////////////////////////////////////////
// ContentLength
////////////////////////////////////////////////
public void setContentLength(long len)
{
setLongHeader(HTTP.CONTENT_LENGTH, len);
}
public long getContentLength()
{
return getLongHeaderValue(HTTP.CONTENT_LENGTH);
}
////////////////////////////////////////////////
// Connection
////////////////////////////////////////////////
public boolean hasConnection()
{
return hasHeader(HTTP.CONNECTION);
}
public void setConnection(String value)
{
setHeader(HTTP.CONNECTION, value);
}
public String getConnection()
{
return getHeaderValue(HTTP.CONNECTION);
}
public boolean isCloseConnection()
{
if (hasConnection() == false)
return false;
String connection = getConnection();
if (connection == null)
return false;
return connection.equalsIgnoreCase(HTTP.CLOSE);
}
public boolean isKeepAliveConnection()
{
if (hasConnection() == false)
return false;
String connection = getConnection();
if (connection == null)
return false;
return connection.equalsIgnoreCase(HTTP.KEEP_ALIVE);
}
////////////////////////////////////////////////
// ContentRange
////////////////////////////////////////////////
public boolean hasContentRange()
{
return (hasHeader(HTTP.CONTENT_RANGE) || hasHeader(HTTP.RANGE));
}
public void setContentRange(long firstPos, long lastPos, long length)
{
String rangeStr = "";
rangeStr += HTTP.CONTENT_RANGE_BYTES + " ";
rangeStr += Long.toString(firstPos) + "-";
rangeStr += Long.toString(lastPos) + "/";
rangeStr += ((0 < length) ? Long.toString(length) : "*");
setHeader(HTTP.CONTENT_RANGE, rangeStr);
}
public long[] getContentRange()
{
long range[] = new long[3];
range[0] = range[1] = range[2] = 0;
if (hasContentRange() == false)
return range;
String rangeLine = getHeaderValue(HTTP.CONTENT_RANGE);
// Thanks for Brent Hills (10/20/04)
if (rangeLine.length() <= 0)
rangeLine = getHeaderValue(HTTP.RANGE);
if (rangeLine.length() <= 0)
return range;
// Thanks for Brent Hills (10/20/04)
StringTokenizer strToken = new StringTokenizer(rangeLine, " =");
// Skip bytes
if (strToken.hasMoreTokens() == false)
return range;
strToken.nextToken(" ");
// Get first-byte-pos
if (strToken.hasMoreTokens() == false)
return range;
String firstPosStr = strToken.nextToken(" -");
try {
range[0] = Long.parseLong(firstPosStr);
}
catch (NumberFormatException e) {};
if (strToken.hasMoreTokens() == false)
return range;
String lastPosStr = strToken.nextToken("-/");
try {
range[1] = Long.parseLong(lastPosStr);
}
catch (NumberFormatException e) {};
if (strToken.hasMoreTokens() == false)
return range;
String lengthStr = strToken.nextToken("/");
try {
range[2] = Long.parseLong(lengthStr);
}
catch (NumberFormatException e) {};
return range;
}
public long getContentRangeFirstPosition()
{
long range[] = getContentRange();
return range[0];
}
public long getContentRangeLastPosition()
{
long range[] = getContentRange();
return range[1];
}
public long getContentRangeInstanceLength()
{
long range[] = getContentRange();
return range[2];
}
////////////////////////////////////////////////
// CacheControl
////////////////////////////////////////////////
public void setCacheControl(String directive)
{
setHeader(HTTP.CACHE_CONTROL, directive);
}
public void setCacheControl(String directive, int value)
{
String strVal = directive + "=" + Integer.toString(value);
setHeader(HTTP.CACHE_CONTROL, strVal);
}
public void setCacheControl(int value)
{
setCacheControl(HTTP.MAX_AGE, value);
}
public String getCacheControl()
{
return getHeaderValue(HTTP.CACHE_CONTROL);
}
////////////////////////////////////////////////
// Server
////////////////////////////////////////////////
public void setServer(String name)
{
setHeader(HTTP.SERVER, name);
}
public String getServer()
{
return getHeaderValue(HTTP.SERVER);
}
////////////////////////////////////////////////
// Host
////////////////////////////////////////////////
public void setHost(String host, int port)
{
String hostAddr = host;
if (HostInterface.isIPv6Address(host) == true)
hostAddr = "[" + host + "]";
setHeader(HTTP.HOST, hostAddr + ":" + Integer.toString(port));
}
public String getHost()
{
return getHeaderValue(HTTP.HOST);
}
////////////////////////////////////////////////
// Date
////////////////////////////////////////////////
public void setDate(Calendar cal)
{
Date date = new Date(cal);
setHeader(HTTP.DATE, date.getDateString());
}
public String getDate()
{
return getHeaderValue(HTTP.DATE);
}
////////////////////////////////////////////////
// Connection
////////////////////////////////////////////////
public boolean hasTransferEncoding()
{
return hasHeader(HTTP.TRANSFER_ENCODING);
}
public void setTransferEncoding(String value)
{
setHeader(HTTP.TRANSFER_ENCODING, value);
}
public String getTransferEncoding()
{
return getHeaderValue(HTTP.TRANSFER_ENCODING);
}
public boolean isChunked()
{
if (hasTransferEncoding() == false)
return false;
String transEnc = getTransferEncoding();
if (transEnc == null)
return false;
return transEnc.equalsIgnoreCase(HTTP.CHUNKED);
}
////////////////////////////////////////////////
// set
////////////////////////////////////////////////
/*
public final static boolean parse(HTTPPacket httpPacket, InputStream in)
{
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
return parse(httpPacket, reader);
}
catch (Exception e) {
Debug.warning(e);
}
return false;
}
*/
}

View File

@ -0,0 +1,495 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: HTTPRequest.java
*
* Revision;
*
* 11/18/02
* - first revision.
* 05/23/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Add a relative URL check to setURI().
* 09/02/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Problem : Devices whose description use absolute urls receive wrong http requests
* - Error : the presence of a base url is not mandatory, the API code makes the assumption that control and event subscription urls are relative
* - Description: The method setURI should be changed as follows
* 02/01/04
* - Added URI parameter methods.
* 03/16/04
* - Removed setVersion() because the method is added to the super class.
* - Changed getVersion() to return the version when the first line string has the length.
* 05/19/04
* - Changed post(HTTPResponse *) to close the socket stream from the server.
* 08/19/04
* - Fixed getFirstLineString() and getHTTPVersion() no to return "HTTP/HTTP/version".
* 08/25/04
* - Added isHeadRequest().
* 08/26/04
* - Changed post(HTTPResponse) not to close the connection.
* - Changed post(String, int) to add a connection header to close.
* 08/27/04
* - Changed post(String, int) to support the persistent connection.
* 08/28/04
* - Added isKeepAlive().
* 10/26/04
* - Brent Hills <bhills@openshores.com>
* - Added a fix to post() when the last position of Content-Range header is 0.
* - Added a Content-Range header to the response in post().
* - Changed the status code for the Content-Range request in post().
* - Added to check the range of Content-Range request in post().
* 03/02/05
* - Changed post() to suppot chunked stream.
*
******************************************************************/
package org.cybergarage.http;
import java.io.*;
import java.net.*;
import java.util.*;
public class HTTPRequest extends HTTPPacket
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public HTTPRequest()
{
}
public HTTPRequest(InputStream in)
{
super(in);
}
public HTTPRequest(HTTPSocket httpSock)
{
this(httpSock.getInputStream());
setSocket(httpSock);
}
////////////////////////////////////////////////
// Method
////////////////////////////////////////////////
private String method = null;
public void setMethod(String value)
{
method = value;
}
public String getMethod()
{
if (method != null)
return method;
return getFirstLineToken(0);
}
public boolean isMethod(String method)
{
String headerMethod = getMethod();
if (headerMethod == null)
return false;
return headerMethod.equalsIgnoreCase(method);
}
public boolean isGetRequest()
{
return isMethod(HTTP.GET);
}
public boolean isPostRequest()
{
return isMethod(HTTP.POST);
}
public boolean isHeadRequest()
{
return isMethod(HTTP.HEAD);
}
public boolean isSubscribeRequest()
{
return isMethod(HTTP.SUBSCRIBE);
}
public boolean isUnsubscribeRequest()
{
return isMethod(HTTP.UNSUBSCRIBE);
}
public boolean isNotifyRequest()
{
return isMethod(HTTP.NOTIFY);
}
////////////////////////////////////////////////
// URI
////////////////////////////////////////////////
private String uri = null;
public void setURI(String value, boolean isCheckRelativeURL)
{
uri = value;
if (isCheckRelativeURL == false)
return;
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (09/02/03)
uri = HTTP.toRelativeURL(uri);
}
public void setURI(String value)
{
setURI(value, false);
}
public String getURI()
{
if (uri != null)
return uri;
return getFirstLineToken(1);
}
////////////////////////////////////////////////
// URI Parameter
////////////////////////////////////////////////
public ParameterList getParameterList()
{
ParameterList paramList = new ParameterList();
String uri = getURI();
if (uri == null)
return paramList;
int paramIdx = uri.indexOf('?');
if (paramIdx < 0)
return paramList;
while (0 < paramIdx) {
int eqIdx = uri.indexOf('=', (paramIdx+1));
String name = uri.substring(paramIdx+1, eqIdx);
int nextParamIdx = uri.indexOf('&', (eqIdx+1));
String value = uri.substring(eqIdx+1, (0 < nextParamIdx) ? nextParamIdx : uri.length());
Parameter param = new Parameter(name, value);
paramList.add(param);
paramIdx = nextParamIdx;
}
return paramList;
}
public String getParameterValue(String name)
{
ParameterList paramList = getParameterList();
return paramList.getValue(name);
}
////////////////////////////////////////////////
// SOAPAction
////////////////////////////////////////////////
public boolean isSOAPAction()
{
return hasHeader(HTTP.SOAP_ACTION);
}
////////////////////////////////////////////////
// Host / Port
////////////////////////////////////////////////
private String requestHost = "";
public void setRequestHost(String host)
{
requestHost = host;
}
public String getRequestHost()
{
return requestHost;
}
private int requestPort = -1;
public void setRequestPort(int host)
{
requestPort = host;
}
public int getRequestPort()
{
return requestPort;
}
////////////////////////////////////////////////
// Socket
////////////////////////////////////////////////
private HTTPSocket httpSocket = null;
public void setSocket(HTTPSocket value)
{
httpSocket = value;
}
public HTTPSocket getSocket()
{
return httpSocket;
}
/////////////////////////// /////////////////////
// local address/port
////////////////////////////////////////////////
public String getLocalAddress()
{
return getSocket().getLocalAddress();
}
public int getLocalPort()
{
return getSocket().getLocalPort();
}
////////////////////////////////////////////////
// parseRequest
////////////////////////////////////////////////
public boolean parseRequestLine(String lineStr)
{
StringTokenizer st = new StringTokenizer(lineStr, HTTP.REQEST_LINE_DELIM);
if (st.hasMoreTokens() == false)
return false;
setMethod(st.nextToken());
if (st.hasMoreTokens() == false)
return false;
setURI(st.nextToken());
if (st.hasMoreTokens() == false)
return false;
setVersion(st.nextToken());
return true;
}
////////////////////////////////////////////////
// First Line
////////////////////////////////////////////////
public String getHTTPVersion()
{
if (hasFirstLine() == true)
return getFirstLineToken(2);
return "HTTP/" + super.getVersion();
}
public String getFirstLineString()
{
return getMethod() + " " + getURI() + " " + getHTTPVersion() + HTTP.CRLF;
}
////////////////////////////////////////////////
// getHeader
////////////////////////////////////////////////
public String getHeader()
{
StringBuilder str = new StringBuilder();
str.append(getFirstLineString());
String headerString = getHeaderString();
str.append(headerString);
return str.toString();
}
////////////////////////////////////////////////
// isKeepAlive
////////////////////////////////////////////////
public boolean isKeepAlive()
{
if (isCloseConnection() == true)
return false;
if (isKeepAliveConnection() == true)
return true;
String httpVer = getHTTPVersion();
boolean isHTTP10 = (0 < httpVer.indexOf("1.0")) ? true : false;
if (isHTTP10 == true)
return false;
return true;
}
////////////////////////////////////////////////
// read
////////////////////////////////////////////////
public boolean read()
{
return super.read(getSocket());
}
////////////////////////////////////////////////
// POST (Response)
////////////////////////////////////////////////
public boolean post(HTTPResponse httpRes)
{
HTTPSocket httpSock = getSocket();
long offset = 0;
long length = httpRes.getContentLength();
if (hasContentRange() == true) {
long firstPos = getContentRangeFirstPosition();
long lastPos = getContentRangeLastPosition();
// Thanks for Brent Hills (10/26/04)
if (lastPos <= 0)
lastPos = length - 1;
if ((firstPos > length ) || (lastPos > length))
return returnResponse(HTTPStatus.INVALID_RANGE);
httpRes.setContentRange(firstPos, lastPos, length);
httpRes.setStatusCode(HTTPStatus.PARTIAL_CONTENT);
offset = firstPos;
length = lastPos - firstPos + 1;
}
return httpSock.post(httpRes, offset, length, isHeadRequest());
//httpSock.close();
}
////////////////////////////////////////////////
// POST (Request)
////////////////////////////////////////////////
private Socket postSocket = null;
public HTTPResponse post(String host, int port, boolean isKeepAlive)
{
HTTPResponse httpRes = new HTTPResponse();
setConnection((isKeepAlive == true) ? HTTP.KEEP_ALIVE : HTTP.CLOSE);
boolean isHeaderRequest = isHeadRequest();
OutputStream out = null;
InputStream in = null;
try {
if (postSocket == null)
postSocket = new Socket(host, port);
out = postSocket.getOutputStream();
PrintStream pout = new PrintStream(out);
pout.print(getHeader());
pout.print(HTTP.CRLF);
boolean isChunkedRequest = isChunked();
String content = getContentString();
int contentLength = 0;
if (content != null)
contentLength = content.length();
if (0 < contentLength) {
if (isChunkedRequest == true) {
String chunSizeBuf = Long.toString(contentLength);
pout.print(chunSizeBuf);
pout.print(HTTP.CRLF);
}
pout.print(content);
if (isChunkedRequest == true)
pout.print(HTTP.CRLF);
}
if (isChunkedRequest == true) {
pout.print("0");
pout.print(HTTP.CRLF);
}
pout.flush();
in = postSocket.getInputStream();
httpRes.set(in, isHeaderRequest);
}
catch (Exception e) {
httpRes.setStatusCode(HTTPStatus.INTERNAL_SERVER_ERROR);
} finally {
if (isKeepAlive == false) {
try {
in.close();
} catch (Exception e) {};
if (in != null)
try {
out.close();
} catch (Exception e) {};
if (out != null)
try {
postSocket.close();
} catch (Exception e) {};
postSocket = null;
}
}
return httpRes;
}
public HTTPResponse post(String host, int port)
{
return post(host, port, false);
}
////////////////////////////////////////////////
// set
////////////////////////////////////////////////
public void set(HTTPRequest httpReq)
{
set((HTTPPacket)httpReq);
setSocket(httpReq.getSocket());
}
////////////////////////////////////////////////
// OK/BAD_REQUEST
////////////////////////////////////////////////
public boolean returnResponse(int statusCode)
{
HTTPResponse httpRes = new HTTPResponse();
httpRes.setStatusCode(statusCode);
httpRes.setContentLength(0);
return post(httpRes);
}
public boolean returnOK()
{
return returnResponse(HTTPStatus.OK);
}
public boolean returnBadRequest()
{
return returnResponse(HTTPStatus.BAD_REQUEST);
}
////////////////////////////////////////////////
// toString
////////////////////////////////////////////////
public String toString()
{
StringBuilder str = new StringBuilder();
str.append(getHeader());
str.append(HTTP.CRLF);
str.append(getContentString());
return str.toString();
}
public void print()
{
System.out.println(toString());
}
}

View File

@ -0,0 +1,21 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: HTTPRequestListener.java
*
* Revision;
*
* 12/13/02
* - first revision.
*
******************************************************************/
package org.cybergarage.http;
public interface HTTPRequestListener
{
public void httpRequestRecieved(HTTPRequest httpReq);
}

View File

@ -0,0 +1,115 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: HTTPResponse.java
*
* Revision;
*
* 11/18/02
* - first revision.
* 10/22/03
* - Changed to initialize a content length header.
* 10/22/04
* - Added isSuccessful().
*
******************************************************************/
package org.cybergarage.http;
import java.io.*;
public class HTTPResponse extends HTTPPacket
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public HTTPResponse()
{
setContentType(HTML.CONTENT_TYPE);
setServer(HTTPServer.getName());
setContent("");
}
public HTTPResponse(HTTPResponse httpRes)
{
set(httpRes);
}
public HTTPResponse(InputStream in)
{
super(in);
}
public HTTPResponse(HTTPSocket httpSock)
{
this(httpSock.getInputStream());
}
////////////////////////////////////////////////
// Status Line
////////////////////////////////////////////////
private int statusCode = 0;
public void setStatusCode(int code)
{
statusCode = code;
}
public int getStatusCode()
{
if (statusCode != 0)
return statusCode;
HTTPStatus httpStatus = new HTTPStatus(getFirstLine());
return httpStatus.getStatusCode();
}
public boolean isSuccessful()
{
return HTTPStatus.isSuccessful(getStatusCode());
}
public String getStatusLineString()
{
return "HTTP/" + getVersion() + " " + getStatusCode() + " " + HTTPStatus.code2String(statusCode) + HTTP.CRLF;
}
////////////////////////////////////////////////
// getHeader
////////////////////////////////////////////////
public String getHeader()
{
StringBuilder str = new StringBuilder();
str.append(getStatusLineString());
str.append(getHeaderString());
return str.toString();
}
////////////////////////////////////////////////
// toString
////////////////////////////////////////////////
public String toString()
{
StringBuilder str = new StringBuilder();
str.append(getStatusLineString());
str.append(getHeaderString());
str.append(HTTP.CRLF);
str.append(getContentString());
return str.toString();
}
public void print()
{
System.out.println(toString());
}
}

View File

@ -0,0 +1,204 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: HTTPServer.java
*
* Revision;
*
* 12/12/02
* - first revision.
* 10/20/03
* - Improved the HTTP server using multithreading.
* 08/27/04
* - Changed accept() to set a default timeout, HTTP.DEFAULT_TIMEOUT, to the socket.
*
******************************************************************/
package org.cybergarage.http;
import java.io.*;
import java.net.*;
import org.cybergarage.util.*;
public class HTTPServer implements Runnable
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public final static String NAME = "CyberHTTP";
public final static String VERSION = "1.0";
public final static int DEFAULT_PORT = 80;
public static String getName()
{
String osName = System.getProperty("os.name");
String osVer = System.getProperty("os.version");
return osName + "/" + osVer + " " + NAME + "/" + VERSION;
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public HTTPServer()
{
serverSock = null;
}
////////////////////////////////////////////////
// ServerSocket
////////////////////////////////////////////////
private ServerSocket serverSock = null;
private InetAddress bindAddr = null;
private int bindPort = 0;
public ServerSocket getServerSock()
{
return serverSock;
}
public String getBindAddress()
{
if (bindAddr == null)
return "";
return bindAddr.toString();
}
public int getBindPort()
{
return bindPort;
}
////////////////////////////////////////////////
// open/close
////////////////////////////////////////////////
public boolean open(String addr, int port)
{
if (serverSock != null)
return true;
try {
bindAddr = InetAddress.getByName(addr);
bindPort = port;
serverSock = new ServerSocket(bindPort, 0, bindAddr);
serverSock.setSoTimeout(10*1000);
}
catch (IOException e) {
return false;
}
return true;
}
public boolean close()
{
if (serverSock == null)
return true;
try {
serverSock.close();
serverSock = null;
bindAddr = null;
bindPort = 0;
}
catch (Exception e) {
Debug.warning(e);
return false;
}
return true;
}
public Socket accept()
{
if (serverSock == null)
return null;
try {
Socket sock = serverSock.accept();
sock.setSoTimeout(HTTP.DEFAULT_PORT * 1000);
return sock;
}
catch (Exception e) {
return null;
}
}
public boolean isOpened()
{
return (serverSock != null) ? true : false;
}
////////////////////////////////////////////////
// httpRequest
////////////////////////////////////////////////
private ListenerList httpRequestListenerList = new ListenerList();
public void addRequestListener(HTTPRequestListener listener)
{
httpRequestListenerList.add(listener);
}
public void removeRequestListener(HTTPRequestListener listener)
{
httpRequestListenerList.remove(listener);
}
public void performRequestListener(HTTPRequest httpReq)
{
int listenerSize = httpRequestListenerList.size();
for (int n=0; n<listenerSize; n++) {
HTTPRequestListener listener = (HTTPRequestListener)httpRequestListenerList.get(n);
listener.httpRequestRecieved(httpReq);
}
}
////////////////////////////////////////////////
// run
////////////////////////////////////////////////
private Thread httpServerThread = null;
public void run()
{
if (isOpened() == false)
return;
Thread thisThread = Thread.currentThread();
while (httpServerThread == thisThread) {
Thread.yield();
Socket sock;
try {
Debug.message("accept ...");
sock = accept();
if (sock != null)
Debug.message("sock = " + sock.getRemoteSocketAddress());
}
catch (Exception e){
Debug.warning(e);
break;
}
HTTPServerThread httpServThread = new HTTPServerThread(this, sock);
httpServThread.start();
Debug.message("httpServThread ...");
}
}
public boolean start()
{
httpServerThread = new Thread(this, "UPnP-HTTPServer");
httpServerThread.start();
return true;
}
public boolean stop()
{
httpServerThread = null;
return true;
}
}

View File

@ -0,0 +1,104 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: HTTPServerList.java
*
* Revision;
*
* 05/08/03
* - first revision.
*
******************************************************************/
package org.cybergarage.http;
import java.util.*;
import org.cybergarage.net.*;
public class HTTPServerList extends Vector
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
private static final long serialVersionUID = 2379889735659369065L;
public HTTPServerList()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public void addRequestListener(HTTPRequestListener listener)
{
int nServers = size();
for (int n=0; n<nServers; n++) {
HTTPServer server = getHTTPServer(n);
server.addRequestListener(listener);
}
}
public HTTPServer getHTTPServer(int n)
{
return (HTTPServer)get(n);
}
////////////////////////////////////////////////
// open/close
////////////////////////////////////////////////
public void close()
{
int nServers = size();
for (int n=0; n<nServers; n++) {
HTTPServer server = getHTTPServer(n);
server.close();
}
}
public boolean open(int port)
{
int nHostAddrs = HostInterface.getNHostAddresses();
for (int n=0; n<nHostAddrs; n++) {
String bindAddr = HostInterface.getHostAddress(n);
HTTPServer httpServer = new HTTPServer();
if (httpServer.open(bindAddr, port) == false) {
close();
clear();
return false;
}
add(httpServer);
}
return true;
}
////////////////////////////////////////////////
// start/stop
////////////////////////////////////////////////
public void start()
{
int nServers = size();
for (int n=0; n<nServers; n++) {
HTTPServer server = getHTTPServer(n);
server.start();
}
}
public void stop()
{
int nServers = size();
for (int n=0; n<nServers; n++) {
HTTPServer server = getHTTPServer(n);
server.stop();
}
}
}

View File

@ -0,0 +1,54 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: HTTPServerThread.java
*
* Revision;
*
* 10/10/03
* - first revision.
*
******************************************************************/
package org.cybergarage.http;
import java.net.*;
public class HTTPServerThread extends Thread
{
private HTTPServer httpServer;
private Socket sock;
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public HTTPServerThread(HTTPServer httpServer, Socket sock)
{
this.httpServer = httpServer;
this.sock = sock;
this.setDaemon(true);
}
////////////////////////////////////////////////
// run
////////////////////////////////////////////////
public void run()
{
HTTPSocket httpSock = new HTTPSocket(sock);
if (httpSock.open() == false)
return;
HTTPRequest httpReq = new HTTPRequest();
httpReq.setSocket(httpSock);
while (httpReq.read() == true) {
httpServer.performRequestListener(httpReq);
if (httpReq.isKeepAlive() == false)
break;
}
httpSock.close();
}
}

View File

@ -0,0 +1,249 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: HTTPSocket.java
*
* Revision;
*
* 12/12/02
* - first revision.
* 03/11/04
* - Added the following methods about chunk size.
* setChunkSize(), getChunkSize().
* 08/26/04
* - Added a isOnlyHeader to post().
* 03/02/05
* - Changed post() to suppot chunked stream.
*
******************************************************************/
package org.cybergarage.http;
import java.io.*;
import java.net.*;
import java.util.*;
public class HTTPSocket
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public HTTPSocket(Socket socket)
{
setSocket(socket);
open();
}
public HTTPSocket(HTTPSocket socket)
{
setSocket(socket.getSocket());
setInputStream(socket.getInputStream());
setOutputStream(socket.getOutputStream());
}
public void finalize()
{
close();
}
////////////////////////////////////////////////
// Socket
////////////////////////////////////////////////
private Socket socket = null;
private void setSocket(Socket socket)
{
this.socket = socket;
}
public Socket getSocket()
{
return socket;
}
////////////////////////////////////////////////
// local address/port
////////////////////////////////////////////////
public String getLocalAddress()
{
return getSocket().getLocalAddress().getHostAddress();
}
public int getLocalPort()
{
return getSocket().getLocalPort();
}
////////////////////////////////////////////////
// in/out
////////////////////////////////////////////////
private InputStream sockIn = null;
private OutputStream sockOut = null;
private void setInputStream(InputStream in)
{
sockIn = in;
}
public InputStream getInputStream()
{
return sockIn;
}
private void setOutputStream(OutputStream out)
{
sockOut = out;
}
private OutputStream getOutputStream()
{
return sockOut;
}
////////////////////////////////////////////////
// open/close
////////////////////////////////////////////////
public boolean open()
{
Socket sock = getSocket();
try {
sockIn = sock.getInputStream();
sockOut = sock.getOutputStream();
}
catch (Exception e) {
return false;
}
return true;
}
public boolean close()
{
try {
if (sockIn != null)
sockIn.close();
if (sockOut != null)
sockOut.close();
getSocket().close();
}
catch (Exception e) {
//Debug.warning(e);
return false;
}
return true;
}
////////////////////////////////////////////////
// post
////////////////////////////////////////////////
private boolean post(HTTPResponse httpRes, byte content[], long contentOffset, long contentLength, boolean isOnlyHeader)
{
httpRes.setDate(Calendar.getInstance());
OutputStream out = getOutputStream();
try {
httpRes.setContentLength(contentLength);
out.write(httpRes.getHeader().getBytes());
out.write(HTTP.CRLF.getBytes());
if (isOnlyHeader == true) {
out.flush();
return true;
}
boolean isChunkedResponse = httpRes.isChunked();
if (isChunkedResponse == true) {
String chunSizeBuf = Long.toString(contentLength);
out.write(chunSizeBuf.getBytes());
out.write(HTTP.CRLF.getBytes());
}
out.write(content, (int)contentOffset, (int)contentLength);
if (isChunkedResponse == true) {
out.write(HTTP.CRLF.getBytes());
out.write("0".getBytes());
out.write(HTTP.CRLF.getBytes());
}
out.flush();
}
catch (Exception e) {
//Debug.warning(e);
return false;
}
return true;
}
private boolean post(HTTPResponse httpRes, InputStream in, long contentOffset, long contentLength, boolean isOnlyHeader)
{
httpRes.setDate(Calendar.getInstance());
OutputStream out = getOutputStream();
try {
httpRes.setContentLength(contentLength);
out.write(httpRes.getHeader().getBytes());
out.write(HTTP.CRLF.getBytes());
if (isOnlyHeader == true) {
out.flush();
return true;
}
boolean isChunkedResponse = httpRes.isChunked();
if (0 < contentOffset)
in.skip(contentOffset);
int chunkSize = HTTP.getChunkSize();
byte readBuf[] = new byte[chunkSize];
long readCnt = 0;
long readSize = (chunkSize < contentLength) ? chunkSize : contentLength;
int readLen = in.read(readBuf, 0, (int)readSize);
while (0 < readLen && readCnt < contentLength) {
if (isChunkedResponse == true) {
String chunSizeBuf = Long.toString(readLen);
out.write(chunSizeBuf.getBytes());
out.write(HTTP.CRLF.getBytes());
}
out.write(readBuf, 0, readLen);
if (isChunkedResponse == true)
out.write(HTTP.CRLF.getBytes());
readCnt += readLen;
readSize = (chunkSize < (contentLength-readCnt)) ? chunkSize : (contentLength-readCnt);
readLen = in.read(readBuf, 0, (int)readSize);
}
if (isChunkedResponse == true) {
out.write("0".getBytes());
out.write(HTTP.CRLF.getBytes());
}
out.flush();
}
catch (Exception e) {
//Debug.warning(e);
return false;
}
return true;
}
public boolean post(HTTPResponse httpRes, long contentOffset, long contentLength, boolean isOnlyHeader)
{
if (httpRes.hasContentInputStream() == true)
return post(httpRes,httpRes.getContentInputStream(), contentOffset, contentLength, isOnlyHeader);
return post(httpRes,httpRes.getContent(), contentOffset, contentLength, isOnlyHeader);
}
}

View File

@ -0,0 +1,185 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: HTTPStatus.java
*
* Revision;
*
* 12/17/02
* - first revision.
* 09/03/03
* - Added CONTINUE_STATUS.
* 10/20/04
* - Brent Hills <bhills@openshores.com>
* - Added PARTIAL_CONTENT and INVALID_RANGE;
* 10/22/04
* - Added isSuccessful().
* 10/29/04
* - Fixed set() to set the version and the response code when the mothod is null.
* - Fixed set() to read multi words of the response sring such as Not Found.
*
******************************************************************/
package org.cybergarage.http;
import java.util.*;
import org.cybergarage.util.*;
public class HTTPStatus
{
////////////////////////////////////////////////
// Code
////////////////////////////////////////////////
public static final int CONTINUE = 100;
public static final int OK = 200;
// Thanks for Brent Hills (10/20/04)
public static final int PARTIAL_CONTENT = 206;
public static final int BAD_REQUEST = 400;
public static final int NOT_FOUND = 404;
public static final int PRECONDITION_FAILED = 412;
// Thanks for Brent Hills (10/20/04)
public static final int INVALID_RANGE = 416;
public static final int INTERNAL_SERVER_ERROR = 500;
public static final String code2String(int code)
{
switch (code) {
case CONTINUE: return "Continue";
case OK: return "OK";
case PARTIAL_CONTENT: return "Partial Content";
case BAD_REQUEST: return "Bad Request";
case NOT_FOUND: return "Not Found";
case PRECONDITION_FAILED: return "Precondition Failed";
case INVALID_RANGE: return "Invalid Range";
case INTERNAL_SERVER_ERROR: return "Internal Server Error";
}
return "";
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public HTTPStatus()
{
setVersion("");
setStatusCode(0);
setReasonPhrase("");
}
public HTTPStatus(String ver, int code, String reason)
{
setVersion(ver);
setStatusCode(code);
setReasonPhrase(reason);
}
public HTTPStatus(String lineStr)
{
set(lineStr);
}
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private String version = "";
private int statusCode = 0;
private String reasonPhrase = "";
public void setVersion(String value)
{
version = value;
}
public void setStatusCode(int value)
{
statusCode = value;
}
public void setReasonPhrase(String value)
{
reasonPhrase = value;
}
public String getVersion()
{
return version;
}
public int getStatusCode()
{
return statusCode;
}
public String getReasonPhrase()
{
return reasonPhrase;
}
////////////////////////////////////////////////
// Status
////////////////////////////////////////////////
final public static boolean isSuccessful(int statCode)
{
if (200 <= statCode && statCode < 300)
return true;
return false;
}
public boolean isSuccessful()
{
return isSuccessful(getStatusCode());
}
////////////////////////////////////////////////
// set
////////////////////////////////////////////////
public void set(String lineStr)
{
if (lineStr == null) {
setVersion(HTTP.VERSION);
setStatusCode(INTERNAL_SERVER_ERROR);
setReasonPhrase(code2String(INTERNAL_SERVER_ERROR));
return;
}
try {
StringTokenizer st = new StringTokenizer(lineStr, HTTP.STATUS_LINE_DELIM);
if (st.hasMoreTokens() == false)
return;
String ver = st.nextToken();
setVersion(ver.trim());
if (st.hasMoreTokens() == false)
return;
String codeStr = st.nextToken();
int code = 0;
try {
code = Integer.parseInt(codeStr);
}
catch (Exception e1) {}
setStatusCode(code);
String reason = "";
while (st.hasMoreTokens() == true) {
if (0 <= reason.length())
reason += " ";
reason += st.nextToken();
}
setReasonPhrase(reason.trim());
}
catch (Exception e) {
Debug.warning(e);
}
}
}

View File

@ -0,0 +1,61 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: Parameter.java
*
* Revision;
*
* 02/01/04
* - first revision.
*
******************************************************************/
package org.cybergarage.http;
public class Parameter
{
private String name = new String();
private String value = new String();
public Parameter()
{
}
public Parameter(String name, String value)
{
setName(name);
setValue(value);
}
////////////////////////////////////////////////
// name
////////////////////////////////////////////////
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
////////////////////////////////////////////////
// value
////////////////////////////////////////////////
public void setValue(String value)
{
this.value = value;
}
public String getValue()
{
return value;
}
}

View File

@ -0,0 +1,60 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: ParameterList.java
*
* Revision;
*
* 02/01/04
* - first revision.
*
******************************************************************/
package org.cybergarage.http;
import java.util.*;
public class ParameterList extends Vector
{
private static final long serialVersionUID = -6026765325018137641L;
public ParameterList()
{
}
public Parameter at(int n)
{
return (Parameter)get(n);
}
public Parameter getParameter(int n)
{
return (Parameter)get(n);
}
public Parameter getParameter(String name)
{
if (name == null)
return null;
int nLists = size();
for (int n=0; n<nLists; n++) {
Parameter param = at(n);
if (name.compareTo(param.getName()) == 0)
return param;
}
return null;
}
public String getValue(String name)
{
Parameter param = getParameter(name);
if (param == null)
return "";
return param.getValue();
}
}

View File

@ -0,0 +1,230 @@
/******************************************************************
*
* CyberHTTP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: HostInterface.java
*
* Revision;
*
* 05/12/03
* - first revision.
* 05/13/03
* - Added support for IPv6 and loopback address.
* 02/15/04
* - Added the following methods to set only a interface.
* - setInterface(), getInterfaces(), hasAssignedInterface()
* 06/30/04
* - Moved the package from org.cybergarage.http to org.cybergarage.net.
* 06/30/04
* - Theo Beisch <theo.beisch@gmx.de>
* - Changed isUseAddress() to isUsableAddress().
*
******************************************************************/
package org.cybergarage.net;
import java.net.*;
import java.util.*;
public class HostInterface
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public static boolean USE_LOOPBACK_ADDR = false;
public static boolean USE_ONLY_IPV4_ADDR = false;
public static boolean USE_ONLY_IPV6_ADDR = false;
////////////////////////////////////////////////
// Network Interfaces
////////////////////////////////////////////////
private static String ifAddress = "";
public final static void setInterface(String ifaddr)
{
ifAddress = ifaddr;
}
public final static String getInterface()
{
return ifAddress;
}
private final static boolean hasAssignedInterface()
{
return (0 < ifAddress.length()) ? true : false;
}
////////////////////////////////////////////////
// Network Interfaces
////////////////////////////////////////////////
// Thanks for Theo Beisch (10/27/04)
private final static boolean isUsableAddress(InetAddress addr)
{
if (USE_LOOPBACK_ADDR == false) {
if (addr.isLoopbackAddress() == true)
return false;
}
if (USE_ONLY_IPV4_ADDR == true) {
if (addr instanceof Inet6Address)
return false;
}
if (USE_ONLY_IPV6_ADDR == true) {
if (addr instanceof Inet4Address)
return false;
}
return true;
}
public final static int getNHostAddresses()
{
if (hasAssignedInterface() == true)
return 1;
int nHostAddrs = 0;
try {
Enumeration nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()){
NetworkInterface ni = (NetworkInterface)nis.nextElement();
Enumeration addrs = ni.getInetAddresses();
while (addrs.hasMoreElements()) {
InetAddress addr = (InetAddress)addrs.nextElement();
if (isUsableAddress(addr) == false)
continue;
nHostAddrs++;
}
}
}
catch(Exception e){};
return nHostAddrs;
}
public final static String getHostAddress(int n)
{
if (hasAssignedInterface() == true)
return getInterface();
int hostAddrCnt = 0;
try {
Enumeration nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()){
NetworkInterface ni = (NetworkInterface)nis.nextElement();
Enumeration addrs = ni.getInetAddresses();
while (addrs.hasMoreElements()) {
InetAddress addr = (InetAddress)addrs.nextElement();
if (isUsableAddress(addr) == false)
continue;
if (hostAddrCnt < n) {
hostAddrCnt++;
continue;
}
String host = addr.getHostAddress();
//if (addr instanceof Inet6Address)
// host = "[" + host + "]";
return host;
}
}
}
catch(Exception e){};
return "";
}
////////////////////////////////////////////////
// isIPv?Address
////////////////////////////////////////////////
public final static boolean isIPv6Address(String host)
{
try {
InetAddress addr = InetAddress.getByName(host);
return (addr instanceof Inet6Address);
}
catch (Exception e) {}
return false;
}
public final static boolean isIPv4Address(String host)
{
try {
InetAddress addr = InetAddress.getByName(host);
return (addr instanceof Inet4Address);
}
catch (Exception e) {}
return false;
}
////////////////////////////////////////////////
// hasIPv?Interfaces
////////////////////////////////////////////////
public final static boolean hasIPv4Addresses()
{
int addrCnt = getNHostAddresses();
for (int n=0; n<addrCnt; n++) {
String addr = getHostAddress(n);
if (isIPv4Address(addr) == true)
return true;
}
return false;
}
public final static boolean hasIPv6Addresses()
{
int addrCnt = getNHostAddresses();
for (int n=0; n<addrCnt; n++) {
String addr = getHostAddress(n);
if (isIPv6Address(addr) == true)
return true;
}
return false;
}
////////////////////////////////////////////////
// hasIPv?Interfaces
////////////////////////////////////////////////
public final static String getIPv4Address()
{
int addrCnt = getNHostAddresses();
for (int n=0; n<addrCnt; n++) {
String addr = getHostAddress(n);
if (isIPv4Address(addr) == true)
return addr;
}
return "";
}
public final static String getIPv6Address()
{
int addrCnt = getNHostAddresses();
for (int n=0; n<addrCnt; n++) {
String addr = getHostAddress(n);
if (isIPv6Address(addr) == true)
return addr;
}
return "";
}
////////////////////////////////////////////////
// getHostURL
////////////////////////////////////////////////
public final static String getHostURL(String host, int port, String uri)
{
String hostAddr = host;
if (isIPv6Address(host) == true)
hostAddr = "[" + host + "]";
return
"http://" +
hostAddr +
":" + Integer.toString(port) +
uri;
}
}

View File

@ -0,0 +1,81 @@
/******************************************************************
*
* CyberSOAP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SOAP.java
*
* Revision;
*
* 12/11/02
* - first revision.
*
******************************************************************/
package org.cybergarage.soap;
import org.cybergarage.xml.*;
public class SOAP
{
public static final String ENVELOPE = "Envelope";
public static final String BODY = "Body";
public static final String RESPONSE = "Response";
public static final String FAULT = "Fault";
public static final String FAULT_CODE = "faultcode";
public static final String FAULT_STRING = "faultstring";
public static final String FAULTACTOR = "faultactor";
public static final String DETAIL = "detail";
public static final String RESULTSTATUS = "ResultStatus";
public static final String UPNP_ERROR = "UPnPError";
public static final String ERROR_CODE = "errorCode";
public static final String ERROR_DESCRIPTION = "errorDescription";
//public static final String XMLNS = "SOAP-ENV";
public static final String XMLNS = "s";
public static final String METHODNS = "u";
public static final String DELIM = ":";
public static final String XMLNS_URL = "http://schemas.xmlsoap.org/soap/envelope/";
public static final String ENCSTYLE_URL = "http://schemas.xmlsoap.org/soap/encoding/";
public static final String CONTENT_TYPE = "text/xml; charset=\"utf-8\"";
public static final String VERSION_HEADER = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
////////////////////////////////////////////////
// createEnvelopeBodyNode
////////////////////////////////////////////////
public final static Node createEnvelopeBodyNode()
{
// <Envelope>
Node envNode = new Node(SOAP.XMLNS + SOAP.DELIM + SOAP.ENVELOPE);
envNode.setAttribute("xmlns" + SOAP.DELIM + SOAP.XMLNS, SOAP.XMLNS_URL);
envNode.setAttribute(SOAP.XMLNS + SOAP.DELIM + "encodingStyle", SOAP.ENCSTYLE_URL);
// <Body>
Node bodyNode = new Node(SOAP.XMLNS + SOAP.DELIM + SOAP.BODY);
envNode.addNode(bodyNode);
return envNode;
}
////////////////////////////////////////////////
// XML Parser
////////////////////////////////////////////////
private static Parser xmlParser;
public final static void setXMLParser(Parser parser)
{
xmlParser = parser;
}
public final static Parser getXMLParser()
{
return xmlParser;
}
}

View File

@ -0,0 +1,183 @@
/******************************************************************
*
* CyberSOAP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SOAPRequest.java
*
* Revision;
*
* 12/11/02
* - first revision.
* 02/13/04
* - Ralf G. R. Bergs <Ralf@Ber.gs>, Inma Marin Lopez <inma@dif.um.es>.
* - Added XML header, <?xml version=\"1.0\"?> to setContent().
* 05/11/04
* - Changed the XML header to <?xml version="1.0" encoding="utf-8"?> in setContent().
*
******************************************************************/
package org.cybergarage.soap;
import java.io.*;
import org.cybergarage.http.*;
import org.cybergarage.xml.*;
import org.cybergarage.util.*;
public class SOAPRequest extends HTTPRequest
{
private final static String SOAPACTION = "SOAPACTION";
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SOAPRequest()
{
setContentType(SOAP.CONTENT_TYPE);
setMethod(HTTP.POST);
}
public SOAPRequest(HTTPRequest httpReq)
{
set(httpReq);
}
////////////////////////////////////////////////
// SOAPACTION
////////////////////////////////////////////////
public void setSOAPAction(String action)
{
setStringHeader(SOAPACTION, action);
}
public String getSOAPAction()
{
return getStringHeaderValue(SOAPACTION);
}
public boolean isSOAPAction(String value)
{
String headerValue = getHeaderValue(SOAPACTION);
if (headerValue == null)
return false;
if (headerValue.equals(value) == true)
return true;
String soapAction = getSOAPAction();
if (soapAction == null)
return false;
return soapAction.equals(value);
}
////////////////////////////////////////////////
// post
////////////////////////////////////////////////
public SOAPResponse postMessage(String host, int port)
{
HTTPResponse httpRes = post(host, port);
SOAPResponse soapRes = new SOAPResponse(httpRes);
byte content[] = soapRes.getContent();
if (content.length <= 0)
return soapRes;
try {
ByteArrayInputStream byteIn = new ByteArrayInputStream(content);
Parser xmlParser = SOAP.getXMLParser();
Node rootNode = xmlParser.parse(byteIn);
soapRes.setEnvelopeNode(rootNode);
}
catch (Exception e) {
Debug.warning(e);
}
return soapRes;
}
////////////////////////////////////////////////
// Node
////////////////////////////////////////////////
private Node rootNode;
private void setRootNode(Node node)
{
rootNode = node;
}
private synchronized Node getRootNode()
{
if (rootNode != null)
return rootNode;
try {
byte content[] = getContent();
ByteArrayInputStream contentIn = new ByteArrayInputStream(content);
Parser parser = SOAP.getXMLParser();
rootNode = parser.parse(contentIn);
}
catch (ParserException e) {
Debug.warning(e);
}
return rootNode;
}
////////////////////////////////////////////////
// XML
////////////////////////////////////////////////
public void setEnvelopeNode(Node node)
{
setRootNode(node);
}
public Node getEnvelopeNode()
{
return getRootNode();
}
public Node getBodyNode()
{
Node envNode = getEnvelopeNode();
if (envNode == null)
return null;
if (envNode.hasNodes() == false)
return null;
return envNode.getNode(0);
}
////////////////////////////////////////////////
// XML Contents
////////////////////////////////////////////////
public void setContent(Node node)
{
// Thanks for Ralf G. R. Bergs <Ralf@Ber.gs>, Inma Marin Lopez <inma@dif.um.es>.
String conStr = "";
conStr += SOAP.VERSION_HEADER;
conStr += "\n";
conStr += node.toString();
setContent(conStr);
}
////////////////////////////////////////////////
// print
////////////////////////////////////////////////
public void print()
{
System.out.println(toString());
if (hasContent() == true)
return;
Node rootElem = getRootNode();
if (rootElem == null)
return;
System.out.println(rootElem.toString());
}
}

View File

@ -0,0 +1,191 @@
/******************************************************************
*
* CyberSOAP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SOAPResponse.java
*
* Revision;
*
* 12/17/02
* - first revision.
* 02/13/04
* - Ralf G. R. Bergs <Ralf@Ber.gs>, Inma Marin Lopez <inma@dif.um.es>.
* - Added XML header, <?xml version="1.0"?> to setContent().
* 05/11/04
* - Changed the XML header to <?xml version="1.0" encoding="utf-8"?> in setContent().
*
******************************************************************/
package org.cybergarage.soap;
import org.cybergarage.http.*;
import org.cybergarage.xml.*;
public class SOAPResponse extends HTTPResponse
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SOAPResponse()
{
setRootNode(SOAP.createEnvelopeBodyNode());
setContentType(XML.CONTENT_TYPE);
}
public SOAPResponse(HTTPResponse httpRes)
{
super(httpRes);
setRootNode(SOAP.createEnvelopeBodyNode());
setContentType(XML.CONTENT_TYPE);
}
public SOAPResponse(SOAPResponse soapRes)
{
super(soapRes);
setEnvelopeNode(soapRes.getEnvelopeNode());
setContentType(XML.CONTENT_TYPE);
}
////////////////////////////////////////////////
// Node
////////////////////////////////////////////////
private Node rootNode;
private void setRootNode(Node node)
{
rootNode = node;
}
private Node getRootNode()
{
return rootNode;
}
////////////////////////////////////////////////
// SOAP Basic
////////////////////////////////////////////////
public void setEnvelopeNode(Node node)
{
setRootNode(node);
}
public Node getEnvelopeNode()
{
return getRootNode();
}
public Node getBodyNode()
{
Node envNode = getEnvelopeNode();
if (envNode == null)
return null;
return envNode.getNodeEndsWith(SOAP.BODY);
}
public Node getMethodResponseNode(String name)
{
Node bodyNode = getBodyNode();
if (bodyNode == null)
return null;
String methodResName = name + SOAP.RESPONSE;
return bodyNode.getNodeEndsWith(methodResName);
}
public Node getFaultNode()
{
Node bodyNode = getBodyNode();
if (bodyNode == null)
return null;
return bodyNode.getNodeEndsWith(SOAP.FAULT);
}
public Node getFaultCodeNode()
{
Node faultNode = getFaultNode();
if (faultNode == null)
return null;
return faultNode.getNodeEndsWith(SOAP.FAULT_CODE);
}
public Node getFaultStringNode()
{
Node faultNode = getFaultNode();
if (faultNode == null)
return null;
return faultNode.getNodeEndsWith(SOAP.FAULT_STRING);
}
public Node getFaultActorNode()
{
Node faultNode = getFaultNode();
if (faultNode == null)
return null;
return faultNode.getNodeEndsWith(SOAP.FAULTACTOR);
}
public Node getFaultDetailNode()
{
Node faultNode = getFaultNode();
if (faultNode == null)
return null;
return faultNode.getNodeEndsWith(SOAP.DETAIL);
}
public String getFaultCode()
{
Node node = getFaultCodeNode();
if (node == null)
return "";
return node.getValue();
}
public String getFaultString()
{
Node node = getFaultStringNode();
if (node == null)
return "";
return node.getValue();
}
public String getFaultActor()
{
Node node = getFaultActorNode();
if (node == null)
return "";
return node.getValue();
}
////////////////////////////////////////////////
// XML Contents
////////////////////////////////////////////////
public void setContent(Node node)
{
// Thanks for Ralf G. R. Bergs <Ralf@Ber.gs>, Inma Marin Lopez <inma@dif.um.es>.
String conStr = "";
conStr += SOAP.VERSION_HEADER;
conStr += "\n";
conStr += node.toString();
setContent(conStr);
}
////////////////////////////////////////////////
// print
////////////////////////////////////////////////
public void print()
{
System.out.println(toString());
if (hasContent() == true)
return;
Node rootElem = getRootNode();
if (rootElem == null)
return;
System.out.println(rootElem.toString());
}
}

View File

@ -0,0 +1,370 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: Action.java
*
* Revision;
*
* 12/05/02
* - first revision.
* 08/30/03
* - Gordano Sassaroli <sassarol@cefriel.it>
* - Problem : When invoking an action that has at least one out parameter, an error message is returned
* - Error : The action post method gets the entire list of arguments instead of only the in arguments
* 01/04/04
* - Added UPnP status methods.
* - Changed about new ActionListener interface.
* 01/05/04
* - Added clearOutputAgumentValues() to initialize the output values before calling performActionListener().
* 07/09/04
* - Thanks for Dimas <cyberrate@users.sourceforge.net> and Stefano Lenzi <kismet-sl@users.sourceforge.net>
* - Changed postControlAction() to set the status code to the UPnPStatus.
*
******************************************************************/
package org.cybergarage.upnp;
import org.cybergarage.xml.*;
import org.cybergarage.util.*;
import org.cybergarage.upnp.xml.*;
import org.cybergarage.upnp.control.*;
public class Action
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public final static String ELEM_NAME = "action";
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private Node serviceNode;
private Node actionNode;
private Node getServiceNode()
{
return serviceNode;
}
public Service getService()
{
return new Service(getServiceNode());
}
public Node getActionNode()
{
return actionNode;
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public Action(Node serviceNode, Node actionNode)
{
this.serviceNode = serviceNode;
this.actionNode = actionNode;
}
public Action(Action action)
{
this.serviceNode = action.getServiceNode();
this.actionNode = action.getActionNode();
}
////////////////////////////////////////////////
// Mutex
////////////////////////////////////////////////
private Mutex mutex = new Mutex();
public void lock()
{
mutex.lock();
}
public void unlock()
{
mutex.unlock();
}
////////////////////////////////////////////////
// isActionNode
////////////////////////////////////////////////
public static boolean isActionNode(Node node)
{
return Action.ELEM_NAME.equals(node.getName());
}
////////////////////////////////////////////////
// name
////////////////////////////////////////////////
private final static String NAME = "name";
public void setName(String value)
{
getActionNode().setNode(NAME, value);
}
public String getName()
{
return getActionNode().getNodeValue(NAME);
}
////////////////////////////////////////////////
// argumentList
////////////////////////////////////////////////
public ArgumentList getArgumentList()
{
ArgumentList argumentList = new ArgumentList();
Node argumentListNode = getActionNode().getNode(ArgumentList.ELEM_NAME);
if (argumentListNode == null)
return argumentList;
int nodeCnt = argumentListNode.getNNodes();
for (int n=0; n<nodeCnt; n++) {
Node node = argumentListNode.getNode(n);
if (Argument.isArgumentNode(node) == false)
continue;
Argument argument = new Argument(getServiceNode(), node);
argumentList.add(argument);
}
return argumentList;
}
public ArgumentList getInputArgumentList()
{
ArgumentList allArgList = getArgumentList();
int allArgCnt = allArgList.size();
ArgumentList argList = new ArgumentList();
for (int n=0; n<allArgCnt; n++) {
Argument arg = allArgList.getArgument(n);
if (arg.isInDirection() == false)
continue;
argList.add(arg);
}
return argList;
}
public ArgumentList getOutputArgumentList()
{
ArgumentList allArgList = getArgumentList();
int allArgCnt = allArgList.size();
ArgumentList argList = new ArgumentList();
for (int n=0; n<allArgCnt; n++) {
Argument arg = allArgList.getArgument(n);
if (arg.isOutDirection() == false)
continue;
argList.add(arg);
}
return argList;
}
public Argument getArgument(String name)
{
ArgumentList argList = getArgumentList();
int nArgs = argList.size();
for (int n=0; n<nArgs; n++) {
Argument arg = argList.getArgument(n);
String argName = arg.getName();
if (argName == null)
continue;
if (name.equals(argName) == true)
return arg;
}
return null;
}
public void setArgumentValues(ArgumentList argList)
{
getArgumentList().set(argList);
}
public void setArgumentValue(String name, String value)
{
Argument arg = getArgument(name);
if (arg == null)
return;
arg.setValue(value);
}
public void setArgumentValue(String name, int value)
{
setArgumentValue(name, Integer.toString(value));
}
private void clearOutputAgumentValues()
{
ArgumentList allArgList = getArgumentList();
int allArgCnt = allArgList.size();
for (int n=0; n<allArgCnt; n++) {
Argument arg = allArgList.getArgument(n);
if (arg.isOutDirection() == false)
continue;
arg.setValue("");
}
}
public String getArgumentValue(String name)
{
Argument arg = getArgument(name);
if (arg == null)
return "";
return arg.getValue();
}
public int getArgumentIntegerValue(String name)
{
Argument arg = getArgument(name);
if (arg == null)
return 0;
return arg.getIntegerValue();
}
////////////////////////////////////////////////
// UserData
////////////////////////////////////////////////
private ActionData getActionData()
{
Node node = getActionNode();
ActionData userData = (ActionData)node.getUserData();
if (userData == null) {
userData = new ActionData();
node.setUserData(userData);
userData.setNode(node);
}
return userData;
}
////////////////////////////////////////////////
// controlAction
////////////////////////////////////////////////
public ActionListener getActionListener()
{
return getActionData().getActionListener();
}
public void setActionListener(ActionListener listener)
{
getActionData().setActionListener(listener);
}
public boolean performActionListener(ActionRequest actionReq)
{
ActionListener listener = (ActionListener)getActionListener();
if (listener == null)
return false;
ActionResponse actionRes = new ActionResponse();
setStatus(UPnPStatus.INVALID_ACTION);
clearOutputAgumentValues();
if (listener.actionControlReceived(this) == true) {
actionRes.setResponse(this);
}
else {
UPnPStatus upnpStatus = getStatus();
actionRes.setFaultResponse(upnpStatus.getCode(), upnpStatus.getDescription());
}
if (Debug.isOn() == true)
actionRes.print();
actionReq.post(actionRes);
return true;
}
////////////////////////////////////////////////
// ActionControl
////////////////////////////////////////////////
private ControlResponse getControlResponse()
{
return getActionData().getControlResponse();
}
private void setControlResponse(ControlResponse res)
{
getActionData().setControlResponse(res);
}
public UPnPStatus getControlStatus()
{
return getControlResponse().getUPnPError();
}
////////////////////////////////////////////////
// postControlAction
////////////////////////////////////////////////
public boolean postControlAction()
{
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (08/30/03)
ArgumentList actionArgList = getArgumentList();
ArgumentList actionInputArgList = getInputArgumentList();
ActionRequest ctrlReq = new ActionRequest();
ctrlReq.setRequest(this, actionInputArgList);
if (Debug.isOn() == true)
ctrlReq.print();
ActionResponse ctrlRes = ctrlReq.post();
if (Debug.isOn() == true)
ctrlRes.print();
setControlResponse(ctrlRes);
// Thanks for Dimas <cyberrate@users.sourceforge.net> and Stefano Lenzi <kismet-sl@users.sourceforge.net> (07/09/04)
int statCode = ctrlRes.getStatusCode();
setStatus(statCode);
if (ctrlRes.isSuccessful() == false)
return false;
ArgumentList outArgList = ctrlRes.getResponse();
actionArgList.set(outArgList);
return true;
}
////////////////////////////////////////////////
// Debug
////////////////////////////////////////////////
public void print()
{
System.out.println("Action : " + getName());
ArgumentList argList = getArgumentList();
int nArgs = argList.size();
for (int n=0; n<nArgs; n++) {
Argument arg = argList.getArgument(n);
String name = arg.getName();
String value = arg.getValue();
String dir = arg.getDirection();
System.out.println(" [" + n + "] = " + dir + ", " + name + ", " + value);
}
}
////////////////////////////////////////////////
// UPnPStatus
////////////////////////////////////////////////
private UPnPStatus upnpStatus = new UPnPStatus();
public void setStatus(int code, String descr)
{
upnpStatus.setCode(code);
upnpStatus.setDescription(descr);
}
public void setStatus(int code)
{
setStatus(code, UPnPStatus.code2String(code));
}
public UPnPStatus getStatus()
{
return upnpStatus;
}
}

View File

@ -0,0 +1,46 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: ActionList.java
*
* Revision:
*
* 12/05/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp;
import java.util.*;
public class ActionList extends Vector
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
private static final long serialVersionUID = 1965922721316119846L;
public final static String ELEM_NAME = "actionList";
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public ActionList()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public Action getAction(int n)
{
return (Action)get(n);
}
}

View File

@ -0,0 +1,70 @@
/******************************************************************
*
* CyberLink for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: AllowedValue.java
*
* Revision:
*
* 03/27/04
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp;
import org.cybergarage.xml.*;
public class AllowedValue
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public final static String ELEM_NAME = "allowedValue";
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private Node allowedValueNode;
public Node getAllowedValueNode()
{
return allowedValueNode;
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public AllowedValue(Node node)
{
allowedValueNode = node;
}
////////////////////////////////////////////////
// isAllowedValueNode
////////////////////////////////////////////////
public static boolean isAllowedValueNode(Node node)
{
return ELEM_NAME.equals(node.getName());
}
////////////////////////////////////////////////
// Value
////////////////////////////////////////////////
public void setValue(String value)
{
getAllowedValueNode().setValue(value);
}
public String getValue()
{
return getAllowedValueNode().getValue();
}
}

View File

@ -0,0 +1,49 @@
/******************************************************************
*
* CyberLink for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: AllowedValueList.java
*
* Revision:
*
* 03/27/04
* - first revision.
* 02/28/05
* - Changed to use AllowedValue instead of String as the member.
*
******************************************************************/
package org.cybergarage.upnp;
import java.util.*;
public class AllowedValueList extends Vector
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
private static final long serialVersionUID = 5740394642751180992L;
public final static String ELEM_NAME = "allowedValueList";
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public AllowedValueList()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public AllowedValue getAllowedValue(int n)
{
return (AllowedValue)get(n);
}
}

View File

@ -0,0 +1,104 @@
/******************************************************************
*
* CyberLink for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: AllowedValueRange.java
*
* Revision:
*
* 03/27/04
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp;
import org.cybergarage.xml.*;
public class AllowedValueRange
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public final static String ELEM_NAME = "allowedValueRange";
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private Node allowedValueRangeNode;
public Node getAllowedValueRangeNode()
{
return allowedValueRangeNode;
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public AllowedValueRange(Node node)
{
allowedValueRangeNode = node;
}
////////////////////////////////////////////////
// isAllowedValueRangeNode
////////////////////////////////////////////////
public static boolean isAllowedValueRangeNode(Node node)
{
return ELEM_NAME.equals(node.getName());
}
////////////////////////////////////////////////
// minimum
////////////////////////////////////////////////
private final static String MINIMUM = "minimum";
public void setMinimum(String value)
{
getAllowedValueRangeNode().setNode(MINIMUM, value);
}
public String getMinimum()
{
return getAllowedValueRangeNode().getNodeValue(MINIMUM);
}
////////////////////////////////////////////////
// maximum
////////////////////////////////////////////////
private final static String MAXIMUM = "maximum";
public void setMaximum(String value)
{
getAllowedValueRangeNode().setNode(MAXIMUM, value);
}
public String getMaximum()
{
return getAllowedValueRangeNode().getNodeValue(MAXIMUM);
}
////////////////////////////////////////////////
// width
////////////////////////////////////////////////
private final static String STEP = "step";
public void setStep(String value)
{
getAllowedValueRangeNode().setNode(STEP, value);
}
public String getStep()
{
return getAllowedValueRangeNode().getNodeValue(STEP);
}
}

View File

@ -0,0 +1,231 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: Argument.java
*
* Revision;
*
* 12/05/02
* - first revision.
* 03/28/04
* - Added getRelatedStateVariable().
* - Changed setRelatedStateVariable() to setRelatedStateVariableName().
* - Changed getRelatedStateVariable() to getRelatedStateVariableName().
* - Added getActionNode() and getAction().
* - Added getServiceNode() and getService().
* - Added the parent service node to the constructor.
*
******************************************************************/
package org.cybergarage.upnp;
import org.cybergarage.xml.*;
import org.cybergarage.upnp.xml.*;
public class Argument
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public final static String ELEM_NAME = "argument";
public final static String IN = "in";
public final static String OUT = "out";
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private Node argumentNode;
private Node serviceNode;
public Node getArgumentNode()
{
return argumentNode;
}
private Node getServiceNode()
{
return serviceNode;
}
public Service getService()
{
return new Service(getServiceNode());
}
public Node getActionNode()
{
Node argumentLinstNode = getArgumentNode().getParentNode();
if (argumentLinstNode == null)
return null;
Node actionNode = argumentLinstNode.getParentNode();
if (actionNode == null)
return null;
if (Action.isActionNode(actionNode) == false)
return null;
return actionNode;
}
public Action getAction()
{
return new Action(getServiceNode(), getActionNode());
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public Argument()
{
argumentNode = new Node();
serviceNode = null;
}
public Argument(Node servNode, Node argNode)
{
serviceNode = servNode;
argumentNode = argNode;
}
public Argument(String name, String value)
{
this();
setName(name);
setValue(value);
}
////////////////////////////////////////////////
// isArgumentNode
////////////////////////////////////////////////
public static boolean isArgumentNode(Node node)
{
return Argument.ELEM_NAME.equals(node.getName());
}
////////////////////////////////////////////////
// name
////////////////////////////////////////////////
private final static String NAME = "name";
public void setName(String value)
{
getArgumentNode().setNode(NAME, value);
}
public String getName()
{
return getArgumentNode().getNodeValue(NAME);
}
////////////////////////////////////////////////
// direction
////////////////////////////////////////////////
private final static String DIRECTION = "direction";
public void setDirection(String value)
{
getArgumentNode().setNode(DIRECTION, value);
}
public String getDirection()
{
return getArgumentNode().getNodeValue(DIRECTION);
}
public boolean isInDirection()
{
String dir = getDirection();
if (dir == null)
return false;
return dir.equalsIgnoreCase(IN);
}
public boolean isOutDirection()
{
return !isInDirection();
}
////////////////////////////////////////////////
// relatedStateVariable
////////////////////////////////////////////////
private final static String RELATED_STATE_VARIABLE = "relatedStateVariable";
public void setRelatedStateVariableName(String value)
{
getArgumentNode().setNode(RELATED_STATE_VARIABLE, value);
}
public String getRelatedStateVariableName()
{
return getArgumentNode().getNodeValue(RELATED_STATE_VARIABLE);
}
public StateVariable getRelatedStateVariable()
{
Service service = getService();
if (service == null)
return null;
String relatedStatVarName = getRelatedStateVariableName();
return service.getStateVariable(relatedStatVarName);
}
////////////////////////////////////////////////
// UserData
////////////////////////////////////////////////
private ArgumentData getArgumentData()
{
Node node = getArgumentNode();
ArgumentData userData = (ArgumentData)node.getUserData();
if (userData == null) {
userData = new ArgumentData();
node.setUserData(userData);
userData.setNode(node);
}
return userData;
}
////////////////////////////////////////////////
// value
////////////////////////////////////////////////
public void setValue(String value)
{
getArgumentData().setValue(value);
}
public void setValue(int value)
{
setValue(Integer.toString(value));
}
public String getValue()
{
return getArgumentData().getValue();
}
public int getIntegerValue()
{
String value = getValue();
try {
return Integer.parseInt(value);
}
catch (Exception e) {
}
return 0;
}
////////////////////////////////////////////////
// Related
////////////////////////////////////////////////
}

View File

@ -0,0 +1,77 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: ArgumentList.java
*
* Revision:
*
* 12/05/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp;
import java.util.*;
public class ArgumentList extends Vector
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
private static final long serialVersionUID = -5412792105767389170L;
public final static String ELEM_NAME = "argumentList";
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public ArgumentList()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public Argument getArgument(int n)
{
return (Argument)get(n);
}
public Argument getArgument(String name)
{
int nArgs = size();
for (int n=0; n<nArgs; n++) {
Argument arg = getArgument(n);
String argName = arg.getName();
if (argName == null)
continue;
if (argName.equals(name) == true)
return arg;
}
return null;
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public void set(ArgumentList inArgList)
{
int nInArgs = inArgList.size();
for (int n=0; n<nInArgs; n++) {
Argument inArg = inArgList.getArgument(n);
String inArgName = inArg.getName();
Argument arg = getArgument(inArgName);
if (arg == null)
continue;
arg.setValue(inArg.getValue());
}
}
}

View File

@ -0,0 +1,917 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: ControlPoint.java
*
* Revision:
*
* 11/18/02
* - first revision.
* 05/13/03
* - Changed to create socket threads each local interfaces.
* (HTTP, SSDPNotiry, SSDPSerachResponse)
* 05/28/03
* - Changed to send m-serach packets from SSDPSearchResponseSocket.
* The socket doesn't bind interface address.
* - SSDPSearchResponsSocketList that binds a port and a interface can't
* send m-serch packets of IPv6 on J2SE v 1.4.1_02 and Redhat 9.
* 07/23/03
* - Suzan Foster (suislief)
* - Fixed a bug. HOST field was missing.
* 07/29/03
* - Synchronized when a device is added by the ssdp message.
* 09/08/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Problem : when an event notification message is received and the message
* contains updates on more than one variable, only the first variable update
* is notified.
* - Error : the other xml nodes of the message are ignored
* - Fix : add two methods to the NotifyRequest for extracting the property array
* and modify the httpRequestRecieved method in ControlPoint
* 12/12/03
* - Added a static() to initialize UPnP class.
* 01/06/04
* - Added the following methods to remove expired devices automatically
* removeExpiredDevices()
* setExpiredDeviceMonitoringInterval()/getExpiredDeviceMonitoringInterval()
* setDeviceDisposer()/getDeviceDisposer()
* 04/20/04
* - Added the following methods.
* start(String target, int mx) and start(String target).
* 06/23/04
* - Added setNMPRMode() and isNMPRMode().
* 07/08/04
* - Added renewSubscriberService().
* - Changed start() to create renew subscriber thread when the NMPR mode is true.
* 08/17/04
* - Fixed removeExpiredDevices() to remove using the device array.
* 10/16/04
* - Oliver Newell <newell@media-rush.com>
* - Added this class to allow ControlPoint applications to be notified when
* the ControlPoint base class adds/removes a UPnP device
* 03/30/05
* - Changed addDevice() to use Parser::parse(URL).
*
*******************************************************************/
package org.cybergarage.upnp;
import org.cybergarage.net.*;
import org.cybergarage.util.*;
import org.cybergarage.xml.*;
import org.cybergarage.http.*;
import org.cybergarage.upnp.control.*;
import org.cybergarage.upnp.ssdp.*;
import org.cybergarage.upnp.device.*;
import org.cybergarage.upnp.event.*;
import java.net.*;
public class ControlPoint implements HTTPRequestListener
{
private final static int DEFAULT_EVENTSUB_PORT = 8058;
private final static int DEFAULT_SSDP_PORT = 8008;
private final static int DEFAULT_EXPIRED_DEVICE_MONITORING_INTERVAL = 60;
private final static String DEFAULT_EVENTSUB_URI = "/evetSub";
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private SSDPNotifySocketList ssdpNotifySocketList;
private SSDPSearchResponseSocketList ssdpSearchResponseSocketList;
private SSDPNotifySocketList getSSDPNotifySocketList()
{
return ssdpNotifySocketList;
}
private SSDPSearchResponseSocketList getSSDPSearchResponseSocketList()
{
return ssdpSearchResponseSocketList;
}
////////////////////////////////////////////////
// Initialize
////////////////////////////////////////////////
static
{
UPnP.initialize();
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public ControlPoint(int ssdpPort, int httpPort)
{
ssdpNotifySocketList = new SSDPNotifySocketList();
ssdpSearchResponseSocketList = new SSDPSearchResponseSocketList();
setSSDPPort(ssdpPort);
setHTTPPort(httpPort);
setDeviceDisposer(null);
setExpiredDeviceMonitoringInterval(DEFAULT_EXPIRED_DEVICE_MONITORING_INTERVAL);
setRenewSubscriber(null);
setNMPRMode(false);
setRenewSubscriber(null);
}
public ControlPoint()
{
this(DEFAULT_SSDP_PORT, DEFAULT_EVENTSUB_PORT);
}
public void finalize()
{
stop();
}
////////////////////////////////////////////////
// Mutex
////////////////////////////////////////////////
private Mutex mutex = new Mutex();
public void lock()
{
mutex.lock();
}
public void unlock()
{
mutex.unlock();
}
////////////////////////////////////////////////
// Port (SSDP)
////////////////////////////////////////////////
private int ssdpPort = 0;
public int getSSDPPort() {
return ssdpPort;
}
public void setSSDPPort(int port) {
ssdpPort = port;
}
////////////////////////////////////////////////
// Port (EventSub)
////////////////////////////////////////////////
private int httpPort = 0;
public int getHTTPPort() {
return httpPort;
}
public void setHTTPPort(int port) {
httpPort = port;
}
////////////////////////////////////////////////
// NMPR
////////////////////////////////////////////////
private boolean nmprMode;
public void setNMPRMode(boolean flag)
{
nmprMode = flag;
}
public boolean isNMPRMode()
{
return nmprMode;
}
////////////////////////////////////////////////
// Device List
////////////////////////////////////////////////
private NodeList devNodeList = new NodeList();
private void addDevice(Node rootNode)
{
devNodeList.add(rootNode);
}
private synchronized void addDevice(SSDPPacket ssdpPacket)
{
if (ssdpPacket.isRootDevice() == false)
return;
String usn = ssdpPacket.getUSN();
String udn = USN.getUDN(usn);
Device dev = getDevice(udn);
if (dev != null) {
dev.setSSDPPacket(ssdpPacket);
return;
}
String location = ssdpPacket.getLocation();
try {
URL locationUrl = new URL(location);
Parser parser = UPnP.getXMLParser();
Node rootNode = parser.parse(locationUrl);
Device rootDev = getDevice(rootNode);
if (rootDev == null)
return;
rootDev.setSSDPPacket(ssdpPacket);
addDevice(rootNode);
// Thanks for Oliver Newell (2004/10/16)
// After node is added, invoke the AddDeviceListener to notify high-level
// control point application that a new device has been added. (The
// control point application must implement the DeviceChangeListener interface
// to receive the notifications)
performAddDeviceListener( rootDev );
}
catch (MalformedURLException me) {
Debug.warning(ssdpPacket.toString());
Debug.warning(me);
}
catch (ParserException pe) {
Debug.warning(ssdpPacket.toString());
Debug.warning(pe);
}
}
private Device getDevice(Node rootNode)
{
if (rootNode == null)
return null;
Node devNode = rootNode.getNode(Device.ELEM_NAME);
if (devNode == null)
return null;
return new Device(rootNode, devNode);
}
public DeviceList getDeviceList()
{
DeviceList devList = new DeviceList();
int nRoots = devNodeList.size();
for (int n=0; n<nRoots; n++) {
Node rootNode = devNodeList.getNode(n);
Device dev = getDevice(rootNode);
if (dev == null)
continue;
devList.add(dev);
}
return devList;
}
public Device getDevice(String name)
{
int nRoots = devNodeList.size();
for (int n=0; n<nRoots; n++) {
Node rootNode = devNodeList.getNode(n);
Device dev = getDevice(rootNode);
if (dev == null)
continue;
if (dev.isDevice(name) == true)
return dev;
Device cdev = dev.getDevice(name);
if (cdev != null)
return cdev;
}
return null;
}
public boolean hasDevice(String name)
{
return (getDevice(name) != null) ? true : false;
}
private void removeDevice(Node rootNode)
{
// Thanks for Oliver Newell (2004/10/16)
// Invoke device removal listener prior to actual removal so Device node
// remains valid for the duration of the listener (application may want
// to access the node)
Device dev = getDevice(rootNode);
if( dev != null && dev.isRootDevice() )
performRemoveDeviceListener( dev );
devNodeList.remove(rootNode);
}
private void removeDevice(Device dev)
{
if (dev == null)
return;
removeDevice(dev.getRootNode());
}
private void removeDevice(String name)
{
Device dev = getDevice(name);
removeDevice(dev);
}
private void removeDevice(SSDPPacket packet)
{
if (packet.isByeBye() == false)
return;
String usn = packet.getUSN();
String udn = USN.getUDN(usn);
removeDevice(udn);
}
////////////////////////////////////////////////
// Expired Device
////////////////////////////////////////////////
private Disposer deviceDisposer;
private long expiredDeviceMonitoringInterval;
public void removeExpiredDevices()
{
DeviceList devList = getDeviceList();
int devCnt = devList.size();
Device dev[] = new Device[devCnt];
for (int n=0; n<devCnt; n++)
dev[n] = devList.getDevice(n);
for (int n=0; n<devCnt; n++) {
if (dev[n].isExpired() == true) {
Debug.message("Expired device = " + dev[n].getFriendlyName());
removeDevice(dev[n]);
}
}
}
public void setExpiredDeviceMonitoringInterval(long interval)
{
expiredDeviceMonitoringInterval = interval;
}
public long getExpiredDeviceMonitoringInterval()
{
return expiredDeviceMonitoringInterval;
}
public void setDeviceDisposer(Disposer disposer)
{
deviceDisposer = disposer;
}
public Disposer getDeviceDisposer()
{
return deviceDisposer;
}
////////////////////////////////////////////////
// Notify
////////////////////////////////////////////////
private ListenerList deviceNotifyListenerList = new ListenerList();
public void addNotifyListener(NotifyListener listener)
{
deviceNotifyListenerList.add(listener);
}
public void removeNotifyListener(NotifyListener listener)
{
deviceNotifyListenerList.remove(listener);
}
public void performNotifyListener(SSDPPacket ssdpPacket)
{
int listenerSize = deviceNotifyListenerList.size();
for (int n=0; n<listenerSize; n++) {
NotifyListener listener = (NotifyListener)deviceNotifyListenerList.get(n);
listener.deviceNotifyReceived(ssdpPacket);
}
}
////////////////////////////////////////////////
// SearchResponse
////////////////////////////////////////////////
private ListenerList deviceSearchResponseListenerList = new ListenerList();
public void addSearchResponseListener(SearchResponseListener listener)
{
deviceSearchResponseListenerList.add(listener);
}
public void removeSearchResponseListener(SearchResponseListener listener)
{
deviceSearchResponseListenerList.remove(listener);
}
public void performSearchResponseListener(SSDPPacket ssdpPacket)
{
int listenerSize = deviceSearchResponseListenerList.size();
for (int n=0; n<listenerSize; n++) {
SearchResponseListener listener = (SearchResponseListener)deviceSearchResponseListenerList.get(n);
listener.deviceSearchResponseReceived(ssdpPacket);
}
}
/////////////////////////////////////////////////////////////////////
// Device status changes (device added or removed)
// Applications that support the DeviceChangeListener interface are
// notified immediately when a device is added to, or removed from,
// the control point.
/////////////////////////////////////////////////////////////////////
ListenerList deviceChangeListenerList = new ListenerList();
public void addDeviceChangeListener(DeviceChangeListener listener)
{
deviceChangeListenerList.add(listener);
}
public void removeDeviceChangeListener(DeviceChangeListener listener)
{
deviceChangeListenerList.remove(listener);
}
public void performAddDeviceListener( Device dev )
{
int listenerSize = deviceChangeListenerList.size();
for (int n=0; n<listenerSize; n++) {
DeviceChangeListener listener = (DeviceChangeListener)deviceChangeListenerList.get(n);
listener.deviceAdded( dev );
}
}
public void performRemoveDeviceListener( Device dev )
{
int listenerSize = deviceChangeListenerList.size();
for (int n=0; n<listenerSize; n++) {
DeviceChangeListener listener = (DeviceChangeListener)deviceChangeListenerList.get(n);
listener.deviceRemoved( dev );
}
}
////////////////////////////////////////////////
// SSDPPacket
////////////////////////////////////////////////
public void notifyReceived(SSDPPacket packet)
{
if (packet.isRootDevice() == true) {
if (packet.isAlive() == true)
addDevice(packet);
if (packet.isByeBye() == true)
removeDevice(packet);
}
performNotifyListener(packet);
}
public void searchResponseReceived(SSDPPacket packet)
{
if (packet.isRootDevice() == true)
addDevice(packet);
performSearchResponseListener(packet);
}
////////////////////////////////////////////////
// M-SEARCH
////////////////////////////////////////////////
private int searchMx = SSDP.DEFAULT_MSEARCH_MX;
public int getSearchMx()
{
return searchMx;
}
public void setSearchMx(int mx)
{
searchMx = mx;
}
public void search(String target, int mx)
{
SSDPSearchRequest msReq = new SSDPSearchRequest(target, mx);
SSDPSearchResponseSocketList ssdpSearchResponseSocketList = getSSDPSearchResponseSocketList();
ssdpSearchResponseSocketList.post(msReq);
}
public void search(String target)
{
search(target, SSDP.DEFAULT_MSEARCH_MX);
}
public void search()
{
search(ST.ROOT_DEVICE, SSDP.DEFAULT_MSEARCH_MX);
}
////////////////////////////////////////////////
// EventSub HTTPServer
////////////////////////////////////////////////
private HTTPServerList httpServerList = new HTTPServerList();
private HTTPServerList getHTTPServerList()
{
return httpServerList;
}
public void httpRequestRecieved(HTTPRequest httpReq)
{
if (Debug.isOn() == true)
httpReq.print();
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (09/08/03)
if (httpReq.isNotifyRequest() == true) {
NotifyRequest notifyReq = new NotifyRequest(httpReq);
String uuid = notifyReq.getSID();
long seq = notifyReq.getSEQ();
PropertyList props = notifyReq.getPropertyList();
int propCnt = props.size();
for (int n = 0; n < propCnt; n++) {
Property prop = props.getProperty(n);
String varName = prop.getName();
String varValue = prop.getValue();
performEventListener(uuid, seq, varName, varValue);
}
httpReq.returnOK();
return;
}
httpReq.returnBadRequest();
}
////////////////////////////////////////////////
// Event Listener
////////////////////////////////////////////////
private ListenerList eventListenerList = new ListenerList();
public void addEventListener(EventListener listener)
{
eventListenerList.add(listener);
}
public void removeEventListener(EventListener listener)
{
eventListenerList.remove(listener);
}
public void performEventListener(String uuid, long seq, String name, String value)
{
int listenerSize = eventListenerList.size();
for (int n=0; n<listenerSize; n++) {
EventListener listener = (EventListener)eventListenerList.get(n);
listener.eventNotifyReceived(uuid, seq, name, value);
}
}
////////////////////////////////////////////////
// Subscription
////////////////////////////////////////////////
private String eventSubURI = DEFAULT_EVENTSUB_URI;
public String getEventSubURI()
{
return eventSubURI;
}
public void setEventSubURI(String url)
{
eventSubURI = url;
}
private String getEventSubCallbackURL(String host)
{
return HostInterface.getHostURL(host, getHTTPPort(), getEventSubURI());
}
public boolean subscribe(Service service, long timeout)
{
if (service.isSubscribed() == true) {
String sid = service.getSID();
return subscribe(service, sid, timeout);
}
Device rootDev = service.getRootDevice();
if (rootDev == null)
return false;
String ifAddress = rootDev.getInterfaceAddress();
SubscriptionRequest subReq = new SubscriptionRequest();
subReq.setSubscribeRequest(service, getEventSubCallbackURL(ifAddress), timeout);
SubscriptionResponse subRes = subReq.post();
if (subRes.isSuccessful() == true) {
service.setSID(subRes.getSID());
service.setTimeout(subRes.getTimeout());
return true;
}
service.clearSID();
return false;
}
public boolean subscribe(Service service)
{
return subscribe(service, Subscription.INFINITE_VALUE);
}
public boolean subscribe(Service service, String uuid, long timeout)
{
SubscriptionRequest subReq = new SubscriptionRequest();
subReq.setRenewRequest(service, uuid, timeout);
if (Debug.isOn() == true)
subReq.print();
SubscriptionResponse subRes = subReq.post();
if (Debug.isOn() == true)
subRes.print();
if (subRes.isSuccessful() == true) {
service.setSID(subRes.getSID());
service.setTimeout(subRes.getTimeout());
return true;
}
service.clearSID();
return false;
}
public boolean subscribe(Service service, String uuid)
{
return subscribe(service, uuid, Subscription.INFINITE_VALUE);
}
public boolean isSubscribed(Service service)
{
if (service == null)
return false;
return service.isSubscribed();
}
public boolean unsubscribe(Service service)
{
SubscriptionRequest subReq = new SubscriptionRequest();
subReq.setUnsubscribeRequest(service);
SubscriptionResponse subRes = subReq.post();
if (subRes.isSuccessful() == true) {
service.clearSID();
return true;
}
return false;
}
public void unsubscribe(Device device)
{
ServiceList serviceList = device.getServiceList();
int serviceCnt = serviceList.size();
for (int n=0; n<serviceCnt; n++) {
Service service = serviceList.getService(n);
if (service.hasSID() == true)
unsubscribe(service);
}
DeviceList childDevList = device.getDeviceList();
int childDevCnt = childDevList.size();
for (int n=0; n<childDevCnt; n++) {
Device cdev = childDevList.getDevice(n);
unsubscribe(cdev);
}
}
public void unsubscribe()
{
DeviceList devList = getDeviceList();
int devCnt = devList.size();
for (int n=0; n<devCnt; n++) {
Device dev = devList.getDevice(n);
unsubscribe(dev);
}
}
////////////////////////////////////////////////
// getSubscriberService
////////////////////////////////////////////////
public Service getSubscriberService(String uuid)
{
DeviceList devList = getDeviceList();
int devCnt = devList.size();
for (int n=0; n<devCnt; n++) {
Device dev = devList.getDevice(n);
Service service = dev.getSubscriberService(uuid);
if (service != null)
return service;
}
return null;
}
////////////////////////////////////////////////
// getSubscriberService
////////////////////////////////////////////////
public void renewSubscriberService(Device dev, long timeout)
{
ServiceList serviceList = dev.getServiceList();
int serviceCnt = serviceList.size();
for (int n=0; n<serviceCnt; n++) {
Service service = serviceList.getService(n);
if (service.isSubscribed() == false)
continue;
String sid = service.getSID();
boolean isRenewed = subscribe(service, sid, timeout);
if (isRenewed == false)
subscribe(service, timeout);
}
DeviceList cdevList = dev.getDeviceList();
int cdevCnt = cdevList.size();
for (int n=0; n<cdevCnt; n++) {
Device cdev = cdevList.getDevice(n);
renewSubscriberService(cdev, timeout);
}
}
public void renewSubscriberService(long timeout)
{
DeviceList devList = getDeviceList();
int devCnt = devList.size();
for (int n=0; n<devCnt; n++) {
Device dev = devList.getDevice(n);
renewSubscriberService(dev, timeout);
}
}
public void renewSubscriberService()
{
renewSubscriberService(Subscription.INFINITE_VALUE);
}
////////////////////////////////////////////////
// Subscriber
////////////////////////////////////////////////
private RenewSubscriber renewSubscriber;
public void setRenewSubscriber(RenewSubscriber sub)
{
renewSubscriber = sub;
}
public RenewSubscriber getRenewSubscriber()
{
return renewSubscriber;
}
////////////////////////////////////////////////
// run
////////////////////////////////////////////////
public boolean start(String target, int mx)
{
stop();
////////////////////////////////////////
// HTTP Server
////////////////////////////////////////
int retryCnt = 0;
int bindPort = getHTTPPort();
HTTPServerList httpServerList = getHTTPServerList();
while (httpServerList.open(bindPort) == false) {
retryCnt++;
if (UPnP.SERVER_RETRY_COUNT < retryCnt)
return false;
setHTTPPort(bindPort + 1);
bindPort = getHTTPPort();
}
httpServerList.addRequestListener(this);
httpServerList.start();
////////////////////////////////////////
// Notify Socket
////////////////////////////////////////
SSDPNotifySocketList ssdpNotifySocketList = getSSDPNotifySocketList();
if (ssdpNotifySocketList.open() == false)
return false;
ssdpNotifySocketList.setControlPoint(this);
ssdpNotifySocketList.start();
////////////////////////////////////////
// SeachResponse Socket
////////////////////////////////////////
int ssdpPort = getSSDPPort();
retryCnt = 0;
SSDPSearchResponseSocketList ssdpSearchResponseSocketList = getSSDPSearchResponseSocketList();
while (ssdpSearchResponseSocketList.open(ssdpPort) == false) {
retryCnt++;
if (UPnP.SERVER_RETRY_COUNT < retryCnt)
return false;
setSSDPPort(ssdpPort + 1);
ssdpPort = getSSDPPort();
}
ssdpSearchResponseSocketList.setControlPoint(this);
ssdpSearchResponseSocketList.start();
////////////////////////////////////////
// search root devices
////////////////////////////////////////
search(target, mx);
////////////////////////////////////////
// Disposer
////////////////////////////////////////
Disposer disposer = new Disposer(this);
setDeviceDisposer(disposer);
disposer.start();
////////////////////////////////////////
// Subscriber
////////////////////////////////////////
if (isNMPRMode() == true) {
RenewSubscriber renewSub = new RenewSubscriber(this);
setRenewSubscriber(renewSub);
renewSub.start();
}
return true;
}
public boolean start(String target)
{
return start(target, SSDP.DEFAULT_MSEARCH_MX);
}
public boolean start()
{
return start(ST.ROOT_DEVICE, SSDP.DEFAULT_MSEARCH_MX);
}
public boolean stop()
{
unsubscribe();
SSDPNotifySocketList ssdpNotifySocketList = getSSDPNotifySocketList();
ssdpNotifySocketList.stop();
ssdpNotifySocketList.close();
ssdpNotifySocketList.clear();
SSDPSearchResponseSocketList ssdpSearchResponseSocketList = getSSDPSearchResponseSocketList();
ssdpSearchResponseSocketList.stop();
ssdpSearchResponseSocketList.close();
ssdpSearchResponseSocketList.clear();
HTTPServerList httpServerList = getHTTPServerList();
httpServerList.stop();
httpServerList.close();
httpServerList.clear();
////////////////////////////////////////
// Disposer
////////////////////////////////////////
Disposer disposer = getDeviceDisposer();
if (disposer != null) {
disposer.stop();
setDeviceDisposer(null);
}
////////////////////////////////////////
// Subscriber
////////////////////////////////////////
RenewSubscriber renewSub = getRenewSubscriber();
if (renewSub != null) {
renewSub.stop();
setRenewSubscriber(null);
}
return true;
}
////////////////////////////////////////////////
// print
////////////////////////////////////////////////
public void print()
{
DeviceList devList = getDeviceList();
int devCnt = devList.size();
Debug.message("Device Num = " + devCnt);
for (int n=0; n<devCnt; n++) {
Device dev = devList.getDevice(n);
Debug.message("[" + n + "] " + dev.getFriendlyName() + ", " + dev.getLeaseTime() + ", " + dev.getElapsedTime());
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,46 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: DeviceList.java
*
* Revision;
*
* 12/04/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp;
import java.util.*;
public class DeviceList extends Vector
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
private static final long serialVersionUID = 3773784061607435126L;
public final static String ELEM_NAME = "deviceList";
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public DeviceList()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public Device getDevice(int n)
{
return (Device)get(n);
}
}

View File

@ -0,0 +1,136 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: Icon.java
*
* Revision;
*
* 11/28/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp;
import org.cybergarage.xml.*;
public class Icon
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public final static String ELEM_NAME = "icon";
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private Node iconNode;
public Node getIconNode()
{
return iconNode;
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public Icon(Node node)
{
iconNode = node;
}
////////////////////////////////////////////////
// isIconNode
////////////////////////////////////////////////
public static boolean isIconNode(Node node)
{
return Icon.ELEM_NAME.equals(node.getName());
}
////////////////////////////////////////////////
// mimeType
////////////////////////////////////////////////
private final static String MIME_TYPE = "mimeType";
public void setMimeType(String value)
{
getIconNode().setNode(MIME_TYPE, value);
}
public String getMimeType()
{
return getIconNode().getNodeValue(MIME_TYPE);
}
////////////////////////////////////////////////
// width
////////////////////////////////////////////////
private final static String WIDTH = "width";
public void setWidth(String value)
{
getIconNode().setNode(WIDTH, value);
}
public String getWidth()
{
return getIconNode().getNodeValue(WIDTH);
}
////////////////////////////////////////////////
// height
////////////////////////////////////////////////
private final static String HEIGHT = "height";
public void setHeight(String value)
{
getIconNode().setNode(HEIGHT, value);
}
public String getHeight()
{
return getIconNode().getNodeValue(HEIGHT);
}
////////////////////////////////////////////////
// depth
////////////////////////////////////////////////
private final static String DEPTH = "depth";
public void setDepth(String value)
{
getIconNode().setNode(DEPTH, value);
}
public String getDepth()
{
return getIconNode().getNodeValue(DEPTH);
}
////////////////////////////////////////////////
// URL
////////////////////////////////////////////////
private final static String URL = "url";
public void setURL(String value)
{
getIconNode().setNode(URL, value);
}
public String getURL()
{
return getIconNode().getNodeValue(URL);
}
}

View File

@ -0,0 +1,46 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: IconList.java
*
* Revision;
*
* 12/04/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp;
import java.util.*;
public class IconList extends Vector
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
private static final long serialVersionUID = -1097238335037012991L;
public final static String ELEM_NAME = "iconList";
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public IconList()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public Icon getIcon(int n)
{
return (Icon)get(n);
}
}

View File

@ -0,0 +1,747 @@
/******************************************************************
*
* CyberLink for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: Service.java
*
* Revision;
*
* 11/28/02
* - first revision.
* 04/12/02
* - Holmes, Arran C <acholm@essex.ac.uk>
* - Fixed SERVICE_ID constant instead of "serviceId".
* 06/17/03
* - Added notifyAllStateVariables().
* 09/03/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Problem : The device does not accepts request for services when control or subscription urls are absolute
* - Error : device methods, when requests are received, search for services that have a controlUrl (or eventSubUrl) equal to the request URI
* but request URI must be relative, so they cannot equal absolute urls
* 09/03/03
* - Steven Yen
* - description: to retrieve service information based on information in URLBase and SCPDURL
* - problem: not able to retrieve service information when URLBase is missing and SCPDURL is relative
* - fix: modify to retrieve host information from Header's Location (required) field and update the
* BaseURL tag in the xml so subsequent information retrieval can be done (Steven Yen, 8.27.2003)
* - note: 1. in the case that Header's Location field combine with SCPDURL is not able to retrieve proper
* information, updating BaseURL would not hurt, since exception will be thrown with or without update.
* 2. this problem was discovered when using PC running MS win XP with ICS enabled (gateway).
* It seems that root device xml file does not have BaseURL and SCPDURL are all relative.
* 3. UPnP device architecture states that BaseURL is optional and SCPDURL may be relative as
* specified by UPnP vendor, so MS does not seem to violate the rule.
* 10/22/03
* - Added setActionListener().
* 01/04/04
* - Changed about new QueryListener interface.
* 01/06/04
* - Moved the following methods to StateVariable class.
* getQueryListener()
* setQueryListener()
* performQueryListener()
* - Added new setQueryListener() to set a listner to all state variables.
* 07/02/04
* - Added serviceSearchResponse().
* - Deleted getLocationURL().
* - Fixed announce() to set the root device URL to the LOCATION field.
* 07/31/04
* - Changed notify() to remove the expired subscribers and not to remove the invalid response subscribers for NMPR.
* 10/29/04
* - Fixed a bug when notify() removes the expired devices().
* 03/23/05
* - Added loadSCPD() to load the description from memory.
* 03/30/05
* - Added isSCPDURL().
* - Removed setDescriptionURL() and getDescriptionURL()
* 03/31/05
* - Added getSCPDData().
* 04/25/05
* - Thanks for Mikael Hakman <mhakman@dkab.net>
* - Changed getSCPDData() to add a XML declaration at first line.
*
******************************************************************/
package org.cybergarage.upnp;
import java.io.*;
import java.net.*;
import org.cybergarage.http.*;
import org.cybergarage.xml.*;
import org.cybergarage.util.*;
import org.cybergarage.upnp.ssdp.*;
import org.cybergarage.upnp.xml.*;
import org.cybergarage.upnp.device.*;
import org.cybergarage.upnp.control.*;
import org.cybergarage.upnp.event.*;
public class Service
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public final static String ELEM_NAME = "service";
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private Node serviceNode;
public Node getServiceNode()
{
return serviceNode;
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public Service(Node node)
{
serviceNode = node;
}
////////////////////////////////////////////////
// Mutex
////////////////////////////////////////////////
private Mutex mutex = new Mutex();
public void lock()
{
mutex.lock();
}
public void unlock()
{
mutex.unlock();
}
////////////////////////////////////////////////
// isServiceNode
////////////////////////////////////////////////
public static boolean isServiceNode(Node node)
{
return Service.ELEM_NAME.equals(node.getName());
}
////////////////////////////////////////////////
// Device/Root Node
////////////////////////////////////////////////
private Node getDeviceNode()
{
Node node = getServiceNode().getParentNode();
if (node == null)
return null;
return node.getParentNode();
}
private Node getRootNode()
{
return getServiceNode().getRootNode();
}
////////////////////////////////////////////////
// Device
////////////////////////////////////////////////
public Device getDevice()
{
return new Device(getRootNode(), getDeviceNode());
}
public Device getRootDevice()
{
return getDevice().getRootDevice();
}
////////////////////////////////////////////////
// serviceType
////////////////////////////////////////////////
private final static String SERVICE_TYPE = "serviceType";
public void setServiceType(String value)
{
getServiceNode().setNode(SERVICE_TYPE, value);
}
public String getServiceType()
{
return getServiceNode().getNodeValue(SERVICE_TYPE);
}
////////////////////////////////////////////////
// serviceID
////////////////////////////////////////////////
private final static String SERVICE_ID = "serviceId";
public void setServiceID(String value)
{
getServiceNode().setNode(SERVICE_ID, value);
}
public String getServiceID()
{
return getServiceNode().getNodeValue(SERVICE_ID);
}
////////////////////////////////////////////////
// isURL
////////////////////////////////////////////////
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (09/03/03)
private boolean isURL(String referenceUrl, String url)
{
if (referenceUrl ==null || url == null)
return false;
boolean ret = url.equals(referenceUrl);
if (ret == true)
return true;
String relativeRefUrl = HTTP.toRelativeURL(referenceUrl, false);
ret = url.equals(relativeRefUrl);
if (ret == true)
return true;
return false;
}
////////////////////////////////////////////////
// SCPDURL
////////////////////////////////////////////////
private final static String SCPDURL = "SCPDURL";
public void setSCPDURL(String value)
{
getServiceNode().setNode(SCPDURL, value);
}
public String getSCPDURL()
{
return getServiceNode().getNodeValue(SCPDURL);
}
public boolean isSCPDURL(String url)
{
return isURL(getSCPDURL(), url);
}
////////////////////////////////////////////////
// controlURL
////////////////////////////////////////////////
private final static String CONTROL_URL = "controlURL";
public void setControlURL(String value)
{
getServiceNode().setNode(CONTROL_URL, value);
}
public String getControlURL()
{
return getServiceNode().getNodeValue(CONTROL_URL);
}
public boolean isControlURL(String url)
{
return isURL(getControlURL(), url);
}
////////////////////////////////////////////////
// eventSubURL
////////////////////////////////////////////////
private final static String EVENT_SUB_URL = "eventSubURL";
public void setEventSubURL(String value)
{
getServiceNode().setNode(EVENT_SUB_URL, value);
}
public String getEventSubURL()
{
return getServiceNode().getNodeValue(EVENT_SUB_URL);
}
public boolean isEventSubURL(String url)
{
return isURL(getEventSubURL(), url);
}
////////////////////////////////////////////////
// SCPD node
////////////////////////////////////////////////
public boolean loadSCPD(String scpdStr) throws InvalidDescriptionException
{
try {
Parser parser = UPnP.getXMLParser();
Node scpdNode = parser.parse(scpdStr);
if (scpdNode == null)
return false;
ServiceData data = getServiceData();
data.setSCPDNode(scpdNode);
}
catch (ParserException e) {
throw new InvalidDescriptionException(e);
}
return true;
}
public boolean loadSCPD(File file) throws ParserException
{
Parser parser = UPnP.getXMLParser();
Node scpdNode = parser.parse(file);
if (scpdNode == null)
return false;
ServiceData data = getServiceData();
data.setSCPDNode(scpdNode);
return true;
}
private Node getSCPDNode(URL scpdUrl) throws ParserException
{
Parser parser = UPnP.getXMLParser();
return parser.parse(scpdUrl);
}
private Node getSCPDNode(File scpdFile) throws ParserException
{
Parser parser = UPnP.getXMLParser();
return parser.parse(scpdFile);
}
private Node getSCPDNode()
{
ServiceData data = getServiceData();
Node scpdNode = data.getSCPDNode();
if (scpdNode != null)
return scpdNode;
String scpdURLStr = getSCPDURL();
try {
URL scpdUrl = new URL(scpdURLStr);
scpdNode = getSCPDNode(scpdUrl);
}
catch (Exception e1) {
Device rootDev = getRootDevice();
String urlBaseStr = rootDev.getURLBase();
// Thanks for Steven Yen (2003/09/03)
if (urlBaseStr == null || urlBaseStr.length() <= 0) {
String location = rootDev.getLocation();
String locationHost = HTTP.getHost(location);
int locationPort = HTTP.getPort(location);
urlBaseStr = HTTP.getRequestHostURL(locationHost, locationPort);
}
scpdURLStr = HTTP.toRelativeURL(scpdURLStr);
String newScpdURLStr = urlBaseStr + scpdURLStr;
try {
URL newScpdURL = new URL(newScpdURLStr);
scpdNode = getSCPDNode(newScpdURL);
}
catch (Exception e2) {
newScpdURLStr = HTTP.getAbsoluteURL(urlBaseStr, scpdURLStr);
try {
URL newScpdURL = new URL(newScpdURLStr);
scpdNode = getSCPDNode(newScpdURL);
}
catch (Exception e3) {
newScpdURLStr = rootDev.getDescriptionFilePath() + scpdURLStr;
try {
scpdNode = getSCPDNode(new File(newScpdURLStr));
}
catch (Exception e4) {
Debug.warning(e4);
}
}
}
}
data.setSCPDNode(scpdNode);
return scpdNode;
}
public byte[] getSCPDData()
{
Node scpdNode = getSCPDNode();
if (scpdNode == null)
return new byte[0];
// Thanks for Mikael Hakman (04/25/05)
String desc = new String();
desc += UPnP.XML_DECLARATION;
desc += "\n";
desc += scpdNode.toString();
return desc.getBytes();
}
////////////////////////////////////////////////
// actionList
////////////////////////////////////////////////
public ActionList getActionList()
{
ActionList actionList = new ActionList();
Node scdpNode = getSCPDNode();
if (scdpNode == null)
return actionList;
Node actionListNode = scdpNode.getNode(ActionList.ELEM_NAME);
if (actionListNode == null)
return actionList;
Node serviceNode = getServiceNode();
int nNode = actionListNode.getNNodes();
for (int n=0; n<nNode; n++) {
Node node = actionListNode.getNode(n);
if (Action.isActionNode(node) == false)
continue;
Action action = new Action(serviceNode, node);
actionList.add(action);
}
return actionList;
}
public Action getAction(String actionName)
{
ActionList actionList = getActionList();
int nActions = actionList.size();
for (int n=0; n<nActions; n++) {
Action action = actionList.getAction(n);
String name = action.getName();
if (name == null)
continue;
if (name.equals(actionName) == true)
return action;
}
return null;
}
////////////////////////////////////////////////
// serviceStateTable
////////////////////////////////////////////////
public ServiceStateTable getServiceStateTable()
{
ServiceStateTable stateTable = new ServiceStateTable();
Node stateTableNode = getSCPDNode().getNode(ServiceStateTable.ELEM_NAME);
if (stateTableNode == null)
return stateTable;
Node serviceNode = getServiceNode();
int nNode = stateTableNode.getNNodes();
for (int n=0; n<nNode; n++) {
Node node = stateTableNode.getNode(n);
if (StateVariable.isStateVariableNode(node) == false)
continue;
StateVariable serviceVar = new StateVariable(serviceNode, node);
stateTable.add(serviceVar);
}
return stateTable;
}
public StateVariable getStateVariable(String name)
{
ServiceStateTable stateTable = getServiceStateTable();
int tableSize = stateTable.size();
for (int n=0; n<tableSize; n++) {
StateVariable var = stateTable.getStateVariable(n);
String varName = var.getName();
if (varName == null)
continue;
if (varName.equals(name) == true)
return var;
}
return null;
}
public boolean hasStateVariable(String name)
{
return (getStateVariable(name) != null ) ? true : false;
}
////////////////////////////////////////////////
// UserData
////////////////////////////////////////////////
public boolean isService(String name)
{
if (name == null)
return false;
if (name.endsWith(getServiceType()) == true)
return true;
if (name.endsWith(getServiceID()) == true)
return true;
return false;
}
////////////////////////////////////////////////
// UserData
////////////////////////////////////////////////
private ServiceData getServiceData()
{
Node node = getServiceNode();
ServiceData userData = (ServiceData)node.getUserData();
if (userData == null) {
userData = new ServiceData();
node.setUserData(userData);
userData.setNode(node);
}
return userData;
}
////////////////////////////////////////////////
// Notify
////////////////////////////////////////////////
private String getNotifyServiceTypeNT()
{
return getServiceType();
}
private String getNotifyServiceTypeUSN()
{
return getDevice().getUDN() + "::" + getServiceType();
}
public void announce(String bindAddr)
{
// uuid:device-UUID::urn:schemas-upnp-org:service:serviceType:v
Device rootDev = getRootDevice();
String devLocation = rootDev.getLocationURL(bindAddr);
String serviceNT = getNotifyServiceTypeNT();
String serviceUSN = getNotifyServiceTypeUSN();
Device dev = getDevice();
SSDPNotifyRequest ssdpReq = new SSDPNotifyRequest();
ssdpReq.setServer(UPnP.getServerName());
ssdpReq.setLeaseTime(dev.getLeaseTime());
ssdpReq.setLocation(devLocation);
ssdpReq.setNTS(NTS.ALIVE);
ssdpReq.setNT(serviceNT);
ssdpReq.setUSN(serviceUSN);
SSDPNotifySocket ssdpSock = new SSDPNotifySocket(bindAddr);
Device.notifyWait();
ssdpSock.post(ssdpReq);
}
public void byebye(String bindAddr)
{
// uuid:device-UUID::urn:schemas-upnp-org:service:serviceType:v
String devNT = getNotifyServiceTypeNT();
String devUSN = getNotifyServiceTypeUSN();
SSDPNotifyRequest ssdpReq = new SSDPNotifyRequest();
ssdpReq.setNTS(NTS.BYEBYE);
ssdpReq.setNT(devNT);
ssdpReq.setUSN(devUSN);
SSDPNotifySocket ssdpSock = new SSDPNotifySocket(bindAddr);
Device.notifyWait();
ssdpSock.post(ssdpReq);
}
public boolean serviceSearchResponse(SSDPPacket ssdpPacket)
{
String ssdpST = ssdpPacket.getST();
if (ssdpST == null)
return false;
Device dev = getDevice();
String serviceNT = getNotifyServiceTypeNT();
String serviceUSN = getNotifyServiceTypeUSN();
if (ST.isAllDevice(ssdpST) == true) {
dev.postSearchResponse(ssdpPacket, serviceNT, serviceUSN);
}
else if (ST.isURNService(ssdpST) == true) {
String serviceType = getServiceType();
if (ssdpST.equals(serviceType) == true)
dev.postSearchResponse(ssdpPacket, serviceType, serviceUSN);
}
return true;
}
////////////////////////////////////////////////
// QueryListener
////////////////////////////////////////////////
public void setQueryListener(QueryListener queryListener)
{
ServiceStateTable stateTable = getServiceStateTable();
int tableSize = stateTable.size();
for (int n=0; n<tableSize; n++) {
StateVariable var = stateTable.getStateVariable(n);
var.setQueryListener(queryListener);
}
}
////////////////////////////////////////////////
// Subscription
////////////////////////////////////////////////
public SubscriberList getSubscriberList()
{
return getServiceData().getSubscriberList();
}
public void addSubscriber(Subscriber sub)
{
getSubscriberList().add(sub);
}
public void removeSubscriber(Subscriber sub)
{
getSubscriberList().remove(sub);
}
public Subscriber getSubscriber(String name)
{
SubscriberList subList = getSubscriberList();
int subListCnt = subList.size();
for (int n=0; n<subListCnt; n++) {
Subscriber sub = subList.getSubscriber(n);
if (sub == null)
continue;
String sid = sub.getSID();
if (sid == null)
continue;
if (sid.equals(name) == true)
return sub;
}
return null;
}
private boolean notify(Subscriber sub, StateVariable stateVar)
{
String varName = stateVar.getName();
String value = stateVar.getValue();
String host = sub.getDeliveryHost();
int port = sub.getDeliveryPort();
NotifyRequest notifyReq = new NotifyRequest();
notifyReq.setRequest(sub, varName, value);
HTTPResponse res = notifyReq.post(host, port);
if (res.isSuccessful() == false)
return false;
sub.incrementNotifyCount();
return true;
}
public void notify(StateVariable stateVar)
{
SubscriberList subList = getSubscriberList();
int subListCnt;
Subscriber subs[];
// Remove expired subscribers.
subListCnt = subList.size();
subs = new Subscriber[subListCnt];
for (int n=0; n<subListCnt; n++)
subs[n] = subList.getSubscriber(n);
for (int n=0; n<subListCnt; n++) {
Subscriber sub = subs[n];
if (sub.isExpired() == true)
removeSubscriber(sub);
}
// Notify to subscribers.
subListCnt = subList.size();
subs = new Subscriber[subListCnt];
for (int n=0; n<subListCnt; n++)
subs[n] = subList.getSubscriber(n);
for (int n=0; n<subListCnt; n++) {
Subscriber sub = subs[n];
if (notify(sub, stateVar) == false) {
/* Don't remove for NMPR specification.
removeSubscriber(sub);
*/
}
}
}
public void notifyAllStateVariables()
{
ServiceStateTable stateTable = getServiceStateTable();
int tableSize = stateTable.size();
for (int n=0; n<tableSize; n++) {
StateVariable var = stateTable.getStateVariable(n);
if (var.isSendEvents() == true)
notify(var);
}
}
////////////////////////////////////////////////
// SID
////////////////////////////////////////////////
public String getSID()
{
return getServiceData().getSID();
}
public void setSID(String id)
{
getServiceData().setSID(id);
}
public void clearSID()
{
setSID("");
setTimeout(0);
}
public boolean hasSID()
{
return StringUtil.hasData(getSID());
}
public boolean isSubscribed()
{
return hasSID();
}
////////////////////////////////////////////////
// Timeout
////////////////////////////////////////////////
public long getTimeout()
{
return getServiceData().getTimeout();
}
public void setTimeout(long value)
{
getServiceData().setTimeout(value);
}
////////////////////////////////////////////////
// AcionListener
////////////////////////////////////////////////
public void setActionListener(ActionListener listener)
{
ActionList actionList = getActionList();
int nActions = actionList.size();
for (int n=0; n<nActions; n++) {
Action action = actionList.getAction(n);
action.setActionListener(listener);
}
}
}

View File

@ -0,0 +1,53 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: ServiceList.java
*
* Revision;
*
* 12/04/02
* - first revision.
* 06/18/03
* - Added caching a ArrayIndexOfBound exception.
*
******************************************************************/
package org.cybergarage.upnp;
import java.util.*;
public class ServiceList extends Vector
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
private static final long serialVersionUID = 6372904993975135597L;
public final static String ELEM_NAME = "serviceList";
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public ServiceList()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public Service getService(int n)
{
Object obj = null;
try {
obj = get(n);
}
catch (Exception e) {};
return (Service)obj;
}
}

View File

@ -0,0 +1,46 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: ServiceStateTable.java
*
* Revision:
*
* 12/06/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp;
import java.util.*;
public class ServiceStateTable extends Vector
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
private static final long serialVersionUID = 7626909231678469365L;
public final static String ELEM_NAME = "serviceStateTable";
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public ServiceStateTable()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public StateVariable getStateVariable(int n)
{
return (StateVariable)get(n);
}
}

View File

@ -0,0 +1,374 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: StateVariable.java
*
* Revision;
*
* 12/06/02
* - first revision.
* 06/17/03
* - Added setSendEvents(), isSendEvents().
* - Changed to send a event after check the eventing state using isSendEvents().
* 01/04/04
* - Added UPnP status methods.
* 01/06/04
* - Added the following methods.
* getQueryListener()
* setQueryListener()
* performQueryListener()
* 01/07/04
* - Added StateVariable() and set();
* - Changed performQueryListener() to use a copy of the StateVariable.
* 03/27/04
* - Thanks for Adavy
* - Added getAllowedValueList() and getAllowedValueRange().
* 05/11/04
* - Added hasAllowedValueList() and hasAllowedValueRange().
* 07/09/04
* - Thanks for Dimas <cyberrate@users.sourceforge.net> and Stefano Lenzi <kismet-sl@users.sourceforge.net>
* - Changed postQuerylAction() to set the status code to the UPnPStatus.
* 11/09/04
* - Theo Beisch <theo.beisch@gmx.de>
* - Changed StateVariable::setValue() to update and send the event when the value is not equal to the current value.
* 11/19/04
* - Rick Keiner <rick@emanciple.com>
* - Fixed setValue() to compare only when the current value is not null.
* 02/28/05
* - Changed getAllowedValueList() to use AllowedValue instead of String as the member.
*
******************************************************************/
package org.cybergarage.upnp;
import org.cybergarage.xml.*;
import org.cybergarage.util.*;
import org.cybergarage.upnp.control.*;
import org.cybergarage.upnp.xml.*;
public class StateVariable extends NodeData
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public final static String ELEM_NAME = "stateVariable";
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private Node stateVariableNode;
private Node serviceNode;
public Node getServiceNode()
{
return serviceNode;
}
public Service getService()
{
Node serviceNode = getServiceNode();
if (serviceNode == null)
return null;
return new Service(serviceNode);
}
public Node getStateVariableNode()
{
return stateVariableNode;
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public StateVariable()
{
this.serviceNode = null;
this.stateVariableNode = new Node();
}
public StateVariable(Node serviceNode, Node stateVarNode)
{
this.serviceNode = serviceNode;
this.stateVariableNode = stateVarNode;
}
////////////////////////////////////////////////
// isStateVariableNode
////////////////////////////////////////////////
public static boolean isStateVariableNode(Node node)
{
return StateVariable.ELEM_NAME.equals(node.getName());
}
////////////////////////////////////////////////
// name
////////////////////////////////////////////////
private final static String NAME = "name";
public void setName(String value)
{
getStateVariableNode().setNode(NAME, value);
}
public String getName()
{
return getStateVariableNode().getNodeValue(NAME);
}
////////////////////////////////////////////////
// dataType
////////////////////////////////////////////////
private final static String DATATYPE = "dataType";
public void setDataType(String value)
{
getStateVariableNode().setNode(DATATYPE, value);
}
public String getDataType()
{
return getStateVariableNode().getNodeValue(DATATYPE);
}
////////////////////////////////////////////////
// dataType
////////////////////////////////////////////////
private final static String SENDEVENTS = "sendEvents";
private final static String SENDEVENTS_YES = "yes";
private final static String SENDEVENTS_NO = "no";
public void setSendEvents(boolean state)
{
getStateVariableNode().setAttribute(SENDEVENTS, (state == true) ? SENDEVENTS_YES : SENDEVENTS_NO);
}
public boolean isSendEvents()
{
String state = getStateVariableNode().getAttributeValue(SENDEVENTS);
if (state == null)
return false;
if (state.equalsIgnoreCase(SENDEVENTS_YES) == true)
return true;
return false;
}
////////////////////////////////////////////////
// set
////////////////////////////////////////////////
public void set(StateVariable stateVar)
{
setName(stateVar.getName());
setValue(stateVar.getValue());
setDataType(stateVar.getDataType());
setSendEvents(stateVar.isSendEvents());
}
////////////////////////////////////////////////
// UserData
////////////////////////////////////////////////
public StateVariableData getStateVariableData ()
{
Node node = getStateVariableNode();
StateVariableData userData = (StateVariableData)node.getUserData();
if (userData == null) {
userData = new StateVariableData();
node.setUserData(userData);
userData.setNode(node);
}
return userData;
}
////////////////////////////////////////////////
// Value
////////////////////////////////////////////////
public void setValue(String value)
{
// Thnaks for Tho Beisch (11/09/04)
String currValue = getStateVariableData().getValue();
// Thnaks for Tho Rick Keiner (11/18/04)
if (currValue != null && currValue.equals(value) == true)
return;
getStateVariableData().setValue(value);
// notify event
Service service = getService();
if (service == null)
return;
if (isSendEvents() == false)
return;
service.notify(this);
}
public void setValue(int value)
{
setValue(Integer.toString(value));
}
public void setValue(long value)
{
setValue(Long.toString(value));
}
public String getValue()
{
return getStateVariableData().getValue();
}
////////////////////////////////////////////////
// AllowedValueList
////////////////////////////////////////////////
public AllowedValueList getAllowedValueList()
{
AllowedValueList valueList= new AllowedValueList();
Node valueListNode = getStateVariableNode().getNode(AllowedValueList.ELEM_NAME);
if (valueListNode == null)
return valueList;
int nNode = valueListNode.getNNodes();
for (int n=0; n<nNode; n++) {
Node node = valueListNode.getNode(n);
if (AllowedValue.isAllowedValueNode(node) == false)
continue;
AllowedValue allowedVal = new AllowedValue(node);
valueList.add(allowedVal);
}
return valueList;
}
public boolean hasAllowedValueList()
{
AllowedValueList valueList = getAllowedValueList();
return (0 < valueList.size()) ? true : false;
}
////////////////////////////////////////////////
// AllowedValueRange
////////////////////////////////////////////////
public AllowedValueRange getAllowedValueRange()
{
Node valueRangeNode = getStateVariableNode().getNode(AllowedValueRange.ELEM_NAME);
if (valueRangeNode == null)
return null;
return new AllowedValueRange(valueRangeNode);
}
public boolean hasAllowedValueRange()
{
return (getAllowedValueRange() != null) ? true : false;
}
////////////////////////////////////////////////
// queryAction
////////////////////////////////////////////////
public QueryListener getQueryListener()
{
return getStateVariableData().getQueryListener();
}
public void setQueryListener(QueryListener listener)
{
getStateVariableData().setQueryListener(listener);
}
public boolean performQueryListener(QueryRequest queryReq)
{
QueryListener listener = getQueryListener();
if (listener == null)
return false;
QueryResponse queryRes = new QueryResponse();
StateVariable retVar = new StateVariable();
retVar.set(this);
retVar.setValue("");
retVar.setStatus(UPnPStatus.INVALID_VAR);
if (listener.queryControlReceived(retVar) == true) {
queryRes.setResponse(retVar);
}
else {
UPnPStatus upnpStatus = retVar.getStatus();
queryRes.setFaultResponse(upnpStatus.getCode(), upnpStatus.getDescription());
}
queryReq.post(queryRes);
return true;
}
////////////////////////////////////////////////
// ActionControl
////////////////////////////////////////////////
public QueryResponse getQueryResponse()
{
return getStateVariableData().getQueryResponse();
}
private void setQueryResponse(QueryResponse res)
{
getStateVariableData().setQueryResponse(res);
}
public UPnPStatus getQueryStatus()
{
return getQueryResponse().getUPnPError();
}
////////////////////////////////////////////////
// ActionControl
////////////////////////////////////////////////
public boolean postQuerylAction()
{
QueryRequest queryReq = new QueryRequest();
queryReq.setRequest(this);
if (Debug.isOn() == true)
queryReq.print();
QueryResponse queryRes = queryReq.post();
if (Debug.isOn() == true)
queryRes.print();
setQueryResponse(queryRes);
// Thanks for Dimas <cyberrate@users.sourceforge.net> and Stefano Lenzi <kismet-sl@users.sourceforge.net> (07/09/04)
if (queryRes.isSuccessful() == false) {
setValue(queryRes.getReturnValue());
return false;
}
setValue(queryRes.getReturnValue());
return true;
}
////////////////////////////////////////////////
// UPnPStatus
////////////////////////////////////////////////
private UPnPStatus upnpStatus = new UPnPStatus();
public void setStatus(int code, String descr)
{
upnpStatus.setCode(code);
upnpStatus.setDescription(descr);
}
public void setStatus(int code)
{
setStatus(code, UPnPStatus.code2String(code));
}
public UPnPStatus getStatus()
{
return upnpStatus;
}
}

View File

@ -0,0 +1,232 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: UPnP.java
*
* Revision;
*
* 11/18/02
* - first revision.
* 05/13/03
* - Added support for IPv6 and loopback address.
* 12/26/03
* - Added support for XML Parser
* 06/18/03
* - Added INMPR03 and INMPR03_VERSION.
*
******************************************************************/
package org.cybergarage.upnp;
import org.cybergarage.upnp.ssdp.*;
//import org.cybergarage.util.*;
import org.cybergarage.xml.*;
import org.cybergarage.xml.parser.*;
import org.cybergarage.soap.*;
import org.cybergarage.net.*;
public class UPnP
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public final static String NAME = "CyberLink";
public final static String VERSION = "1.7";
public final static int SERVER_RETRY_COUNT = 100;
public final static int DEFAULT_EXPIRED_DEVICE_EXTRA_TIME = 60;
public final static String getServerName()
{
String osName = System.getProperty("os.name");
String osVer = System.getProperty("os.version");
return osName + "/" + osVer + " UPnP/1.0 " + NAME + "/" + VERSION;
}
public final static String INMPR03 = "INMPR03";
public final static String INMPR03_VERSION = "1.0";
public final static int INMPR03_DISCOVERY_OVER_WIRELESS_COUNT = 4;
public final static String XML_DECLARATION = "<?xml version=\"1.0\"?>";
////////////////////////////////////////////////
// Enable / Disable
////////////////////////////////////////////////
public final static int USE_ONLY_IPV6_ADDR = 1;
public final static int USE_LOOPBACK_ADDR = 2;
public final static int USE_IPV6_LINK_LOCAL_SCOPE = 3;
public final static int USE_IPV6_SUBNET_SCOPE = 4;
public final static int USE_IPV6_ADMINISTRATIVE_SCOPE = 5;
public final static int USE_IPV6_SITE_LOCAL_SCOPE = 6;
public final static int USE_IPV6_GLOBAL_SCOPE = 7;
public final static int USE_SSDP_SEARCHRESPONSE_MULTIPLE_INTERFACES = 8;
public final static int USE_ONLY_IPV4_ADDR = 9;
public final static void setEnable(int value)
{
switch (value) {
case USE_ONLY_IPV6_ADDR:
{
HostInterface.USE_ONLY_IPV6_ADDR = true;
}
break;
case USE_ONLY_IPV4_ADDR:
{
HostInterface.USE_ONLY_IPV4_ADDR = true;
}
break;
case USE_LOOPBACK_ADDR:
{
HostInterface.USE_LOOPBACK_ADDR = true;
}
break;
case USE_IPV6_LINK_LOCAL_SCOPE:
{
SSDP.setIPv6Address(SSDP.IPV6_LINK_LOCAL_ADDRESS);
}
break;
case USE_IPV6_SUBNET_SCOPE:
{
SSDP.setIPv6Address(SSDP.IPV6_SUBNET_ADDRESS);
}
break;
case USE_IPV6_ADMINISTRATIVE_SCOPE:
{
SSDP.setIPv6Address(SSDP.IPV6_ADMINISTRATIVE_ADDRESS);
}
break;
case USE_IPV6_SITE_LOCAL_SCOPE:
{
SSDP.setIPv6Address(SSDP.IPV6_SITE_LOCAL_ADDRESS);
}
break;
case USE_IPV6_GLOBAL_SCOPE:
{
SSDP.setIPv6Address(SSDP.IPV6_GLOBAL_ADDRESS);
}
break;
}
}
public final static void setDisable(int value)
{
switch (value) {
case USE_ONLY_IPV6_ADDR:
{
HostInterface.USE_ONLY_IPV6_ADDR = false;
}
break;
case USE_ONLY_IPV4_ADDR:
{
HostInterface.USE_ONLY_IPV4_ADDR = false;
}
break;
case USE_LOOPBACK_ADDR:
{
HostInterface.USE_LOOPBACK_ADDR = false;
}
break;
}
}
public final static boolean isEnabled(int value)
{
switch (value) {
case USE_ONLY_IPV6_ADDR:
{
return HostInterface.USE_ONLY_IPV6_ADDR;
}
case USE_ONLY_IPV4_ADDR:
{
return HostInterface.USE_ONLY_IPV4_ADDR;
}
case USE_LOOPBACK_ADDR:
{
return HostInterface.USE_LOOPBACK_ADDR;
}
}
return false;
}
////////////////////////////////////////////////
// UUID
////////////////////////////////////////////////
private static final String toUUID(int seed)
{
String id = Integer.toString((int)(seed & 0xFFFF), 16);
int idLen = id.length();
String uuid = "";
for (int n=0; n<(4-idLen); n++)
uuid += "0";
uuid += id;
return uuid;
}
public static final String createUUID()
{
long time1 = System.currentTimeMillis();
long time2 = (long)((double)System.currentTimeMillis() * Math.random());
return
toUUID((int)(time1 & 0xFFFF)) + "-" +
toUUID((int)((time1 >> 32) | 0xA000) & 0xFFFF) + "-" +
toUUID((int)(time2 & 0xFFFF)) + "-" +
toUUID((int)((time2 >> 32) | 0xE000) & 0xFFFF);
}
////////////////////////////////////////////////
// XML Parser
////////////////////////////////////////////////
private static Parser xmlParser;
public final static void setXMLParser(Parser parser)
{
xmlParser = parser;
SOAP.setXMLParser(parser);
}
public final static Parser getXMLParser()
{
return xmlParser;
}
////////////////////////////////////////////////
// Initialize
////////////////////////////////////////////////
static
{
////////////////////////////
// Interface Option
////////////////////////////
setXMLParser(new JaxpParser());
//setXMLParser(new kXML2Parser());
////////////////////////////
// Interface Option
////////////////////////////
/*
if (HostInterface.hasIPv6Addresses() == true)
setEnable(USE_ONLY_IPV6_ADDR);
*/
////////////////////////////
// Debug Option
////////////////////////////
//Debug.on();
}
public final static void initialize()
{
// Dummy function to call UPnP.static
}
}

View File

@ -0,0 +1,81 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: UPnPStatus.java
*
* Revision;
*
* 11/18/02
* - first revision.
* 01/03/04
* - Changed the class name from UPnPError to UPnPStatus.
*
******************************************************************/
package org.cybergarage.upnp;
public class UPnPStatus
{
////////////////////////////////////////////////
// Code
////////////////////////////////////////////////
public static final int INVALID_ACTION = 401;
public static final int INVALID_ARGS = 402;
public static final int OUT_OF_SYNC = 403;
public static final int INVALID_VAR = 404;
public static final int PRECONDITION_FAILED = 412;
public static final int ACTION_FAILED = 501;
public static final String code2String(int code)
{
switch (code) {
case INVALID_ACTION: return "Invalid Action";
case INVALID_ARGS: return "Invalid Args";
case OUT_OF_SYNC: return "Out of Sync";
case INVALID_VAR: return "Invalid Var";
case PRECONDITION_FAILED: return "Precondition Failed";
case ACTION_FAILED: return "Action Failed";
}
return "";
}
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private int code;
private String description;
public UPnPStatus()
{
setCode(0);
setDescription("");
}
public UPnPStatus(int code, String desc)
{
setCode(code);
setDescription(desc);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -0,0 +1,23 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: ActionListener.java
*
* Revision;
*
* 01/16/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.control;
import org.cybergarage.upnp.*;
public interface ActionListener
{
public boolean actionControlReceived(Action action);
}

View File

@ -0,0 +1,145 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: ControlRequest.java
*
* Revision;
*
* 01/29/03
* - first revision.
* 05/09/05
* - Changed getActionName() to return when the delimiter is not found.
*
******************************************************************/
package org.cybergarage.upnp.control;
import org.cybergarage.http.*;
import org.cybergarage.xml.*;
import org.cybergarage.soap.*;
import org.cybergarage.upnp.*;
public class ActionRequest extends ControlRequest
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public ActionRequest()
{
}
public ActionRequest(HTTPRequest httpReq)
{
set(httpReq);
}
////////////////////////////////////////////////
// Action
////////////////////////////////////////////////
public Node getActionNode()
{
Node bodyNode = getBodyNode();
if (bodyNode == null)
return null;
if (bodyNode.hasNodes() == false)
return null;
return bodyNode.getNode(0);
}
public String getActionName()
{
Node node = getActionNode();
if (node == null)
return "";
String name = node.getName();
if (name == null)
return "";
int idx = name.indexOf(SOAP.DELIM)+1;
if (idx < 0)
return "";
return name.substring(idx, name.length());
}
public ArgumentList getArgumentList()
{
Node actNode = getActionNode();
int nArgNodes = actNode.getNNodes();
ArgumentList argList = new ArgumentList();
for (int n=0; n<nArgNodes; n++) {
Argument arg = new Argument();
Node argNode = actNode.getNode(n);
arg.setName(argNode.getName());
arg.setValue(argNode.getValue());
argList.add(arg);
}
return argList;
}
////////////////////////////////////////////////
// setRequest
////////////////////////////////////////////////
public void setRequest(Action action, ArgumentList argList)
{
Service service = action.getService();
setRequestHost(service);
setEnvelopeNode(SOAP.createEnvelopeBodyNode());
Node envNode = getEnvelopeNode();
Node bodyNode = getBodyNode();
Node argNode = createContentNode(service, action, argList);
bodyNode.addNode(argNode);
setContent(envNode);
String serviceType = service.getServiceType();
String actionName = action.getName();
String soapAction = "\"" +
serviceType +
"#" + actionName +
"\"";
setSOAPAction(soapAction);
}
////////////////////////////////////////////////
// Contents
////////////////////////////////////////////////
private Node createContentNode(Service service, Action action, ArgumentList argList)
{
String actionName = action.getName();
String serviceType = service.getServiceType();
Node actionNode = new Node();
actionNode.setName(Control.NS, actionName);
actionNode.setNameSpace(Control.NS, serviceType);
int argListCnt = argList.size();
for (int n=0; n<argListCnt; n++) {
Argument arg = argList.getArgument(n);
Node argNode = new Node();
argNode.setName(arg.getName());
argNode.setValue(arg.getValue());
actionNode.addNode(argNode);
}
return actionNode;
}
////////////////////////////////////////////////
// post
////////////////////////////////////////////////
public ActionResponse post()
{
SOAPResponse soapRes = postMessage(getRequestHost(), getRequestPort());
return new ActionResponse(soapRes);
}
}

View File

@ -0,0 +1,120 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: ActionResponse.java
*
* Revision;
*
* 01/29/03
* - first revision.
* 09/02/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Problem : Action Responses do not contain the mandatory header field EXT
* - Error : ActionResponse class does not set the EXT header
*
******************************************************************/
package org.cybergarage.upnp.control;
import org.cybergarage.upnp.*;
import org.cybergarage.http.*;
import org.cybergarage.soap.*;
import org.cybergarage.xml.*;
public class ActionResponse extends ControlResponse
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public ActionResponse()
{
setHeader(HTTP.EXT, "");
}
public ActionResponse(SOAPResponse soapRes)
{
super(soapRes);
setHeader(HTTP.EXT, "");
}
////////////////////////////////////////////////
// Response
////////////////////////////////////////////////
public void setResponse(Action action)
{
setStatusCode(HTTPStatus.OK);
Node bodyNode = getBodyNode();
Node resNode = createResponseNode(action);
bodyNode.addNode(resNode);
Node envNode = getEnvelopeNode();
setContent(envNode);
}
private Node createResponseNode(Action action)
{
String actionName = action.getName();
Node actionNameResNode = new Node(SOAP.METHODNS + SOAP.DELIM + actionName + SOAP.RESPONSE);
Service service = action.getService();
if (service != null) {
actionNameResNode.setAttribute(
"xmlns:" + SOAP.METHODNS,
service.getServiceType());
}
ArgumentList argList = action.getArgumentList();
int nArgs = argList.size();
for (int n=0; n<nArgs; n++) {
Argument arg = argList.getArgument(n);
if (arg.isOutDirection() == false)
continue;
Node argNode = new Node();
argNode.setName(arg.getName());
argNode.setValue(arg.getValue());
actionNameResNode.addNode(argNode);
}
return actionNameResNode;
}
////////////////////////////////////////////////
// getResponse
////////////////////////////////////////////////
private Node getActionResponseNode()
{
Node bodyNode = getBodyNode();
if (bodyNode == null || bodyNode.hasNodes() == false)
return null;
return bodyNode.getNode(0);
}
public ArgumentList getResponse()
{
ArgumentList argList = new ArgumentList();
Node resNode = getActionResponseNode();
if (resNode == null)
return argList;
int nArgs = resNode.getNNodes();
for (int n=0; n<nArgs; n++) {
Node node = resNode.getNode(n);
String name = node.getName();
String value = node.getValue();
Argument arg = new Argument(name, value);
argList.add(arg);
}
return argList;
}
}

View File

@ -0,0 +1,29 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: Control.java
*
* Revision;
*
* 01/20/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.control;
public class Control
{
public final static String NS = "u";
public final static String QUERY_SOAPACTION = "urn:schemas-upnp-org:control-1-0#QueryStateVariable";
public final static String XMLNS = "urn:schemas-upnp-org:control-1-0";
public final static String QUERY_STATE_VARIABLE = "QueryStateVariable";
public final static String QUERY_STATE_VARIABLE_RESPONSE = "QueryStateVariableResponse";
public final static String VAR_NAME = "varName";
public final static String RETURN = "return";
}

View File

@ -0,0 +1,128 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: ControlRequest.java
*
* Revision:
*
* 01/29/03
* - first revision.
* 05/22/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Description: inserted a check at the beginning of the setRequestHost method
* - Problem : If the host does not start with a '/', the device could refuse the control action
* - Error : it is not an error, but adding the '/' when missing allows the integration with the Intel devices
* 09/02/03
* - Giordano Sassaroli <sassarol@cefriel.it> / Suzan Foster
* - Problem : NullpointerException thrown for devices whose description use absolute urls
* - Error : the presence of a base url is not mandatory, the API code makes the assumption that control and event subscription urls are relative.
* If the baseUrl is not present, the request host and port should be extracted from the control/subscription url
* - Description: The method setRequestHost/setService should be changed as follows
* 02/17/04
* - Rob van den Boomen <rob.van.den.boomen@philips.com>
* - Fixed to set a URLBase from the SSDP header when the URLBase of the description is null.
* 02/18/04
* - Andre <andre@antiheld.net>
* - The xml nodes controlUrl and eventSubUrl can contain absolut urls, but these absolut urls may have
* different ports than the base url! (so seen on my SMC 7004ABR Barricade Router, where xml files are
* requested from port 80, but soap requests are made on port 5440). Therefore whenever a request is made,
* the port specified by the controlUrl or eventSubUrl node should be used, else no response will be returned
* (oddly, there was a response returned even on port 80, but with empty body tags. but the correct response
* finally came from port 5440).
* - Fixed to get the port from the control url when it is absolute.
* 03/20/04
* - Thanks for Thomas Schulz <tsroyale at users.sourceforge.net>
* - Fixed setRequestHost() for Sony's UPnP stack when the URLBase has the path.
*
******************************************************************/
package org.cybergarage.upnp.control;
import java.net.*;
import org.cybergarage.http.*;
import org.cybergarage.soap.*;
import org.cybergarage.upnp.*;
public class ControlRequest extends SOAPRequest
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public ControlRequest()
{
}
public ControlRequest(HTTPRequest httpReq)
{
set(httpReq);
}
////////////////////////////////////////////////
// Query
////////////////////////////////////////////////
public boolean isQueryControl()
{
return isSOAPAction(Control.QUERY_SOAPACTION);
}
public boolean isActionControl()
{
return !isQueryControl();
}
////////////////////////////////////////////////
// setRequest
////////////////////////////////////////////////
protected void setRequestHost(Service service)
{
String ctrlURL = service.getControlURL();
// Thanks for Thomas Schulz (2004/03/20)
String urlBase = service.getRootDevice().getURLBase();
if (urlBase != null && 0 < urlBase.length()){
try {
URL url = new URL(urlBase);
String basePath = url.getPath();
int baseLen = basePath.length();
if (0 < baseLen) {
if (1 < baseLen || (basePath.charAt(0) != '/'))
ctrlURL = basePath + ctrlURL;
}
}
catch (MalformedURLException e) {}
}
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (05/21/03)
setURI(ctrlURL, true);
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> and Suzan Foster (09/02/03)
// Thanks for Andre <andre@antiheld.net> (02/18/04)
String postURL = "";
if (HTTP.isAbsoluteURL(ctrlURL) == true)
postURL = ctrlURL;
if (postURL == null || postURL.length() <= 0)
postURL = service.getRootDevice().getURLBase();
// Thanks for Rob van den Boomen <rob.van.den.boomen@philips.com> (02/17/04)
// BUGFIX, set urlbase from location string if not set in description.xml
if (postURL == null || postURL.length() <= 0)
postURL = service.getRootDevice().getLocation();
String reqHost = HTTP.getHost(postURL);
int reqPort = HTTP.getPort(postURL);
setHost(reqHost, reqPort);
setRequestHost(reqHost);
setRequestPort(reqPort);
}
}

View File

@ -0,0 +1,168 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: ControlResponse.java
*
* Revision;
*
* 01/29/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.control;
import org.cybergarage.http.*;
import org.cybergarage.xml.*;
import org.cybergarage.soap.*;
import org.cybergarage.upnp.*;
public class ControlResponse extends SOAPResponse
{
public static final String FAULT_CODE = "Client";
public static final String FAULT_STRING = "UPnPError";
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public ControlResponse()
{
setServer(UPnP.getServerName());
}
public ControlResponse(SOAPResponse soapRes)
{
super(soapRes);
}
////////////////////////////////////////////////
// FaultResponse
////////////////////////////////////////////////
public void setFaultResponse(int errCode, String errDescr)
{
setStatusCode(HTTPStatus.INTERNAL_SERVER_ERROR);
Node bodyNode = getBodyNode();
Node faultNode = createFaultResponseNode(errCode, errDescr);
bodyNode.addNode(faultNode);
Node envNode = getEnvelopeNode();
setContent(envNode);
}
public void setFaultResponse(int errCode)
{
setFaultResponse(errCode, UPnPStatus.code2String(errCode));
}
////////////////////////////////////////////////
// createFaultResponseNode
////////////////////////////////////////////////
private Node createFaultResponseNode(int errCode, String errDescr)
{
// <s:Fault>
Node faultNode = new Node(SOAP.XMLNS + SOAP.DELIM + SOAP.FAULT);
// <faultcode>s:Client</faultcode>
Node faultCodeNode = new Node(SOAP.FAULT_CODE);
faultCodeNode.setValue(SOAP.XMLNS + SOAP.DELIM + FAULT_CODE);
faultNode.addNode(faultCodeNode);
// <faultstring>UPnPError</faultstring>
Node faultStringNode = new Node(SOAP.FAULT_STRING);
faultStringNode.setValue(FAULT_STRING);
faultNode.addNode(faultStringNode);
// <detail>
Node detailNode = new Node(SOAP.DETAIL);
faultNode.addNode(detailNode);
// <UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
Node upnpErrorNode = new Node(FAULT_STRING);
upnpErrorNode.setAttribute("xmlns", Control.XMLNS);
detailNode.addNode(upnpErrorNode);
// <errorCode>error code</errorCode>
Node errorCodeNode = new Node(SOAP.ERROR_CODE);
errorCodeNode.setValue(errCode);
upnpErrorNode.addNode(errorCodeNode);
// <errorDescription>error string</errorDescription>
Node errorDesctiprionNode = new Node(SOAP.ERROR_DESCRIPTION);
errorDesctiprionNode.setValue(errDescr);
upnpErrorNode.addNode(errorDesctiprionNode);
return faultNode;
}
////////////////////////////////////////////////
// UPnP Error
////////////////////////////////////////////////
private UPnPStatus upnpErr = new UPnPStatus();
private Node getUPnPErrorNode()
{
Node detailNode = getFaultDetailNode();
if (detailNode == null)
return null;
return detailNode.getNodeEndsWith(SOAP.UPNP_ERROR);
}
private Node getUPnPErrorCodeNode()
{
Node errorNode = getUPnPErrorNode();
if (errorNode == null)
return null;
return errorNode.getNodeEndsWith(SOAP.ERROR_CODE);
}
private Node getUPnPErrorDescriptionNode()
{
Node errorNode = getUPnPErrorNode();
if (errorNode == null)
return null;
return errorNode.getNodeEndsWith(SOAP.ERROR_DESCRIPTION);
}
public int getUPnPErrorCode()
{
Node errorCodeNode = getUPnPErrorCodeNode();
if (errorCodeNode == null)
return -1;
String errorCodeStr = errorCodeNode.getValue();
try {
return Integer.parseInt(errorCodeStr);
}
catch (Exception e) {
return -1;
}
}
public String getUPnPErrorDescription()
{
Node errorDescNode = getUPnPErrorDescriptionNode();
if (errorDescNode == null)
return "";
return errorDescNode.getValue();
}
public UPnPStatus getUPnPError()
{
int code = 0;
String desc = "";
code = getUPnPErrorCode();
desc = getUPnPErrorDescription();
upnpErr.setCode(code);
upnpErr.setDescription(desc);
return upnpErr;
}
}

View File

@ -0,0 +1,25 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: QueryListener.java
*
* Revision;
*
* 01/30/03
* - first revision.
* 01/04/04
* - Changed the interface.
*
******************************************************************/
package org.cybergarage.upnp.control;
import org.cybergarage.upnp.*;
public interface QueryListener
{
public boolean queryControlReceived(StateVariable stateVar);
}

View File

@ -0,0 +1,117 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: QueryRequest.java
*
* Revision;
*
* 01/29/03
* - first revision.
* 09/02/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Error : redundant code, the setRequest method in QueryRequest invokes setURI even if after a couple of rows setRequestHost is invoked
*
******************************************************************/
package org.cybergarage.upnp.control;
import org.cybergarage.http.*;
import org.cybergarage.xml.*;
import org.cybergarage.soap.*;
import org.cybergarage.upnp.*;
public class QueryRequest extends ControlRequest
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public QueryRequest()
{
}
public QueryRequest(HTTPRequest httpReq)
{
set(httpReq);
}
////////////////////////////////////////////////
// Qyery
////////////////////////////////////////////////
private Node getVarNameNode()
{
Node bodyNode = getBodyNode();
if (bodyNode == null)
return null;
if (bodyNode.hasNodes() == false)
return null;
Node queryStateVarNode = bodyNode.getNode(0);
if (queryStateVarNode == null)
return null;
if (queryStateVarNode.hasNodes() == false)
return null;
return queryStateVarNode.getNode(0);
}
public String getVarName()
{
Node node = getVarNameNode();
if (node == null)
return "";
return node.getValue();
}
////////////////////////////////////////////////
// setRequest
////////////////////////////////////////////////
public void setRequest(StateVariable stateVar)
{
Service service = stateVar.getService();
setRequestHost(service);
setEnvelopeNode(SOAP.createEnvelopeBodyNode());
Node envNode = getEnvelopeNode();
Node bodyNode = getBodyNode();
Node qeuryNode = createContentNode(stateVar);
bodyNode.addNode(qeuryNode);
setContent(envNode);
setSOAPAction(Control.QUERY_SOAPACTION);
}
////////////////////////////////////////////////
// Contents
////////////////////////////////////////////////
private Node createContentNode(StateVariable stateVar)
{
Node queryVarNode = new Node();
queryVarNode.setName(Control.NS, Control.QUERY_STATE_VARIABLE);
queryVarNode.setNameSpace(Control.NS, Control.XMLNS);
Node varNode = new Node();
varNode.setName(Control.NS, Control.VAR_NAME);
varNode.setValue(stateVar.getName());
queryVarNode.addNode(varNode);
return queryVarNode;
}
////////////////////////////////////////////////
// post
////////////////////////////////////////////////
public QueryResponse post()
{
SOAPResponse soapRes = postMessage(getRequestHost(), getRequestPort());
return new QueryResponse(soapRes);
}
}

View File

@ -0,0 +1,97 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: QueryResponse.java
*
* Revision;
*
* 01/30/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.control;
import org.cybergarage.upnp.*;
import org.cybergarage.http.*;
import org.cybergarage.soap.*;
import org.cybergarage.xml.*;
public class QueryResponse extends ControlResponse
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public QueryResponse()
{
}
public QueryResponse(SOAPResponse soapRes)
{
super(soapRes);
}
////////////////////////////////////////////////
// Qyery
////////////////////////////////////////////////
private Node getReturnNode()
{
Node bodyNode = getBodyNode();
if (bodyNode == null)
return null;
if (bodyNode.hasNodes() == false)
return null;
Node queryResNode = bodyNode.getNode(0);
if (queryResNode == null)
return null;
if (queryResNode.hasNodes() == false)
return null;
return queryResNode.getNode(0);
}
public String getReturnValue()
{
Node node = getReturnNode();
if (node == null)
return "";
return node.getValue();
}
////////////////////////////////////////////////
// Response
////////////////////////////////////////////////
public void setResponse(StateVariable stateVar)
{
String var = stateVar.getValue();
setStatusCode(HTTPStatus.OK);
Node bodyNode = getBodyNode();
Node resNode = createResponseNode(var);
bodyNode.addNode(resNode);
Node envNodee = getEnvelopeNode();
setContent(envNodee);
}
private Node createResponseNode(String var)
{
Node queryResNode = new Node();
queryResNode.setName(Control.NS, Control.QUERY_STATE_VARIABLE_RESPONSE);
queryResNode.setNameSpace(Control.NS, Control.XMLNS);
Node returnNode = new Node();
returnNode.setName(Control.RETURN);
returnNode.setValue(var);
queryResNode.addNode(returnNode);
return queryResNode;
}
}

View File

@ -0,0 +1,65 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: RenewSubscriber.java
*
* Revision:
*
* 07/07/04
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.control;
import org.cybergarage.util.*;
import org.cybergarage.upnp.*;
public class RenewSubscriber extends ThreadCore
{
public final static long INTERVAL = 120;
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public RenewSubscriber(ControlPoint ctrlp)
{
setControlPoint(ctrlp);
}
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private ControlPoint ctrlPoint;
public void setControlPoint(ControlPoint ctrlp)
{
ctrlPoint = ctrlp;
}
public ControlPoint getControlPoint()
{
return ctrlPoint;
}
////////////////////////////////////////////////
// Thread
////////////////////////////////////////////////
public void run()
{
ControlPoint ctrlp = getControlPoint();
long renewInterval = INTERVAL * 1000;
while (isRunnable() == true) {
try {
Thread.sleep(renewInterval);
} catch (InterruptedException e) {}
ctrlp.renewSubscriberService();
}
}
}

View File

@ -0,0 +1,68 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: Advertiser.java
*
* Revision;
*
* 12/24/03
* - first revision.
* 06/18/04
* - Changed to advertise every 25%-50% of the periodic notification cycle for NMPR;
*
******************************************************************/
package org.cybergarage.upnp.device;
import org.cybergarage.util.*;
import org.cybergarage.upnp.*;
public class Advertiser extends ThreadCore
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public Advertiser(Device dev)
{
setDevice(dev);
}
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private Device device;
public void setDevice(Device dev)
{
device = dev;
}
public Device getDevice()
{
return device;
}
////////////////////////////////////////////////
// Thread
////////////////////////////////////////////////
public void run()
{
Device dev = getDevice();
long leaseTime = dev.getLeaseTime();
long notifyInterval;
while (isRunnable() == true) {
notifyInterval = (leaseTime/4) + (long)((float)leaseTime * (Math.random() * 0.25f));
notifyInterval *= 1000;
try {
Thread.sleep(notifyInterval);
} catch (InterruptedException e) {}
dev.announce();
}
}
}

View File

@ -0,0 +1,24 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: MAN.java
*
* Revision;
*
* 12/30/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.device;
public class Description
{
public final static String LOADING_EXCEPTION = "Couldn't load a specified description file ";
public final static String NOROOT_EXCEPTION = "Couldn't find a root node";
public final static String NOROOTDEVICE_EXCEPTION = "Couldn't find a root device node";
}

View File

@ -0,0 +1,27 @@
/******************************************************************
*
* CyberLink for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: DeviceChangeListener.java
*
* Revision;
*
* 09/12/04
* - Oliver Newell <newell@media-rush.com>
* - Added this class to allow ControlPoint applications to
* be notified when the ControlPoint base class adds/removes
* a UPnP device
*
******************************************************************/
package org.cybergarage.upnp.device;
import org.cybergarage.upnp.Device;
public interface DeviceChangeListener
{
public void deviceAdded( Device dev );
public void deviceRemoved( Device dev );
}

View File

@ -0,0 +1,65 @@
/******************************************************************
*
* CyberLink for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: Disposer.java
*
* Revision:
*
* 01/05/04
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.device;
import org.cybergarage.upnp.*;
import org.cybergarage.util.*;
public class Disposer extends ThreadCore
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public Disposer(ControlPoint ctrlp)
{
setControlPoint(ctrlp);
}
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private ControlPoint ctrlPoint;
public void setControlPoint(ControlPoint ctrlp)
{
ctrlPoint = ctrlp;
}
public ControlPoint getControlPoint()
{
return ctrlPoint;
}
////////////////////////////////////////////////
// Thread
////////////////////////////////////////////////
public void run()
{
ControlPoint ctrlp = getControlPoint();
long monitorInterval = ctrlp.getExpiredDeviceMonitoringInterval() * 1000;
while (isRunnable() == true) {
try {
Thread.sleep(monitorInterval);
} catch (InterruptedException e) {}
ctrlp.removeExpiredDevices();
//ctrlp.print();
}
}
}

View File

@ -0,0 +1,43 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: InvalidDescriptionException.java
*
* Revision;
*
* 12/26/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.device;
import java.io.*;
public class InvalidDescriptionException extends Exception
{
private static final long serialVersionUID = -3144583349586910509L;
public InvalidDescriptionException()
{
super();
}
public InvalidDescriptionException(String s)
{
super(s);
}
public InvalidDescriptionException(String s, File file)
{
super(s + " (" + file.toString() + ")");
}
public InvalidDescriptionException(Exception e)
{
super(e.getMessage());
}
}

View File

@ -0,0 +1,31 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: MAN.java
*
* Revision;
*
* 12/30/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.device;
public class MAN
{
public final static String DISCOVER = "ssdp:discover";
public final static boolean isDiscover(String value)
{
if (value == null)
return false;
if (value.equals(MAN.DISCOVER) == true)
return true;
return value.equals("\"" + MAN.DISCOVER + "\"");
}
}

View File

@ -0,0 +1,30 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: NT.java
*
* Revision;
*
* 12/09/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.device;
public class NT
{
public final static String ROOTDEVICE = "upnp:rootdevice";
public final static String EVENT = "upnp:event";
public final static boolean isRootDevice(String ntValue)
{
if (ntValue == null)
return false;
return ntValue.startsWith(ROOTDEVICE);
}
}

View File

@ -0,0 +1,38 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: NTS.java
*
* Revision;
*
* 12/09/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.device;
public class NTS
{
public final static String ALIVE = "ssdp:alive";
public final static String BYEBYE = "ssdp:byebye";
public final static String PROPCHANGE = "upnp:propchange";
public final static boolean isAlive(String ntsValue)
{
if (ntsValue == null)
return false;
return ntsValue.startsWith(NTS.ALIVE);
}
public final static boolean isByeBye(String ntsValue)
{
if (ntsValue == null)
return false;
return ntsValue.startsWith(NTS.BYEBYE);
}
}

View File

@ -0,0 +1,23 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: DeviceNotifyListener.java
*
* Revision;
*
* 11/18/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.device;
import org.cybergarage.upnp.ssdp.*;
public interface NotifyListener
{
public void deviceNotifyReceived(SSDPPacket ssdpPacket);
}

View File

@ -0,0 +1,71 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: ST.java
*
* Revision;
*
* 01/07/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.device;
public class ST
{
public final static String ALL_DEVICE = "ssdp:all";
public final static String ROOT_DEVICE = "upnp:rootdevice";
public final static String UUID_DEVICE = "uuid";
public final static String URN_DEVICE = "urn:schemas-upnp-org:device:";
public final static String URN_SERVICE = "urn:schemas-upnp-org:service:";
public final static boolean isAllDevice(String value)
{
if (value == null)
return false;
if (value.equals(ALL_DEVICE) == true)
return true;
return value.equals("\"" + ALL_DEVICE + "\"");
}
public final static boolean isRootDevice(String value)
{
if (value == null)
return false;
if (value.equals(ROOT_DEVICE) == true)
return true;
return value.equals("\"" + ROOT_DEVICE + "\"");
}
public final static boolean isUUIDDevice(String value)
{
if (value == null)
return false;
if (value.startsWith(UUID_DEVICE) == true)
return true;
return value.startsWith("\"" + UUID_DEVICE);
}
public final static boolean isURNDevice(String value)
{
if (value == null)
return false;
if (value.startsWith(URN_DEVICE) == true)
return true;
return value.startsWith("\"" + URN_DEVICE);
}
public final static boolean isURNService(String value)
{
if (value == null)
return false;
if (value.startsWith(URN_SERVICE) == true)
return true;
return value.startsWith("\"" + URN_SERVICE);
}
}

View File

@ -0,0 +1,23 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SearchListener.java
*
* Revision;
*
* 11/18/02b
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.device;
import org.cybergarage.upnp.ssdp.*;
public interface SearchListener
{
public void deviceSearchReceived(SSDPPacket ssdpPacket);
}

View File

@ -0,0 +1,23 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SearchResponseListener.java
*
* Revision;
*
* 11/18/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.device;
import org.cybergarage.upnp.ssdp.*;
public interface SearchResponseListener
{
public void deviceSearchResponseReceived(SSDPPacket ssdpPacket);
}

View File

@ -0,0 +1,40 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: USN.java
*
* Revision;
*
* 12/09/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.device;
public class USN
{
public final static String ROOTDEVICE = "upnp:rootdevice";
public final static boolean isRootDevice(String usnValue)
{
if (usnValue == null)
return false;
return usnValue.endsWith(ROOTDEVICE);
}
public final static String getUDN(String usnValue)
{
if (usnValue == null)
return "";
int idx = usnValue.indexOf("::");
if (idx < 0)
return usnValue.trim();
String udnValue = new String(usnValue.getBytes(), 0, idx);
return udnValue.trim();
}
}

View File

@ -0,0 +1,21 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: EventListener.java
*
* Revision;
*
* 11/18/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.event;
public interface EventListener
{
public void eventNotifyReceived(String uuid, long seq, String varName, String value);
}

View File

@ -0,0 +1,187 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SOAPRequest.java
*
* Revision;
*
* 12/11/02
* - first revision.
* 05/22/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Description: removed the xml namespace
* - Problem : Notification messages refer to uncorrect variable names
* - Error : The NotifyRequest class introduces the XML namespace in variable names, too
* 05/22/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Problem : Notification messages refer to uncorrect variable names
* - Error : The NotifyRequest class introduces the XML namespace in variable names, too
* - Description : removed the xml namespace
* 09/03/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Problem : Notification messages refer to uncorrect variable names
* - Error : The NotifyRequest class introduces the XML namespace in variable names, too
* - Description: removed the xml namespace
* 09/08/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Problem : when an event notification message is received and the message
* contains updates on more than one variable, only the first variable update
* is notified.
* - Error : the other xml nodes of the message are ignored
* - Fix : add two methods to the NotifyRequest for extracting the property array
* and modify the httpRequestRecieved method in ControlPoint
*
******************************************************************/
package org.cybergarage.upnp.event;
import org.cybergarage.http.*;
import org.cybergarage.xml.*;
import org.cybergarage.soap.*;
import org.cybergarage.upnp.device.*;
public class NotifyRequest extends SOAPRequest
{
private final static String XMLNS = "e";
private final static String PROPERTY = "property";
private final static String PROPERTYSET = "propertyset";
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public NotifyRequest()
{
}
public NotifyRequest(HTTPRequest httpReq)
{
set(httpReq);
}
////////////////////////////////////////////////
// NT
////////////////////////////////////////////////
public void setNT(String value)
{
setHeader(HTTP.NT, value);
}
////////////////////////////////////////////////
// NTS
////////////////////////////////////////////////
public void setNTS(String value)
{
setHeader(HTTP.NTS, value);
}
////////////////////////////////////////////////
// SID
////////////////////////////////////////////////
public void setSID(String id)
{
setHeader(HTTP.SID, Subscription.toSIDHeaderString(id));
}
public String getSID()
{
return Subscription.getSID(getHeaderValue(HTTP.SID));
}
////////////////////////////////////////////////
// SEQ
////////////////////////////////////////////////
public void setSEQ(long value)
{
setHeader(HTTP.SEQ, Long.toString(value));
}
public long getSEQ()
{
return getLongHeaderValue(HTTP.SEQ);
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public boolean setRequest(Subscriber sub, String varName, String value)
{
String sid = sub.getSID();
long notifyCnt = sub.getNotifyCount();
String host = sub.getDeliveryHost();
String path = sub.getDeliveryPath();
int port = sub.getDeliveryPort();
setMethod(HTTP.NOTIFY);
setURI(path);
setHost(host, port);
setNT(NT.EVENT);
setNTS(NTS.PROPCHANGE);
setSID(sid);
setSEQ(notifyCnt);
setContentType(XML.CONTENT_TYPE);
Node propSetNode = createPropertySetNode(varName, value);
setContent(propSetNode);
return true;
}
private Node createPropertySetNode(String varName, String value)
{
Node propSetNode = new Node(/*XMLNS + SOAP.DELIM + */PROPERTYSET);
propSetNode.setNameSpace(XMLNS, Subscription.XMLNS);
Node propNode = new Node(/*XMLNS + SOAP.DELIM + */PROPERTY);
propSetNode.addNode(propNode);
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (05/22/03)
//Node varNameNode = new Node(XMLNS + SOAP.DELIM + varName);
Node varNameNode = new Node(varName);
varNameNode.setValue(value);
propNode.addNode(varNameNode);
return propSetNode;
}
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (09/08/03)
private Property getProperty(Node varNode)
{
Property prop = new Property();
if (varNode == null)
return prop;
// remove the event namespace
String variableName = varNode.getName();
int index = variableName.lastIndexOf(':');
if (index != -1)
variableName = variableName.substring(index + 1);
prop.setName(variableName);
prop.setValue(varNode.getValue());
return prop;
}
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (09/08/03)
public PropertyList getPropertyList() {
PropertyList properties = new PropertyList();
Node varSetNode = getEnvelopeNode();
for (int i = 0; i<varSetNode.getNNodes(); i++){
Node propNode = varSetNode.getNode(i);
if (propNode == null)
continue;
Property prop = getProperty(propNode.getNode(0));
properties.add(prop);
}
return properties;
}
}

View File

@ -0,0 +1,65 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: Subscriber.java
*
* Revision;
*
* 01/29/03
* - first revision.
* 05/22/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Problem : the setName method does not set the name of the property
* - Error : the method contains a bug:
* 06/18/03
* - Fixed a bug when a null value is received to the name and the value of property.
*
******************************************************************/
package org.cybergarage.upnp.event;
public class Property
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public Property()
{
}
////////////////////////////////////////////////
// name
////////////////////////////////////////////////
private String name = "";
public String getName() {
return name;
}
public void setName(String val) {
if (val == null)
val = "";
name = val;
}
////////////////////////////////////////////////
// value
////////////////////////////////////////////////
private String value = "";
public String getValue() {
return value;
}
public void setValue(String val) {
if (val == null)
val = "";
value = val;
}
}

View File

@ -0,0 +1,46 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: PropertyList.java
*
* Revision;
*
* 09/08/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.event;
import java.util.*;
public class PropertyList extends Vector
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
private static final long serialVersionUID = 8718064210738306226L;
public final static String ELEM_NAME = "PropertyList";
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public PropertyList()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public Property getProperty(int n)
{
return (Property)get(n);
}
}

View File

@ -0,0 +1,180 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: Subscriber.java
*
* Revision;
*
* 01/29/03
* - first revision.
* 07/31/04
* - Added isExpired().
* 10/26/04
* - Oliver Newell <newell@media-rush.com>
* - Added support the intinite time and fixed a bug in isExpired().
*
******************************************************************/
package org.cybergarage.upnp.event;
import java.net.*;
public class Subscriber
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public Subscriber()
{
renew();
}
////////////////////////////////////////////////
// SID
////////////////////////////////////////////////
private String SID = null;
public String getSID() {
return SID;
}
public void setSID(String sid) {
SID = sid;
}
////////////////////////////////////////////////
// deliveryURL
////////////////////////////////////////////////
private String ifAddr = "";
public void setInterfaceAddress(String addr)
{
ifAddr = addr;
}
public String getInterfaceAddress()
{
return ifAddr;
}
////////////////////////////////////////////////
// deliveryURL
////////////////////////////////////////////////
private String deliveryURL = "";
public String getDeliveryURL() {
return deliveryURL;
}
public void setDeliveryURL(String deliveryURL) {
this.deliveryURL = deliveryURL;
try {
URL url = new URL(deliveryURL);
deliveryHost = url.getHost();
deliveryPath = url.getPath();
deliveryPort = url.getPort();
}
catch (Exception e) {}
}
private String deliveryHost = "";
private String deliveryPath = "";
private int deliveryPort = 0;
public String getDeliveryHost() {
return deliveryHost;
}
public String getDeliveryPath() {
return deliveryPath;
}
public int getDeliveryPort() {
return deliveryPort;
}
////////////////////////////////////////////////
// Timeout
////////////////////////////////////////////////
private long timeOut = 0;
public long getTimeOut() {
return timeOut;
}
public void setTimeOut(long value) {
timeOut = value;
}
public boolean isExpired()
{
long currTime = System.currentTimeMillis();
// Thanks for Oliver Newell (10/26/04)
if(timeOut == Subscription.INFINITE_VALUE )
return false;
// Thanks for Oliver Newell (10/26/04)
long expiredTime = getSubscriptionTime() + getTimeOut()*1000;
if (expiredTime < currTime)
return true;
return false;
}
////////////////////////////////////////////////
// SubscriptionTIme
////////////////////////////////////////////////
private long subscriptionTime = 0;
public long getSubscriptionTime() {
return subscriptionTime;
}
public void setSubscriptionTime(long time) {
subscriptionTime = time;
}
////////////////////////////////////////////////
// SEQ
////////////////////////////////////////////////
private long notifyCount = 0;
public long getNotifyCount() {
return notifyCount;
}
public void setNotifyCount(int cnt) {
notifyCount = cnt;
}
public void incrementNotifyCount() {
if (notifyCount == Long.MAX_VALUE) {
notifyCount = 1;
return;
}
notifyCount++;
}
////////////////////////////////////////////////
// renew
////////////////////////////////////////////////
public void renew()
{
setSubscriptionTime(System.currentTimeMillis());
setNotifyCount(0);
}
}

View File

@ -0,0 +1,48 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SubscriberList.java
*
* Revision;
*
* 01/31/03
* - first revision.
* 06/18/03
* - Fixed to catch ArrayIndexOutOfBounds.
*
******************************************************************/
package org.cybergarage.upnp.event;
import java.util.*;
public class SubscriberList extends Vector
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
private static final long serialVersionUID = -648427977601494972L;
public SubscriberList()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public Subscriber getSubscriber(int n)
{
Object obj = null;
try {
obj = get(n);
}
catch (Exception e) {}
return (Subscriber)obj;
}
}

View File

@ -0,0 +1,75 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: ST.java
*
* Revision;
*
* 01/31/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.event;
import org.cybergarage.upnp.*;
public class Subscription
{
public final static String XMLNS = "urn:schemas-upnp-org:event-1-0";
public final static String TIMEOUT_HEADER = "Second-";
public final static String INFINITE_STRING = "infinite";
public final static int INFINITE_VALUE = -1;
public final static String UUID = "uuid:";
public final static String SUBSCRIBE_METHOD = "SUBSCRIBE";
public final static String UNSUBSCRIBE_METHOD = "UNSUBSCRIBE";
////////////////////////////////////////////////
// Timeout
////////////////////////////////////////////////
public final static String toTimeoutHeaderString(long time)
{
if (time == Subscription.INFINITE_VALUE)
return Subscription.INFINITE_STRING;
return Subscription.TIMEOUT_HEADER + Long.toString(time);
}
public final static long getTimeout(String headerValue)
{
int minusIdx = headerValue.indexOf('-');
long timeout = Subscription.INFINITE_VALUE;
try {
String timeoutStr = headerValue.substring(minusIdx+1, headerValue.length());
timeout = Long.parseLong(timeoutStr);
}
catch (Exception e) {}
return timeout;
}
////////////////////////////////////////////////
// SID
////////////////////////////////////////////////
public static final String createSID()
{
return UPnP.createUUID();
}
public final static String toSIDHeaderString(String id)
{
return Subscription.UUID + id;
}
public final static String getSID(String headerValue)
{
if (headerValue == null)
return "";
return headerValue.substring(Subscription.UUID.length(), headerValue.length());
}
}

View File

@ -0,0 +1,221 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SubscriptionRequest.java
*
* Revision;
*
* 01/31/03
* - first revision.
* 05/21/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Description: inserted a check at the beginning of the setService method
* - Problem : If the EventSubURL does not start with a '/', the device could refuse event subscription
* - Error : it is not an error, but adding the '/' when missing allows the integration with the Intel devices
* 09/02/03
* - Giordano Sassaroli <sassarol@cefriel.it>
* - Problem : NullpointerException thrown for devices whose description use absolute urls
* - Error : the presence of a base url is not mandatory, the API code makes the assumption that control and event subscription urls are relative. If the baseUrl is not present, the request host and port should be extracted from the control/subscription url
* - Description: The method setRequestHost/setService should be changed as follows
* 06/11/04
* - Markus Thurner <markus.thurner@fh-hagenberg.at> (06/11/2004)
* - Changed setServie() to get the host address from the SSDP Location field when the URLBase is null.
* 12/06/04
* - Grzegorz Lehmann <grzegorz.lehmann@dai-labor.de>
* - Stefano Lenzi <kismet-sl@users.sourceforge.net>
* - Fixed getSID() to loop between getSID() and hasSID();
*
********************************************************************/
package org.cybergarage.upnp.event;
import org.cybergarage.http.*;
import org.cybergarage.upnp.*;
import org.cybergarage.upnp.device.*;
public class SubscriptionRequest extends HTTPRequest
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SubscriptionRequest()
{
}
public SubscriptionRequest(HTTPRequest httpReq)
{
set(httpReq);
}
////////////////////////////////////////////////
// setRequest
////////////////////////////////////////////////
private void setService(Service service)
{
String eventSubURL = service.getEventSubURL();
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (05/21/03)
setURI(eventSubURL, true);
String urlBaseStr = "";
Device dev = service.getDevice();
if (dev != null)
urlBaseStr = dev.getURLBase();
if (urlBaseStr == null || urlBaseStr.length() <= 0) {
Device rootDev = service.getRootDevice();
if (rootDev != null)
urlBaseStr = rootDev.getURLBase();
}
// Thansk for Markus Thurner <markus.thurner@fh-hagenberg.at> (06/11/2004)
if (urlBaseStr == null || urlBaseStr.length() <= 0) {
Device rootDev = service.getRootDevice();
if (rootDev != null)
urlBaseStr = rootDev.getLocation();
}
// Thanks for Giordano Sassaroli <sassarol@cefriel.it> (09/02/03)
if (urlBaseStr == null || urlBaseStr.length() <= 0) {
if (HTTP.isAbsoluteURL(eventSubURL))
urlBaseStr = eventSubURL;
}
String reqHost = HTTP.getHost(urlBaseStr);
int reqPort = HTTP.getPort(urlBaseStr);
setHost(reqHost, reqPort);
setRequestHost(reqHost);
setRequestPort(reqPort);
}
public void setSubscribeRequest(Service service, String callback, long timeout)
{
setMethod(Subscription.SUBSCRIBE_METHOD);
setService(service);
setCallback(callback);
setNT(NT.EVENT);
setTimeout(timeout);
}
public void setRenewRequest(Service service, String uuid, long timeout)
{
setMethod(Subscription.SUBSCRIBE_METHOD);
setService(service);
setSID(uuid);
setTimeout(timeout);
}
public void setUnsubscribeRequest(Service service)
{
setMethod(Subscription.UNSUBSCRIBE_METHOD);
setService(service);
setSID(service.getSID());
}
////////////////////////////////////////////////
// NT
////////////////////////////////////////////////
public void setNT(String value)
{
setHeader(HTTP.NT, value);
}
public String getNT()
{
return getHeaderValue(HTTP.NT);
}
public boolean hasNT()
{
String nt = getNT();
return (nt != null && 0 < nt.length()) ? true : false;
}
////////////////////////////////////////////////
// CALLBACK
////////////////////////////////////////////////
private final static String CALLBACK_START_WITH = "<";
private final static String CALLBACK_END_WITH = ">";
public void setCallback(String value)
{
setStringHeader(HTTP.CALLBACK, value, CALLBACK_START_WITH, CALLBACK_END_WITH);
}
public String getCallback()
{
return getStringHeaderValue(HTTP.CALLBACK, CALLBACK_START_WITH, CALLBACK_END_WITH);
}
public boolean hasCallback()
{
String callback = getCallback();
return (callback != null && 0 < callback.length()) ? true : false;
}
////////////////////////////////////////////////
// SID
////////////////////////////////////////////////
public void setSID(String id)
{
setHeader(HTTP.SID, Subscription.toSIDHeaderString(id));
}
public String getSID()
{
// Thanks for Grzegorz Lehmann and Stefano Lenzi(12/06/04)
String sid = Subscription.getSID(getHeaderValue(HTTP.SID));
if (sid == null)
return "";
return sid;
}
public boolean hasSID()
{
String sid = getSID();
return (sid != null && 0 < sid.length()) ? true : false;
}
////////////////////////////////////////////////
// Timeout
////////////////////////////////////////////////
public final void setTimeout(long value)
{
setHeader(HTTP.TIMEOUT, Subscription.toTimeoutHeaderString(value));
}
public long getTimeout()
{
return Subscription.getTimeout(getHeaderValue(HTTP.TIMEOUT));
}
////////////////////////////////////////////////
// post (Response)
////////////////////////////////////////////////
public void post(SubscriptionResponse subRes)
{
super.post(subRes);
}
////////////////////////////////////////////////
// post
////////////////////////////////////////////////
public SubscriptionResponse post()
{
HTTPResponse httpRes = post(getRequestHost(), getRequestPort());
return new SubscriptionResponse(httpRes);
}
}

View File

@ -0,0 +1,84 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SubscriptionResponse.java
*
* Revision;
*
* 01/29/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.event;
import org.cybergarage.upnp.*;
import org.cybergarage.http.*;
public class SubscriptionResponse extends HTTPResponse
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SubscriptionResponse()
{
setServer(UPnP.getServerName());
}
public SubscriptionResponse(HTTPResponse httpRes)
{
super(httpRes);
}
////////////////////////////////////////////////
// Error
////////////////////////////////////////////////
public void setResponse(int code)
{
setStatusCode(code);
setContentLength(0);
}
////////////////////////////////////////////////
// Error
////////////////////////////////////////////////
public void setErrorResponse(int code)
{
setStatusCode(code);
setContentLength(0);
}
////////////////////////////////////////////////
// SID
////////////////////////////////////////////////
public void setSID(String id)
{
setHeader(HTTP.SID, Subscription.toSIDHeaderString(id));
}
public String getSID()
{
return Subscription.getSID(getHeaderValue(HTTP.SID));
}
////////////////////////////////////////////////
// Timeout
////////////////////////////////////////////////
public void setTimeout(long value)
{
setHeader(HTTP.TIMEOUT, Subscription.toTimeoutHeaderString(value));
}
public long getTimeout()
{
return Subscription.getTimeout(getHeaderValue(HTTP.TIMEOUT));
}
}

View File

@ -0,0 +1,198 @@
/******************************************************************
*
* CyberLink for Java
*
* Copyright (C) Satoshi Konno 2002-2004
*
* File: HTTPMU.java
*
* Revision;
*
* 11/18/02
* - first revision.
* 09/03/03
* - Changed to open the socket using setReuseAddress().
* 12/10/03
* - Fixed getLocalAddress() to return a valid interface address.
* 02/28/04
* - Added getMulticastInetAddress(), getMulticastAddress().
* 11/19/04
* - Theo Beisch <theo.beisch@gmx.de>
* - Changed send() to set the TTL as 4.
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import java.net.*;
import java.util.*;
import org.cybergarage.http.*;
import org.cybergarage.util.*;
public class HTTPMUSocket
{
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private InetSocketAddress ssdpMultiGroup = null;
private MulticastSocket ssdpMultiSock = null;
private NetworkInterface ssdpMultiIf = null;
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public HTTPMUSocket()
{
}
public HTTPMUSocket(String addr, int port, String bindAddr)
{
open(addr, port, bindAddr);
}
protected void finalize()
{
close();
}
////////////////////////////////////////////////
// bindAddr
////////////////////////////////////////////////
public String getLocalAddress()
{
InetAddress mcastAddr = ssdpMultiGroup.getAddress();
Enumeration addrs = ssdpMultiIf.getInetAddresses();
while (addrs.hasMoreElements()) {
InetAddress addr = (InetAddress)addrs.nextElement();
if (mcastAddr instanceof Inet6Address && addr instanceof Inet6Address)
return addr.getHostAddress();
if (mcastAddr instanceof Inet4Address && addr instanceof Inet4Address)
return addr.getHostAddress();
}
return "";
}
////////////////////////////////////////////////
// MulticastAddr
////////////////////////////////////////////////
public InetAddress getMulticastInetAddress()
{
return ssdpMultiGroup.getAddress();
}
public String getMulticastAddress()
{
return getMulticastInetAddress().getHostAddress();
}
////////////////////////////////////////////////
// open/close
////////////////////////////////////////////////
public boolean open(String addr, int port, String bindAddr)
{
try {
ssdpMultiSock = new MulticastSocket(null);
ssdpMultiSock.setReuseAddress(true);
InetSocketAddress bindSockAddr = new InetSocketAddress(port);
ssdpMultiSock.bind(bindSockAddr);
ssdpMultiGroup = new InetSocketAddress(InetAddress.getByName(addr), port);
ssdpMultiIf = NetworkInterface.getByInetAddress(InetAddress.getByName(bindAddr));
ssdpMultiSock.joinGroup(ssdpMultiGroup, ssdpMultiIf);
}
catch (Exception e) {
Debug.warning(e);
return false;
}
return true;
}
public boolean close()
{
if (ssdpMultiSock == null)
return true;
try {
ssdpMultiSock.leaveGroup(ssdpMultiGroup, ssdpMultiIf);
ssdpMultiSock = null;
}
catch (Exception e) {
//Debug.warning(e);
return false;
}
return true;
}
////////////////////////////////////////////////
// send
////////////////////////////////////////////////
public boolean send(String msg, String bindAddr, int bindPort)
{
try {
MulticastSocket msock;
if ((bindAddr) != null && (0 < bindPort)) {
msock = new MulticastSocket(null);
msock.bind(new InetSocketAddress(bindAddr, bindPort));
}
else
msock = new MulticastSocket();
DatagramPacket dgmPacket = new DatagramPacket(msg.getBytes(), msg.length(), ssdpMultiGroup);
// Thnaks for Tho Beisch (11/09/04)
msock.setTimeToLive(4);
msock.send(dgmPacket);
msock.close();
}
catch (Exception e) {
Debug.warning(e);
return false;
}
return true;
}
public boolean send(String msg)
{
return send(msg, null, -1);
}
////////////////////////////////////////////////
// post (HTTPRequest)
////////////////////////////////////////////////
public boolean post(HTTPRequest req, String bindAddr, int bindPort)
{
return send(req.toString(), bindAddr, bindPort);
}
public boolean post(HTTPRequest req)
{
return send(req.toString(), null, -1);
}
////////////////////////////////////////////////
// reveive
////////////////////////////////////////////////
public SSDPPacket receive()
{
byte ssdvRecvBuf[] = new byte[SSDP.RECV_MESSAGE_BUFSIZE];
SSDPPacket recvPacket = new SSDPPacket(ssdvRecvBuf, ssdvRecvBuf.length);
recvPacket.setLocalAddress(getLocalAddress());
try {
ssdpMultiSock.receive(recvPacket.getDatagramPacket());
recvPacket.setTimeStamp(System.currentTimeMillis());
}
catch (Exception e) {
//Debug.warning(e);
}
return recvPacket;
}
}

View File

@ -0,0 +1,239 @@
/******************************************************************
*
* CyberLink for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: HTTPMU.java
*
* Revision;
*
* 11/20/02
* - first revision.
* 12/12/03
* - Inma Mar?n <inma@DIF.UM.ES>
* - Changed open(addr, port) to send IPv6 SSDP packets.
* - The socket binds only the port without the interface address.
* - The full binding socket can send SSDP IPv4 packets. Is it a bug of J2SE v.1.4.2-b28 ?.
* 01/06/04
* - Oliver Newell <olivern@users.sourceforge.net>
* - Added to set a current timestamp when the packet are received.
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import java.net.*;
import org.cybergarage.util.*;
public class HTTPUSocket
{
////////////////////////////////////////////////
// Member
////////////////////////////////////////////////
private DatagramSocket ssdpUniSock = null;
//private MulticastSocket ssdpUniSock = null;
public DatagramSocket getDatagramSocket()
{
return ssdpUniSock;
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public HTTPUSocket()
{
open();
}
public HTTPUSocket(String bindAddr, int bindPort)
{
open(bindAddr, bindPort);
}
public HTTPUSocket(int bindPort)
{
open(bindPort);
}
protected void finalize()
{
close();
}
////////////////////////////////////////////////
// bindAddr
////////////////////////////////////////////////
private String localAddr = "";
public void setLocalAddress(String addr)
{
localAddr = addr;
}
public String getLocalAddress()
{
if (0 < localAddr.length())
return localAddr;
return ssdpUniSock.getLocalAddress().getHostAddress();
}
////////////////////////////////////////////////
// open
////////////////////////////////////////////////
public boolean open()
{
close();
try {
ssdpUniSock = new DatagramSocket();
}
catch (Exception e) {
Debug.warning(e);
return false;
}
return true;
}
public boolean open(String bindAddr, int bindPort)
{
close();
try {
// Bind only using the port without the interface address. (2003/12/12)
InetSocketAddress bindSock = new InetSocketAddress(/*InetAddress.getByName(bindAddr), */ bindPort);
ssdpUniSock = new DatagramSocket(null);
ssdpUniSock.setReuseAddress(true);
ssdpUniSock.bind(bindSock);
}
catch (Exception e) {
Debug.warning(e);
return false;
}
setLocalAddress(bindAddr);
return true;
}
public boolean open(int bindPort)
{
close();
try {
InetSocketAddress bindSock = new InetSocketAddress(bindPort);
ssdpUniSock = new DatagramSocket(null);
ssdpUniSock.setReuseAddress(true);
ssdpUniSock.bind(bindSock);
}
catch (Exception e) {
//Debug.warning(e);
return false;
}
return true;
}
////////////////////////////////////////////////
// close
////////////////////////////////////////////////
public boolean close()
{
if (ssdpUniSock == null)
return true;
try {
ssdpUniSock.close();
ssdpUniSock = null;
}
catch (Exception e) {
Debug.warning(e);
return false;
}
return true;
}
////////////////////////////////////////////////
// send
////////////////////////////////////////////////
public boolean post(String addr, int port, String msg)
{
try {
InetAddress inetAddr = InetAddress.getByName(addr);
DatagramPacket dgmPacket = new DatagramPacket(msg.getBytes(), msg.length(), inetAddr, port);
ssdpUniSock.send(dgmPacket);
}
catch (Exception e) {
Debug.warning("addr = " +ssdpUniSock.getLocalAddress().getHostName());
Debug.warning("port = " + ssdpUniSock.getLocalPort());
Debug.warning(e);
return false;
}
return true;
}
////////////////////////////////////////////////
// reveive
////////////////////////////////////////////////
public SSDPPacket receive()
{
byte ssdvRecvBuf[] = new byte[SSDP.RECV_MESSAGE_BUFSIZE];
SSDPPacket recvPacket = new SSDPPacket(ssdvRecvBuf, ssdvRecvBuf.length);
recvPacket.setLocalAddress(getLocalAddress());
try {
ssdpUniSock.receive(recvPacket.getDatagramPacket());
recvPacket.setTimeStamp(System.currentTimeMillis());
}
catch (Exception e) {
//Debug.warning(e);
return null;
}
return recvPacket;
}
////////////////////////////////////////////////
// join/leave
////////////////////////////////////////////////
/*
boolean joinGroup(String mcastAddr, int mcastPort, String bindAddr)
{
try {
InetSocketAddress mcastGroup = new InetSocketAddress(InetAddress.getByName(mcastAddr), mcastPort);
NetworkInterface mcastIf = NetworkInterface.getByInetAddress(InetAddress.getByName(bindAddr));
ssdpUniSock.joinGroup(mcastGroup, mcastIf);
}
catch (Exception e) {
Debug.warning(e);
return false;
}
return true;
}
boolean leaveGroup(String mcastAddr, int mcastPort, String bindAddr)
{
try {
InetSocketAddress mcastGroup = new InetSocketAddress(InetAddress.getByName(mcastAddr), mcastPort);
NetworkInterface mcastIf = NetworkInterface.getByInetAddress(InetAddress.getByName(bindAddr));
ssdpUniSock.leaveGroup(mcastGroup, mcastIf);
}
catch (Exception e) {
Debug.warning(e);
return false;
}
return true;
}
*/
}

View File

@ -0,0 +1,77 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SSDP.java
*
* Revision;
*
* 11/18/02
* - first revision.
* 05/13/03
* - Added constants for IPv6.
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
public class SSDP
{
////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////
public static final int PORT = 1900;
public static final String ADDRESS = "239.255.255.250";
public static final String IPV6_LINK_LOCAL_ADDRESS = "FF02::C";
public static final String IPV6_SUBNET_ADDRESS = "FF03::C";
public static final String IPV6_ADMINISTRATIVE_ADDRESS = "FF04::C";
public static final String IPV6_SITE_LOCAL_ADDRESS = "FF05::C";
public static final String IPV6_GLOBAL_ADDRESS = "FF0E::C";
private static String IPV6_ADDRESS;
public static final void setIPv6Address(String addr)
{
IPV6_ADDRESS = addr;
}
public static final String getIPv6Address()
{
return IPV6_ADDRESS;
}
public static final int DEFAULT_MSEARCH_MX = 3;
public static final int RECV_MESSAGE_BUFSIZE = 1024;
////////////////////////////////////////////////
// Initialize
////////////////////////////////////////////////
static
{
setIPv6Address(IPV6_LINK_LOCAL_ADDRESS);
}
////////////////////////////////////////////////
// LeaseTime
////////////////////////////////////////////////
public final static int getLeaseTime(String cacheCont)
{
int equIdx = cacheCont.indexOf('=');
int mx = 0;
try {
String mxStr = new String(cacheCont.getBytes(), equIdx+1, cacheCont.length() - (equIdx+1));
mx = Integer.parseInt(mxStr);
}
catch (Exception e) {}
return mx;
}
}

View File

@ -0,0 +1,31 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SSDPMSearchRequest.java
*
* Revision;
*
* 01/14/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import org.cybergarage.http.*;
public class SSDPNotifyRequest extends SSDPRequest
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SSDPNotifyRequest()
{
setMethod(HTTP.NOTIFY);
setURI("*");
}
}

View File

@ -0,0 +1,131 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: SSDPNotifySocket.java
*
* Revision;
*
* 11/20/02
* - first revision.
* 05/13/03
* - Added support for IPv6.
* 02/20/04
* - Inma Marin Lopez <inma@dif.um.es>
* - Added a multicast filter using the SSDP pakcet.
* 04/20/05
* - Mikael Hakman <mhakman@dkab.net>
* - Handle receive() returning null.
* - Added close() in stop().
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import java.net.*;
import org.cybergarage.net.*;
import org.cybergarage.util.*;
import org.cybergarage.http.*;
import org.cybergarage.upnp.*;
public class SSDPNotifySocket extends HTTPMUSocket implements Runnable
{
private boolean useIPv6Address;
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SSDPNotifySocket(String bindAddr)
{
String addr = SSDP.ADDRESS;
useIPv6Address = false;
if (HostInterface.isIPv6Address(bindAddr) == true) {
addr = SSDP.getIPv6Address();
useIPv6Address = true;
}
open(addr, SSDP.PORT, bindAddr);
setControlPoint(null);
}
////////////////////////////////////////////////
// ControlPoint
////////////////////////////////////////////////
private ControlPoint controlPoint = null;
public void setControlPoint(ControlPoint ctrlp)
{
this.controlPoint = ctrlp;
}
public ControlPoint getControlPoint()
{
return controlPoint;
}
////////////////////////////////////////////////
// post (SSDPNotifySocket)
////////////////////////////////////////////////
public boolean post(SSDPNotifyRequest req)
{
String ssdpAddr = SSDP.ADDRESS;
if (useIPv6Address == true)
ssdpAddr = SSDP.getIPv6Address();
req.setHost(ssdpAddr, SSDP.PORT);
return post((HTTPRequest)req);
}
////////////////////////////////////////////////
// run
////////////////////////////////////////////////
private Thread deviceNotifyThread = null;
public void run()
{
Thread thisThread = Thread.currentThread();
ControlPoint ctrlPoint = getControlPoint();
while (deviceNotifyThread == thisThread) {
Thread.yield();
SSDPPacket packet = receive();
// Thanks for Mikael Hakman (04/20/05)
if (packet == null)
continue;
// Thanks for Inma (02/20/04)
InetAddress maddr = getMulticastInetAddress();
InetAddress pmaddr = packet.getHostInetAddress();
if (maddr.equals(pmaddr) == false) {
Debug.warning("Invalidate Multicast Recieved : " + maddr + "," + pmaddr);
continue;
}
if (ctrlPoint != null)
ctrlPoint.notifyReceived(packet);
}
}
public void start()
{
deviceNotifyThread = new Thread(this, "UPnP-SSDPNotifySocket");
deviceNotifyThread.setDaemon(true);
deviceNotifyThread.start();
}
public void stop()
{
// Thanks for Mikael Hakman (04/20/05)
close();
deviceNotifyThread = null;
}
}

View File

@ -0,0 +1,106 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: HTTPServerList.java
*
* Revision;
*
* 05/11/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import java.util.*;
import org.cybergarage.net.*;
import org.cybergarage.upnp.*;
public class SSDPNotifySocketList extends Vector
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
private static final long serialVersionUID = -7066290881503106399L;
public SSDPNotifySocketList()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public SSDPNotifySocket getSSDPNotifySocket(int n)
{
return (SSDPNotifySocket)get(n);
}
////////////////////////////////////////////////
// ControlPoint
////////////////////////////////////////////////
public void setControlPoint(ControlPoint ctrlPoint)
{
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPNotifySocket sock = getSSDPNotifySocket(n);
sock.setControlPoint(ctrlPoint);
}
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public boolean open()
{
int nHostAddrs = HostInterface.getNHostAddresses();
for (int n=0; n<nHostAddrs; n++) {
String bindAddr = HostInterface.getHostAddress(n);
SSDPNotifySocket ssdpNotifySocket = new SSDPNotifySocket(bindAddr);
add(ssdpNotifySocket);
}
return true;
}
public void close()
{
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPNotifySocket sock = getSSDPNotifySocket(n);
sock.close();
}
clear();
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public void start()
{
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPNotifySocket sock = getSSDPNotifySocket(n);
sock.start();
}
}
public void stop()
{
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPNotifySocket sock = getSSDPNotifySocket(n);
sock.stop();
}
}
}

View File

@ -0,0 +1,243 @@
/******************************************************************
*
* CyberLink for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: SSDPPacket.java
*
* Revision;
*
* 11/18/02
* - first revision.
* 05/13/03
* - Added getLocalAddress().
* 11/01/04
* - Theo Beisch <theo.beisch@gmx.de>
* - Fixed isRootDevice() to check the ST header.
* 11/19/04
* - Theo Beisch <theo.beisch@gmx.de>
* - Changed getRemoteAddress() to return the adresss instead of the host name.
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import java.net.*;
import org.cybergarage.http.*;
import org.cybergarage.upnp.device.*;
public class SSDPPacket
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SSDPPacket(byte[] buf, int length)
{
dgmPacket = new DatagramPacket(buf, length);
}
////////////////////////////////////////////////
// DatagramPacket
////////////////////////////////////////////////
private DatagramPacket dgmPacket = null;
public DatagramPacket getDatagramPacket()
{
return dgmPacket;
}
////////////////////////////////////////////////
// addr
////////////////////////////////////////////////
private String localAddr = "";
public void setLocalAddress(String addr)
{
localAddr = addr;
}
public String getLocalAddress()
{
return localAddr;
}
////////////////////////////////////////////////
// Time
////////////////////////////////////////////////
private long timeStamp;
public void setTimeStamp(long value)
{
timeStamp = value;
}
public long getTimeStamp()
{
return timeStamp;
}
////////////////////////////////////////////////
// Remote host
////////////////////////////////////////////////
public InetAddress getRemoteInetAddress()
{
return getDatagramPacket().getAddress();
}
public String getRemoteAddress()
{
// Thanks for Theo Beisch (11/09/04)
return getDatagramPacket().getAddress().getHostAddress();
}
public int getRemotePort()
{
return getDatagramPacket().getPort();
}
////////////////////////////////////////////////
// Access Methods
////////////////////////////////////////////////
public byte[] packetBytes = null;
public byte[] getData()
{
if (packetBytes != null)
return packetBytes;
DatagramPacket packet = getDatagramPacket();
int packetLen = packet.getLength();
String packetData = new String(packet.getData(), 0, packetLen);
packetBytes = packetData.getBytes();
return packetBytes;
}
////////////////////////////////////////////////
// Access Methods
////////////////////////////////////////////////
public String getHost()
{
return HTTPHeader.getValue(getData(), HTTP.HOST);
}
public String getCacheControl()
{
return HTTPHeader.getValue(getData(), HTTP.CACHE_CONTROL);
}
public String getLocation()
{
return HTTPHeader.getValue(getData(), HTTP.LOCATION);
}
public String getMAN()
{
return HTTPHeader.getValue(getData(), HTTP.MAN);
}
public String getST()
{
return HTTPHeader.getValue(getData(), HTTP.ST);
}
public String getNT()
{
return HTTPHeader.getValue(getData(), HTTP.NT);
}
public String getNTS()
{
return HTTPHeader.getValue(getData(), HTTP.NTS);
}
public String getServer()
{
return HTTPHeader.getValue(getData(), HTTP.SERVER);
}
public String getUSN()
{
return HTTPHeader.getValue(getData(), HTTP.USN);
}
public int getMX()
{
return HTTPHeader.getIntegerValue(getData(), HTTP.MX);
}
////////////////////////////////////////////////
// Access Methods
////////////////////////////////////////////////
public InetAddress getHostInetAddress()
{
String addrStr = "127.0.0.1";
String host = getHost();
int canmaIdx = host.lastIndexOf(":");
if (0 <= canmaIdx) {
addrStr = host.substring(0, canmaIdx);
if (addrStr.charAt(0) == '[')
addrStr = addrStr.substring(1, addrStr.length());
if (addrStr.charAt(addrStr.length()-1) == ']')
addrStr = addrStr.substring(0, addrStr.length()-1);
}
InetSocketAddress isockaddr = new InetSocketAddress(addrStr, 0);
return isockaddr.getAddress();
}
////////////////////////////////////////////////
// Access Methods (Extension)
////////////////////////////////////////////////
public boolean isRootDevice()
{
if (NT.isRootDevice(getNT()) == true)
return true;
// Thanks for Theo Beisch (11/01/04)
if (ST.isRootDevice(getST()) == true)
return true;
return USN.isRootDevice(getUSN());
}
public boolean isDiscover()
{
return MAN.isDiscover(getMAN());
}
public boolean isAlive()
{
return NTS.isAlive(getNTS());
}
public boolean isByeBye()
{
return NTS.isByeBye(getNTS());
}
public int getLeaseTime()
{
return SSDP.getLeaseTime(getCacheControl());
}
////////////////////////////////////////////////
// toString
////////////////////////////////////////////////
public String toString()
{
return new String(getData());
}
}

View File

@ -0,0 +1,104 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SSDPRequest.java
*
* Revision;
*
* 01/14/03
* - first revision.
* 03/16/04
* - Thanks for Darrell Young
* - Fixed to set v1.1 to the HTTP version.;
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import org.cybergarage.http.*;
public class SSDPRequest extends HTTPRequest
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SSDPRequest()
{
setVersion(HTTP.VERSION_11);
}
////////////////////////////////////////////////
// NT
////////////////////////////////////////////////
public void setNT(String value)
{
setHeader(HTTP.NT, value);
}
public String getNT()
{
return getHeaderValue(HTTP.NT);
}
////////////////////////////////////////////////
// NTS
////////////////////////////////////////////////
public void setNTS(String value)
{
setHeader(HTTP.NTS, value);
}
public String getNTS()
{
return getHeaderValue(HTTP.NTS);
}
////////////////////////////////////////////////
// Location
////////////////////////////////////////////////
public void setLocation(String value)
{
setHeader(HTTP.LOCATION, value);
}
public String getLocation()
{
return getHeaderValue(HTTP.LOCATION);
}
////////////////////////////////////////////////
// USN
////////////////////////////////////////////////
public void setUSN(String value)
{
setHeader(HTTP.USN, value);
}
public String getUSN()
{
return getHeaderValue(HTTP.USN);
}
////////////////////////////////////////////////
// CacheControl
////////////////////////////////////////////////
public void setLeaseTime(int len)
{
setHeader(HTTP.CACHE_CONTROL, "max-age=" + Integer.toString(len));
}
public int getLeaseTime()
{
String cacheCtrl = getHeaderValue(HTTP.CACHE_CONTROL);
return SSDP.getLeaseTime(cacheCtrl);
}
}

View File

@ -0,0 +1,126 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SSDPResponse.java
*
* Revision;
*
* 01/14/03
* - first revision.
* 01/23/04
* - Oliver Newell
* - Overided HTTPResponse::getHeader() for Intel UPnP control points.
* 03/16/04
* - Thanks for Darrell Young
* - Fixed to set v1.1 to the HTTP version.
* 10/20/04
* - Brent Hills <bhills@openshores.com>
* - Added setMYNAME() and getMYNAME().
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import org.cybergarage.http.*;
public class SSDPResponse extends HTTPResponse
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SSDPResponse()
{
setVersion(HTTP.VERSION_11);
}
////////////////////////////////////////////////
// ST (SearchTarget)
////////////////////////////////////////////////
public void setST(String value)
{
setHeader(HTTP.ST, value);
}
public String getST()
{
return getHeaderValue(HTTP.ST);
}
////////////////////////////////////////////////
// Location
////////////////////////////////////////////////
public void setLocation(String value)
{
setHeader(HTTP.LOCATION, value);
}
public String getLocation()
{
return getHeaderValue(HTTP.LOCATION);
}
////////////////////////////////////////////////
// USN
////////////////////////////////////////////////
public void setUSN(String value)
{
setHeader(HTTP.USN, value);
}
public String getUSN()
{
return getHeaderValue(HTTP.USN);
}
////////////////////////////////////////////////
// MYNAME
////////////////////////////////////////////////
public void setMYNAME(String value)
{
setHeader(HTTP.MYNAME, value);
}
public String getMYNAME()
{
return getHeaderValue(HTTP.MYNAME);
}
////////////////////////////////////////////////
// CacheControl
////////////////////////////////////////////////
public void setLeaseTime(int len)
{
setHeader(HTTP.CACHE_CONTROL, "max-age=" + Integer.toString(len));
}
public int getLeaseTime()
{
String cacheCtrl = getHeaderValue(HTTP.CACHE_CONTROL);
return SSDP.getLeaseTime(cacheCtrl);
}
////////////////////////////////////////////////
// getHeader (Override)
////////////////////////////////////////////////
public String getHeader()
{
StringBuilder str = new StringBuilder();
str.append(getStatusLineString());
str.append(getHeaderString());
str.append(HTTP.CRLF); // for Intel UPnP control points.
return str.toString();
}
}

View File

@ -0,0 +1,61 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SSDPMSearchRequest.java
*
* Revision;
*
* 11/19/02
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import org.cybergarage.net.*;
import org.cybergarage.http.*;
import org.cybergarage.upnp.device.*;
public class SSDPSearchRequest extends SSDPRequest
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SSDPSearchRequest(String serachTarget, int mx)
{
setMethod(HTTP.M_SEARCH);
setURI("*");
setHeader(HTTP.ST, serachTarget);
setHeader(HTTP.MX, Integer.toString(mx));
setHeader(HTTP.MAN, "\"" + MAN.DISCOVER + "\"");
}
public SSDPSearchRequest(String serachTarget)
{
this(serachTarget, SSDP.DEFAULT_MSEARCH_MX);
}
public SSDPSearchRequest()
{
this(ST.ROOT_DEVICE);
}
////////////////////////////////////////////////
// HOST
////////////////////////////////////////////////
public void setLocalAddress(String bindAddr)
{
String ssdpAddr = SSDP.ADDRESS;
if (HostInterface.isIPv6Address(bindAddr) == true)
ssdpAddr = SSDP.getIPv6Address();
setHost(ssdpAddr, SSDP.PORT);
}
}

View File

@ -0,0 +1,35 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SSDPSearchResponse.java
*
* Revision;
*
* 01/14/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import org.cybergarage.http.*;
import org.cybergarage.upnp.*;
public class SSDPSearchResponse extends SSDPResponse
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SSDPSearchResponse()
{
setStatusCode(HTTPStatus.OK);
setCacheControl(Device.DEFAULT_LEASE_TIME);
setHeader(HTTP.SERVER, UPnP.getServerName());
setHeader(HTTP.EXT, "");
}
}

View File

@ -0,0 +1,107 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: SSDPSearchResponseSocket.java
*
* Revision;
*
* 11/20/02
* - first revision.
* 05/28/03
* - Added post() to send a SSDPSearchRequest.
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import org.cybergarage.upnp.*;
public class SSDPSearchResponseSocket extends HTTPUSocket implements Runnable
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public SSDPSearchResponseSocket()
{
setControlPoint(null);
}
public SSDPSearchResponseSocket(String bindAddr, int port)
{
super(bindAddr, port);
setControlPoint(null);
}
////////////////////////////////////////////////
// ControlPoint
////////////////////////////////////////////////
private ControlPoint controlPoint = null;
public void setControlPoint(ControlPoint ctrlp)
{
this.controlPoint = ctrlp;
}
public ControlPoint getControlPoint()
{
return controlPoint;
}
////////////////////////////////////////////////
// run
////////////////////////////////////////////////
private Thread deviceSearchResponseThread = null;
public void run()
{
Thread thisThread = Thread.currentThread();
ControlPoint ctrlPoint = getControlPoint();
while (deviceSearchResponseThread == thisThread) {
Thread.yield();
SSDPPacket packet = receive();
if (packet == null)
break;
if (ctrlPoint != null)
ctrlPoint.searchResponseReceived(packet);
}
}
public void start()
{
deviceSearchResponseThread = new Thread(this, "UPnP-SSDPSearchResponseSocket");
deviceSearchResponseThread.setDaemon(true);
deviceSearchResponseThread.start();
}
public void stop()
{
deviceSearchResponseThread = null;
}
////////////////////////////////////////////////
// post
////////////////////////////////////////////////
public boolean post(String addr, int port, SSDPSearchResponse res)
{
return post(addr, port, res.getHeader());
}
////////////////////////////////////////////////
// post
////////////////////////////////////////////////
public boolean post(String addr, int port, SSDPSearchRequest req)
{
return post(addr, port, req.toString());
}
}

View File

@ -0,0 +1,144 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: SSDPSearchResponseSocketList.java
*
* Revision;
*
* 05/08/03
* - first revision.
* 05/28/03
* - Added post() to send a SSDPSearchRequest.
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import java.util.*;
import org.cybergarage.net.*;
import org.cybergarage.upnp.*;
public class SSDPSearchResponseSocketList extends Vector
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
private static final long serialVersionUID = 4509857798038125744L;
public SSDPSearchResponseSocketList()
{
}
////////////////////////////////////////////////
// ControlPoint
////////////////////////////////////////////////
public void setControlPoint(ControlPoint ctrlPoint)
{
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n);
sock.setControlPoint(ctrlPoint);
}
}
////////////////////////////////////////////////
// get
////////////////////////////////////////////////
public SSDPSearchResponseSocket getSSDPSearchResponseSocket(int n)
{
return (SSDPSearchResponseSocket)get(n);
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public boolean open(int port)
{
try {
int nHostAddrs = HostInterface.getNHostAddresses();
for (int n=0; n<nHostAddrs; n++) {
String bindAddr = HostInterface.getHostAddress(n);
SSDPSearchResponseSocket socket = new SSDPSearchResponseSocket(bindAddr, port);
add(socket);
}
}
catch (Exception e) {
stop();
close();
clear();
return false;
}
return true;
}
public boolean open()
{
return open(SSDP.PORT);
}
public void close()
{
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n);
sock.close();
}
clear();
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public void start()
{
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n);
sock.start();
}
}
public void stop()
{
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n);
sock.stop();
}
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public boolean post(SSDPSearchRequest req)
{
boolean ret = true;
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPSearchResponseSocket sock = getSSDPSearchResponseSocket(n);
String bindAddr = sock.getLocalAddress();
req.setLocalAddress(bindAddr);
String ssdpAddr = SSDP.ADDRESS;
if (HostInterface.isIPv6Address(bindAddr) == true)
ssdpAddr = SSDP.getIPv6Address();
//sock.joinGroup(ssdpAddr, SSDP.PORT, bindAddr);
if (sock.post(ssdpAddr, SSDP.PORT, req) == false)
ret = false;
//sock.leaveGroup(ssdpAddr, SSDP.PORT, bindAddr);
}
return ret;
}
}

View File

@ -0,0 +1,118 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: SSDPSearchSocket.java
*
* Revision;
*
* 12/30/02
* - first revision.
* 05/13/03
* - Added support for IPv6.
* 05/28/03
* - Moved post() for SSDPSearchRequest to SSDPResponseSocketList.
* 04/20/05
* - Mikael Hakman <mhakman@dkab.net>
* - Added close() in stop().
* - Added test for null return from receive() in run().
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import org.cybergarage.net.*;
import org.cybergarage.util.*;
import org.cybergarage.upnp.device.*;
public class SSDPSearchSocket extends HTTPMUSocket implements Runnable
{
public SSDPSearchSocket()
{
}
public SSDPSearchSocket(String bindAddr)
{
open(bindAddr);
}
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
public boolean open(String bindAddr)
{
String addr = SSDP.ADDRESS;
if (HostInterface.isIPv6Address(bindAddr) == true) {
addr = SSDP.getIPv6Address();
}
return open(addr, SSDP.PORT, bindAddr);
}
////////////////////////////////////////////////
// deviceSearch
////////////////////////////////////////////////
private ListenerList deviceSearchListenerList = new ListenerList();
public void addSearchListener(SearchListener listener)
{
deviceSearchListenerList.add(listener);
}
public void removeSearchListener(SearchListener listener)
{
deviceSearchListenerList.remove(listener);
}
public void performSearchListener(SSDPPacket ssdpPacket)
{
int listenerSize = deviceSearchListenerList.size();
for (int n=0; n<listenerSize; n++) {
SearchListener listener = (SearchListener)deviceSearchListenerList.get(n);
listener.deviceSearchReceived(ssdpPacket);
}
}
////////////////////////////////////////////////
// run
////////////////////////////////////////////////
private Thread deviceSearchThread = null;
public void run()
{
Thread thisThread = Thread.currentThread();
while (deviceSearchThread == thisThread) {
Thread.yield();
SSDPPacket packet = receive();
// Thanks for Mikael Hakman (04/20/05)
if (packet == null)
continue;
if (packet.isDiscover() == true)
performSearchListener(packet);
}
}
public void start()
{
deviceSearchThread = new Thread(this,"UPnP-SSDPSearchSocket");
deviceSearchThread.setDaemon(true);
deviceSearchThread.start();
}
public void stop()
{
// Thanks for Mikael Hakman (04/20/05)
close();
deviceSearchThread = null;
}
}

View File

@ -0,0 +1,105 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: SSDPSearchSocketList.java
*
* Revision;
*
* 05/08/03
* - first revision.
* 05/28/03
* - Moved post() for SSDPSearchRequest to SSDPResponseSocket.
* - Removed open(int).
*
******************************************************************/
package org.cybergarage.upnp.ssdp;
import java.util.*;
import org.cybergarage.net.*;
import org.cybergarage.upnp.device.*;
public class SSDPSearchSocketList extends Vector
{
////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////
private static final long serialVersionUID = 4071292828166415028L;
public SSDPSearchSocketList()
{
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public SSDPSearchSocket getSSDPSearchSocket(int n)
{
return (SSDPSearchSocket)get(n);
}
public void addSearchListener(SearchListener listener)
{
int nServers = size();
for (int n=0; n<nServers; n++) {
SSDPSearchSocket sock = getSSDPSearchSocket(n);
sock.addSearchListener(listener);
}
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public boolean open()
{
int nHostAddrs = HostInterface.getNHostAddresses();
for (int n=0; n<nHostAddrs; n++) {
String bindAddr = HostInterface.getHostAddress(n);
SSDPSearchSocket ssdpSearchSocket = new SSDPSearchSocket(bindAddr);
add(ssdpSearchSocket);
}
return true;
}
public void close()
{
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPSearchSocket sock = getSSDPSearchSocket(n);
sock.close();
}
clear();
}
////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////
public void start()
{
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPSearchSocket sock = getSSDPSearchSocket(n);
sock.start();
}
}
public void stop()
{
int nSockets = size();
for (int n=0; n<nSockets; n++) {
SSDPSearchSocket sock = getSSDPSearchSocket(n);
sock.stop();
}
}
}

View File

@ -0,0 +1,57 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: ActionData.java
*
* Revision;
*
* 03/28/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.xml;
import org.cybergarage.upnp.control.*;
public class ActionData extends NodeData
{
public ActionData()
{
}
////////////////////////////////////////////////
// ActionListener
////////////////////////////////////////////////
private ActionListener actionListener = null;
public ActionListener getActionListener() {
return actionListener;
}
public void setActionListener(ActionListener actionListener) {
this.actionListener = actionListener;
}
////////////////////////////////////////////////
// ControlResponse
////////////////////////////////////////////////
private ControlResponse ctrlRes = null;
public ControlResponse getControlResponse()
{
return ctrlRes;
}
public void setControlResponse(ControlResponse res)
{
ctrlRes = res;
}
}

View File

@ -0,0 +1,41 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: ArgumentData.java
*
* Revision;
*
* 02/24/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.xml;
public class ArgumentData extends NodeData
{
public ArgumentData()
{
}
////////////////////////////////////////////////
// value
////////////////////////////////////////////////
private String value = "";
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
}

View File

@ -0,0 +1,170 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: DeviceData.java
*
* Revision;
*
* 03/28/03
* - first revision.
* 12/25/03
* - Added Advertiser functions.
*
******************************************************************/
package org.cybergarage.upnp.xml;
import java.io.*;
import org.cybergarage.util.*;
import org.cybergarage.http.*;
import org.cybergarage.upnp.*;
import org.cybergarage.upnp.ssdp.*;
import org.cybergarage.upnp.device.*;
public class DeviceData extends NodeData
{
public DeviceData()
{
}
////////////////////////////////////////////////
// description
////////////////////////////////////////////////
private String descriptionURI = null;
private File descriptionFile = null;
public File getDescriptionFile() {
return descriptionFile;
}
public String getDescriptionURI() {
return descriptionURI;
}
public void setDescriptionFile(File descriptionFile) {
this.descriptionFile = descriptionFile;
}
public void setDescriptionURI(String descriptionURI) {
this.descriptionURI = descriptionURI;
}
////////////////////////////////////////////////
// description
////////////////////////////////////////////////
private String location = "";
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
////////////////////////////////////////////////
// LeaseTime
////////////////////////////////////////////////
private int leaseTime = Device.DEFAULT_LEASE_TIME;
public int getLeaseTime()
{
return leaseTime;
}
public void setLeaseTime(int val)
{
leaseTime = val;
}
////////////////////////////////////////////////
// HTTPServer
////////////////////////////////////////////////
private HTTPServerList httpServerList = new HTTPServerList();
public HTTPServerList getHTTPServerList() {
return httpServerList;
}
////////////////////////////////////////////////
// httpPort
////////////////////////////////////////////////
private int httpPort = Device.HTTP_DEFAULT_PORT;
public int getHTTPPort() {
return httpPort;
}
public void setHTTPPort(int port) {
httpPort = port;
}
////////////////////////////////////////////////
// controlActionListenerList
////////////////////////////////////////////////
private ListenerList controlActionListenerList = new ListenerList();
public ListenerList getControlActionListenerList() {
return controlActionListenerList;
}
/*
public void setControlActionListenerList(ListenerList controlActionListenerList) {
this.controlActionListenerList = controlActionListenerList;
}
*/
////////////////////////////////////////////////
// SSDPSearchSocket
////////////////////////////////////////////////
private SSDPSearchSocketList ssdpSearchSocketList = new SSDPSearchSocketList();
public SSDPSearchSocketList getSSDPSearchSocketList() {
return ssdpSearchSocketList;
}
////////////////////////////////////////////////
// SSDPPacket
////////////////////////////////////////////////
private SSDPPacket ssdpPacket = null;
public SSDPPacket getSSDPPacket() {
return ssdpPacket;
}
public void setSSDPPacket(SSDPPacket packet) {
ssdpPacket = packet;
}
////////////////////////////////////////////////
// Advertiser
////////////////////////////////////////////////
private Advertiser advertiser = null;
public void setAdvertiser(Advertiser adv)
{
advertiser = adv;
}
public Advertiser getAdvertiser()
{
return advertiser;
}
}

View File

@ -0,0 +1,43 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: ActionData.java
*
* Revision;
*
* 03/28/03
* - first revision.
*
******************************************************************/
package org.cybergarage.upnp.xml;
import org.cybergarage.xml.*;
public class NodeData
{
public NodeData()
{
setNode(null);
}
////////////////////////////////////////////////
// Node
////////////////////////////////////////////////
private Node node;
public void setNode(Node node)
{
this.node = node;
}
public Node getNode()
{
return node;
}
}

View File

@ -0,0 +1,98 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: ServiceData.java
*
* Revision;
*
* 03/28/03
* - first revision.
* 01/06/04
* - Moved setQueryListener() and getQueryListener() to StateVariableData class.
* 03/30/05
* - Removed setDescriptionURL() and getDescriptionURL().
*
******************************************************************/
package org.cybergarage.upnp.xml;
import org.cybergarage.util.*;
import org.cybergarage.xml.*;
import org.cybergarage.upnp.event.*;
public class ServiceData extends NodeData
{
public ServiceData()
{
}
////////////////////////////////////////////////
// controlActionListenerList
////////////////////////////////////////////////
private ListenerList controlActionListenerList = new ListenerList();
public ListenerList getControlActionListenerList() {
return controlActionListenerList;
}
////////////////////////////////////////////////
// scpdNode
////////////////////////////////////////////////
private Node scpdNode = null;
public Node getSCPDNode() {
return scpdNode;
}
public void setSCPDNode(Node node) {
scpdNode = node;
}
////////////////////////////////////////////////
// SubscriberList
////////////////////////////////////////////////
private SubscriberList subscriberList = new SubscriberList();
public SubscriberList getSubscriberList() {
return subscriberList;
}
////////////////////////////////////////////////
// SID
////////////////////////////////////////////////
private String sid = "";
public String getSID() {
return sid;
}
public void setSID(String id) {
sid = id;
}
////////////////////////////////////////////////
// Timeout
////////////////////////////////////////////////
private long timeout = 0;
public long getTimeout()
{
return timeout;
}
public void setTimeout(long value)
{
timeout = value;
}
}

View File

@ -0,0 +1,73 @@
/******************************************************************
*
* CyberUPnP for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File:StateVariableData.java
*
* Revision;
*
* 02/05/03
* - first revision.
* 01/06/04
* - Added setQueryListener() and getQueryListener().
*
******************************************************************/
package org.cybergarage.upnp.xml;
import org.cybergarage.upnp.control.*;
public class StateVariableData extends NodeData
{
public StateVariableData()
{
}
////////////////////////////////////////////////
// value
////////////////////////////////////////////////
private String value = "";
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
////////////////////////////////////////////////
// QueryListener
////////////////////////////////////////////////
private QueryListener queryListener = null;
public QueryListener getQueryListener() {
return queryListener;
}
public void setQueryListener(QueryListener queryListener) {
this.queryListener = queryListener;
}
////////////////////////////////////////////////
// QueryResponse
////////////////////////////////////////////////
private QueryResponse queryRes = null;
public QueryResponse getQueryResponse()
{
return queryRes;
}
public void setQueryResponse(QueryResponse res)
{
queryRes = res;
}
}

View File

@ -0,0 +1,51 @@
/******************************************************************
*
* CyberUtil for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: Debug.java
*
* Revision;
*
* 11/18/02
* - first revision.
*
******************************************************************/
package org.cybergarage.util;
public final class Debug
{
public static boolean enabled = false;
public static final void on() {
enabled = true;
}
public static final void off() {
enabled = false;
}
public static boolean isOn() {
return enabled;
}
public static final void message(String s) {
if (enabled == true)
System.out.println("CyberGarage message : " + s);
}
public static final void message(String m1, String m2) {
if (enabled == true)
System.out.println("CyberGarage message : ");
System.out.println(m1);
System.out.println(m2);
}
public static final void warning(String s) {
System.out.println("CyberGarage warning : " + s);
}
public static final void warning(String m, Exception e) {
System.out.println("CyberGarage warning : " + m + " (" + e.getMessage() + ")");
}
public static final void warning(Exception e) {
warning(e.getMessage());
e.printStackTrace();
}
}

Some files were not shown because too many files have changed in this diff Show More