A Peer Service Example

Let’s use our RestoNet example to illustrate how the RestoPeer can publish itself as a service and how the HungryPeer can discover and use that service.

The RestoPeer as a Peer Service

First, we’ll modify the RestoPeer class to publish a service advertisement rather than a pipe advertisement. To do so, we’ll use the createRestoPeerService( ) method of the RestoPeer class:

// This routine creates a module specification advertisement to be
// associated with a RestoPeer
private void createRestoPeerService(  ) {
    try {
        // First, create the module class advertisement associated with the
        // service. The module class advertisement advertises the existence
        // of the service. To access the service, a peer will have to
        // discover the associated module specification advertisement.
        ModuleClassAdvertisement mcadv = (ModuleClassAdvertisement)
                              AdvertisementFactory.newAdvertisement(
                              ModuleClassAdvertisement.getAdvertisementType(  ));

                          mcadv.setName("JXTAMOD:RestoNet:Service:" + brand);
                          mcadv.setDescription("RestoPeer service");

                          ModuleClassID mcID = IDFactory.newModuleClassID(  ); mcadv.setModuleClassID(mcID); // Publish the module class advertisement in my local cache and to // peergroup disco.publish(mcadv, DiscoveryService.ADV); disco.remotePublish(mcadv, DiscoveryService.ADV); // Create the module specification advertisement associated // with the service. The module specification advertisement will // contain all the information necessary for a client to contact the ...

Get JXTA in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.