Skip to main content

problem in discovering peers in a custom Peer Group

4 replies [Last post]
Joined: 2007-01-31

Hello, I'm newbie in JXTA, and I have a problem in discovering peers in a custom Peer Group.
I have this configuration:

SimplePeer <---> RDVPeer <---> SimpleDiscoverer

Each peer searches for a custom peer group and if the group is found the peer joins the group, otherwise the peer creates a new group and then joins it. In this way the first peer that rises creates the new group and the other peers joins it.
Methods for Creating and Joining the custom PeerGroup are:

		private PeerGroup createGroup() {<br />
			PeerGroupAdvertisement adv;<br />
			String peerName = this.getHostname();<br />
			System.out.println("Creating a new group advertisement");</p>
<p>			try {<br />
				// create a new all purpose peergroup.<br />
				ModuleImplAdvertisement implAdv =<br />
<p>				myPeerGroup = netPeerGroup.newGroup(null,                 // Assign new group ID<br />
						implAdv,              // The implem. adv<br />
						this.getPeerGroupName(),      // The name<br />
						"This is MyWorldPeerGroup, published by " + peerName); // Helpful descr.</p>
<p>				// print the name of the group, the peer group ID and the description<br />
				adv = myPeerGroup.getPeerGroupAdvertisement();<br />
				PeerGroupID GID = adv.getPeerGroupID();<br />
				System.out.println("  Group = " + adv.getName() +<br />
						"\n  Group ID = " + GID.toString() +<br />
						"\n  Group Description = " + adv.getDescription());</p>
<p>			} catch (Exception eee) {<br />
				System.out.println("Group creation failed with " + eee.getMessage());<br />
				eee.printStackTrace();<br />
				return (null);<br />
<p>			try {<br />
				// publish this advertisement<br />
				//(send out to other peers and rendezvous peer)<br />
				discoSvc.publish(adv);<br />
				discoSvc.remotePublish(adv);<br />
				System.out.println("Group " + adv.getName() + " published successfully.");<br />
			} catch (Exception e) {<br />
				System.out.println("Error publishing group advertisement: " + e.getMessage());<br />
				e.printStackTrace();<br />
				return (null);<br />
			}<br />
			return myPeerGroup;<br />

Where discoSvc is the Discovery Service from the NetPeerGroup:
discoSvc = netPeerGroup.getDiscoveryService();

			private void joinGroup(PeerGroup grp) {<br />
				System.out.println("Joining peer group...");</p>
<p>				StructuredDocument creds = null;</p>
<p>				try {<br />
					// Generate the credentials for the Peer Group<br />
					AuthenticationCredential authCred =<br />
						new AuthenticationCredential( grp, null, creds );</p>
<p>					// Get the MembershipService from the peer group<br />
					MembershipService membership = grp.getMembershipService();</p>
<p>					// Get the Authenticator from the Authentication creds<br />
					Authenticator auth = membership.apply(authCred);</p>
<p>					// Check if everything is okay to join the group<br />
					if (auth.isReadyForJoin()){<br />
						Credential myCred = membership.join(auth);</p>
<p>						System.out.println("Successfully joined group " +<br />
<p>						// display the credential as a plain text document.<br />
						System.out.println("\nCredential: ");<br />
						StructuredTextDocument doc = (StructuredTextDocument)<br />
						myCred.getDocument(new MimeMediaType("text/plain"));</p>
<p>						StringWriter out = new StringWriter();<br />
						doc.sendToWriter(out);<br />
						System.out.println(out.toString());<br />
						out.close();<br />
					}<br />
					else<br />
						System.out.println("Failure: unable to join group");<br />
				}<br />
				catch (Exception e){<br />
					System.out.println("Failure in authentication.");<br />
					e.printStackTrace();<br />
				}<br />

If the custom PeerGroup is found, all peers do:

<br />
public void discoveryEvent(DiscoveryEvent event) {</p>
<p>		System.out.println("Got a Group Discovery Event!");<br />
		Object obj = null;<br />
		Enumeration res = event.getSearchResults();<br />
		if (res != null){<br />
			if (!res.hasMoreElements()){<br />
				System.out.println("No Search Results.");<br />
			}<br />
			int unknownEvents=0;<br />
			while (res.hasMoreElements()) {<br />
				obj = res.nextElement();<br />
				if (obj instanceof PeerGroupAdvertisement){<br />
					PeerGroupAdvertisement adv = (PeerGroupAdvertisement) obj;<br />
					System.out.println("Peer Group name from getSearchResults() = " + adv.getName());<br />
					if (adv.getName().equals(this.getPeerGroupName())){<br />
//						Here the Peer Group should be taken<br />
						try {<br />
							this.setFoundPG(netPeerGroup.newGroup(adv));<br />
						} catch (PeerGroupException e) {<br />
							System.out.println("Exception while creating the PeerGroup" +<br />
									" from the received Peer Group Advertisement" +<br />
									e.getMessage());<br />
							e.printStackTrace();<br />
						}<br />
//						Peer Group found!<br />
						this.setGroupFound(true);<br />
					}<br />
					else {<br />
						System.out.println("Peer Group " + adv.getName() + " doesn't match with " + this.getPeerGroupName());<br />
					}<br />
				}<br />
				else {<br />
<p>				}<br />
			}<br />
			if (unknownEvents>0)<br />
				System.out.println("Total unknown advertisements: "+unknownEvents);<br />
		}<br />
		else {<br />
			System.out.println("Empty Search Results.");<br />
<p>	} 

Once a peer has joined the group, a discovery service is retrieved from that group and it is used for publishing a PeerAdvertisement (by the SimplePeer) or for discovering the peers in the group.

All Peers do:

<br />
	private void setDiscoveryServiceForGroup(PeerGroup peerGrp){</p>
<p>		myDiscoSvc = peerGrp.getDiscoveryService();</p>
<p>	} 

SimplePeer do:

<br />
	private void publish() throws IOException{</p>
<p>myDiscoSvc.publish(peerAdvertisement);<br />
		myDiscoSvc.remotePublish(peerAdvertisement, 7200000);</p>
<p>	} 

SimpleDiscoverer do:

<br />
	public void run() {<br />
		//		Add ourselves as a DiscoveryListener for DiscoveryResponse events<br />
		myDiscoSvc.addDiscoveryListener(this);<br />
		System.out.println("\nSending a Discovery Message for Peers");<br />
		//		look for any peer<br />
		myDiscoSvc.getRemoteAdvertisements(null, DiscoveryService.PEER,<br />
				"Name", this.getWantedPeerName(), 15);<br />
		System.out.println("Searching Peers containing: " + this.getWantedPeerName());</p>
<p>	} 

With all these methods neither SimpleDiscoverer manages to find the peer advertisement of SimplePeer, nor RDVPeer sees in its local cache the PeerAdvertisements of the others.
It's important to say that everythig works fine when i use the netPeerGroup and its discoSvc.
I hope my explainations has been sufficently clear, and i thank you in advance for any help.



Message was edited by: pietro78

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-07-10

> You need to have one of the peer in the new peergroup act as a rdv (call startRDV) if you don't have multicast enable.

Is this a requirement for every new peer-group?

I have the same problem as reported by pietro78. Exclusively publishing the peers via DiscoveryService results does not help. Now each peer sees itself twice in the JXTA Shell, once for the netgroup, once for the peer-group, but the other peers are not visible. Any additional hints? Thanks.

Joined: 2003-06-16

You need to have one of the peer in the new peergroup act as a rdv (call startRDV) if you don't have multicast enable.



Joined: 2007-01-31

Thanks a lot! Now it works! I have created a RendezVous Service for myPeerGroup both in the RDVPeer and in SimpleDiscoverer: for the SimpleDiscoverer i had to wait for being connected to myPeerGroupRendezVousService before performing a discovery in the PeerGroup.
I added this method in RDVPeer:
[code]private void startGroupRDV(PeerGroup peerGrp, String peerName) {

System.out.println("Starting RendezVous Service for " + peerGrp.getPeerGroupName() + "...");

myrdvService = peerGrp.getRendezVousService();

System.out.println("Status is: " + myrdvService.getRendezVousStatus().toString());
System.out.println("... RendezVous Service started.\n");



And this method in SimpleDiscoverer

private synchronized void connectedGroupRendezdVous(PeerGroup peerGrp) {
System.out.println("Checking if connected to a RendezVous for " +
peerGrp.getPeerGroupName() + "...");
myRdvSvc = peerGrp.getRendezVousService();
int i = 0;
while (!myRdvSvc.isConnectedToRendezVous()){
System.out.println("Waiting for a RendezVous connection. Attempt number: " + i);
try {
} catch (InterruptedException e) {
Enumeration en = myRdvSvc.getConnectedRendezVous();
while (en.hasMoreElements()) {
PeerID element = (PeerID) en.nextElement();
System.out.println("Connected to RendezVous: " + element);


I hope that could help someone who has same problems...

Thanks again


Joined: 2003-06-16

Perfect. Thanks for posting the updated fix.