📄 restopeer.java
字号:
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownServiceException;
import java.util.Enumeration;
import net.jxta.discovery.DiscoveryService;
import net.jxta.exception.PeerGroupException;
import net.jxta.id.IDFactory;
import net.jxta.peergroup.NetPeerGroupFactory;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.protocol.PeerGroupAdvertisement;
public class RestoPeer {
private PeerGroup netpg = null; // NetPeerGroup
private PeerGroup restoNet = null;// User Define PeerGroup
private String brand = "Chez JXTA"; // Brand of my restaurants
private int timeout = 3000; // Time-Out,can be adjusted
private DiscoveryService disco; // 发现服务
private PipeService pipes; // 管道服务
static String groupURL = "jxta:uuid-4d6172676572696e204272756e6f202002";
public static void main(String[] args) {
RestoPeer myapp = new RestoPeer();
myapp.startJxta();
// System.exit(0);
}
public void startJxta() {
try {
netpg = new NetPeerGroupFactory().getInterface();
} catch (PeerGroupException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
joinRestoNet();
System.out.println("urn:"+groupURL);
System.out.println(restoNet.getPeerGroupID());
System.out.println(restoNet.getPeerGroupName());
System.out.println("This Peer's Name:" + restoNet.getPeerName());
try {
Enumeration enu = restoNet.getDiscoveryService().getLocalAdvertisements(DiscoveryService.PEER,"","");
while((enu != null) && enu.hasMoreElements()){
PeerAdvertisement adv = (PeerAdvertisement)enu.nextElement();
System.out.println(adv.getName());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 加入拍卖组
private void joinRestoNet() {
int count = 3; //试图发现的最高次数
System.out
.println("试图发现 RestoNet 对等组");
//从NetPeerGroup获得发现服务
DiscoveryService hdisco = netpg.getDiscoveryService();
Enumeration ae = null; //记录发现的对等体。
// 循环直到我们发现RestoNet对等组或是直到我们达到了试图预期发现的次数。
while (count-- > 0) {
try {
// 第一次搜索对等体的本地缓存来查找RestoNet对等组通告。
// 通过NetPeerGroup组提供的发现服务发现"Name"属性为"RestoNet"的对等组
ae = hdisco.getLocalAdvertisements(DiscoveryService.GROUP,
"Name", "RestoNet");
// 如果发现RestoNet对等组通告,该方法完成,退出循环。
if ((ae != null) && ae.hasMoreElements()) {
break;
}
// 如果我们没有在本地找到它,便发送发现远程请求。
// 参数依次为要查找的对等体ID,为空时不以此为发现条件;发现的通告类型,取值还有PEER,和ADV;
// 要发现的通告属性名称;属性取值;需获取的最大通告数量;发现监听器
hdisco.getRemoteAdvertisements(null, DiscoveryService.GROUP,
"Name", "RestoNet", 1, null);
// 线程延时一下等待对等体內该发现请求。
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*-------------------------------------------
* 以上为循环发现目标组过程,以下为加入过程
* ----------------------------------------*/
// 创建一个对等组通告引用
PeerGroupAdvertisement restoNetAdv = null;
// 检查我们是否找到RestoNet通告。如果没有找到,表示我们可能是第一个试图加入该组的对等体,
//或是其他知道RestoNet组的对等体成员已经关闭或不可到达
// 万一出现这种情况,我们必须创建一个RestoNet对等组。
if (ae == null || !ae.hasMoreElements()) {
// 如果该组不在,给出提示信息,创建该组
System.out
.println("Could not find the RestoNext peergroup;createing me");
try {
// 创建一个新的对等组RestoNet,全能对等组
// 通过NetPeerGroup获得一个一般对等组的通告。
ModuleImplAdvertisement implAdv = netpg
.getAllPurposePeerGroupImplAdvertisement();
// 通过NetPeerGroup创建一个新的对等组,JXTA会自行发布该对等组通告,
//参数依次为对等组ID,通告,组名,描述
restoNet = netpg.newGroup(mkGroupID(), implAdv, "RestoNet",
"RestoNet,Inc.");
// 获得一个对等组通告
restoNetAdv = netpg.getPeerGroupAdvertisement();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
// RestoNet通告在缓存内找到意味着我们可以加入这个存在的组。
// 在集合中提取一个对等组通告元素
restoNetAdv = (PeerGroupAdvertisement) ae.nextElement();
try {
// 加入该对等组,由于该通告已经发布,JXTA不会再行发布。
restoNet = netpg.newGroup(restoNetAdv);
System.out
.println("Found the RestoNet Peergroup advertisement;joined existing group//找到RestoNet对等组,并加入存在的该组");
} catch (PeerGroupException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 获得RestoNet提供的发现服务和管道服务
disco = restoNet.getDiscoveryService();
pipes = restoNet.getPipeService();
System.out.println("RestoNet Restaurant_(" + brand + ") is on-line");
return;
}
private PeerGroupID mkGroupID() throws URISyntaxException,
MalformedURLException, UnknownServiceException {
// 根据groupURL返回一个对等组ID,URI参数为“协议名”,“主机名”,“文件名”
// IDFactory的该方法已由fromURI替代
//return (PeerGroupID) IDFactory.fromURL(new URL("urn", "", groupURL));
return (PeerGroupID)IDFactory.fromURI(new URI(groupURL));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -