Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Pipe Binding Protocol API

The pipe binding protocol is a mix of capabilities that includes the routing of connections and the interfaces for communicating between peers. In other words, the pipe binding protocol describes how we create connections between peers and how we send information.

The Pipe Binding API, as opposed to the JXTA protocol specification, goes a step further by adding the programmatic structure for your application to make connections to send and receive messages. In addition, the API works as a framework for extension to create different types of pipes and different control models.

What Are Pipes?

Pipes can be compared to sockets or streams, in other words, just a channel between computers to transfer data. The difference is that you assume that the P2P network has a few things different from a normal network in that you assume that the connection is not necessarily direct and that there are many protocols that can be used. The pipe binder is a layer above the messy network that hides everything from the developer and adds capabilities that the network does not have via the base protocols alone.

One example of additional capabilities is the ability to cross a firewall. It is rather simple to use HTTP to cross a firewall to connect a server to a client. However, what about connecting two peers, each behind their own firewalls? Suddenly the problem is a little more difficult. Now imagine connecting a dozen clients together at the same time with each one behind its own firewall. Now the problems are almost impossible by normal means. But, by hiding the complexity in the protocol, along with using other JXTA protocols like the peer resolver and peer endpoint router, even the most convoluted interconnection of peers can be simply accessed with a simple pipe abstraction.

So, think of a pipe API as a second cousin to Java's stream API. There are a few differences, but many of the patterns you would normally use are the same. There are also a few extra functions to make our lives easier in a P2P network.

From our prior discussions, remember that there are different types of pipes. These can be asynchronous, encrypted, and other types. The API allows us to select the type we are requesting. Note that the default type of pipe is asynchronous and unidirectional.

The Pipe Service

The pipe service is obtained from your group via the following in the PeerGroup class:

PipeService getPipeService();

The class that is returned implements the PipeService interface. The UML diagram in Figure 3.16 shows the interface and the methods it contains. There are three types of methods in the interface—input pipe creation, output pipe creation, and a method to remove output pipe creation listeners (OutputPipeListener).

Figure 3.16 UML of PipeService interface.


Both input and output pipe creation methods use a pipe advertisement to create the pipe. The type of pipe is specified in the advertisement. The UML for the PipeAdvertisement is shown in Figure 3.17.

A pipe advertisement contains several pieces of information that will be used to find the pipe and to create the pipe. Specific implementations of the advertisement will contain additional information if required.

To find a pipe, the name is usually used, but the ID ensures that you have found a specific pipe.

Figure 3.17 UML of PipeAdvertisement class.

The type of pipe is the most important for creation. There are many different types, and the initially supported types are propagate, secure, and non-blocking (these pipe types are explained fully in Chapter 2).


The InputPipe interface (shown in Figure 3.18) has a close method and two methods for receiving messages. The poll method waits for messages up to a certain timeout value. The waitForMessage method will wait indefinitely for a message.

Figure 3.18 UML of InputPipe interface.

As you might guess, waiting for messages and polling for them will take a bit of work and possibly threads to process messages appropriately. The good news is that you can use the PipeMsgListener to listen to messages. We will cover PipeMsgListener later in this section.


The OutputPipe interface, shown in Figure 3.19, is simpler as the InputPipe interface. The only two methods are close and send.

Figure 3.19 UML of OutputPipe interface.

One Way Pipes?

The initial representation of pipes are one way. The direction of a pipe flows from an output pipe on one peer to an input pipe on another peer. There are implementations of two way pipes, but they are really a wrapper around two pipes created in opposite directions.

The reason for pipes being one way is because of the nature of the P2P network. If the only pathway is via HTTP, full bi-directional capability is not easily created. Remember that HTTP is a response/request protocol. In fact, any bi-directional HTTP pipe is, by definition, a simulation.

Because many peers will be behind firewalls or other barriers, the most logical medium is HTTP. By creating an underlying protocol that can always be deconstructed into messages, the designers have ensured that most peers can be accessed. This also means that you can communicate to multiple peers using the same messages but different protocols. This may seem inefficient, but connectivity was preferred over speed.

The Pipe Process

Pipes are created in a specific order. The process is that the listener must open an input pipe before the talker creates an output pipe. If there is no input pipe available, an output pipe will fail because it cannot connect. This is very similar to a socket that needs to have a listener opened before another machine can connect to it.

The following is a sequence of events for a peer that waits for others to communicate with it:

  1. Group advertises pipe advertisement.

  2. Listener peer creates an input pipe from advertisement.

  3. Talk peer creates output pipe addressed to the listener peer.

  4. Talk peer sends message on pipe.

  5. Listener peer receives message.

The following is the opposite:

  1. Peer wanting information opens an input pipe.

  2. Peer wanting information advertises the pipe.

  3. Peer with data opens an output pipe to the input pipe.

  4. Peer with output pipe sends data.

Connecting Pipes

There are two ways to connect pipes. First is a blind pipe and the second is a peer-addressed pipe. A blind pipe is no different from a stream opened on a port to accept the first caller to a server. A listener pipe is always blind and will accept a connection from any peer. Output pipes can be both blind and specifically addressed.

You may at first be uncomfortable with blind pipes, but they are not different from how we dealt with sockets in the traditional client server world. The one difference is that the resources are group based and not peer based in P2P. The peer you connect to in many P2P applications is not important. For example, a group of peers sharing storage or processing would not care who connected to what peer, only that they connected to a peer within the group that guarantees a resource.

