lector: aliyev h.u. lecture №8 telecommunication software design for peer - to - peer networking...

25
Lector: Aliyev H.U. Lecture №8 Telecommunication software design for Peer - to - Peer Networking TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES THE DEPARTMENT OF DATA COMMUNICATION NETWORKS AND SYSTEMS

Upload: alaina-matthews

Post on 29-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Lector: Aliyev H.U.

Lecture №8 Telecommunication software design for Peer

- to - Peer Networking

TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES

THE DEPARTMENT OFDATA COMMUNICATION NETWORKS AND SYSTEMS

Introduction • Peer - to - peer networking, often referred to as P2P, is perhaps one of the most useful

and yet• misunderstood technologies to emerge in recent years. When people think of P2P they

usually think of one thing: sharing music fi les, often illegally. This is because file - sharing applications such as BitTorrent have risen in popularity at a staggering rate, and these applications use P2P technology to work.

• Although P2P is used in fi le - sharing applications, that doesn't t mean it doesn't have other applications. Indeed, as you see in this chapter, P2P can be used for a vast array of applications, and is becoming more and more important in the interconnected world in which we live. You learn about this in the first part of this chapter, when you look at an overview of P2P technologies.

• Microsoft has not been oblivious to the emergence of P2P, and has been developing its own tools

• and technologies to use it. You can use the Microsoft Windows Peer - to - Peer Networking platform as a communication framework for P2P applications. This platform includes the important components Peer Name Resolution Protocol (PNRP) and People Near Me (PNM). Also, version 3.5 of the .NET Framework introduced a new namespace, System.Net.PeerToPeer, and several new types and features that you can use to build P2P applications yourself with minimal effort.

Peer-to-Peer Systems• Peer to peer systems or P2P are service infrastructures. They can be defined as distributed• systems consisting of interconnected nodes able to self organize into network topologies with

the purpose of sharing resources such as content, CPU cycles, storage and bandwidth, capable of adapting to failures and accommodating transient populations of nodes while maintaining accept table connectivity and performance without requiring the intermediation or support of a global centralised servers or authorities (Androutsellis Theotokis and Spinellis, 2004).

• Rather than concentrating sophisticated server processing and resource management in a• relatively low number of specialist nodes or servers, for some high resourced client, service

access devices can also themselves act as on demand servers. Computer nodes in a P2P system can act as both clients and servers. Client and server are considered more as dynamic organisational roles for peers that can be changed in an ad hoc way. P2P interaction often uses an ad hoc application router or service overlay network. Ad hoc is generally applied to physical networks in which communication takes place without any pre existing infrastructure set up between the communicating computers, e.g., Mobile Ad hoc Networks or MANETs. P2P service infrastructures can overlay ad hoc networks.

• P2P applications include: content sharing in which anyone can share and publish; spontaneous user collaboration in real time such as VoIP; ad hoc wireless device interaction such as home devices discovering each other and sharing traffic reports in an ad hoc network of cars as network nodes; distributed computation such as sharing processing power to solve complex problems, e.g., the SETI@home13 project, distributed database systems, sensor net applications (Section 6.3) and various middleware services such as privacy protection via anonymous distribution of information.

A P2P computing model offers several important benefits:

• • lower cost of ownership for content sharing: by eliminating specialized server costs and by distributing the maintenance costs through multiple low cost peers that can interact;

• • performance enhancements: the resources of all the nodes can be used for storage, computation and data exchange rather than focusing resources mostly in the server type nodes;

• • ad hoc resource utilization and sharing: as demand for particular services peaks, more nodes can act as servers, for example, as file servers, to meet demand;

• • autonomous control and ownership: peers can have a greater degree of and exercise more decentralized, autonomous control over their data and resources. No reliance on a central server to collect and relay information.

• • anonymity and privacy: session based IDs and addresses can be assigned and can be hidden and masked as data gets routed; one cannot tell who has created data, who is querying data, who is storing data etc.;

• • fault tolerance: there are no central servers that can be attacked or can cause complete system failure, instead, alternative paths and servers can be used when one fails

PEER - TO - PEER NETWORKING OVERVIEW

• Peer - to - peer networking is an alternative approach to network communication.

• To understand how P2P differs from the “ standard ” approach to network communication it is helpful to take a step backward and look at client - server communications.

• Client - server communications are ubiquitous in• networked applications today.

Client - Server Architecture• Traditionally, you interact with applications over a network (including the Internet) using a

client - server• architecture. Web sites are a great example of this. When you look at a web site• you send a request over the Internet to a web server, which then returns the information• that you require. If you want to download a fi le, you do so directly from• the web server.• Similarly, desktop applications that include local or wide area network connectivity• will typically connect to a single server, for example, a database server or a• server that hosts other services

Client - Server Architecture• With every client that is added an increased load is placed on the

server, which must communicate with each client. To return to the web site example, this increased communication load is how web sites collapse.

• When there is too much traffic the server simply becomes unresponsive. There are of course scaling options that you can implement to mitigate this situation. You can scale up

• by increasing the power and resources available to the server, or you can scale out by adding additional servers. Scaling up is of course limited by the technology available and the cost of better hardware.

• Scaling out is potentially more flexible, but requires an additional infrastructure layer to ensure that clients either communicate with individual servers or maintain session state independent of the server with which they are communicating. Plenty of solutions are available for this, such as web or server farm products.

P2P Architecture• The peer - to - peer approach is completely different from either the scaling up or scaling

out approach. With P2P, instead of focusing on and attempting to streamline the communication between the server and its clients, you instead look at ways in which clients can communicate with each other.

• Say, for example, that the web site that clients are communicating with is www.wrox.com. In our imaginary scenario, Wrox has announced that a new version of this book is to be released on the wrox.com web site and will be free to download to anyone who wants it; however, it will be removed after one day.

• Before the book becomes available on the web site you might imagine that an awful lot of people will be looking at the web site and refreshing their browsers, waiting for the fi le to appear. When the fi le is available, everyone will try to download it at the same time, and more than likely the wrox.com web server will collapse under the strain.

• You could use P2P technology to prevent this web server collapse. Instead of sending the fi le directly from the server to all the clients, you send the fi le to just a few clients. A few of the remaining clients then download the fi le from the clients that already have it, a few more clients download it from those second – level clients, and so on. In fact, this process is made even faster by splitting the fi le into chunks and dividing these chunks among clients, some of whom download it directly from the server, and some whom download chunks from other clients. This is how file - sharing technologies such as BitTorrent work, and is illustrated in

• Figure.

P2P Architecture

P2P Architectural Challenges• There are still problems to solve in the file - sharing architecture discussed here.

For a start, how do clients detect that other clients exist, and how do they locate chunks of the fi le that other clients might have? Also, how can you ensure optimal communication between clients that may be separated by entire continents?

• Every client participating in a P2P network application must be able to perform the following operations to

• overcome these problems:• It must be able to discover other clients.• It must be able to connect to other clients.• It must be able to communicate with other clients.• The discovery problem has two obvious solutions. You can either keep a list of the

clients on the server so clients can obtain this list and contact other clients (known as peers ), or you can use an infrastructure (for example PNRP, covered in the next section) that enables clients to fi nd each other directly. Most fi le – sharing systems use the “ list on a server ” solution by using servers known as trackers. Also, in fi le - sharing systems any client may act as a server as shown in Figure , by declaring that it has a fi le available and registering it with a tracker. In fact, a pure P2P network needs no servers at all, just peers.

P2P Architectural Challenges• The connection problem is a more subtle one, and concerns the overall structure

of the networks used by a P2P application. If you have one group of clients, all of which can communicate with one another, the topology of the connections between these clients can become extremely complex. You can often improve performance by having more than one group of clients, each of which consists of connections between clients in that group, but not to clients in other groups. If you can make these groups locale - based you will get an additional performance boost, because clients can communicate with each other with fewer hops between

• networked computers. • Communication is perhaps a problem of lesser importance, because

communication protocols such as TCP/IP are well established and can be reused here. There is, however, scope for improvement in both high - level technologies (for example, you can use WCF services and therefore all the functionality that WCF offers) and low - level protocols (such as multicast protocols to send data to multiple endpoints simultaneously).

P2P Terminology• In the previous sections you were introduced to the concept of a peer, which is how clients are referred to in• a P2P network. The word “ client ” makes no sense in a P2P network because there is not necessarily a server• to be a client of.• Groups of peers that are connected to each other are known by the interchangeable terms meshes,• clouds, or graphs. A given group can be said to be well - connected if at least one of the following• statements applies:• There is a connection path between every pair of peers, so that every peer can connect to any other• peer as required.• There are a relatively small number of connections to traverse between any pair of peers.• Removing a peer will not prevent other peers from connecting to each other.• Note that this does not mean that every peer must be able to connect to every other peer directly. In fact,• if you analyze a network mathematically you will fi nd that peers need to connect only to a relatively small• number of other peers for these conditions to be met.• Another P2P concept to be aware of is fl ooding. Flooding is the way in which a single piece of data may be• propagated through a network to all peers, or of querying other nodes in a network to locate a specifi c piece• of data. In unstructured P2P networks this is a fairly random process of contacting nearest neighbor peers,• which in turn contact their nearest neighbors, and so on until every peer in the network is contacted. It is• also possible to create structured P2P networks such that there are well - defi ned pathways for queries and• data fl ow among peers.

P2P Solutions

• When you have an infrastructure for P2P you can start to develop not just improved versions of client – server applications, but entirely new applications. P2P is particularly suited to the following classes of applications:

• - Content distribution applications, including the fi le - sharing applications discussed earlier;

• - Collaboration applications, such as desktop sharing and shared whiteboard applications;

• - Multi - user communication applications that allow users to communicate and exchange data directly rather than through a server;

• Distributed processing applications, as an alternative to supercomputing applications that process enormous amounts of data;

• Web 2.0 applications that combine some or all the above in dynamic, next - generation web applications.

MICROSOFT WINDOWS PEER - TO - PEER NETWORKING

• The Microsoft Windows Peer - to - Peer Networking platform is Microsoft's implementation of P2P technology. It is part of Windows XP SP2, Windows Vista, and Windows 7, and is also available as an add - on for Windows XP SP1. It includes two technologies that you can use when creating .NET P2P applications:

• The Peer Name Resolution Protocol (PNRP), which is used to publish and resolve peer addresses;

• The People Near Me server, which is used to locate local peers (currently for Vista and Windows 7 only).

Peer Name Resolution Protocol (PNRP)

• You can of course use any protocol at your disposal to implement a P2P application, but if you are working in a Microsoft Windows environment (and, let ’ s face it, if you ’ re reading this book you probably are) it makes sense to at least consider PNRP. There have been two versions of PNRP released to date. PNRP version 1 was included in Windows XP SP2, Windows XP Professional x64 Edition, and Windows XP SP1 with the Advanced Networking Pack for Windows XP. PNRP version 2 was released with Windows Vista, and was made available to Windows XP SP2 users through a separate download (see KB920342 at support.microsoft.com/kb/920342 ). Windows 7 also uses version 2. Version 1 and version 2 of PNRP are not compatible, and this chapter covers only version 2.

• In itself, PNRP doesn't ’ t give you everything you need to create a P2P application. Rather, it is one of the underlying technologies that you use to resolve peer addresses. PNRP enables a client to register an endpoint (known as a peer name ) that is automatically circulated among peers in a cloud. This peer name is encapsulated in a PNRP ID. A peer that discovers the PNRP ID is able to use PNRP to resolve it to the actual peer name, and can then communicate directly with the associated client.

• For example, you might define a peer name that represents a WCF service endpoint. You could use PNRP to register this peer name in a cloud as a PNRP ID. A peer running a suitable client application that uses a discovery mechanism that can identify peer names for the service you are exposing might then discover this PNRP ID. Once discovered, the peer would use PNRP to locate the endpoint of the WCF service and then use that service.

PNRP IDs• PNRP IDs are 256 - bit identifiers. The low - order 128 bits are used to uniquely

identify a particular peer, and the high - order 128 bits identify a peer name. The high - order 128 bits are a hashed combination of a hashed public key from the publishing peer and a string of up to 149 characters that identifies the peer name. The hashed public key (known as the authority ) combined with this string (the classifier ) are together referred to as the P2P ID. It is also possible to use a value of 0 instead of a hashed public key, in which case the peer name is said to be unsecured (as opposed to secured peer names, which use a public key).

• The structure of a PNRP ID is illustrated in Figure.

PNRP IDs

• The PNRP service on a peer is responsible for maintaining a list of PNRP IDs, including the ones

• that it publishes as well as a cached list of those it has obtained by PNRP service instances else where in the cloud. When a peer attempts to resolve a PNRP ID, the PNRP service either uses a cached copy of the endpoint to resolve the peer that published the PNRP or it asks its neighbors if they can resolve it. Eventually a connection to the publishing peer is made and the PNRP service can resolve the PNRP ID.

• Note that all this happens without you having to intervene in any way. All you have to do is ensure that peers know what to do with peer names after they have resolved them using their local PNRP service.

• Peers can use PNRP to locate PNRP IDs that match a particular P2P ID. You can use this to implement a very basic form of discovery for unsecured peer names. This is because if several peers

• expose an unsecured peer name that uses the same classifier, the P2P ID will be the same. Of course, because any peer can use an unsecured peer name you have no guarantee that the endpoint you connect to will be the sort of endpoint you expect, so this is only really a viable solution for discovery over a local network.

PNRP Clouds• In the preceding discussion you learned how PNRP registers and resolves peer names

in clouds. A cloud is maintained by a seed server, which can be any server running the PNRP service that maintains a record of at least one peer. Two types of clouds are available to the PNRP service:

• Link local — These clouds consist of the computers attached to a local network. A PC may be connected to more than one link local cloud if it has multiple network adapters.

• Global — This cloud consists of computers connected to the Internet by default, although it is also possible to define a private global cloud. The difference is that Microsoft maintains the seed server for the global Internet cloud, whereas if you define a private global cloud you must use your own seed server. If you use your own seed server you must ensure that all peers connect to it by configuring policy settings.

• You can discover what clouds you are connected to with the following command:• netsh p2p pnrp cloud show list. A typical result is shown in Figure.

PNRP Clouds• Figure shows that a single cloud is available, and that it is a link local cloud. You can tell

this from both the name and the Scope value, which is 3 for link local clouds and 1 for global clouds. To connect to a global cloud you must have a global IPv6 address. The computer used to generate Figure 45 - 5 does not have one, which is why only a local cloud is available.

• Clouds may be in one of the following states:• Active — If the state of a cloud is active , you can use it to publish and resolve peer

names.• Alone — If the peer you are querying the cloud from is not connected to any other

peers, it will have a state of alone .• No Net — If the peer is not connected to a network, the cloud state may change from

active to no net.• Synchronizing — Clouds will be in the synchronizing state when the peer is connecting

to them.• This state will change to another state extremely quickly because this connection does

not take long, so you will probably never see a cloud in this state.• Virtual — The PNRP service connects to clouds only as required by peer name

registration and resolution. If a cloud connection has been inactive for more than 15 minutes it may enter the virtual state.

BUILDING P2P APPLICATIONS• Now that you have learned what P2P networking is and what technologies

are available to .NET developers to implement P2P applications, it ’ s time to look at how you can build them. From the preceding discussion you know that you will be using PNRP to publish, distribute, and resolve peer names, so the first thing you look at here is how to achieve that using .NET. Next you look at how to use PNM as a framework for a P2P application. This can be advantageous because if you use PNM you do not have to implement your own discovery mechanisms.

• To examine these subjects you need to learn about the classes in the following namespaces:

• System.Net.PeerToPeer• System.Net.PeerToPeer.Collaboration

• To use these classes you must have a reference to the System.Net.dll assembly.

System.Net.PeerToPeer

• The classes in the System.Net.PeerToPeer namespace encapsulate the API for PNRP and enable you to interact with the PNRP service. You will use these classes for two main tasks:

• Registering peer names; Resolving peer names. In the following sections, all the types referred to come from the System.Net.PeerToPeer namespace unless otherwise specified.Registering Peer Names. To register a peer name you must carry out the following steps:

• 1. Create a secured or unsecured peer name with a specified classifier.• 2. Configure a registration for the peer name, providing as much of the following optional information as you

