JXTA, P2P programming technology routine (1)

zhaozj2021-02-16  54

In this example, we will design and write a distributed JXTA application to solve parallel computing problems. We will build this app in a repeated form, expand its capabilities and use the API set in each step. These original code can only overwrite the most important part, you need all code to check on the website

Some subsets of some large calculations can be solved with parallel methods. Parallel implementation of a job means you can decompose a problem into several small child problems, which can be performed at the same time. When a sub-problem is over, it will return its own results to the main program, and the main program combines these returns to a larger answer.

For example, consider working in any 2 rigs. The number is those natural numbers that can only be removed by itself and 1. Those who can be smaller than those smaller than they are smaller than their own. Therefore, the easiest way to generate a list of rigmeous numbers is to eliminate all its compliance in the natural number list, and the remaining is the number.

This method repeatedly eliminates the number of natural numbers. It uses each of the numbers to remove 2 to its square root. If any of the numbers are not there, then this is a compliance, and it is marked. When these repeated (work) are over, all the marked numbers will be eliminated, and the remaining is the number.

But if it is a long list, there are millions of lengths, then we divide it into a few small list, then perform the above method for each list. Each sub-calculation is distributed to different machines on the network to perform, fully utilize distribution computing resources. The quality query is one of these large problems that can be decomposed in parallel. Now there are many popular P2P-style software, such as the seti @ Home project, its goal is to decode signal from outer space, looking for alien intelligent organisms Similar projects also allow users to share the resources of their idle CPU to simulate the folding of proteins or decode DNA strings.

In this application, the main program will request two numbers to the user, and then generate a number of rigid numbers between 2 characters. This main program will first try to find other peers that provide rigid query services on the JXTA network, and then distribute the list of the list to them. When a peer completes its part, it will return an array of prime numbers in this clip.

For this allocation, we must let all Peer can announce (advertise) it has the ability to have the ability to query, so that other peer can be found and connected.

JXTA Application Design

One of the most common aspects of this application is that each peer plays both the master process role and a slave role (slave), and it is possible that a subordinate program also decides whether further decomposition of this problem Subtasks. This service mode / customer mode operation is the essence of P2P programming. We define it as SM / CM operation

Message definition

When we design a JXTA program, we must endure JXTA is a message-based system: the main protocol in 2 peer is mainly through the message. In this way, the first job of the design program is to define messaging. In this line of query applications, a peer passes a message containing 2 borders to another Peer, the party is accepted in these two borders in the number of numbers, returning to the previous peer

Net.jxta.endpoint.Message class summarizes a message that allows you to give any messages a key (key). We will use the key-value in the table below to represent the instance of this class.

Table 16.1. Request Message

Key

Value

ServiceConstants.low_int

Lower Boundary of The (SUB) List

ServiceConstants.high_int

Upper Boundary of the (SUB) ListTable 16.2. Response Message

Key

Value

ServiceConstants.low_int

Lower Boundary of The (SUB) List

ServiceConstants.high_int

Upper boundary of the (sub) list

ServiceConstants.primelist

.

Service definition and discovery

Next, we must define a way to let the main program find a subordinate program. In other words, we let Peer get the other Peer that provides this service in advance.

As I mentioned earlier, a JXTA service is defined by its Module class and Specification. Therefore, we will define an advertisement for this rigmer query service module and specification. Then let a peer provides a service that makes these notices to spread in the JXTA network. This rigor query Module class will use JXTACLASS: com.sams.p2p.primeCruncher this name, Module's SPEC will use the name jxtaspec: com.sams.p2p.primecruncher this name

The main program will use this name to discover the instructions for the announcement Module.

Therefore, in addition to the definition of the message, the service name string is also the information that Peer should get in the design phase. All peer interactions will be discovered when running.

Service implementation

When a rigmer query service starts, it will initialize the JXTA platform to get the channel to World and Net Peer Group. When only started, Peer will create and publish its notices, including its Module class and module description notices.

Template Description Notice will contain a pipeline notice. Those who find one template for this service will make this pipe notice, and connect to that service through this pipe.

After publishing the notice, our service opens an input channel and monitors the messages come in. When a message arrives, this service attempts to get the two boundary numbers from this message, and then pass them to a component that only produces a rigmer linked list. When the component returns the result (a numerous array), this rigidity service will generate a message containing the result and send this message back to the client. In the first repetition, the service will simply print out the message it accepts. In the next refining, it will open a pipe to send the result back to the client. The client will combine these results obtained from each peer and store the last number into the file. .

Listing 16.2 Outline of PrimePeer and Initialization of a Jxta Peer

Package primecruncher;

Import net.jxta.peergroup.peergroup;

Import net.jxta.peergroup.peergroupfactory;

Import net.jxta.peergroup.peergroupid;

Import net.jxta.discovery.discoveryService;

Import net.jxta.pipe.pipeservice;

Import net.jxta.pipe.inputpipe;

Import net.jxta.pipe.pipeId;

Import net.jxta.exception.peergroupException;

Import net.jxta.protocol.moduleclassadvertisement;

Import net.jxta.protocol.modulespecadvertisement; import net.jxta.protocol.pipeadvertisement;

Import net.jxta.document. *;

Import net.jxta.platform.moduleclassID;

Import net.jxta.platform.modulespecid;

Import net.jxta.id.idfactory;

Import net.jxta.endpoint.Message;

Import java.io.fileinputstream;

Import java.io.ioException;

Import java.io.fileoutputstream;

Import java.io.stringwriter;

Public class princimepeer {

Private static peergroup group;

Private static discoveryservice discosvc;

Private static pipeservice pipesvc;

Private InputPipe InputPipe;

Private static final string PIPE_ADV_FILE = "primeserver_pipe.adv";

Public static void main (string [] argv) {

PrimePeer PP = New primepeer ();

PP.StartJXTA ();

pp.doadvertise ();

pp.startService ();

}

Public princimepeer () {

}

Private void startjxta () {

Try {

Group = peergroupfactory.newnetpeergroup ();

Discosvc = group.getdiscoveryService ();

Pipesvc = group.getpidipeservice ();

} catch (peergroupException e) {

System.out.println ("Cannot Create Net Peer Group: E.GetMessage

));

System.exit (-1);

}

}

/ **

* CREATE AND PROPAGATE Advertisements

* /

Private void doadvertise () {

...

}

/ *

* Start Up The Service, Listen for Incoming Messages on The Service's Input Pipe.

* /

Private void startservice () {

...

}

/ **

* Compute The Requested List of Prime Numbers.

* /

Private void processInput (string high, string low) {

...

}

}

In this StartJXTA () service initialization method, we first get a reference to World Peer Group: This is made through a static PeerGroupFacRory. Calling this method will prepare for JXTA's run. In the next step, we will get a reference to the Peer Group service provided by 2 Net Peer Group: DiscoveryService and the PipeService. We will use them when creating a service notice.

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

New Post(0)