Skip to main content

How to register an application in a peer group?

9 replies [Last post]
soloss
Offline
Joined: 2007-02-11
Points: 0

Hello, I try to register an application in a peer group, so that the application will automatically start when the peer group start. The following code is to create a new peer group that has a custom P service and a pList application. The intention is to automatically start pList application when the Peer Group start.

But, I test the code, the application is not starting when call newGroup.startApp(null)

Is there any sample code to register an application in a new peer group?

public void createPeerGroup() throws Exception, PeerGroupException
{
String name = "my peer group";
String descr = "A peer group";
DiscoveryService discovery = netPeerGroup.getDiscoveryService();

ModuleImplAdvertisement allPurposeImplAdv = netPeerGroup.getAllPurposePeerGroupImplAdvertisement();

ModuleClassAdvertisement pClassAdv = createModuleClassAdv(P.refModuleClassID, "P", "P");
ModuleSpecAdvertisement pSpecAdv = createModuleSpecAdv(PImp.refModuleSpecID, "P", "P Specification");
ModuleImplAdvertisement pImplAdv = createModuleImplAdv(allPurposeImplAdv, pSpecAdv,"the reference p service implementation", "com.xxxx.p.PImpl");

ModuleClassAdvertisement listClassAdv = createModuleClassAdv(List.refModuleClassID, "pList", "pList");
ModuleSpecAdvertisement listSpecAdv = createModuleSpecAdv(List.refModuleSpecID, "pList", "pList Specification");
ModuleImplAdvertisement listImplAdv = createModuleImplAdv(allPurposeImplAdv, listSpecAdv,"the reference pList implementation", "com.xxxx.pList");

StdPeerGroupParamAdv params = new StdPeerGroupParamAdv(allPurposeImplAdv.getParam());
Map services = params.getServices();
services.put(pClassAdv.getModuleClassID(),pImplAdv);
params.setServices(services);

Map applications = new Hashtable();
applications.put(listClassAdv.getModuleClassID(),listImplAdv);
params.setApps(applications);

allPurposeImplAdv.setParam((StructuredDocument) params.getDocument(new MimeMediaType("text", "xml")));
allPurposeImplAdv.setModuleSpecID((ModuleSpecID) IDFactory.fromURL( new URL(refPeerGroupSpecID)));

discovery.publish(pClassAdv);
discovery.publish(pSpecAdv);
discovery.remotePublish(pClassAdv);
discovery.remotePublish(pSpecAdv);

discovery.publish(allPurposeImplAdv);
discovery.remotePublish(allPurposeImplAdv);

PeerGroupID groupID = (PeerGroupID) IDFactory.fromURL(new URL(refPeerGroupID));

newGroup = netPeerGroup.newGroup(groupID,allPurposeImplAdv,name,descr);

discovery.remotePublish(newGroup.getPeerGroupAdvertisement());
newGroup.startApp(null);
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
soloss
Offline
Joined: 2007-02-11
Points: 0

Help!

dave_scullion
Offline
Joined: 2007-03-19
Points: 0

Have you got anywhere with this yet, I'm just coding some JXTA apps and services and have had no problems. Can you post your code for

createModuleClassAdv(P.refModuleClassID, "P", "P");
createModuleSpecAdv(PImp.refModuleSpecID, "P", "P Specification");
createModuleImplAdv(allPurposeImplAdv, pSpecAdv,"the reference p service implementation",

and I'll have a look...
If you copied them from one of the many books on JXTA (which I think you might have given their names) then they might be slightly outdated - in which case I can give you some working code...

soloss
Offline
Joined: 2007-02-11
Points: 0

The following are the codes,

private ModuleClassAdvertisement createModuleClassAdv(String moduleClassID, String name, String description)
throws UnknownServiceException, MalformedURLException
{
ModuleClassID classID = (ModuleClassID) IDFactory.fromURL(new URL((moduleClassID)));
ModuleClassAdvertisement moduleClassAdv =(ModuleClassAdvertisement) AdvertisementFactory.newAdvertisement(ModuleClassAdvertisement.getAdvertisementType());
moduleClassAdv.setDescription(description);
moduleClassAdv.setModuleClassID(classID);
moduleClassAdv.setName(name);
return moduleClassAdv;
}

private ModuleImplAdvertisement createModuleImplAdv(ModuleImplAdvertisement groupImpl,ModuleSpecAdvertisement moduleSpecAdv, String description, String code)
{
ModuleSpecID specID = moduleSpecAdv.getModuleSpecID();
ModuleImplAdvertisement moduleImplAdv = (ModuleImplAdvertisement) AdvertisementFactory.newAdvertisement(ModuleImplAdvertisement.getAdvertisementType());
moduleImplAdv.setCode(code);
moduleImplAdv.setCompat(groupImpl.getCompat());
moduleImplAdv.setDescription(description);
moduleImplAdv.setModuleSpecID(specID);
moduleImplAdv.setProvider("mine");
moduleImplAdv.setUri("http://www.xxx.com");
return moduleImplAdv;
}

private ModuleSpecAdvertisement createModuleSpecAdv(String moduleSpecID, String name, String description)
throws UnknownServiceException, MalformedURLException
{
ModuleSpecID specID = (ModuleSpecID) IDFactory.fromURL(new URL((moduleSpecID)));
ModuleSpecAdvertisement moduleSpecAdv =(ModuleSpecAdvertisement) AdvertisementFactory.newAdvertisement(ModuleSpecAdvertisement.getAdvertisementType());
moduleSpecAdv.setCreator("mine");
moduleSpecAdv.setModuleSpecID(specID);
moduleSpecAdv.setDescription(description);
moduleSpecAdv.setName(name);
moduleSpecAdv.setSpecURI("http://www.xxx.com");
moduleSpecAdv.setVersion("1.0");
return moduleSpecAdv;
}

I basically uses the code from "JXTA: Java P2P Programming" by Brendon J. Wilson. I modified the old code to make them comilable on the jxta.2.4.1 version.

I know the code is outdate, so I modify some parts according to the new jxta version.

If you have worked on the same sample code, I appreciated very much that you could share the latest working code

Thanks

dave_scullion
Offline
Joined: 2007-03-19
Points: 0

Dear Solos,

For the most-part your code looks fine (you follow the same steps as I do) but I believe your problem lies with the adverts you are creating and I would suggest trying two things:

1) you should use the URI versions of the IDFactory methods i.e. IDFactory.fromURI(new URI("urn:jxta:uuid-B8D5B5F190CH4131BC239F57YHC8B64D0"));

the URL versions are depreciated... (p.s. that ID is not a valid ID so dont copy and past it).....

2) Perhaps more importantly, make SURE the string you give to the setCode() method IS 100% correct or you aint going anywhere. Personally I NEVER hard code these as strings, you should use myadv.setCode(MyServiceImplementation.class.getName()).

