Azureus源码剖析(一)
整个项目运行的起点在com.aelitis.azureus.ui.Main这个类中,它只有一个main主方法,运用Java的反射机制来调用真正的起始点类org.gudy.azureus2.ui.swt.Main的实例对象。
代码如下:
final Class startupClass = Class.forName("org.gudy.azureus2.ui.swt.Main");
final Constructor constructor = startupClass.getConstructor(new Class[] {
String[].class
});
constructor.newInstance(new Object[] {
args
});
final Constructor constructor = startupClass.getConstructor(new Class[] {
String[].class
});
constructor.newInstance(new Object[] {
args
});
而在org.gudy.azureus2.ui.swt.Main这个类中有一个成员变量
StartServer startServer
它是一个监听服务器,对本地的6880端口进行监听,监听的对象是torrent种子文件的URL地址,在它的构造函数中有如下语句:
socket = new ServerSocket(6880, 50, InetAddress.getByName("127.0.0.1")); //种子文件监听服务器
state = STATE_LISTENING; //服务器状态设置为“监听中”
state = STATE_LISTENING; //服务器状态设置为“监听中”
对此我们来做个小实验,通过tcp连接到Azureus的种子文件监听服务器上,向它传递待下载的种子文件列表,从而让Azureus去下载。
package com.vista.test;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* @author phinecos
* @since 2009-5-6
*
*/
public class MyClass
{
private String[] files = null;
public MyClass(String[] files)
{
this.files = files;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
for (String file: files)
{
sb.append(file);
}
return sb.toString();
}
public void sendMessage()
{
Socket socket = null;
PrintWriter pw = null;
String msg = "StartSocket: passing startup args to already-running Azureus java process listening on [127.0.0.1: 6880]";
String IpAddr = "127.0.0.1";
int port = 6880;
final String ACCESS_STRING = "Azureus Start Server Access";
try
{
socket = new Socket(IpAddr,port);
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF8"));
StringBuffer buffer = new StringBuffer(ACCESS_STRING + ";args;");
for (int i = 0; i < files.length; ++i)
{
String file = files[i].replaceAll("&","&&").replaceAll(";","&;");//字符转义
buffer.append(file);
buffer.append(';');
}
pw.println(buffer.toString());
pw.flush();
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
if (pw != null)
pw.close();
}
catch(Exception e){}
try
{
if (socket != null)
socket.close();
}
catch (IOException e) {}
}
}
}
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* @author phinecos
* @since 2009-5-6
*
*/
public class MyClass
{
private String[] files = null;
public MyClass(String[] files)
{
this.files = files;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
for (String file: files)
{
sb.append(file);
}
return sb.toString();
}
public void sendMessage()
{
Socket socket = null;
PrintWriter pw = null;
String msg = "StartSocket: passing startup args to already-running Azureus java process listening on [127.0.0.1: 6880]";
String IpAddr = "127.0.0.1";
int port = 6880;
final String ACCESS_STRING = "Azureus Start Server Access";
try
{
socket = new Socket(IpAddr,port);
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF8"));
StringBuffer buffer = new StringBuffer(ACCESS_STRING + ";args;");
for (int i = 0; i < files.length; ++i)
{
String file = files[i].replaceAll("&","&&").replaceAll(";","&;");//字符转义
buffer.append(file);
buffer.append(';');
}
pw.println(buffer.toString());
pw.flush();
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
if (pw != null)
pw.close();
}
catch(Exception e){}
try
{
if (socket != null)
socket.close();
}
catch (IOException e) {}
}
}
}
package com.vista.test;
import java.lang.reflect.Constructor;
/**
*
* @author phinecos
* @since 2009-5-6
*
*/
public class Main
{
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args)
{
try
{
final Class myClass = Class.forName("com.vista.test.MyClass");
final Constructor myConstructor = myClass.getConstructor(new Class[]{String[].class});
String[] torrents = {"c:\\1001.torrent", "c:\\1002.torrent"};//种子文件列表
MyClass c1 = (MyClass)myConstructor.newInstance(new Object[]{torrents});
System.out.println(c1.toString());
c1.sendMessage();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
先启动Azureus,再使用此用例进行测试,结果如下图,这表明这两个种子文件都被解析并开始下载了。
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2009-05-06 18:40 Phinecos(洞庭散人) 阅读(2444) 评论(0) 编辑 收藏 举报