choose: A TCP port number.• The cloud or clouds with which to register the peer name (if unspecified, PNRP will register the peer name in all

available clouds): A comment of up to 39 characters; Up to 4,096 bytes of additional data.• Whether to generate endpoints for the peer name automatically (the default behavior, where endpoints will be

generated from the IP address or addresses of the peer and, if specified, the port number): A collection of endpoints.

• 3. Use the peer name registration to register the peer name with the local PNRP service.• After Step 3 the peer name will be available to all peers in the selected cloud (or clouds). Peer registration• continues until it is explicitly stopped, or until the process that registered the peer name is terminated.• To create a peer name you use the PeerName class. You create an instance of this class from a string

representation of a P2P ID in the form authority.classifier or from a classifier string and a PeerNameType.• You can use PeerNameType.Secured or PeerNameType.Unsecured. For example:• PeerName pn = new PeerName("Peer classifier", PeerNameType.Secured);• Because an unsecured peer name uses an authority value of 0, the following lines of code are equivalent:• PeerName pn = new PeerName("Peer classifier", PeerNameType.Unsecured);• PeerName pn = new PeerName("0.Peer classifier");• After you have a PeerName instance you can use it along with a port number to initialize a• PeerNameRegistration object: PeerNameRegistration pnr = new PeerNameRegistration(pn, 8080);

System.Net.PeerToPeer• Alternatively, you can set the PeerName and (optionally) the Port properties on a PeerNameRegistration• object created using its default parameter. You can also specify a Cloud instance as a third parameter of• the PeerNameRegistration constructor, or through the Cloud property. You can obtain a Cloud instance• from the cloud name or by using one of the following static members of Cloud :• Cloud.Global — This static property obtains a reference to the global cloud. This may be a private• global cloud depending on peer policy configuration.• Cloud.AllLinkLocal — This static fi eld gets a cloud that contains all the link local clouds available to the peer.• Cloud.Available — This static fi eld gets a cloud that contains all the clouds that are available to the• peer, which includes link local clouds and (if available) the global cloud.• When created, you can set the Comment and Data properties if you want. Be aware of the limitations of these

properties, though. You will receive a PeerToPeerException if you try to set Comment to a string greater than 39 Unicode characters or an ArgumentOutOfRangeException if you try to set Data to a byte[] greater than 4,096 bytes. You can also add endpoints by using the EndPointCollection property.

• This property is a System.Net.IPEndPointCollection collection of System.Net.IPEndPoint objects.• If you use the EndPointCollection property you might also want to set the UseAutoEndPointSelection• property to false to prevent automatic generation of endpoints.• When you are ready to register the peer name you can call the PeerNameRegistration.Start() method.• To remove a peer name registration from the PNRP service you use the PeerNameRegistration.Stop()• method.• The following code registers a secured peer name with a comment:• PeerName pn = new PeerName("Peer classifier", PeerNameType.Unsecured);• PeerNameRegistration pnr = new PeerNameRegistration(pn, 8080);• pnr.Comment = "Get pizza here";• pnr.Start();

System.Net.PeerToPeer.Collaboration

• The classes in the System.Net.PeerToPeer.Collaboration namespace provide a framework you can use to create applications that use the People Near Me service and the P2P collaboration API. As mentioned earlier, at the time of writing this is only possible if you are using Windows Vista or Windows 7.

• You can use the classes in this namespace to interact with peers and applications in a number of ways, including

• Signing in and signing out;• Discovering peers;• Managing contacts and detecting peer presence.• You can also use the classes in this namespace to invite other users to

join an application, and to exchange data between users and applications. However, to do this you need to create your own PNM - capable applications, which is beyond the scope of this chapter.

SUMMARY

• This lesson demonstrated how to implement peer - to - peer functionality in your applications by using the P2P classes in .NET 4.

• You have looked at the types of solutions that P2P makes possible and how these solutions are structured, how to use PNRP and PNM, and how to use the types in the System.Net.PeerToPeer and System.Net.

• .PeerToPeer.Collaboration namespaces. You also saw the extremely useful technique of exposing WCF services as P2P endpoints.

• If you are interested in developing P2P applications, it is well worth investigating PNM further. It is also worth looking at the peer channel, by which WCF services can broadcast communications among multiple clients simultaneously.

Q&A?