📄 chat.java
字号:
print("Starting Rendezvous...");
netRendezVousService.startRendezVous();
print("done!\n");
}
else {
print("Waiting to connect to Rendezvous..");
while(!netRendezVousService.isConnectedToRendezVous()) {
print(".");
try {
Thread.sleep(2 * 1000);
}
catch(InterruptedException e) {
}
}
print("connected!\n");
}
netDiscoveryService.addDiscoveryListener(this);
uninitialized = false;
isInitializing = false;
}
catch(net.jxta.exception.PeerGroupException e) {
System.out.println("fatal error : group creation failure");
e.printStackTrace();
System.exit(-1);
}
catch(Exception e) {
System.out.println("unknown error type : occured during group creation");
e.printStackTrace();
System.exit(-1);
}
}
public synchronized void run()
{
int count;
java.util.Enumeration j, k;
HandledGroup handledG;
HandledPipe handledP;
Joined handledJ;
try {
while(true) {
if((Groups.size() == 0) && (handling.size() == 0) && (pipes.size() == 0)) return;
// As long as there are groups to join or create,
// pipes to look for or create,
// or groups to promote
j = handling.elements();
while(j.hasMoreElements()) { // Traverse the groups that are to be handled
handledG = (HandledGroup)j.nextElement();
if(handledG.Counter < 6)
// The group has been looked up less than 6 times,
// i.e. for less than a minute.
netDiscoveryService.getRemoteAdvertisements // Try again
(null,
net.jxta.discovery.DiscoveryService.GROUP,
"Name",
"Chat." + handledG.GroupName,
5);
else if(handledG.creating) { // The group can safely be created
print(" Creating group " + handledG.GroupName + "...");
net.jxta.peergroup.PeerGroup grp;
net.jxta.protocol.PeerGroupAdvertisement adv;
try {
net.jxta.protocol.ModuleImplAdvertisement implAdv =
netPeerGroup.getAllPurposePeerGroupImplAdvertisement();
grp = netPeerGroup.newGroup(null,
implAdv,
"Chat." + handledG.GroupName,
"Chat group");
adv = grp.getPeerGroupAdvertisement();
print("done!\n");
}
catch(Exception e) {
print("\nGroup creation failed with " + e.toString() + '\n');
continue;
}
try {
print("Publishing group...");
netDiscoveryService.remotePublish(adv, netDiscoveryService.GROUP);
print("done!\n");
}
catch(Exception e) {
print("\nError publishing group advertisement\n");
e.printStackTrace();
continue;
}
print("Joining " + handledG.GroupName + "...");
net.jxta.document.StructuredDocument creds = null;
try {
net.jxta.credential.AuthenticationCredential authCred =
new net.jxta.credential.AuthenticationCredential(grp,
null,
creds);
net.jxta.membership.MembershipService membership =
grp.getMembershipService();
net.jxta.membership.Authenticator auth =
membership.apply(authCred);
if(auth.isReadyForJoin()) {
net.jxta.credential.Credential myCred =
membership.join(auth);
print("joined!\n");
Groups.put(handledG.GroupName,
new Joined(grp,
handledG.UserName,
handledG.Listener,
adv));
print("Looking for group pipe.\n");
pipes.put(handledG.GroupName,
new HandledPipe(handledG.GroupName));
if(!runner.isAlive()) runner.start();
}
else print("\nFailure: unable to join group\n");
}
catch(Exception e) {
print("\nFailure in authentication.\n");
e.printStackTrace();
}
handling.remove(handledG.GroupName);
}
else {
// The group has been looked up 6 times or more,
// i.e. for more than a minute
// We don't expect to find it, so we stop looking
handledG.creating = true;
print("Failure joining " +
handledG.GroupName +
" : Group not found.\nTrying to create group\n");
}
++handledG.Counter;
}
j = pipes.elements();
while(j.hasMoreElements()) { // Traverse the pipes that are to be handled
handledP = (HandledPipe)j.nextElement();
if(handledP.Counter < 4)
// The pipe has been looked up less than 4 times,
// i.e. for less than 40 seconds.
netDiscoveryService.getRemoteAdvertisements // Try again
(null,
netDiscoveryService.ADV,
"Name",
"Chat.pipe." + handledP.GroupName,
5);
else if(handledP.Counter > 4) {
// The group has been looked up 4 times or more,
// i.e. for more than 40 seconds
// so we assume it doen't exist and create it
print("Pipe not found for group " +
handledP.GroupName +
". Creating pipe...");
Joined group = (Joined)Groups.get(handledP.GroupName);
net.jxta.protocol.PipeAdvertisement adv =
(net.jxta.protocol.PipeAdvertisement)
net.jxta.document.AdvertisementFactory.newAdvertisement
(net.jxta.protocol.PipeAdvertisement.getAdvertisementType());
adv.setPipeID(net.jxta.id.IDFactory.newPipeID
(group.netPeerGroup.getPeerGroupID()));
adv.setName("Chat.pipe." + handledP.GroupName);
adv.setType(netPipeService.PropagateType);
try {
group.netInPipe = netPipeService.createInputPipe(adv, group);
group.netOutPipe = netPipeService.createOutputPipe(adv, -1);
print("done!\n");
}
catch(Exception e) {
print("\nPipe creation failed with " + e.toString() + '\n');
continue;
}
try {
print("Publishing pipe...");
netDiscoveryService.remotePublish(adv, netDiscoveryService.ADV);
print("done!\n");
}
catch(Exception e) {
print("\nError publishing pipe advertisement\n");
e.printStackTrace();
continue;
}
pipes.remove(handledP.GroupName);
}
++handledP.Counter;
}
j = Groups.elements();
while(j.hasMoreElements()) {
handledJ = (Joined)j.nextElement();
netDiscoveryService.remotePublish(handledJ.netAdvertisement,
netDiscoveryService.GROUP);
}
try {
Thread.sleep(10 * 1000); // Wait 10 seconds before trying again
}
catch(Exception e) {
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public void discoveryEvent(net.jxta.discovery.DiscoveryEvent event)
{
while(isHandling);
isHandling = true;
net.jxta.protocol.DiscoveryResponseMsg res = event.getResponse();
// We got a response to a lookup
String name = "unknown";
net.jxta.protocol.PeerAdvertisement peerAdv = res.getPeerAdvertisement();
if(peerAdv != null) {
name = peerAdv.getName();
}
net.jxta.document.Advertisement adv = null;
net.jxta.protocol.PeerGroupAdvertisement pgadv = null;
net.jxta.protocol.PipeAdvertisement padv = null;
java.util.Enumeration enum = res.getAdvertisements();
net.jxta.peergroup.PeerGroup grp;
if(enum != null) {
while(enum.hasMoreElements()) {
adv = (net.jxta.document.Advertisement)enum.nextElement();
if(adv.getAdvType().compareTo
(net.jxta.protocol.PeerGroupAdvertisement.getAdvertisementType()) == 0) {
// It was a peer group response
pgadv = (net.jxta.protocol.PeerGroupAdvertisement)adv;
String GroupName = pgadv.getName().substring(5);
HandledGroup handled = (HandledGroup)handling.get(GroupName);
if(handled != null) {
if(handled.creating)
print("Cannot create group " + GroupName + " : it already exists.\n");
print("Joining " + GroupName + "...");
net.jxta.peergroup.PeerGroupID GID = pgadv.getPeerGroupID();
try {
grp = netPeerGroup.newGroup(GID);
}
catch(Exception e) {
print("\nGroup creation failed with " + e.toString() + '\n');
continue;
}
net.jxta.document.StructuredDocument creds = null;
try {
net.jxta.credential.AuthenticationCredential authCred =
new net.jxta.credential.AuthenticationCredential(grp,
null,
creds);
net.jxta.membership.MembershipService membership =
grp.getMembershipService();
net.jxta.membership.Authenticator auth =
membership.apply(authCred);
if(auth.isReadyForJoin()) {
net.jxta.credential.Credential myCred =
membership.join(auth);
print("joined!\n");
Groups.put(handled.GroupName,
new Joined(grp,
handled.UserName,
handled.Listener,
pgadv));
print("Looking for group pipe.\n");
pipes.put(handled.GroupName,
new HandledPipe(handled.GroupName));
if(!runner.isAlive()) runner.start();
}
else print("\nFailure: unable to join group\n");
}
catch(Exception e) {
print("\nFailure in authentication.\n");
e.printStackTrace();
}
handling.remove(GroupName);
}
}
else if(adv.getAdvType().compareTo
(net.jxta.protocol.PipeAdvertisement.getAdvertisementType()) == 0) {
padv = (net.jxta.protocol.PipeAdvertisement)adv;
String GroupName = padv.getName().substring(10);
Joined group = (Joined)Groups.get(GroupName);
if((group != null) && (group.netOutPipe == null)) {
try {
print("Connecting to pipe to group " + GroupName + "...");
group.netInPipe = netPipeService.createInputPipe(padv, group);
group.netOutPipe = netPipeService.createOutputPipe(padv, -1);
print("done!\n");
}
catch(Exception e) {
print("\nPipe creation failed with " + e.toString() + '\n');
continue;
}
try {
print("Informing group members...");
net.jxta.endpoint.Message msg = new net.jxta.endpoint.Message();
net.jxta.endpoint.StringMessageElement sme =
new net.jxta.endpoint.StringMessageElement("msg",
group.UserName +
" has joined.",
null);
msg.addMessageElement(null, sme);
group.netOutPipe.send(msg);
}
catch(Exception e) {
print("\nFailed to inform group members\n");
e.printStackTrace();
continue;
}
print("done!\n");
}
pipes.remove(GroupName);
}
else print("Received unhandled advertisement type: " + adv.getAdvType() + '\n');
}
}
isHandling = false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -