JXTA, P2P programming technology routine (2)

zhaozj2021-02-16  57

Create and release notice

As we said earlier, the JXTA virtual network relies on the JXTA ID to identify network resources. The discovery of these resources is notified. Net.jxta.id package contains ID classes, and colleagues contain factories that create a variety of IDs.

In JXTA, Net.jxta.Document.document is a data universal container. A document in JXTA is defined by the content of the MIME media type. In this way, the document is similar to the HTTP stream (stream. JXTA does not attempt to explain the content of the document: This is part of an application layer protocol). An advertisement is a structuredocument consisting of a nested multi-layer element similar to the XML structured document, which allows a document to be operated when its data is not physically represence.

Just as any other StructureDocument, an advertisement can be represented by XML or simple text format. An advertisement includes the ID, the type of advertisement it wants to notify, and the type of advertisement, and the expired time absolute value. The JXTA API provides a convenient factory, AdvertisementFactory to create a variety of notification types. Listing 16.3 illustrates a ModuleClassADvertisement creation through this plant. Note that ModuleClassID is added to the notification)

Listing 16.3 Creating and Advertising A Module Class

Private void doadvertise () {

ModuleClassADvertisement classad =

(ModuleClassAdvertisement) AdvertisementFactory.Newadvertisement (

ModuleClassAdvertisement.getadvertisementType ());

ModuleClassID classid = idfactory.newmoduleclassid ();

ClassAd.SetModuleclassId (ClassID);

ClassAd.setname (ServiceConstants.class_name);

ClassAd.SetDescription ("a prime number crunching service.");

Try {

Discosvc.publish (Classad, DiscoveryService.adv);

Discovc.RemotePublish (Classad, DiscoveryService.adv);

System.out.Println ("Published Module Class Adv.");

} catch (ioexception e) {

System.out.println ("Trouble Publishing Module Class Adv:"

E.getMessage ());

}

JXTA NET.JXTA.DISCOVERY.DiscoveryService is a Group Service provided by NET Peer Group. It provides publishing discovery and 2 modes of science and remote. Local mode In the PEER-locally buffered, the local release is discovered, and local publishing is to let the advertisement into the local buffer. The remote is to discover and release throughout the Peer group. Therefore, request messages are propagated in the case of our earlier protocol through the JXTA virtual network, and respond to these requests when they are in the end. Therefore, remote discovery is a asynchronous process, and finds a certain amount of notification on the network. Listing16.3 Description ModuleClassADvertisement remote and native two types of release methods. The previous process, we create a Modulespec ID through the IDFactory class and get an announcement from AdvertisementFactory. (See liSTING 16.4)

Listing 16.4 Creating a New Modulespecadvertisement

Modulespecadvertisement Specad =

(Modulespecadvertisement) AdvertisementFactory.Newadvertisement (

Modulespecadvertisement.getadvertisementtype ());

Modulespecid specid = idfactory.newmodulespecid; classid;

Specad.SetModulesPecId (Speci);

Specad.setname (ServiceConstants.SPEC_NAME);

Specad.SetDescription ("Specification for a Prime Number Crunch");

Specad.SetCreator ("Sams Publishing");

Specad.setspecuri ("http://www.samspulishing.com/p2p/primecruncher);

Specad.setversion ("Version 1.0");

We should remember that Modulespecadvertisement defines a telegram protocol, or a network behavior, to access a service, so we need to provide a PiPEADVERTISEMENT as a parameter to modulespecadvertisement. Because Module's announcement will be placed in the network of Peer, then we must confirm that each modulespecadvertisement is in the same PIPE. In this way, we have to advertise the PIPE to a permanent memory and read data from this memory when creating a new pipe. (If this notice is not stored to disk, then recreate a new one.)

Listing 16.5 Creating a Pipe Advertisement

PipeadVertiTisement Pipead = NULL;

Try {

FileInputStream IS = New FileInputStream (PIPE_ADV_FILE);

Pipead = (Pipeadvertisement) AdvertisementFactory. Newadvertisement

New MimeMediaType ("Text / XML"), IS);

Is.close ();

} catch (ioexception e) {

Pipead = (pipeadvertisement) AdvertisementFactory. Newadvertisement (PipeadVertiTisement.GetadvertisementType ());

Pipeid pid = idfactory.newpipeid (Group.getpeerGroupId ());

Pipead.SetPipeId (PID);

// save pipead in file

Document PipeAddoc = Pipead.getDocument (New MimeMediatype ("Text / XML"));

Try {

FileOutputStream OS = New FileoutputStream (PIPE_ADV_FILE);

PipeAddoc.sendTostream (OS);

Os.flush ();

Os.Close ();

System.out.println ("Wrote Pipe Advertisement to Disk.");

} catch (ioException ex) {

System.out.println ("Can't Save Pipe Advertisement To File"

PIPE_ADV_FILE);

System.exit (-1);

}

}

The following code segment saves a pipe advertisement on the disk as an XML format, for example, running this code to get the XML document later.

URN: JXTA: UUID-59616261646162614E5047205032503382CCB236202640F5A242ACE15A8F9D7C04

JxTaunicast

Then we pass the PipeadVertisement as a parameter to Modulespecadvertisement, such as Listing 16.6

Listing 16.6 Adding The Pipeadvertisement as a parameter to the modulespecadvertisement

Specad.SetPipeAdvertisement (Pipeadv);

At this time, we are ready to post Modulespecadvertisement to local and remote.

Listing 16.7 Local and Remote Publishing of A ModulespecadVertisement

Try {

Discovc.publish (Specad, DiscoveryService.adv);

Discovc.Remotepublish (Specad, DiscoveryService.adv);

System.out.println ("Published Module Spec Adv";

} catch (ioexception e) {

System.out.Println ("Trouble Publishing Module Spec Adv:"

E.getMessage ());

}

(In Listing 16.8, we finally built an InputPipe on this pipeline notice.

Listing 16.8 InputPipe Creation from a pipeadvertisement // Create An Input Pipe Based on The Advertisement

Try {

InputPipe = pipesvc.createInputpipe (PIPEAD);

System.out.println ("CREATED Input Pipe");

} catch (ioexception e) {

System.out.println ("Can't create Input Pipe." E.getMessage ());

}

}

These are all the steps that release a new JXTA service. We know that a Module's infleaves describe the functionality in the peer group: it is a very abstract concept, a bit similar to the interface of Java definition API, but does not provide implementation. An Module's instructions are announced in another aspect that the telegram protocol has been discounted to access a service. In this case, this telegram protocol includes an InputPipe that allows other peer to send a message to him, which is these messages that contain two boundary values ​​required)

Processing Messages from an inputpipe (Processing Messages from InputPipe)

The next step is to implement the message that the prime query peer processing is accepted, and we will operate the message, calculate the required rigor sequence, and send a response.

Listing 16.9 Processing Messages on An InputPipe

Private void startservice () {

While (true) {

Message msg = NULL;

Try {

MSG = INPUTPIPE.WAITFORMESSAGE ();

} catch (interruptedexception ex) {

InputPipe.close ();

Return;

}

String highint = msg.getstring (serviceConstants.High_INT);

String lowint = msg.getstring (serviceConstants.low_int);

IF (highint! = null || lowint! = null) {

ProcessInput (HIGHINT, LOWINT);

}

}

}

Just as before, Net.JXTA.Endpoint.Message object was sent between 2 peer by EndPointService) (a message included a set of MessageElements, indicating that a destination makes it more convenient to pass through the JXTA network. One message The element can be any byte array, and the message also includes the ability to extract the element in a string. When a new message element is indicated, it can be associated with a MIME type, just a string as an element value. In this implementation, we refer to the key value of ServiceConstants.high_int and serviceConstants.low_int, if these two elements are valid strings, we will pass them in a private way: ProcessInput ()

ProcessInput () works on the execution of this algorithm, generating a list including all prices (between low_int and high_int), in order to save space, we will not write this part of the code here.

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

New Post(0)