To do useful work, P2P applications must be able to discover and interact with each other. This article provides an implementation of IP multicast-based discovery.
Before a software entity can participate in direct peer-to-peer interaction with P2P applications, the entity must discover the appropriate Peer-to-Peer Points to interact. All feasible P2P architectures provide a solution for Problem Discovery. In this article, I will describe the implementation of one of the mechanisms. Let's start today's discussion through review. The P2P application's Peer Discovery allows P2P applications to locate and interact with each other. There are multiple methods for implementing peer-to-peer service discovery. The simplest mechanism is the explicit point-to-point configuration. This mechanism works by requiring each peer to know all other peers that it may interact with and connect them. The main advantage of Point-to-Point configuration is simplicity. Its main disadvantage is its lack of flexibility and the ability to expand to a large network of peer nodes.
Another common model found is to use a central directory as an intermediary. This model is popular among many traditional non-P2P distributed applications, and its advantage is that it is well understood. Register your presence with the central directory and use the central directory to locate other peers. The main advantage of this model is its ease of management and scalability. However, its centralized design will lead to single point of failure. Therefore, it lacks the ability to resist the dangers caused by its natural power or the increase in the number of online surfing users.
Many popular P2P applications use the network model instead of the central directory. In the network model, a single peer only knows the peer identity on the local network. Each peer is used as the directory of the Peer that is connected to it. Peer nodes collaborate by spreading directory queries to adjacent peer nodes and returning relevant responses. The main advantage of this model is that it is not centralized. Its main drawback is that propagation query consumes a lot of network and processing capabilities.
There are several variants of the above three mechanisms. Without discussing these variants, let's move on and study another discovery mechanism.
IP multicast discovery
The multicast model is similar to the network model for maintaining the directory of each peer. However, peer nodes do not cooperate to implement large-scale network queries. In addition, peer nodes use the features provided by the network (IP Multicast) to locate and identify other peer nodes.
IP multicast is connectionless and unreliable (unlike TCP/IP, Which is connection-oriented and reliable ). Although it uses IP datagram, multicast IP datagram can be sent from one host to another, unlike unicast IP datagram.
Peer nodes regularly use IP multicast to declare their own existence. The announcement contains their host names and a port for normal communication. The peer that is interested in the message detects the message, extracts the host name and port number, and uses the message to establish a communication channel.
Review is enough. Let's get started with the code.
Simple client and server
Starting from a simple example, this example demonstrates how two processes use IP multicast for communication. To simplify the demo, I will introduce the examples from the client and server processes. P2P applications usually implement these two processes. It is not easy to divide them into clients or servers.
In this example, the server process loops and waits for the arrival of the datagram packet. Each time a packet is received, the server prints a brief Diagnostic message to the console. How simple is the client role? It multicast a single datagram packet and exits.
Listing 1 and 2 demonstrate how these two parts are combined. The comments in the Code indicate what is happening.
List 1. Simple Server
Public
Class Server
{
Public
Static
Void
Main (string [] arstring)
{
Try
{
// Create a multicast datatesocket for processing ing IP
// Multicast packets. Join the multicast group
// 230.0.0.1, port 7777.
Multicastsocket = new multicastsocket (7777 );
Inetaddress = inetaddress. getbyname ("230.0.0.1 ");
Multicastsocket. joingroup (inetaddress );
// Loop forever and receive messages from clients. Print
// The specified ed messages.
While (true)
{
Byte [] ARB = new byte [100];
Datagrampacket = new datagrampacket (ARB, Arb. Length );
Multicastsocket. Receive (datagrampacket );
System. Out. println (new string (ARB ));
}
}
Catch (exception)
{
Exception. printstacktrace ();
}
}
}
List 2. Simple Client
Public
Class Client
{
Public
Static
Void
Main (string [] arstring)
{
Try
{
// Create a datatepackage and send it to the Multicast
// Group at 230.0.0.1, port 7777.
Byte [] ARB = new byte [] {'h', 'E', 'l', 'l', 'O '};
Inetaddress = inetaddress. getbyname ("230.0.0.1 ");
Datagrampacket =
New datagrampacket (ARB, Arb. length, inetaddress, 7777 );
Multicastsocket = new multicastsocket ();
Multicastsocket. Send (datagrampacket );
}
Catch (exception)
{
Exception. printstacktrace ();
}
}
}
The two classes in the java.net package make it run. The java.net. datagrampacket class saves the data contained in the IP datagram package. The java.net. multicastsocket class creates a multicast socket that is adjusted to a specific multicast group.
Component discovery
Although the above example is a good demonstration of IP multicast, it does not indicate what is needed to implement IP multicast-based peer point discovery. To make it useful, we need a feature that is not limited to software components that send and receive packets. Ideally, this component will understand the source Peer of the packet it receives and discard some information as appropriate, this information is about the peers that it considers to have disappeared, died, or left in other ways.
In this new design, a peer is a member of a multicast group. Keep in mind that messages sent to multicast groups are transparently routed to all members of the group.
The design includes two core classes and three interfaces (I use the term "interface", which seems less rigorous? Technically, it is an interface and two abstract classes ). An instance of the member class is a member of a multicast group. This class manages all the communication details. An instance of the membermanager class is responsible for understanding other Members involved in multicast groups.
A peer node sends a message to a multicast group to declare its presence to the peer node in the multicast group. Each message contains information about the Peer of the sent message? It is usually the host name and the port used for normal (unrelated to discovery) communication. The member and membermanager classes have almost no idea about the content of these messages. The access to this information belongs to the applications that use these two classes.
There are three interfaces that span the boundaries between the message passing/discovery layer and the application layer that uses it. They are reference abstract classes, message interfaces, and messagefactory abstract classes. The application must provide the implementation of these three interfaces.
The reference abstract class defines references to multicast group members. The membermanager class manages a reference set. The application will implement a specific version of this class, which will contain any reference logic required by the application. This class defines two methods, whose names are ipvsinternal () and hashcodeinternal (), and The equals () and hashcode () methods are redefined to call these methods. In this way, it forces the implementer to implement these two key functions? Membermanager depends on them.
The message interface defines the application view of message data exchanged through network code. Does the application regard the message as an advanced concept relative to the application running range? Similar to the concept of host name and port. The network code sends a packet consisting of bytes. The implementation of the message interface defines how to convert these advanced information into bytes. Reference is a key part of information, and all messages must be included. Therefore, this interface requires the implementation of methods for reading and writing reference.
The last part of the problem is the abstract class messagefactory. This class defines the mechanism for generating a new message instance. The network code hidden in the member class uses a factory to create a message instance for extracting data from multicast datagram. Each messagefactory instance has a randomly generated identity, which is used to filter messages to be sent from received messages.
In short, these five classes and interfaces (Member, membermanager, reference, message, and messagefactory) constitute a simple framework for Peer-to-Peer Discovery. Of course, there is room for improvement. You can easily add an event-based mechanism to notify interested listeners of the appearance or disappearance of members. A flexible mechanism for filtering received messages is useful, but its implementation is difficult. I leave these suggestions for readers.
Member class
The complete source code of the framework described above is too long to be detailed here, so let's only study part of the code of the member class, because it contains most of the Operation content. More accurately, operations occur in two internal classes: The memberclient class and the memberserver class.
Consider the first example again. It consists of a client that sends IP multicast data packets and a server that receives the data packets. In this example (listing 3 and 4), two separate applications perform these two functions. P2P applications act like clients and servers. Therefore, it is appropriate for our P2P applications to include both.
Listing 3. memberclient class
Private
Class memberclient
Extends thread
{
Public
Void
Run ()
{
Try
{
While (true)
{
Try
{
Message message = m_messagefactory.createsendmessage ();
Reference reference = message. createreference ();
Message. writereference (reference );
Message. Sync ();
Byte [] ARB = message. getbytearray ();
Datagrampacket = new datagrampacket (ARB, Arb. Length );
Datagrampacket. setaddress (m_inetaddress );
Datagrampacket. setport (m_nport );
Multicastsocket = new multicastsocket ();
Multicastsocket. Send (datagrampacket );
}
Catch (ioexception)
{
Ioexception. printstacktrace ();
}
Try
{
Synchronized (this)
{
Wait (m_nperiod );
}
}
Catch (interruptedexception)
{
Break;
}
}
}
Catch (throwable)
{
Throwable. printstacktrace ();
}
}
}
Listing 3 contains the source code of the memberclient class. Like the client in Listing 1, this client creates a multicastsocket instance and uses it to send an ingress rampacket instance. The mongorampacket instance contains a reference to the sender's peer. The reference is encoded as a byte array. As long as the Peer is active and running, the client broadcasts this message at regular intervals.
Listing 4. memberserver class
Private
Class memberserver
Extends thread
{
Public
Void
Run ()
{
Try
{
Multicastsocket = new multicastsocket (m_nport );
Multicastsocket. joingroup (m_inetaddress );
While (true)
{
Message message = m_messagefactory.createreceivemessage ();
Byte [] ARB = message. getbytearray ();
Datagrampacket = new datagrampacket (ARB, Arb. Length );
Multicastsocket. Receive (datagrampacket );
Message. Sync ();
If (m_messagefactory.ismine (Message) = false)
{
Reference reference = message. createreference ();
Message. readreference (reference );
M_membermanager.addreference (reference );
}
}
}
Catch (throwable)
{
Throwable. printstacktrace ();
}
}
}
The memberserver class is similar to the server in Listing 2 in many ways. In addition to creating the required code, you can use it to collect the appropriate datagram from the established communication (wire, the server also decodes encoded references, creates messages, and delivers messages to the membermanager instance for storage. Good job.
The rest of the class consists of the read and write methods used for various features, and the START () and stop () Methods used to control the lifecycle of the class.
P2P applications
The Peer-to-Peer Discovery framework is complete, and all the rest is to integrate it into existing P2P applications. There are few changes to the original P2P application.
First, in its predecessor, P2P applications expect to list all known peers in its feature files. Although this is good for demonstration purposes (recall the point-to-point configuration discussed above), and it runs well at most four equal points, but in the end it is too restrictive. Therefore, it removes the need to read and manage peer code in the feature file and replace it with the code of the above discovery mechanism.
Secondly, because the feature files enumerate the peers, they seem to have persisted. If existing applications assume they will not disappear, they will sometimes be lucky to succeed. However, in the real world of P2P, things are more likely to change. The new application has been redesigned for better recovery when the peer point disappears.
The updated source code can be found in references.