📄 upnpdevicefactory.java
字号:
package no.auc.one.portableplayer.communication;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.apache.log4j.*;
public abstract class UPnPDeviceFactory {
private static Logger LOG = Logger.getLogger("DevFac");
private UPnPDiscovery discoDevices = new UPnPDiscovery();
public void cancel() throws IOException {
discoDevices.cancel();
}
/**
* Searches for devices using UPnPDiscovery and retrieves description for
* the devices found.
*
* This method swaps the content of the Vector returned by UPnPDiscovery
* with a UPnPDevice.
*
* @param searchString String to be searched for.
* @param deviceHandler Handler for this kind of search string. I.e. it
* should be able to handle the devices expected to
* be found by this search.
* @return Devices found.
*/
protected Vector findUPnPDevice(
String searchString,
UPnPDeviceDescriptionHandler deviceHandler)
{
Vector devList = null;
Vector locationUrls = discoDevices.start(
searchString,
5); // max duration set to 5 seconds
if (locationUrls == null || locationUrls.size() <= 0) {
LOG.debug("No devices discovered.");
return null;
}
devList = locationUrls;
for(int i = 0; i < locationUrls.size(); i++) {
Object o = locationUrls.elementAt(i);
if (o == null || !(o instanceof String)) {
locationUrls.removeElementAt(i--);
continue;
}
String url = (String)o;
UPnPDevice dev = getUPnPDevice(url, deviceHandler);
if (dev == null) {
locationUrls.removeElementAt(i--);
} else {
devList.setElementAt(dev, i);
}
}
return devList;
}
public UPnPDevice getUPnPDevice(
String locationUrl,
UPnPDeviceDescriptionHandler deviceHandler)
{
UPnPDevice dev = null;
HttpConnection conn = null;
try{
conn = (HttpConnection)Connector.open(locationUrl);
conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("Content-Length","0");
SAXParserFactory pfactory = SAXParserFactory.newInstance();
SAXParser parser = pfactory.newSAXParser();
deviceHandler.setLocationUrl(locationUrl);
InputStream is = conn.openInputStream();
if (is != null) {
parser.parse(is, deviceHandler);
dev = deviceHandler.getDevice();
// Output the elements of this device :)
LOG.debug(
"Device found found " +
" Name:" + dev.friendlyName +
" URLBase:" + dev.urlBase);
}
} catch (ParserConfigurationException pce) {
LOG.debug("PCE exception in getUPnPDevice" + pce.toString());
pce.printStackTrace();
} catch (SAXException se) {
LOG.debug("SAXExceptio in getUPnPDevice" + se.toString());
se.printStackTrace();
} catch (ConnectionNotFoundException cnfe) {
LOG.debug("Connection not found. " + cnfe.toString());
cnfe.printStackTrace();
} catch (IOException ioe) {
LOG.debug("Error while getting device description. " + ioe.toString());
ioe.printStackTrace();
} catch (NullPointerException npe){
System.out.println("Nullpointerexception: " + npe);
npe.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
// Don't care. Do nothing
}
}
System.err.println("Finished getUPnPDevice");
}
return dev;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -