Pipe Messages

The next step in the implementation of our RestoPeer is to implement its handleFriesRequest( ) method. This method is responsible for reading requests from HungryPeers and sending responses to them; it shows us how to pass messages over pipes.

Here’s how we implement this method:

// Method to handle fries auction requests from HungryPeers. The method // waits for HungryPeer requests' pipe messages to arrive. Incoming requests // contain a pipe advertisement for responding to the HungryPeer's requester // and a fries size. The method generates a bid offer for the request, opens // an output pipe to the HungryPeer requester, and sends the response. private void handleFriesRequest( ) { InputStream ip = null; // Input stream to read message PipeAdvertisement hungryPipe = null; // HungryPeer requester pipe StructuredDocument request = null; // Request document StructuredDocument bid = null; // Response document // Document mime types MimeMediaType mimeType = new MimeMediaType("text", "xml"); Element el = null; // Element in document String name = null; // Name of the sender String size = null; // Fries size requested OutputPipe pipeOut = null; // Output pipe to respond to // HungryPeer requester System.out.println("RestoNet Restaurant (" + brand + ") waiting for HungryPeer requests"); // Loop waiting for HungryPeer requests while (true) { Message msg = null; // Incoming pipe message try { // Block until a message arrives on the RestoPeer pipe msg = pipeIn.waitForMessage( ...

Get JXTA in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.