Blind Output Pipe

Calling the following method in the pipe service creates a blind output pipe. The parameters are only the advertisement and a timeout that defines how long to wait for a connection. The timeout is the number of milliseconds to wait or, if –1, to wait forever:

OutputPipe createOutputPipe ( PipeAdvertisement adv
              , long timeout)throws IOException;

This method will block until the pipe connects to an input pipe or the timeout occurs. If you look at the implementation, actual behavior will depend on the type of pipe in the pipe advertisement. For example, if the type is unspecified or if the advertisement type is PipeService.UnicastType, a non-blocking unicast pipe is created. Because there is no endpoint specified, the system just looks for the same pipe advertisement published by another peer and attempts a connection.

If the type of pipe is a propagate pipe, all peers in the group that have open input propagate pipes will simply listen for messages because you are not specifying a peer. Propagate is much more a construct than a physical connection like the unicast pipe. There is not a specific search for peers with the same pipe, because messages are delivered by routing and not by a specific connection. The OutputPipe object returned would send messages to any peer that has an equivalent input pipe.

Remember that unicast and propagate pipes are unreliable. This means that you need to have code that accounts for dropped messages.

Blind Output Pipe with Listener

The method for creating a blind output pipe with a listener is a little different from one that directly returns a pipe. The key difference is that as peers are found, the listener is called with a new output pipe. The listener interface and the event class are shown in Figure 3.20.

The following is a simple interface that allows you to create output pipes as input pipes appear on other peers:

void createOutputPipe ( PipeAdvertisement adv
            , OutputPipeListener listener)throws IOException;

Figure 3.20 UML of OutputPipeListener interface and OutputPipeEvent class.

Addressed Output Pipe

With an addressed pipe, you are looking to connect with a specific peer. The create method will block until that peer is connected. If the peer cannot be found within a specific time, the creation fails.

The following method signature from the PipeService class adds an Enumeration of peers. Be careful to only pass in multiple peers if the pipe is a type of propagate pipe.:

OutputPipe createOutputPipe ( PipeAdvertisement adv
              , Enumeration peers
              , long timeout) throws IOException;

Blind Input Pipe

Input pipes are a bit different from output pipes because they are not addressed. In other words, they are always blind. The following method signature from the PipeService class returns an input pipe object. To receive a connection, you need to call the waitForMessage method that blocks until an output pipe on another peer attempts to connect.

InputPipe createInputPipe (PipeAdvertisement adv) throws IOException;

Blind Input Pipe with Listener

The signature for the next input pipe type allows you to specify a message listener. Note that this is not a listener for pipes but for messages. There is no need to process multiple output pipes connecting to the input pipe. The input pipe is only created to listen to one connection. If you want to accept messages on another peer, you need to create a new input pipe:

InputPipe createInputPipe ( PipeAdvertisement adv
             , PipeMsgListener listener) throws IOException;

The UML of the listener and the listener event in Figure 3.21 shows how simple the system is. The listener's only method is pipeMsgEvent, which passes a PipeMsgEvent that, in turn, passes the Message object received.

Figure 3.21 UML of PipeMsgListener interface and PipeMsgEvent class.

Unicast input pipes are simply waiting for messages sent to the propagate pipe's ID. For this reason, this method should return as soon as the pipe is initialized. Note that there may not ever be a corresponding propagate output pipe and the listener may never be called.

With the listener, there is no need to process messages by waiting. Instead, the listener is called every time a message is received. To remove a PipeMsgListener, close the input pipe.


The pipeMsgEvent method in your implementation of the PipeMsgListener must be thread safe. This method could easily become a bottleneck if the method is synchronized causing the thread to block for the entire run time of the method. Use synchronized blocks inside the method around shared objects. The method should be callable as soon as possible to process as many messages as possible.

Removing the Output Listener

To stop accepting requests for output pipes, you need to remove the listener. After the listener is removed, the output pipe is no longer available. The following is the signature for the removeOutputPipeListener method:

OutputPipeListener removeOutputPipeListener(String pipeID
                      ,OutputPipeListener listener);


Messages are meant to be very simple containers of data. In the following lines, the message is created in the context of the pipe service. The data is created by adding a tag called Test and setting it with the raw bytes created from the "Hello, World!" string:

Message msg = peerGroup.getPipeService ().createMessage ();
msg.setBytes ("Test", "Hello, World!".getBytes ());

Bidirectional Pipes

The BidirectionalPipeService is an optional service that you can use to create bi-directional pipes. As we discussed when talking about one-way pipes, a bi-directional pipe is really two one-way pipes in complementary directions. The BidirectionalPipeService class and accompanying classes are shown in Figure 3.22.

Figure 3.22 UML of BidirectionalPipeService and related classes and interfaces.

Wire Pipes (One-to-Many)

The Wirepipe class is used to create a pipe that will broadcast a copy of each message to each of a list of specified peers. Only peers that have accepted the pipe will receive messages.

Reliable Pipes

This ReliablePipeService class provides reliable message-delivery pipes. Each of the messages will be received in the order that they were sent, and the message is guaranteed to reach its destination. Reliable pipes can also be encrypted.

  • + Share This
  • 🔖 Save To Your Account