Skip to main content

How to create a custom peer group? and can discovered by another peer?

1 reply [Last post]
yangjiangong
Offline
Joined: 2008-08-10
Points: 0

Hi,everybody.
i have read most of the thread here ,and foun many people were puzzled about creating group,discover group,and join group.so ,I thing if there has some expert would like to summerise these questions(espesially provids sample code)?

I have the same question.I write two applications,one create a custom peer group,anthor start to find that group,and try to join it.And the two app run on one computer.(I thind this case is very common for most new comers to JXTA).but another peer can't find the custom peer group,why?

here is my code:
---------------------App 1 : this piece of code is to create the custom peer group------

try {
manager = new NetworkManager(NetworkManager.ConfigMode.EDGE, "HelloWorld");
System.out.println("Starting JXTA");
manager.startNetwork();
netPeerGroup = manager.getNetPeerGroup();
System.out.println("JXTA Started");
} catch (PeerGroupException pe) {
System.out.println("");
pe.printStackTrace();
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
discSvc = netPeerGroup.getDiscoveryService();
//Create my owe peer group
try {

ModuleImplAdvertisement implAdv =
netPeerGroup.getAllPurposePeerGroupImplAdvertisement();

myPeerGroup= netPeerGroup.newGroup(null, //The ID of that group. If null then a new group ID will be chosen
implAdv, //The advertisement of the implementation to be used
"NewGroup", //The name of the group
"New peer group created by the default world group."//A description of this group
);

} catch (Exception e) {
System.out.println("New group creation failed!");
e.printStackTrace();
}

if(Module.START_OK == myPeerGroup.startApp(new String[0])) {
System.out.println("Create the new peer group,ID is:" + myPeerGroup.getPeerGroupID() + " | Name is:" + myPeerGroup.getPeerGroupName());

}

-----------------App 2 : the following piece of code is to discover the group-------------
try {
manager = new NetworkManager(NetworkManager.ConfigMode.EDGE, "HelloWorld");
manager.startNetwork();
netPeerGroup = manager.getNetPeerGroup();
System.out.println("JXTA Started");
} catch (PeerGroupException pe) {
pe.printStackTrace();
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
discoverySvc = netPeerGroup.getDiscoveryService();
Enumeration en = null;
PeerGroup pg = null;

try {
en = discoverySvc.getLocalAdvertisements(DiscoveryService.GROUP, null, null);
while (en.hasMoreElements()) {
pg = (PeerGroup)en.nextElement();
String ToDisplay = "Found " + pg.getClass().getSimpleName();
System.out.println(ToDisplay);
}

} catch (IOException e) {
System.out.println("Fail to find peer group!");
e.printStackTrace();
}

--------------------------End--------------------------------------
I can detect that after runing the App 2 to discover the peer group,the varian en is not null,but has no element,why?

thanks in advance!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
GABIKA6
Offline
Joined: 2011-09-12
Points: 0

Hi!

I tried your code.

In my opinion discoverySvc.getLocalAdvertisements(DiscoveryService.GROUP, null, null); returns with a type of Advertisement. So it throws Exception (for me).

I've changed your last try block to this, and it works.

Enumeration<Advertisement> en = null;
Advertisement pg = null;

try {

System.out.println("Discovering...\n");
en = discoSvc.getLocalAdvertisements(DiscoveryService.GROUP, null, null);
while (en.hasMoreElements()) {
pg = en.nextElement();
String ToDisplay = "Found " + pg.getClass().getSimpleName();
System.out.println(ToDisplay);
}

} catch (IOException e) {
System.out.println("Fail to find peer group!");

}