msgsend.java
来自「jxta新建点组的示例程序」· Java 代码 · 共 112 行
JAVA
112 行
import net.jxta.discovery.DiscoveryService;
import net.jxta.document.AdvertisementFactory;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.pipe.OutputPipe;
import net.jxta.pipe.OutputPipeEvent;
import net.jxta.pipe.OutputPipeListener;
import net.jxta.pipe.PipeID;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.peergroup.*;
import net.jxta.rendezvous.RendezVousService;
import net.jxta.rendezvous.RendezvousEvent;
import net.jxta.rendezvous.RendezvousListener;
import java.io.IOException;
public class MsgSend implements Runnable,OutputPipeListener, RendezvousListener {
public final static String MESSAGE_NAME_SPACE = "chatmsg";
private String peerName;
private boolean waitForRendezvous = false;
private PipeService pipeService;
private PipeAdvertisement pipeAdv;
private OutputPipe outputPipe;
private PeerGroup netPeerGroup;
// 指向集合点服务对象
private RendezVousService rendezvous;
/**
* 初始化参数
*/
public MsgSend(boolean waitForRendezvous,PipeService pipeService,
PipeAdvertisement pipeAdv, String peerName , PeerGroup netPeerGroup) {
this.waitForRendezvous = waitForRendezvous;
this.pipeService = pipeService;
this.pipeAdv = pipeAdv;
this.peerName = peerName;
this.netPeerGroup = netPeerGroup; }
public synchronized void run() {
try {
// 获取默认点组的集合点服务
rendezvous = netPeerGroup.getRendezVousService();
// 注册集合点服务的时间处理对象
rendezvous.addListener(this);
pipeService.createOutputPipe(pipeAdv, this);
if (!rendezvous.isConnectedToRendezVous()) {
try {
wait();
pipeService.createOutputPipe(pipeAdv, this);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
System.out.println("创建输出管道失败");
e.printStackTrace();
System.exit(-1);
}
}
/**
* 输出管道的监听事件
*/
public void outputPipeEvent(OutputPipeEvent event) {
// 获得输出管道的对象
outputPipe = event.getOutputPipe();
Message msg;
try {
msg = new Message();
for (int i=0;i<5;i++) {
msg = new Message();
String msgs = peerName+i;
StringMessageElement sme = new StringMessageElement(
MESSAGE_NAME_SPACE, msgs, null);
msg.addMessageElement(null, sme);
// 将这个对等点的名字加到消息中
StringMessageElement peerN = new StringMessageElement(
"peerName", peerName, null);
msg.addMessageElement(null, peerN);
// 发送消息
outputPipe.send(msg);
}
} catch (IOException e) {
System.out.println("failed to send message");
e.printStackTrace();
System.exit(-1);
}
// stop();
}
/**
* 处理集合点事件
*/
public synchronized void rendezvousEvent(RendezvousEvent event) {
if (event.getType() == event.RDVCONNECT
|| event.getType() == event.RDVRECONNECT) {
notify();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?