JXTA P2P共享--管道

P2P文件共享实现:
--管道篇
1. 用netbeans创建两个项目P2PClient,P2PServer,分别创建包javax.p2p.client,javax.p2p.server,现在就可以将java文件放到包中。
2. 向项目里添加库文件
a) bcprov-jdk14.jar
b) bcprov-jdk16-138.jar
c) javax.servlet.jar
d) jdom.jar
e) jxta.jar
f) jxtaext.jar
g) log4j.jar
h) org.mortbay.jetty.jar
i) swixml.jar
3. 现在可以进行开发了。
4. 利用pipe建立通讯
PipeServer:
实现接口:
public class PipeServer implements PipeMsgListener这样它就可以处理管道消息了。
必须重载public void pipeMsgEvent(PipeMsgEvent event)来实现消息的处理
重载的函数下面会讲到
//类中的对象
static PeerGroup netPeerGroup = null; //对等组对象
transient NetworkManager manager; //网络管理器(新),p2p网络的管理
private PipeService pipeService; //管道服务
private PipeAdvertisement pipeAdv; //管道广告
private InputPipe inputPipe = null; //输入管道
main()直接调用PipeServer的构造函数,然后调用它的start方法
//代码见下
PipeServer server = new PipeServer();
server.start();
//查看构造函数
manager = null;
try {
manager = new net.jxta.platform.NetworkManager (NetworkManager.ConfigMode.ADHOC,"PipeServer",
new File(new File(".cache"), "PipeServer").toURI());
//初始化网络管理器,参数为模式,名称,保存位置
manager.startNetwork(); //开始网络服务
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
netPeerGroup = manager.getNetPeerGroup(); //利用管理器得到对等组
pipeService = netPeerGroup.getPipeService(); //利用对等组得到管道服务
pipeAdv = PipeClient.getPipeAdvertisement(); //得到要广播的广告
//start()
try {
System.out.println("Creating input pipe");
inputPipe = pipeService.createInputPipe(pipeAdv, this); //得到输入管道
} catch (IOException io) {
io.printStackTrace();
return;
}

if (inputPipe == null) { //没有得到输入管道
System.out.println(" cannot open InputPipe");
System.exit(-1);
}

System.out.println("Waiting for msgs on input pipe");
//stop() 这个函数并没有在程序中被调用,因为一直在监听,没有停止
public void stop() {
// Close the input pipe
inputPipe.close(); //关闭输入管道
// Stop JXTA
manager.stopNetwork(); //停止网络
}
//下面讲消息处理函数
public void pipeMsgEvent(PipeMsgEvent event) {
Message msg;
try {
// Obtain the message from the event
msg = event.getMessage(); //根据event获得管道消息
if (msg == null) { //如果获得空消息的话
System.out.println("Received an empty message");
return;
}
// dump the message content to screen
printMessageStats(msg, true); //调用函数打印出消息
} catch (Exception e) {
e.printStackTrace();
return;
}
// get all the message elements
Message.ElementIterator en = msg.getMessageElements();
//获得消息的所有元素
if (!en.hasNext()) { //只进行一次遍历
return;
}
// get the message element in the name space PipeClient.MESSAGE_NAME_SPACE
MessageElement msgElement = msg.getMessageElement(null, PipeClient.MESSAGE_NAME_SPACE);
// 获得PipeClient.MESSAGE_NAME_SPACE段的数据,
// 即消息的创建时间
// 对应于PipeClient发送消息时这一个数据段
// Get message
if (msgElement.toString() == null) { //如果获得一个空数据
System.out.println("null msg received");
} else {
Date date = new Date(System.currentTimeMillis());
System.out.println("Message received at :" + date.toString());
//打印出当前时间
System.out.println("Message created at :" + msgElement.toString());
//打印出创建时间,toString已经被重载
}
}
//打印消息的函数,verbose表示是否把消息体打印出来
public static void printMessageStats(Message msg, boolean verbose) {
try {
CountingOutputStream cnt;
ElementIterator it = msg.getMessageElements();
//和上面相同,获得消息的所有元素
System.out.println("------------------Begin Message---------------------");
WireFormatMessage serialed = WireFormatMessageFactory.toWire(msg, new MimeMediaType("application/x-jxta-msg"), null); //包装类
System.out.println("Message Size :" + serialed.getByteLength());
//获得消息的大小
while (it.hasNext()) { //遍历消息
MessageElement el = it.next(); //得到消息元素
String eName = el.getElementName(); //得到消息元素的名称
cnt = new CountingOutputStream(new DevNullOutputStream());
//创建一个输出流
el.sendToStream(cnt); //将消息发送到输出流里
long size = cnt.getBytesWritten(); //得到消息元素的长度
System.out.println("Element " + eName + " : " + size);
if (verbose) {
System.out.println("[" + el + "]");
}
}
System.out.println("-------------------End Message----------------------");
} catch (Exception e) {
e.printStackTrace();
}
}
以上是Server的实现,下面来讲Client的实现
PipeClient:
实现接口:
class PipeClient implements OutputPipeListener 在创建输出管道后实现特定功能
必须重载public void outputPipeEvent(OutputPipeEvent event) 来实现,在调用了pipeService.createOutputPipe(pipeAdv, this)后去调用这个函数。
//对象
public final static String MESSAGE_NAME_SPACE = "PipeTutorial";
//管道名,未来会被server使用
private boolean waitForRendezvous = false; //系统变量
private PipeService pipeService; //管道服务
private PipeAdvertisement pipeAdv; //管道广告
private OutputPipe outputPipe; //输出管道
private final Object lock = new Object(); //用于同步的对象
private NetworkManager manager; //网络管理器
public final static String PIPEIDSTR =
"urn:jxta:uuid-59616261646162614E50472050325033C0C1DE89719B456691A596B983BA0E1004"; //预定义管道ID
// main()
String value = System.getProperty("RDVWAIT", "false");
//获得系统属性RDVWAIT,如果没有此属性,默认值为false
boolean waitForRendezvous = Boolean.valueOf(value);
PipeClient client = new PipeClient(waitForRendezvous);
client.start();
//构造函数
this.waitForRendezvous = waitForRendezvous;
try {
manager = new net.jxta.platform.NetworkManager (NetworkManager.ConfigMode.ADHOC, "PipeClient",
new File(new File(".cache"), "PipeClient").toURI());
//实例化网络管理器,和server端相同
manager.startNetwork(); //启动服务
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}

pipeService = manager.getNetPeerGroup().getPipeService(); //获得管道服务
pipeAdv = getPipeAdvertisement(); //实例化要广播的管道广告
//Start()
try {
if (waitForRendezvous) { //默认是false,所以不会执行
System.out.println("Waiting for Rendezvous Connection");
manager.waitForRendezvousConnection(0);
System.out.println("Connected to Rendezvous, attempting to create a OutputPipe");
}

pipeService.createOutputPipe(pipeAdv, this); //创建输出管道
//自动执行outputPipeEvent
try {
synchronized (lock) {
lock.wait();
}
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
}
} catch (IOException e) {
System.out.println("OutputPipe creation failure");
e.printStackTrace();
System.exit(-1);
}
//输出管道事件outputPipeEvent
public void outputPipeEvent(OutputPipeEvent event) {
System.out.println("Received the output pipe resolution event");
outputPipe = event.getOutputPipe(); //从event里得到输出管道
Message msg;
try {
System.out.println("Sending message");
msg = new Message(); //新建一个消息
Date date = new Date(System.currentTimeMillis());
//得到当前时间,放到消息的
//MESSAGE_NAME_SPACE元素里
// add a string message element with the current date
StringMessageElement sme = new StringMessageElement(MESSAGE_NAME_SPACE, date.toString(), null);
//实例化一个消息元素
msg.addMessageElement(null, sme); //将消息元素放到消息里
// send the message
outputPipe.send(msg); //利用输出管道发送消息
System.out.println("message sent");
} catch (IOException e) {
System.out.println("failed to send message");
e.printStackTrace();
System.exit(-1);
}
stop(); //停止服务
}
//stop
public void stop() {
// Close the output pipe
outputPipe.close(); //关闭输出管道
// Stop JXTA
manager.stopNetwork(); //停止管理器
synchronized (lock) {
// done.
lock.notify();
}
}
运行结果:
客户程序:
截取一段:
服务程序:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值