📄 hungrypeer.java
字号:
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import net.jxta.discovery.DiscoveryService;
import net.jxta.exception.PeerGroupException;
import net.jxta.peergroup.NetPeerGroupFactory;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PeerGroupAdvertisement;
public class HungryPeer {
private PeerGroup netpg = null;
private PeerGroup restoNet = null;
private int timeout = 3000;
private DiscoveryService disco = null;
private PipeService pipes = null;
private Vector restaurantAdvs = new Vector();
public static void main(String[] args) {
HungryPeer hp = new HungryPeer();
hp.startJxta();
System.exit(0);
}
private boolean joinRestoNet() {
int count = 3;
DiscoveryService hdiso = netpg.getDiscoveryService();
Enumeration ae = null;
while (count-- > 0) {
try {
ae = hdiso.getLocalAdvertisements(DiscoveryService.GROUP,
"Name", "RestoNet");
if ((ae != null) && (ae.hasMoreElements())) {
break;
}
hdiso.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();
}
}
// 判断是否有对等组通告,没有则返回假,有则加入
if (ae == null || !ae.hasMoreElements()) {
return false;
} else {
PeerGroupAdvertisement pgadv = (PeerGroupAdvertisement) ae
.nextElement();
try {
restoNet = netpg.newGroup(pgadv);
} catch (PeerGroupException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
disco = restoNet.getDiscoveryService();
pipes = restoNet.getPipeService();
System.out.println("Peer Name:" + restoNet.getPeerName()+ "joined the restonet PeerGroup");
return true;
}
public void startJxta() {
try {
netpg = new NetPeerGroupFactory().getInterface();
} catch (PeerGroupException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!joinRestoNet()){
System.out.println("Sorry could not find the RestoNet");
System.exit(2);
}else{
System.out.println("this peer " + restoNet.getPeerName() +" join the RestoNet");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -