upnpimpl.java
来自「Azureus is a powerful, full-featured, cr」· Java 代码 · 共 738 行 · 第 1/2 页
JAVA
738 行
if ( USE_HTTP_CONNECTION ){
HttpURLConnection con = (HttpURLConnection)control.openConnection();
con.setRequestProperty( "SOAPAction", "\""+ soap_action + "\"");
con.setRequestProperty( "Content-Type", "text/xml; charset=\"utf-8\"" );
con.setRequestProperty( "User-Agent", "Azureus (UPnP/1.0)" );
con.setRequestMethod( "POST" );
con.setDoInput( true );
con.setDoOutput( true );
OutputStream os = con.getOutputStream();
PrintWriter pw = new PrintWriter( new OutputStreamWriter(os, "UTF-8" ));
pw.println( request );
pw.flush();
con.connect();
if ( con.getResponseCode() == 405 ){
// gotta retry with M-POST method
con = (HttpURLConnection)control.openConnection();
con.setRequestProperty( "Content-Type", "text/xml; charset=\"utf-8\"" );
con.setRequestMethod( "M-POST" );
con.setRequestProperty( "MAN", "\"http://schemas.xmlsoap.org/soap/envelope/\"; ns=01" );
con.setRequestProperty( "01-SOAPACTION", "\""+ soap_action + "\"");
con.setDoInput( true );
con.setDoOutput( true );
os = con.getOutputStream();
pw = new PrintWriter( new OutputStreamWriter(os, "UTF-8" ));
pw.println( request );
pw.flush();
con.connect();
return( parseXML(con.getInputStream()));
}else{
return( parseXML(con.getInputStream()));
}
}else{
Socket socket = new Socket(control.getHost(), control.getPort());
PrintWriter pw = new PrintWriter(new OutputStreamWriter( socket.getOutputStream(), "UTF8" ));
String url_target = control.toString();
int p1 = url_target.indexOf( "://" ) + 3;
p1 = url_target.indexOf( "/", p1 );
url_target = url_target.substring( p1 );
pw.print( "POST " + url_target + " HTTP/1.1" + NL );
pw.print( "Content-Type: text/xml; charset=\"utf-8\"" + NL );
pw.print( "SOAPAction: \"" + soap_action + "\"" + NL );
pw.print( "User-Agent: Azureus (UPnP/1.0)" + NL );
pw.print( "Host: " + control.getHost() + NL );
pw.print( "Content-Length: " + request.getBytes( "UTF8" ).length + NL );
pw.print( "Connection: Keep-Alive" + NL );
pw.print( "Pragma: no-cache" + NL + NL );
pw.print( request );
pw.flush();
InputStream is = socket.getInputStream();
String reply_header = "";
while(true){
byte[] buffer = new byte[1];
if ( is.read( buffer ) <= 0 ){
throw( new IOException( "Premature end of input stream" ));
}
reply_header += (char)buffer[0];
if ( reply_header.endsWith( NL+NL )){
break;
}
}
p1 = reply_header.indexOf( NL );
String first_line = reply_header.substring( 0, p1 ).trim();
if ( first_line.indexOf( "200" ) == -1 ){
throw( new IOException( "HTTP request failed:" + first_line ));
}
return( parseXML( is ));
}
}
protected File
getTraceFile()
{
try{
this_mon.enter();
trace_index++;
if ( trace_index == 6 ){
trace_index = 1;
}
return( new File( plugin_interface.getUtilities().getAzureusUserDir(), "upnp_trace" + trace_index + ".log" ));
}finally{
this_mon.exit();
}
}
public PluginInterface
getPluginInterface()
{
return( plugin_interface );
}
public void
reportActivity(
ResourceDownloader downloader,
String activity )
{
log( activity );
}
public void
failed(
ResourceDownloader downloader,
ResourceDownloaderException e )
{
log( e );
}
public void
log(
Throwable e )
{
log( e.toString());
}
public void
log(
String str )
{
List old_listeners;
try{
this_mon.enter();
old_listeners = new ArrayList(log_listeners);
log_history.add( str );
if ( log_history.size() > 32 ){
log_history.remove(0);
}
}finally{
this_mon.exit();
}
for (int i=0;i<old_listeners.size();i++){
((UPnPLogListener)old_listeners.get(i)).log( str );
}
}
public void
addLogListener(
UPnPLogListener l )
{
List old_logs;
try{
this_mon.enter();
old_logs = new ArrayList(log_history);
log_listeners.add( l );
}finally{
this_mon.exit();
}
for (int i=0;i<old_logs.size();i++){
l.log((String)old_logs.get(i));
}
}
public void
removeLogListener(
UPnPLogListener l )
{
log_listeners.remove( l );
}
public void
addRootDeviceListener(
UPnPListener l )
{
List old_locations;
try{
this_mon.enter();
old_locations = new ArrayList(root_locations.values());
rd_listeners.add( l );
}finally{
this_mon.exit();
}
for (int i=0;i<old_locations.size();i++){
l.rootDeviceFound(((UPnPRootDevice)old_locations.get(i)));
}
}
public void
removeRootDeviceListener(
UPnPListener l )
{
rd_listeners.remove( l );
}
public static void
main(
String[] args )
{
try{
UPnP upnp = UPnPFactory.getSingleton(null); // won't work with null ....
upnp.addRootDeviceListener(
new UPnPListener()
{
public void
rootDeviceFound(
UPnPRootDevice device )
{
try{
processDevice( device.getDevice() );
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}
});
upnp.addLogListener(
new UPnPLogListener()
{
public void
log(
String str )
{
System.out.println( str );
}
});
Thread.sleep(20000);
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}
protected static void
processDevice(
UPnPDevice device )
throws UPnPException
{
if ( device.getDeviceType().equalsIgnoreCase("urn:schemas-upnp-org:device:WANConnectionDevice:1")){
System.out.println( "got device");
UPnPService[] services = device.getServices();
for (int i=0;i<services.length;i++){
UPnPService s = services[i];
if ( s.getServiceType().equalsIgnoreCase( "urn:schemas-upnp-org:service:WANIPConnection:1")){
System.out.println( "got service" );
UPnPAction[] actions = s.getActions();
for (int j=0;j<actions.length;j++){
System.out.println( actions[j].getName());
}
UPnPStateVariable[] vars = s.getStateVariables();
for (int j=0;j<vars.length;j++){
System.out.println( vars[j].getName());
}
UPnPStateVariable noe = s.getStateVariable("PortMappingNumberOfEntries");
System.out.println( "noe = " + noe.getValue());
UPnPWANIPConnection wan_ip = (UPnPWANIPConnection)s.getSpecificService();
UPnPWANConnectionPortMapping[] ports = wan_ip.getPortMappings();
wan_ip.addPortMapping( true, 7007, "Moo!" );
UPnPAction act = s.getAction( "GetGenericPortMappingEntry" );
UPnPActionInvocation inv = act.getInvocation();
inv.addArgument( "NewPortMappingIndex", "0" );
UPnPActionArgument[] outs = inv.invoke();
for (int j=0;j<outs.length;j++){
System.out.println( outs[j].getName() + " = " + outs[j].getValue());
}
}
}
}else{
UPnPDevice[] kids = device.getSubDevices();
for (int i=0;i<kids.length;i++){
processDevice( kids[i] );
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?