Interacting with a Remote Peer via a Shell


After we've created a pipe to the peer offering the prime-finding service, we can use that pipe to interact with the peer. Recall from Chapter 16 that the interaction between the prime-finding service and a peer wanting to use that service is via a message exchange: A client peer would need to format a message and send it to the prime-finder service's output pipe. The message must contain the advertisement for an input pipe through which the prime finder service can send back its response. Figure 17.4 shows that interaction.

Figure 17.4. Message interfaces between the prime-finder service and other peers.

graphics/17fig04.gif

Therefore, we first must create a message from the JXTA Shell, which is accomplished via the mkmsg command:

 JXTA>querymessage = mkmsg  

A message initially contains no message elements. We will need to add message elements corresponding to the high and low numbers, indicating the range of prime numbers we want to search for, a job ID, as well as an input pipe advertisement. We will then use the put command to add each of those elements to the message.

The put command takes three arguments: a reference to a message, a unique identifier tag, and a document. The unique tag can be any string. put assigns the document's content to the unique tag inside the message. The document argument must refer to a structured document assigned to a shell variable.

At the time of this writing, the JXTA Shell does not have a built-in command to construct structured documents from the command line. As we've seen earlier, the shell does have the capability, however, to import the contents of an arbitrary file and assign that content to a document. Therefore, we will create three files, corresponding to the high, low, and jobID message elements, respectively. These documents are very simplistic XML files.

The high.xml file is as follows:

 <?xml version="1.0"?>  <high> 100 </high> 

Here's low.xml:

 <?xml version="1.0"?> <low> 10 </low> 

And this is id.xml:

 <?xml version="1.0"?>  <id> 12345 </id> 

We can now import each of these files and assign their contents to shell variables:

 JXTA>importfile -f /export/home/myfiles/high.xml highdoc  JXTA>importfile -f /export/home/myfiles/low.xml lowdoc JXTA>importfile -f /export/home/myfiles/id.xml iddoc 

You can verify the contents of any of the these variables with the cat command:

 JXTA> cat highdoc  <?xml version="1.0"?> <high> 100 </high> 

We are ready to use the put command to assign these documents to the message. Note that the service's wire protocol specifies that each message element be tagged with a well-known name. Those names are contained in the ServiceConstants interface (see Chapter 16). We will use the appropriate names from that interface when assigning each message element to the message:

 JXTA>put querymessage HIGH_INT highdoc  JXTA>put querymessage LOW_INT lowdoc JXTA>put querymessage JOBID iddoc 

Finally, we must specify an input pipe so that we can receive a response from the prime-finder service. Creating an input pipe from the JXTA shell is similar to creating an output pipe: You first create a pipe advertisement with the mkadv command, and then create the pipe itself from that advertisement.

 JXTA>inpipeadv = mkadv -p  JXTA>inputpipe = mkpipe -i inpipeadv 

The -i parameter to mkpipe specifies that we are interested in creating an input pipe, not an output pipe.

The final step in preparing the message to be sent is to add the input pipe's advertisement as an element to the message. Because the put command accepts only document arguments, and because a pipe advertisement is not a JXTA-structured document type, we need to again overcome this limitation by exporting the advertisement to the filesystem and reading it back from that file into a document. The resulting document can then be assigned as a message element:

 JXTA>exportfile -f /export/home/mydocuments/inpipeadv.adv inpipeadv  JXTA>importfile -f /export/home/mydocuments/inpipeadv.adv pipeadvdoc JXTA>put querymessage PIPEADV pipeadvdoc 

Finally, we are ready to transmit the message via the prime-finder service's output pipe. The send command accomplishes that:

 JXTA>send primeservice querymessage  

The prime-finder service has no knowledge that it is interacting with a user via the JXTA Shell rather than the client application we constructed in Chapter 16 because both use identical wire protocols. The service's response is also identical: Upon computing the list of prime numbers between the HIGH and LOW values, the service constructs a response message, opens a pipe back to the client, and sends that response message back via the newly opened pipe. Because we specified the advertisement of the input pipe we created earlier, the service will use that pipe to send its response. We instruct the input pipe to receive a message using the recv command:

 JXTA>response = recv inputpipe  recv has received a message 

At this point, the reply message the service sent is assigned to the response environment variable. The reply message protocol specifies (see Chapter 16) that a RESULT tag mark the message element containing the service's response. We can obtain that message element via the get command, which is the counterpart of put:

 JXTA>result = get response RESULT  

Finally, we can display the contents of the response with the cat command:

 JXTA>cat result  <?xml version="1.0"?> <!DOCTYPE RESULT> <RESULT>      <JOBID>          12345      </JOBID>      <LOW_INT>           10      </LOW_INT>      <HIGH_INT>           100      </HIGH_INT>      <STARTTIME>           1021004688485      </STARTTIME>      <ENDTIME>           1021004688485      </ENDTIME>      <RESULTSTRING>           11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97      </RESULTSTRING> </RESULT> 

Thus, we were able to interact with the remote peer's service entirely via the JXTA Shell, without having to write a single line of code. Granted, this prime-finding method communicates with only one remote peer, but the example demonstrates some of the simple yet powerful JXTA Shell capabilities.



JavaT P2P Unleashed
JavaT P2P Unleashed
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 209

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net