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.

PipeAdvertisement

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).

InputPipe

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.

OutputPipe

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.

Caution

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

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

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020