JXTA, P2P programming technology routine (3)

zhaozj2021-02-16  62

Volume query client

The purpose of this client is to distribute the quantity as much as possible to other peer. Consider the number of items between 1 and 10,000, when a peer receives this message, it needs to decide to give a few peer this matter. Therefore, it needs to be constantly discovering the peer that advertises you have a lot of money, and provides a buffer for their notices. For example, a peer has another 10 peer and it works together, then it may be in the first message In 1 as a Low_int, 1000 as a high_int, in another message, in 1001 as a low_int, 2000 as high_int, in this type. Finally, the client will open a pipe to these 10 Peer, then transfer messages to them .

The client's SKELETON looks similar to the server, and it is also initialized NET Peer Group, then gets Group discovery services and pipeline services. .

Listing 16.10 primeclient

Package primecruncher;

Import net.jxta.peergroup.peergroup;

Import net.jxta.peergroup.peergroupfactory;

Import net.jxta.discovery.discoveryService;

Import net.jxta.discovery.discoveryListener;

Import net.jxta.discovery.discoveryEvent;

Import net.jxta.pipe.pipeservice;

Import net.jxta.pipe.outputpipe;

Import net.jxta.pipe.pipeId;

Import net.jxta.exception.peergroupException;

Import net.jxta.protocol.discoveryResponsemsg;

Import net.jxta.protocol.modulespecadvertisement;

Import net.jxta.protocol.pipeadvertisement;

Import net.jxta.document.structuredTextDocument;

Import net.jxta.document.mimemediatype;

Import net.jxta.document.textelement;

Import net.jxta.document.advertisementFactory;

Import net.jxta.id.idfactory;

Import net.jxta.endpoint.Message;

Import java.util.enumeration;

Import java.io.stringwriter;

Import java.io.ioException;

Import java.net.URL;

Import java.net.malformedurlexception;

Import java.net.unknownServiceException;

Import java.util.hashset;

Import java.util.set;

Public class primeclient imports discoveryListener {

Private static peergroup group;

Private static discoveryservice discosvc;

Private static pipeservice pipesvc;

Private outputpipe outputpipe;

Private set adverts = new hashset ();

Public primeclient () {

}

Public static void main (string [] argv) {client cl = new client ();

CL.StartJXTA ();

CL.dodiscovery ();

}

Public int [] Processprimes (int low, int high) {

}

Private void startjxta () {

Try {

Group = peergroupfactory.newnetpeergroup ();

Discosvc = group.getdiscoveryService ();

Pipesvc = group.getpidipeservice ();

} catch (peergroupException e) {

System.out.println ("CAN't create net peer group:"

E.getMessage ());

System.exit (-1);

}

}

PRIVATE VOID DODISCOVERY () {

}

}

Although primepeer's key responsibility is to advertise its own service and processing received, PrimeClient must participate in the service discovery process.

The DODISCOVERY () method initializes the service discovery. First, Peer investigates yourself and buffers, in the current number calculation module's instructions to find a notice with the name attribute

Listing 16.11 Performing Local Discovery

System.out.Println ("Starting Service Discovery ...");

System.out.println ("Searching Local Cache for"

ServiceConstants.Spec_name "Advertisements");

ENUMERATION RES = NULL;

Try {

Res = discosvc.getlocaladvertisements (discoveryservice.adv,

"Name", serviceConstants.Spec_name);

} catch (ioexception e) {

System.out.println ("IO Exception.");

}

IF (res! = null) {

WHILE (res.hasmorelements ()) {

ProcessADV (Modulespecadvertisement) res.nextelement ());

}

}

Then, Peer initializes the remote notice discovery, remote discovery means that the discovery request will propagate in the JXTA network. When the appropriate notice is discovered, it will respond. Therefore, remote discovery is a asynchronous process, which delivers DiscoveryListener as a value (argument) to the getRemoteAdVertiTisements () method of DiscoveryService. In addition, we must also explain the number of notices we need to get from each peer.

Once the remote discovery is initialized, the discovered announcement is stored in the local advertisement cache so that when the next time the peer starts working, it will discover an advertisement from this buffer.

Listing 16.12 Initiating Remote Service Discovery

System.out.Println ("Starting Remote Discovery ...");

Discosvc.getRemoteadvertisements (Null, DiscoveryService.adv, "Name", ServiceConstants.Spec_name, 1, THIS)

}

DiscoveryListener illustrates a discoveryEvent () method will find a matching standard notice at each time called, a discoveryEvent includes a discoveryReponsemSG that contains an actual notice found in remote discovery. We have obtained the enumeration of these notices and then operate each.

Listing 16.13 Implementing a DiscoveryListener

Public void discoveryEvent (discoveryEvent event) {

System.out.println ("DiscoveryEvent Called");

DiscoveryResponsemsg Mes = Event.getResponse ();

// Thase Contain The Responses Found

ENUMERATION RES = MES.GETRESPONSES ();

IF (res! = null) {

WHILE (res.hasmorelements ()) {

ProcessADV (Modulespecadvertisement) res.nextelement ());

}

}

}

The processadv () method is very simple. It inserts each modulespecadvertisement to a collection, this collection guarantees that there is no storage of repeated notices, which is used as the buffer of the Module's instructions.

Private Void ProcessAdv (Modulespecadvertisement AD) {

Adverts.Add (ad);

}

转载请注明原文地址:https://www.9cbs.com/read-23225.html

New Post(0)