本文主要讲解的是如何单纯发布管道广告,及利用发现的管道进行简单的信息通信。PeerServer发布管道广告,等待其它PeerClient发现及连接,然后向PeerServer发送一条内容为“你是谁”的信息。下面给出的两个类可以放在同个目录下运行。
public class PeerServer {
private DiscoveryService discoveryService = null;
private PipeService pipeService = null;
private PeerGroup restoNet = null;
private PeerGroupID peerGroupID = null;
private InputPipe inputPipe = null;
public static void main(String[] args) {
Logger.getLogger("net.jxta").setLevel(Level.SEVERE);
PeerServer peer1 = new PeerServer();
peer1.launchJXTA();
}
private void launchJXTA() {
try {
NetworkConfigurator config = new NetworkConfigurator();//设置配置可以跳过第1次运行时出现的配置UI
config.setPrincipal("peer1");// Peer名称
config.setPassword("888888888");// Peer密码
config.save();
restoNet = new NetPeerGroupFactory().getInterface();
peerGroupID = restoNet.getPeerGroupID();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
discoveryService = restoNet.getDiscoveryService();// 取得NetPeerGroup的发现服务
pipeService = restoNet.getPipeService();// 取得NetPeerGroup的管道服务
startServer();// 开始启动
}
private void startServer() {
publishPipeAdvertisement();// 发布管道广告
}
//创建及发布管道广告
private void publishPipeAdvertisement() {
PipeAdvertisement pipeAdv = createPipeAdvertisement();
// -----------------以下这段代码只是为了把管道广告的内容打印出来--------------------------
StructuredTextDocument doc = (StructuredTextDocument) pipeAdv
.getDocument(MimeMediaType.XMLUTF8);
StringWriter out = new StringWriter();
try {
doc.sendToWriter(out);
System.out.println(out.toString());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
// -----------------------------打印结束--------------------------------------------
try {
discoveryService.publish(pipeAdv);//本地发布管道广告
discoveryService.remotePublish(pipeAdv);//远程发布管道广告
inputPipe = pipeService.createInputPipe(pipeAdv);// 创建输入管道
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
System.out.println("等待其它Peer端信息的到达.........");
Message msg;
try {
msg = inputPipe.waitForMessage();// 监听输入管道是否有信息传进来
} catch (InterruptedException e) {
inputPipe.close();
System.out.println("接收其它Peer信息出错!");
return;// 如果出现异常则返回
}
String receiveContent = null;
Message.ElementIterator en = msg.getMessageElements();// 取得到信息
if (!en.hasNext()) {
return;
}
MessageElement msgElement = msg.getMessageElement(null, "DataTag");
if (msgElement.toString() != null) {
receiveContent = msgElement.toString();
}
if (receiveContent != null) {
System.out.println("接收信息内容: "
+ receiveContent);
} else {
System.out
.println("没有内容");
}
}
}
// 生成管道广告,在这里我们是直接从代码中生成管道广告,当然我们可以读管道广告文件
private PipeAdvertisement createPipeAdvertisement() {
PipeAdvertisement pipeAdvertisement = null;
pipeAdvertisement = (PipeAdvertisement) AdvertisementFactory
.newAdvertisement(PipeAdvertisement.getAdvertisementType());// 创建一个管道广告
pipeAdvertisement.setPipeID(createPipeID(peerGroupID));
pipeAdvertisement.setName("Pipe");
pipeAdvertisement.setDescription("JXTA create first pipe");
pipeAdvertisement.setType(PipeService.UnicastType);// 管道类型,管道类型在JXTA
return pipeAdvertisement;
}
// 生成管道ID
private PipeID createPipeID(PeerGroupID groupID) {
PipeID pipeID = null;
pipeID = IDFactory.newPipeID(groupID);
return pipeID;
}
}
先执行上面这个类,再执行下面PeerClient类。
public class PeerClient {
private PeerGroup netpg = null;// PeerGroup
private DiscoveryService disco; // 发现服务
private PipeService pipeSev; // 管道服务
private PipeAdvertisement pipeAdv = null;// 管道广告
private OutputPipe outputPipe;// 输入管道
public static void main(String[] args) {
Logger.getLogger("net.jxta").setLevel(Level.SEVERE);
PeerClient peer2 = new PeerClient();
peer2.launchJXTA();
}
private void launchJXTA() {
System.out.println("Lauching Peer into JXTA NetWork...");
try {
NetworkConfigurator config = new NetworkConfigurator();
config.setPrincipal("peer2");
config.setPassword("888888888");
config.save();
netpg = new NetPeerGroupFactory().getInterface();
} catch (Exception e) {
System.out.println("Unable to create PeerGroup - Failure");
System.exit(-1);
}
startClient();
}
private void startClient() {
System.out.println("搜索管道广告....");
disco = netpg.getDiscoveryService();//获取NetGroup发现服务
pipeSev = netpg.getPipeService();//获取NetGroup管道服务
Enumeration en;
while (true) {
try {
en = disco.getLocalAdvertisements(DiscoveryService.ADV, "Name",
"Pipe");// 本地发现广告,后面对应了广告中Name标签,值为Pipe的管道广告
if ((en != null) && en.hasMoreElements()) {
break;
}
disco.getRemoteAdvertisements(null, DiscoveryService.ADV,
"Name", "Pipe", 1, null);// 远程发现广告
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(".");
}
System.out.println("已经找到管道广告.......");
pipeAdv = (PipeAdvertisement) en
.nextElement();
if (null == pipeAdv) {
System.out.println("没有找到管道广告");
}
try {
outputPipe = pipeSev.createOutputPipe(pipeAdv, 10000);// 创建输出管道,其实是连接Peer1中的输入管道
} catch (IOException e) {
e.printStackTrace();
}
String data = "你是谁";// 我们要发送的信息内容
Message msg = new Message();
StringMessageElement sme = new StringMessageElement("DataTag", data,
null);
msg.addMessageElement(null, sme);
try {
outputPipe.send(msg);// 发送信息
System.out.println("信息 \"" + data
+ "\" 已经发送");
} catch (IOException e) {
e.printStackTrace();
System.out
.println("发送信息失败!!");
}
}
}