forked from I2P_Developers/i2p.i2p
Cyberlink for Java v2.1 (2011-09-16) from SVN,
with extra parsers in xml/parsers removed. Diverging from original v1.7 checkin in prep for merging.
This commit is contained in:
@@ -5,11 +5,11 @@
|
||||
* Copyright (C) Satoshi Konno 2002-2004
|
||||
*
|
||||
* File: ControlPoint.java
|
||||
*
|
||||
* Revision:
|
||||
*
|
||||
* 11/18/02
|
||||
* - first revision.
|
||||
*
|
||||
* Revision:
|
||||
*
|
||||
* 11/18/02
|
||||
* - first revision.
|
||||
* 05/13/03
|
||||
* - Changed to create socket threads each local interfaces.
|
||||
* (HTTP, SSDPNotiry, SSDPSerachResponse)
|
||||
@@ -54,37 +54,62 @@
|
||||
* the ControlPoint base class adds/removes a UPnP device
|
||||
* 03/30/05
|
||||
* - Changed addDevice() to use Parser::parse(URL).
|
||||
* 04/12/06
|
||||
* - Added setUserData() and getUserData() to set a user original data object.
|
||||
*
|
||||
*******************************************************************/
|
||||
|
||||
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.*;
|
||||
package org.cybergarage.upnp;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
public class ControlPoint implements HTTPRequestListener
|
||||
{
|
||||
import java.net.InetAddress;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.cybergarage.http.HTTPRequest;
|
||||
import org.cybergarage.http.HTTPRequestListener;
|
||||
import org.cybergarage.http.HTTPServerList;
|
||||
import org.cybergarage.net.HostInterface;
|
||||
import org.cybergarage.upnp.control.RenewSubscriber;
|
||||
import org.cybergarage.upnp.device.DeviceChangeListener;
|
||||
import org.cybergarage.upnp.device.Disposer;
|
||||
import org.cybergarage.upnp.device.NotifyListener;
|
||||
import org.cybergarage.upnp.device.ST;
|
||||
import org.cybergarage.upnp.device.SearchResponseListener;
|
||||
import org.cybergarage.upnp.device.USN;
|
||||
import org.cybergarage.upnp.event.EventListener;
|
||||
import org.cybergarage.upnp.event.NotifyRequest;
|
||||
import org.cybergarage.upnp.event.Property;
|
||||
import org.cybergarage.upnp.event.PropertyList;
|
||||
import org.cybergarage.upnp.event.Subscription;
|
||||
import org.cybergarage.upnp.event.SubscriptionRequest;
|
||||
import org.cybergarage.upnp.event.SubscriptionResponse;
|
||||
import org.cybergarage.upnp.ssdp.SSDP;
|
||||
import org.cybergarage.upnp.ssdp.SSDPNotifySocketList;
|
||||
import org.cybergarage.upnp.ssdp.SSDPPacket;
|
||||
import org.cybergarage.upnp.ssdp.SSDPSearchRequest;
|
||||
import org.cybergarage.upnp.ssdp.SSDPSearchResponseSocketList;
|
||||
import org.cybergarage.util.Debug;
|
||||
import org.cybergarage.util.ListenerList;
|
||||
import org.cybergarage.util.Mutex;
|
||||
import org.cybergarage.xml.Node;
|
||||
import org.cybergarage.xml.NodeList;
|
||||
import org.cybergarage.xml.Parser;
|
||||
import org.cybergarage.xml.ParserException;
|
||||
|
||||
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;
|
||||
////////////////////////////////////////////////
|
||||
// Member
|
||||
////////////////////////////////////////////////
|
||||
|
||||
private SSDPNotifySocketList ssdpNotifySocketList;
|
||||
private SSDPSearchResponseSocketList ssdpSearchResponseSocketList;
|
||||
|
||||
private SSDPNotifySocketList getSSDPNotifySocketList()
|
||||
{
|
||||
@@ -105,14 +130,13 @@ public class ControlPoint implements HTTPRequestListener
|
||||
UPnP.initialize();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// Constructor
|
||||
////////////////////////////////////////////////
|
||||
|
||||
public ControlPoint(int ssdpPort, int httpPort)
|
||||
{
|
||||
ssdpNotifySocketList = new SSDPNotifySocketList();
|
||||
ssdpSearchResponseSocketList = new SSDPSearchResponseSocketList();
|
||||
////////////////////////////////////////////////
|
||||
// Constructor
|
||||
////////////////////////////////////////////////
|
||||
|
||||
public ControlPoint(int ssdpPort, int httpPort,InetAddress[] binds){
|
||||
ssdpNotifySocketList = new SSDPNotifySocketList(binds);
|
||||
ssdpSearchResponseSocketList = new SSDPSearchResponseSocketList(binds);
|
||||
|
||||
setSSDPPort(ssdpPort);
|
||||
setHTTPPort(httpPort);
|
||||
@@ -124,7 +148,11 @@ public class ControlPoint implements HTTPRequestListener
|
||||
|
||||
setNMPRMode(false);
|
||||
setRenewSubscriber(null);
|
||||
}
|
||||
}
|
||||
|
||||
public ControlPoint(int ssdpPort, int httpPort){
|
||||
this(ssdpPort,httpPort,null);
|
||||
}
|
||||
|
||||
public ControlPoint()
|
||||
{
|
||||
@@ -179,7 +207,7 @@ public class ControlPoint implements HTTPRequestListener
|
||||
public void setHTTPPort(int port) {
|
||||
httpPort = port;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// NMPR
|
||||
////////////////////////////////////////////////
|
||||
@@ -196,35 +224,35 @@ public class ControlPoint implements HTTPRequestListener
|
||||
return nmprMode;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// Device List
|
||||
////////////////////////////////////////////////
|
||||
|
||||
private NodeList devNodeList = new NodeList();
|
||||
|
||||
private void addDevice(Node rootNode)
|
||||
{
|
||||
devNodeList.add(rootNode);
|
||||
}
|
||||
|
||||
private synchronized void addDevice(SSDPPacket ssdpPacket)
|
||||
////////////////////////////////////////////////
|
||||
// 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);
|
||||
String usn = ssdpPacket.getUSN();
|
||||
String udn = USN.getUDN(usn);
|
||||
Device dev = getDevice(udn);
|
||||
if (dev != null) {
|
||||
dev.setSSDPPacket(ssdpPacket);
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
String location = ssdpPacket.getLocation();
|
||||
try {
|
||||
String location = ssdpPacket.getLocation();
|
||||
try {
|
||||
URL locationUrl = new URL(location);
|
||||
Parser parser = UPnP.getXMLParser();
|
||||
Node rootNode = parser.parse(locationUrl);
|
||||
Node rootNode = parser.parse(locationUrl);
|
||||
Device rootDev = getDevice(rootNode);
|
||||
if (rootDev == null)
|
||||
return;
|
||||
@@ -237,16 +265,16 @@ public class ControlPoint implements HTTPRequestListener
|
||||
// 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) {
|
||||
catch (ParserException pe) {
|
||||
Debug.warning(ssdpPacket.toString());
|
||||
Debug.warning(pe);
|
||||
}
|
||||
}
|
||||
Debug.warning(pe);
|
||||
}
|
||||
}
|
||||
|
||||
private Device getDevice(Node rootNode)
|
||||
{
|
||||
@@ -257,26 +285,26 @@ public class ControlPoint implements HTTPRequestListener
|
||||
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++) {
|
||||
|
||||
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);
|
||||
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;
|
||||
@@ -284,18 +312,18 @@ public class ControlPoint implements HTTPRequestListener
|
||||
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)
|
||||
{
|
||||
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
|
||||
@@ -304,29 +332,29 @@ public class ControlPoint implements HTTPRequestListener
|
||||
if( dev != null && dev.isRootDevice() )
|
||||
performRemoveDeviceListener( dev );
|
||||
|
||||
devNodeList.remove(rootNode);
|
||||
}
|
||||
|
||||
private void removeDevice(Device dev)
|
||||
devNodeList.remove(rootNode);
|
||||
}
|
||||
|
||||
protected 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);
|
||||
protected 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);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
@@ -353,7 +381,7 @@ public class ControlPoint implements HTTPRequestListener
|
||||
|
||||
public void setExpiredDeviceMonitoringInterval(long interval)
|
||||
{
|
||||
expiredDeviceMonitoringInterval = interval;
|
||||
expiredDeviceMonitoringInterval = interval;
|
||||
}
|
||||
|
||||
public long getExpiredDeviceMonitoringInterval()
|
||||
@@ -371,9 +399,9 @@ public class ControlPoint implements HTTPRequestListener
|
||||
return deviceDisposer;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
// Notify
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
private ListenerList deviceNotifyListenerList = new ListenerList();
|
||||
|
||||
@@ -392,10 +420,14 @@ public class ControlPoint implements HTTPRequestListener
|
||||
int listenerSize = deviceNotifyListenerList.size();
|
||||
for (int n=0; n<listenerSize; n++) {
|
||||
NotifyListener listener = (NotifyListener)deviceNotifyListenerList.get(n);
|
||||
listener.deviceNotifyReceived(ssdpPacket);
|
||||
try{
|
||||
listener.deviceNotifyReceived(ssdpPacket);
|
||||
}catch(Exception e){
|
||||
Debug.warning("NotifyListener returned an error:", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// SearchResponse
|
||||
////////////////////////////////////////////////
|
||||
@@ -417,7 +449,13 @@ public class ControlPoint implements HTTPRequestListener
|
||||
int listenerSize = deviceSearchResponseListenerList.size();
|
||||
for (int n=0; n<listenerSize; n++) {
|
||||
SearchResponseListener listener = (SearchResponseListener)deviceSearchResponseListenerList.get(n);
|
||||
listener.deviceSearchResponseReceived(ssdpPacket);
|
||||
try{
|
||||
listener.deviceSearchResponseReceived(ssdpPacket);
|
||||
}catch(Exception e){
|
||||
Debug.warning("SearchResponseListener returned an error:", e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -465,10 +503,11 @@ public class ControlPoint implements HTTPRequestListener
|
||||
public void notifyReceived(SSDPPacket packet)
|
||||
{
|
||||
if (packet.isRootDevice() == true) {
|
||||
if (packet.isAlive() == true)
|
||||
if (packet.isAlive() == true){
|
||||
addDevice(packet);
|
||||
if (packet.isByeBye() == true)
|
||||
}else if (packet.isByeBye() == true){
|
||||
removeDevice(packet);
|
||||
}
|
||||
}
|
||||
performNotifyListener(packet);
|
||||
}
|
||||
@@ -479,12 +518,12 @@ public class ControlPoint implements HTTPRequestListener
|
||||
addDevice(packet);
|
||||
performSearchResponseListener(packet);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// M-SEARCH
|
||||
////////////////////////////////////////////////
|
||||
|
||||
private int searchMx = SSDP.DEFAULT_MSEARCH_MX;
|
||||
////////////////////////////////////////////////
|
||||
// M-SEARCH
|
||||
////////////////////////////////////////////////
|
||||
|
||||
private int searchMx = SSDP.DEFAULT_MSEARCH_MX;
|
||||
|
||||
public int getSearchMx()
|
||||
{
|
||||
@@ -711,7 +750,7 @@ public class ControlPoint implements HTTPRequestListener
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// getSubscriberService
|
||||
////////////////////////////////////////////////
|
||||
@@ -769,11 +808,11 @@ public class ControlPoint implements HTTPRequestListener
|
||||
return renewSubscriber;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// run
|
||||
////////////////////////////////////////////////
|
||||
|
||||
public boolean start(String target, int mx)
|
||||
////////////////////////////////////////////////
|
||||
// run
|
||||
////////////////////////////////////////////////
|
||||
|
||||
public boolean start(String target, int mx)
|
||||
{
|
||||
stop();
|
||||
|
||||
@@ -803,7 +842,7 @@ public class ControlPoint implements HTTPRequestListener
|
||||
return false;
|
||||
ssdpNotifySocketList.setControlPoint(this);
|
||||
ssdpNotifySocketList.start();
|
||||
|
||||
|
||||
////////////////////////////////////////
|
||||
// SeachResponse Socket
|
||||
////////////////////////////////////////
|
||||
@@ -845,9 +884,9 @@ public class ControlPoint implements HTTPRequestListener
|
||||
renewSub.start();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean start(String target)
|
||||
{
|
||||
return start(target, SSDP.DEFAULT_MSEARCH_MX);
|
||||
@@ -858,10 +897,10 @@ public class ControlPoint implements HTTPRequestListener
|
||||
return start(ST.ROOT_DEVICE, SSDP.DEFAULT_MSEARCH_MX);
|
||||
}
|
||||
|
||||
public boolean stop()
|
||||
public boolean stop()
|
||||
{
|
||||
unsubscribe();
|
||||
|
||||
|
||||
SSDPNotifySocketList ssdpNotifySocketList = getSSDPNotifySocketList();
|
||||
ssdpNotifySocketList.stop();
|
||||
ssdpNotifySocketList.close();
|
||||
@@ -897,9 +936,25 @@ public class ControlPoint implements HTTPRequestListener
|
||||
setRenewSubscriber(null);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// userData
|
||||
////////////////////////////////////////////////
|
||||
|
||||
private Object userData = null;
|
||||
|
||||
public void setUserData(Object data)
|
||||
{
|
||||
userData = data;
|
||||
}
|
||||
|
||||
public Object getUserData()
|
||||
{
|
||||
return userData;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// print
|
||||
////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user