try this and get back to me....

Dave

p.s. I presume the refModuleClassID's and refModuleSpecID's are generated by you... if you have copied these straight out of the book then they may not work... sorry if this is an obvious thing to say and you've already done this but I just want to cover all the bases....

dave_scullion
Offline
Joined: 2007-03-19
Points: 0

ps sorry for the late reply... been busy lately

soloss
Offline
Joined: 2007-02-11
Points: 0

Hi Dave,

Thanks for your suggestion, I tried the suggestions:
1, use URI instead URL
2. use ServeiceImp.class.getName() instead of hardcoded class String
3. regeneated new moduleClassID, moduleSpecID.

I get the same error as before,
"net.jxta.exception.ServiceNotFoundException: urn:jxta:uuid-1AC7675DE2A64C60BD53F9C94DBB24A905
at net.jxta.impl.peergroup.GenericPeerGroup.lookupService(GenericPeerGro
up.java:516)
at net.jxta.impl.peergroup.RefCountPeerGroupInterface.lookupService(RefC
ountPeerGroupInterface.java:254)
at net.jxta.impl.peergroup.RefCountPeerGroupInterface.lookupService(RefC
ountPeerGroupInterface.java:218)"

Do u have any simple working code to share?

Thanks

dave_scullion
Offline
Joined: 2007-03-19
Points: 0

Hi Solos,

I actually just posted a question on the forum a couple of hours ago...

Q: "Clarification needed: multiple peer services in a custom peergroup..."

I have a slightly different problem to you but the code I pasted in my question is my working code... at the end of my code I call startapp() and the application starts up and lookupservice works fine...

The code does more than you need but you can strip out the stuff you dont require.

Dave

soloss
Offline
Joined: 2007-02-11
Points: 0

Thanks,

I got pass this issue, the code and suggestions help!

iztok_cergol
Offline
Joined: 2007-05-04
Points: 0

Hi! I'm new to JXTA, tried to register a Service, but it doesn't work. I've tried your suggestions and read your discussions, but perhaps I've missed something... where do you call startapp? Do you have any working example?

I saw a lot of methods are deprecated... for example:

netPeerGroup = PeerGroupFactory.newNetPeerGroup();

or

StdPeerGroupParamAdv params = new StdPeerGroupParamAdv(implAdv.getParam());

and half of the createPeerGroup() method is also deprecated.....
Does it exist any new method od procedure to register peergroup services?

Iztok