JXTA, P2P programming technology routine (4)

zhaozj2021-02-16  57

Advertising treatment

After we have established the discovery listener, it will not stop adding some newly discovered Module descriptions to our local buffer. When the ProcessPrimes () method is called, the customer peer will try to connect to the PEER representative of the Module documentation, access to their input channel, pass a message to initialize this peer's rigid number discovery service. The first element of this method is to determine that we can entrust the peer collection, you should remember that a notice has a term limit, so we have to eliminate those notified notices. Public int [] processPrimes (int low, int high) {Set setCopy = null; synchronized (adverts) {Set setCopy = (Set) adverts.clone ();} ArrayList workingList = new ArrayList (); ArrayList expired = new ArrayList ( ); long currentTime = System.getCurrentTimeMillis (); Iterator it = workingSet.iterator (); while (it.hasNext ()) {ModuleSpecAdvertisement ad = (ModuleSpecAdvertisement) it.next (); if (ad.getLocalExpirationTime ()> currentTime (2 * 60 * 1000)) {WorkingList.AddeElement (AD);} else {expired.addelement (AD);}} RemoveExpired (Expired); The aforementioned block performs the service discovered in a simple buffer Manage, let the removeExpired () method delete those expired and upcoming announcements (there are not detaoveexpired ()). When we have a collection of effective notices, we can start working on them to get pipe notices we need to send messages. Note that the distribution of this work is human: some peer may be more powerful than other peer, some of which may be better network connection. These differences should be considered when allocating, in fact, it will not deliberately assign work to excessive fragments, because network communication time may be more time than spending the actual calculation. (The purpose of this example is how to get a pipe advertisement from a Modulespecadvertisement, which creates a new message, and how to send a message through the pipe.) Listing 16.14 shows how these natural numbers are divided into a sub-list Each sub-column can also correspond to a message that will be sent to other Peer.

The message is inserted into a Hash table map, the mapping key value shows the status of the message: Is it sent? Will we receive its results? Listing 16.14 Creating New Messagesmap MessageMap = New HashMap (); INT Size = WorkingList.Size () INT MOD = HIGH% Size; High - = MOD; INT Perpiece = high / size; for (INT i = 0; i

// we neither sent the message, nor did we get a response StatusMap statusMap = new StatusMap (false, false); StatusMap statusMap = new StatusMap (false, false); messageMap.put (statusMap, msg);} StatusMap is a pair of Boolean, not listing our final step is to extract pipe notifications from each modulespecadvertisement, open each pipe, send a message to that pipe, then mark this message as "already sent". I should remember that an advertisement is a structured document. It is similar to the XML document. It can be easily converted into a text document and then print it out. It is very beneficial to see the content of the notice when developing and testing. Listing 16.15 Printing an AdvertisementCollection ads = messageMap.values ​​(); Iterator it = ads.iterator (); while (it.hasNext ()) {ModuleSpecAdvertisement ad = (ModuleSpecAdvertisement) it.next (); // First, print out ModuleSpec advertisement on standard output StructuredTextDocument doc = (StructuredTextDocument) ad.getDocument (new MimeMediaType ( "text / plain")); try {StringWriter out = new StringWriter (); doc.sendToWriter (out); System.out.println (out) Out.close ();} catch (ioException e) {} ... As we have previously discussed, a StructuredTextDocument class includes a lot of elements, one of which is a parameter. When we construct Modulespecadvertisement for our services, we use it as a parameter into the pipeline notice of this service. This parameter is exactly another structuredDocument element, and we can manipulate it with the same way as action XML documents. During parsing the parameter elements of the notice, we first get the ID and type of this notice, the channel ID is uniform, and there is an overview in the JXTA description. Is an example of this URN. URN: JXTA: UUID-59616261646162614E5047205032503382CCB236202640F5A242ACE15A8F9D7C04 IDFACTORY class has a PIPEID object based on each URN. That is, we use the PIPE ID to associate a mechanism for a PIPE announcement.

Listing 16.16 Working with Advertisement Parameters StructuredTextDocument param = (StructuredTextDocument) ad.getParam (); String pipeID = null; String pipeType = null; Enumeration en = null; if (! Param = null) {en = param.getChildren ( "jxta: Pipeadvertisement ");} enumeration child = null; if (en! = Null) {child = ((textelement) en.nextelement ()). GetChildren ();} if (child! = Null) {while (child.hasmorelements )) {Textelement EL = (textelement) child.nextelement (); string elementname = el.getname (); if (elementname.equals ("id")) {pipeid = el.gettextValue ();} if (Elementname.Equals) ("Type")) {pipetype = el.gettextValue ();}}}} (pipeid! = Null || PIPETY ! Pe = null) {PipeAdvertisement pipeAdvert = (PipeAdvertisement) AdvertisementFactory.newAdvertisement (PipeAdvertisement.getAdvertisementType ()); try {URL pidURL = new URL (pipeID); PipeID pid = (PipeID) IDFactory.fromURL (pidURL); pipeAdvert.setPipeID (PID);} catch (mAlformedurlexception e) {system.out.println ("WRONG URL:" E.GETMESSAGE ()); Return;

} catch (unknownServiceException e) {system.out.println ("Unknown Service:" E.GETMESSAGE ()); Return;}} Based on this pipeadvertisement, we now have the ability to construct an output pipeline to connect remote Peer input pipeline . Such as Listing 16.17, you should remember that a pipe is a one-way communication channel, so we don't expect to get a remote Peer's receipt from this pipe. Remote Peer makes a necessary similar work, open a pipeline to connect back to the client, then along with The result is sent back together. Listing 16.17 Creating an Output Pipe try {outputPipe = pipeSvc.createOutputPipe (pipeAdvert, 30000); outputPipe.send (msg); System.out.println ( "Sent message on output pipe");} catch (IOException e) {System. Out.println ("CAN't Send Message THROUGH PIPE: E.GetMessage ());}} Interesting is that this pipe creation mechanism is: a peer may be connected to it between it in sending modulespecadvertisement and a client. Identity, though, this peer virtual identity will not change in the JXTA network, and the service is guaranteed to be connected by the Directors notified by Modulespecadvertisement. To turn on the application service, determine all JXTA classes in the ClassPath and enter the following command. Java PrimeCruncher.primepeer You can also run the following parameters that allow you to make a JXTA login interface. java -D net.jxta.tls.principal = USERNAME -Dnet.jxta.tls.password = PASSWORD primecruncher.PrimePeerBy substituting your JXTA username and password you can run the client similarly :( JXTA replace your user name and password you like You can run the client.) Java -dnet.jxta.tls.princiPal = username -dnet.jxta.tls.password = password primecruncher.primeclient This is a simple development of P2P, including some probably, I hope to everyone A little help, my translation level is also limited, I hope to understand.

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

New Post(0)