JXTA Exploration: Analysis of the Implementation and Principle of Bi-Directional Pipes
* What is pipe (PIPE)?
The pipe is a more important concept in JXTA. The pipe is a virtual channel between Peer. Typically, we believe that peer-to-peer communication is a single communication connection. But it is not always like this. Many Peer cannot be connected directly because of the presence of firewalls, NAT and other obstacles. At this time, the pipe is more like a virtual layer on a variety of communication protocols, which can provide relay support to the PEER for the gateway.
The advantage of using it is that there is no need to care about the true address and protocol used by Peer, using abstract pipelines, can provide powerful features for P2P applications and reduce complexity.
* Why analyze the implementation principle of bidirectional pipeline?
In J2SE reference implementation in JXTA, three most basic types of pipes are provided, respectively:
A unionessType
B One-way Safety Pipeline (UnicastSecureType)
C Broadcast Pipeline (PropagateType)
In practical applications, some more useful types can be formed by a combination of basic types of pipes, such as bidirectional PIPE. Because we know that in P2P applications, such as chat, transfer files, etc., you need to implement two-way pipes. Two useful classes (Jxtabidipipe and JxtaserverPipe) are available in JXTA's J2SE reference implementation, and a two-way pipe is implemented by a smart mechanism.
Although when using two-way pipes, there is no need to understand the internal implementation methods, but I think that the implementation of the bidirectional pipeline has certain help to use the bidirectional pipe, which is also useful for further analysis of JXTA, hoping to pass the following analysis, you can give JXTA Some useful prompts for developers, and also as a reference.
Because JXTA has been in constant development after launch, there are many inconsistencies between 2.0 and 1.0 versions, so the code listed in this article is based on the latest JXTA2.0 API, in JXTA's J2SE reference implementation 2.3.1 Debug pass on the version.
* The process of establishing two-way pipes with JXTA
Applications to write a two-way pipe need to use the following two classes: Jxtabidipipe and JxtaserverPipe, they have implemented a handshake protocol, that is, using a structured XML document that is commonly filled in the connection, using Message to describe this document in JXTA. This message has the following content
Communication data
In the process of actually establishing and using a two-way pipe, do not need to understand this Message, here is just the principle established by the back analysis.
We know, even in the P2P system, there is also a connection initiation node (set to peera), and a connection acceptance node (here is assumed to peerb)
First initiate the JXTabidipiPipe connection using the following statement in the programs in the program:
Try {
FileInputStream IS = New FileInputStream ("Pipe.adv"); Eg.PipeAdv =
(Pipeadvertisement) AdvertisementFactory.Newadvertisement (MimeMediatype.xmlutf8, IS);
Is.close ();
System.out.println ("CREANG THE BIDI PIPE");
Eg.pipe = new jxtabidipipe ();
Eg.pipe.connect (eg.netpeergroup,
NULL,
Eg.pipeadv,
180000,
// register as a message listener
EG);
} catch (exception e) {
System.out.println ("Failed to Read / Parse Pipe Advertisement);
E.PrintStackTrace ();
System.exit (-1);
}
In the Node B, the following method responds to this connection: (JXTASERVERPIPE is used here, this is a secondary class, its behavior is very similar to the commonly used Serversocket in JDK development)
Try {
FileInputStream IS = New FileInputStream ("pipe.adv");
Eg.pipeadv =
(Pipeadvertisement) AdvertisementFactory.Newadvertisement (MimeMediatype.xmlutf8, IS);
Is.close ();
Eg.ServerPipe = New JxtaserverPipe (Eg.NetpeerGroup, Eg.PipeAdv);
// We want to block unsteil a connection is establish
Eg.ServerPipe.SetPiPiPipetimeout (0);
JXTabidipipe bipipe = serverpipe.accept ();
IF (bipipe! = null) {
System.out.Println ("Bidi Pipe Created");
ReceiveAndsendTestMessage (bipipe);
}
} catch (exception e) {
System.out.println ("Failed to Read / Parse Pipe Advertisement);
E.PrintStackTrace ();
System.exit (-1);
}
After the connection between the two-way pipeline is established, simple communication can be made.
Note that the behavior of Jxtabidipipe and JxtaserverPipe is very similar to Socket and Serversocket in JDK. I think this is JXTA designer intention to make it more convenient to use these classes more convenient to make Java developers.
* JXTA Bidirectional Connection Object (Jxtabidipipe Type Object)
You can use IsBound () to determine if the two-way connection of the JxtabidipiPipe object is established.
You can use getMessage () to receive messages on JXTabidipipes.
You can send a message on the JXTabidipi with SendMessage ().
* Source code analysis of the principles of bidirectional pipeline
Before analysis, the rough establishment of a one-way pipe in JXTA is now analyzed, because the bidirectional pipe is actually a combination of two two-way pipelines.
The establishment of one-way pipeline is relatively simple (take two nodes Peera and Peerb as an example)
(1) First PEERA establishes an input pipe (INPUT PIPE) in accordance with a pipeline advertisement; (2) Peerb establishes an output pipe according to the same advertisement;
(3) If the PEERB's output pipe is established, Peerb will receive a corresponding connection has been established (by response to OutputPipeEvent), then a one-way connection from b to a is established, can pass in A Respond to PIPEMSGEvent to get the message of the message sent, and get the content of the message.
In addition to only one-way transmission, a one-way pipe is only one-way transmission, and there is a significant disadvantage that it requires communication between communication to use a common pipeline (advertisement generally expressed in the XML document released within a group), and When both parties send a message, the pipeline advertisement defined by this document has been used until it is used to establish a new connection.
If you need to use two-way pipelines, you need to use two shared pipe advertisements in the two-way communication process, and in the two-way communication process.
In fact, bidirectional pipes implemented in JXTA, only use a shared pipe advertising publicpipea2b advertisement (consider service implementation within the group), and then establish two internal use of pipeline advertisements through the smart mechanism (Ie not publishing, respectively, for the Advertisement of PrivatePipeb2a and PrivatePipeA2B, and use the two internal pipeline ad to achieve two-way communication, and after the establishment of communication, the original shared pipe advertisement is useless for the communication process, and it can be The communication pipe is again utilized.
The establishment of a two-way pipeline is analyzed in the time order: (the letters behind the serial number indicate which node is done, such as "1b" means that the action occurs in Peer B)
(1B) Node B New JXTASERVERPIPE object, JXTAServerPipe itself is an inputpipe, using a shared advertisement when it is constructed, the PUBLICPIPEA2B's Advertisement builds a input pipe waiting connection.
(2A) Node A Newly created a input pipe InputPipe (that is, the advertisement of this pipe, the PrivatePipeb2a's Advertisement is encapsulated into a message Message, this Message message filled with Credential, Reqpipe, Rempeer, Reliable four Some, we only pay attention to the ReqPipe section, which is stored here is the advertisement privatePipeb2a advertisement (refer to the creagenmessage method for jxTabidipipe)
(3A) Node A uses publicpipea2b's Advertisement to establish an output pipeline OutputPipe so that a Aàb unidirectional connection is established with Node B. After the connection is established, the Message generated by the (2A) is sent immediately with this pipe PUBLICPIPEA2B, and then it will enter. (Refer to JXTabidipipe's Connect Method)
(4b) Node B is created an OutputPipe (already PrivatePipeb2a) in the Reqpipe section included in the Message accepted by the above (2A), which is connected to the PrivatePipeb2a of the above (2A). On this step, an internal connection pipeline PrivatePipeb2a of B to A is established. (Refer to JXTASERVERPIPE ProcessMessage Method)
(5b) Node B Newly creates an InputPipe (the input pipe of PrivatePipeA2b), and fills in the Message's Rempeer section, which is the Advertisement of PrivatePipeA2b, (actually filled in Credential, Rempipe, Rempeer three Some, we only pay attention to the Rempeer section), and the connection pipeline PrivatePipeb2a created in step (4b) Send this MESSAGE back to Node A. (Refer to the JXTASERVERPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPE) (6A) Receive this message to establish an output pipeline OutputPipe immediately after receiving this message. In this step, a new A to B connection pipeline privatePipeA2b is established, and the previously established A to B pipe PUBLICPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPEA2B then ends the waiting (3A) waiting, then one containing PrivatePipeA2B and The bidirectional pipe of PrivatePipeb2a, that is, a JXTabidipipe object is officially established (binding) successful.
* Conclusion
There is also a pair of JXTA's class JXTASOCKET and JXTASERVERSOCKET, the implementation mechanism and the above method are roughly similar. According to JXTA developers, JxtabidipiPE and JXTASERVERPIPE apply to small data applications, such as instant messaging, chat information, etc .; and Jxtasocket and JxtaserVersocket apply. Communication in large data, such as file transmission, etc. I will analyze the example of JXTASOCKET and JXTASERVERSOCKET in the next article.
For the program used in this article, please refer to the Jxtabidipipe section inside http://www.jxta.org/progguideexamples.zip, you can also download JXTA's J2SE reference implementation 2.3.1 Source code: http: // Download. JXTA.ORG/Build/release/2.3.1/jxta-src-2.3.1.zip
(Thanks to the discussion and summary of the peers of JXTA, the inspiration, please indicate the source) YANQLV2004 / 10/23