📄 upnphosting.java
字号:
headerPairsNtAndUsn[2] = new String[]{
"upnp:rootdevice",
device.udn() + "::upnp:rootdevice"};
int i = 0;
for(Enumeration e = serviceTable.elements();
e.hasMoreElements(); i ++) {
UPnPHostingService service =
(UPnPHostingService)e.nextElement();
headerPairsNtAndUsn[3 + i] = new String[]{
service.serviceType(),
device.udn() + "::" + service.serviceType()};
}
StringBuffer msg = new StringBuffer(256);
msg.append("NOTIFY * HTTP/1.1\r\n");
msg.append("HOST: 239.255.255.255:1900\r\n");
msg.append("CACHE-CONTROL: max-age = ");
msg.append(notifyPeriod);
msg.append("\r\n");
msg.append("LOCATION: http://");
msg.append(server.getLocalAddress());
msg.append(":");
msg.append(server.getLocalPort());
msg.append("/");
msg.append(SsdpHttpHandler.NAMESPACE_NAME);
msg.append("/");
msg.append(device.udn().replace(':', '_'));
msg.append("/devicedescription.xml\r\n");
String commonHeaders = msg.toString();
UDPDatagramConnection conn = (UDPDatagramConnection)Connector.open(
"datagram://239.255.255.250:1900");
for(i = 0; i < numMsgsNeeded; i++) {
StringBuffer notifyMsg = new StringBuffer(512);
notifyMsg.append(commonHeaders);
notifyMsg.append("NT: ");
notifyMsg.append(headerPairsNtAndUsn[i][0]);
notifyMsg.append("\r\nNTS: ssdp:alive\r\n");
notifyMsg.append("SERVER: MIDP/2.0 UPnP/1.0 ONEPP/1.2\r\n");
notifyMsg.append("USN: ");
notifyMsg.append(headerPairsNtAndUsn[i][1]);
notifyMsg.append("\r\n\r\n");
byte[] msgBytes = notifyMsg.toString().getBytes();
Datagram dgSend = conn.newDatagram(msgBytes, msgBytes.length);
conn.send(dgSend);
}
conn.close();
} catch(ConnectionNotFoundException cnfe) {
LOG.fatal(cnfe);
} catch(IOException ioe) {
LOG.fatal(ioe);
}
}
}
/**
* Internal class for keeping reference to the hosted devices in the
* record store.
*/
private final class HostingCacheContainer {
public Object entity;
public int recordId;
// public Hashtable serviceIds = new Hashtable(2);
public HostingCacheContainer(int id, Object o) {
recordId = id;
entity = o;
}
}
private String generateDeviceDescription(UPnPHostingDevice device) {
StringBuffer devDesc = new StringBuffer(2048);
try {
devDesc.append("<?xml version=\"1.0\"?>\r\n");
devDesc.append("<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\r\n");
devDesc.append(" <specVersion>\r\n");
devDesc.append(" <major>");
devDesc.append(device.specVersionMajor());
devDesc.append("</major>\r\n");
devDesc.append(" <minor>");
devDesc.append(device.specVersionMinor());
devDesc.append("</minor>\r\n");
devDesc.append(" </specVersion>\r\n");
devDesc.append(" <URLBase>http://");
devDesc.append(server.getLocalAddress());
devDesc.append(":");
devDesc.append(server.getLocalPort());
devDesc.append("/");
devDesc.append(SsdpHttpHandler.NAMESPACE_NAME);
devDesc.append("/");
devDesc.append(device.udn().replace(':', '_'));
devDesc.append("/</URLBase>\r\n");
devDesc.append(" <device>\r\n");
devDesc.append(" <deviceType>");
devDesc.append(device.deviceType());
devDesc.append("</deviceType>\r\n");
devDesc.append(" <friendlyName>");
devDesc.append(device.friendlyName());
devDesc.append("</friendlyName>\r\n");
devDesc.append(" <manufacturer>");
devDesc.append(device.manufacturer());
devDesc.append("</manufacturer>\r\n");
devDesc.append(" <manufacturerURL>");
devDesc.append(device.manufacturerUrl());
devDesc.append("</manufacturerURL>\r\n");
devDesc.append(" <modelDescription>");
devDesc.append(device.modelDescription());
devDesc.append("</modelDescription>\r\n");
devDesc.append(" <modelName>");
devDesc.append(device.modelName());
devDesc.append("</modelName>\r\n");
devDesc.append(" <modelNumber>");
devDesc.append(device.modelNumber());
devDesc.append("</modelNumber>\r\n");
devDesc.append(" <modelURL>");
devDesc.append(device.modelUrl());
devDesc.append("</modelURL>\r\n");
devDesc.append(" <serialNumber>");
devDesc.append(device.serialNumber());
devDesc.append("</serialNumber>\r\n");
devDesc.append(" <UDN>");
devDesc.append(device.udn());
devDesc.append("</UDN>\r\n");
devDesc.append(" <UPC>");
devDesc.append("</UPC>\r\n");
javax.microedition.lcdui.Image[] icons = device.iconList();
if (icons != null) {
devDesc.append(" <iconList>\r\n");
for (int i = 0; i < icons.length; i++) {
devDesc.append(" <icon>\r\n");
devDesc.append(" <mimetype>");
// XXX devDesc.append(icons[i].getMime());
devDesc.append("</mimetype>\r\n");
devDesc.append(" <width>");
devDesc.append(icons[i].getWidth());
devDesc.append("</width>\r\n");
devDesc.append(" <height>");
devDesc.append(icons[i].getHeight());
devDesc.append("</height>\r\n");
devDesc.append(" <depth>");
// XXX devDesc.append(icons[i].depth());
devDesc.append("</depth>\r\n");
devDesc.append(" <url>");
// XXX devDesc.append(icons[i].url());
devDesc.append("</url>\r\n");
devDesc.append(" </icon>");
}
devDesc.append(" </iconList>");
}
devDesc.append(" <serviceList>\r\n");
Hashtable serviceTable = device.serviceTable();
if (serviceTable == null || serviceTable.size() < 1) {
throw new IllegalArgumentException(
"Device must contain at least one service. Service list can not be null");
}
for (Enumeration e = serviceTable.elements();
e.hasMoreElements(); )
{
UPnPHostingService service = (UPnPHostingService)e.nextElement();
String specialServiceId = service.serviceId().replace(':', '_');
devDesc.append(" <service>\r\n");
devDesc.append(" <serviceType>");
devDesc.append(service.serviceType());
devDesc.append("</serviceType>\r\n");
devDesc.append(" <serviceId>");
devDesc.append(service.serviceId());
devDesc.append("</serviceId>\r\n");
devDesc.append(" <SCPDURL>");
devDesc.append("scpd/");
devDesc.append(specialServiceId);
devDesc.append("</SCPDURL>\r\n");
devDesc.append(" <controlURL>");
devDesc.append("control/");
devDesc.append(specialServiceId);
devDesc.append("</controlURL>\r\n");
devDesc.append(" <eventSubURL>");
devDesc.append("events/");
devDesc.append(specialServiceId);
devDesc.append("</eventSubURL>\r\n </service>\r\n");
}
devDesc.append(" </serviceList>\r\n");
//
// No support for embedded devices and presentation currently
//
devDesc.append(" </device>\r\n</root>");
} catch (IOException ioe) {
LOG.fatal(
"Error with connection when creating URLBase for device");
LOG.fatal(ioe);
}
return devDesc.toString();
}
private String generateServiceDescription(UPnPHostingService service) {
StringBuffer srvDesc = new StringBuffer(2048);
try {
srvDesc.append("<?xml version=\"1.0\"?>\r\n");
srvDesc.append("<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">\r\n");
srvDesc.append(" <specVersion>\r\n");
srvDesc.append(" <major>");
srvDesc.append(service.specVersionMajor());
srvDesc.append("</major>\r\n");
srvDesc.append(" <minor>");
srvDesc.append(service.specVersionMinor());
srvDesc.append("</minor>\r\n");
srvDesc.append(" </specVersion>\r\n");
//
// Service can include 0 or more actions
Hashtable actions = service.actionTable();
if (actions != null) {
srvDesc.append(" <actionList>\r\n");
for (Enumeration e = actions.elements();
e.hasMoreElements(); )
{
UPnPAction action = (UPnPAction)e.nextElement();
srvDesc.append(" <action>\r\n");
srvDesc.append(" <name>");
//TODO Check if name < 32 characters
srvDesc.append(action.getName());
srvDesc.append("</name>\r\n");
// Each action may have >= 0 arguments
//
UPnPActionArgument[] args = action.getArgumentList();
if (args != null) {
srvDesc.append(" <argumentList>\r\n");
for (int j = 0; j < args.length; j++) {
srvDesc.append(" <argument>\r\n");
srvDesc.append(" <name>");
srvDesc.append(args[j].getName());
srvDesc.append("</name>\r\n");
srvDesc.append(" <direction>");
if (UPnPActionArgument.ARGUMENT_DIRECTION_IN ==
args[j].getDirection())
{
srvDesc.append("in");
} else {
srvDesc.append("out");
}
srvDesc.append("</direction>\r\n");
if (args[j].isReturnValue()) {
srvDesc.append(" <retval/>\r\n");
}
srvDesc.append(" <relatedStateVariable>");
srvDesc.append(args[j].getRelatedStateVariable());
srvDesc.append("</relatedStateVariable>\r\n");
srvDesc.append(" </argument>\r\n");
}
srvDesc.append(" </argumentList>\r\n");
}
srvDesc.append(" </action>\r\n");
}
srvDesc.append(" </actionList>\r\n");
}
UPnPStateVariable[] stateVariables = service.stateVariableList();
if (null == stateVariables) {
throw new IllegalArgumentException(
"stateVariableList can not be null. Service must contain " +
"at least one state variable. ");
}
srvDesc.append(" <serviceStateTable>\r\n");
for (int i = 0; i < stateVariables.length; i++) {
srvDesc.append(" <stateVariable sendEvents=\"");
srvDesc.append(
stateVariables[i].isEvented() ? "yes" : "no");
srvDesc.append("\">\r\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -