JXTA4J2ME Implementation Architecture
Searching on a JXTA Network
As explained in the second part, the search method of the PeerNetwork class performs the searching for you. The PeerNetwork.search method takes two parameters, namely the type of the resource that you want to search (peer, group, or pipe) and a query string (name of the resource that you are looking for).
Internally, the search method performs two steps:
- Authors a search query message.
- Sends the search message to the JXTA relay.
// Assuming "peerNetwork" is a PeerNetwork instance. int requestId = peerNetwork.search (PeerNetwrok.PEER, "Waxsys");
This method call will internally generate an array of following elements following the message header:
// The message header jxmg 0 01 05 proxy 05 // List of elements: // Identifies the message as a Search request/query 1- jxel 2 0 07 request 06 search // Type of resource to search for 2- jxel 2 0 04 type 04 Peer // Attribute of the source to search for. // It is hard-coded by JXTA4J2ME as "Name" 3- jxel 2 0 04 attr 04 Name // Name of the resource to search for. 4- jxel 2 0 05 value 06 Waxsys // Request identifier to match responses. 5- jxel 2 0 09 requestId 01 1
We have already discussed the details of the format of the message header and the elements. Now let's see what they mean:
The above message consists of five elements, all of which belong to the "proxy" namespace.
The first element is the request tag that identifies this message as a request query message.
The second element defines the resource that you want to search for. The first parameter that the user supplies (type of the resource to search for) to the PeerNetwork.search method call will be copied as the data field of this element.
The third and fourth elements define a name value pair. The current JXTA4J2ME implementation has hard-coded the data field of the third element as "Name", which means the search request will always try to match the names of resources (peer, group, or pipe) with the data string of the fourth element.
The fifth element is a request identifier. The relay will include this identifier with the response to identify that the response is for this particular search message. The search message generates a request identifier for each search message call and returns the identifier to the calling application, so that it can keep a record of the identifier to match responses.
The search message now needs to be added to the outgoing messages queue. This is the job of a private method named PeerNetwork.sendMessage. The search method will call the sendMessage method and hand the newly authored search message to it. The sendMessage method adds two new elements to the message and then places the Message object to the outgoing messages queue:
// The message header jxmg 0 01 05 proxy 07 // List of elements 1. jxel 2 0 07 request 06 search 2. jxel 2 0 04 type 04 Peer 3. jxel 2 0 04 attr 04 Name 4. jxel 2 0 05 value 06 Waxsys 5. jxel 2 0 09 requestId 01 1 6. jxel 1 0 27 EndpointDestinationAddress 84 HTTP://127.0.0.1:8080/urn:jxta:uuid-7. DEADBEEFDEAFBABAFEEDBABE0000000E05/CrystalGroup 7. jxel 1 0 22 EndpointSourceAddress 95 http://JxtaHttpClientuuid- 59616261646162614A7874615032503384EB6855E3AD46E09696871CC17E/
The first five elements in the above code are the same as those authored by the search method described above.
The last two elements (sixth and seventh) are added by the sendMessage method. The sixth element is the address of the (destination) JXTA relay, while the seventh element identifies the (source) J2ME device on the JXTA network.
Sending Messages to a JXTA Pipe
Recall the PeerNetwrk.send message call discussed in the last article. This method is used to send messages to specific pipes in the JXTA network. However, JXTA4J2ME cannot send messages directly to the pipe. Rather, we will send the message to the relay and include appropriate elements in the message that will tell the relay which pipe is to be used as the transport for the message to its destination peer(s).
You have seen how a search query message is sent. The message is internally authored and then sent to the JXTA relay. This procedure is similar for sending messages to a pipe. The only difference is that search messages are authored internally inside the PeerNetwork.search method and then a call is made to the sendMessage method. When you want to send your own message to a pipe, you will author it yourself and then call the send method of the PeerNetwork class. The send method will internally call the sendMessage method, which will add its two elements and then adds the message to the outgoing messages queue.
The following message consists of nine elements, out of which only two were authored by the client; the rest of the elements were appended by the message sending procedure.
// Message header jxmg 0 01 05 proxy 09 // Elements authored internally by the PeerNetwoek.send() method: 1. jxel 2 0 07 request 06 send // Send request element 2. jxel 2 0 09 requestId 01 1 // Request identifier 3. jxel 2 0 09 name 08 PipeName // Name of the pipe 4. jxel 2 0 09 id 06 PipeID // Pipe ID 5. jxel 2 0 09 type 11 JxtaUnicast // Type of the pipe // Elements authored by the client application/JXTA4J2ME user. // Notice that here we are using the default (empty) namespace. 6. jxel 0 0 04 Name 06 Waxsys // peer name element 7. jxel 0 0 07 Message 04 Hello WaxSys // message for peer // element // Two elements authored by the PeerNetwork.sendMessage() method. 8. jxel 1 0 27 EndpointDestinationAddress 84 HTTP://127.0.0.1:8080/urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE0000000 E05/CrystalGroup 9. jxel 1 0 22 EndpointSourceAddress 95 http://JxtaHttpClientuuid- 59616261646162614A7874615032503384EB6855E3AD46E09696871CC17E/
Polling for incoming messages
The PeerNetwork.poll method call checks for incoming messages on the relay. Internally, it will first check whether there are any outgoing messages in the queue waiting to be sent. It will extract (de-queue) the first message from the queue and append that message to its poll message. If there are no messages, an empty message will be used for polling. It will then call the poll method of the HttpMessenger class, which will send the poll message.
As explained above, the HttpMessenger class is here to provide abstraction for various configurations in J2ME. That's why the actual HTTP communication is the responsibility of the HttpMessenger class.
Following is a typical HTTP request that will be used for polling:
POST /relay HTTP/1.1 Host: 220.127.116.11 Connection: keep-alive x-jxta-command: poll x-jxta-client: uuid-59616261646162614A7874615032503384EB6855E3AD 46E09696871CC17E x-jxta-timeout: 1000 Content-length: 0
In this article, we have described the internal functioning of JXTA4J2ME classes.
We started with the PeerNetwork class and explained the PeerNetwork operation over the sandwiched HttpMessenger class. We also provided sample HTTP messages that request a leased connection into the JXTA network. We then discussed the format of JXTA messages that are exchanged between relays and J2ME devices. Next, we described how messages are authored internally and queued as outgoing traffic. At the end, we discussed the polling procedure that checks with the relay if there are any incoming messages.
In the next article, we'll present a list of value-added J2ME applications that can be built using the JXTA set of protocols. We will also discuss the design of a couple of such applications.
About the Author
Bilal Siddiqui is an Electronics Engineer, an XML consultant, and the co-founder of WaxSys, a company focused on simplifying e-Business. After graduating in Electronics Engineering from the University of Engineering and Technology, Lahore, in 1995, he began designing software solutions for industrial control systems. Later, he turned to XML and used his experience programming in C++ to build Web- and WAP-based XML processing tools, server-side parsing solutions, and service applications. He is a technology evangelist and a frequently published technical author. Bilal has also contributed to a couple of books, namely Java P2P Unleashed and Web Services Business Strategies and Architectures. Readers may contact Bilal at firstname.lastname@example.org.
- Read the first and the second parts of this series.
- Check out the official JXTA4J2ME page at Sun's Web site.
- This book on JXTA is available on-line and the author is inviting everyone to steal it for free.
- Compute Power Market, or CPM for short, is a very interesting grid computing application of the JXTA technology. Visit the project's Web site for details.
Page 2 of 2