Peer - to - Peer Networking

Download Peer - to - Peer Networking

Post on 14-Dec-2014

885 views

Category:

Documents

7 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

<ul><li> 1. 45Peer-to-Peer NetworkingWHAT S IN THIS CHAPTER?An overview of P2P The Microsoft Windows Peer-to-Peer Networking platform, including PNRPand PNM Building P2P applications with the .NET Framework Peer-to -peer networking, often referred to as P2P, is perhaps one of the most useful and yetmisunderstood technologies to emerge in recent years. When people think of P2P they usually thinkof one thing: sharing music les, often illegally. This is because le - sharing applications such asBitTorrent have risen in popularity at a staggering rate, and these applications use P2P technologyto work.Although P2P is used in le-sharing applications, that doesnt mean it doesnt have other applications.Indeed, as you see in this chapter, P2P can be used for a vast array of applications, and is becomingmore and more important in the interconnected world in which we live. You learn about this in the rst 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 toolsand technologies to use it. You can use the Microsoft Windows Peer- to - Peer Networking platformas a communication framework for P2P applications. This platform includes the important compo-nents 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 typesand features that you can use to build P2P applications yourself with minimal effort. PEER-TO -PEER NETWORKING OVERVIEWPeer-to -peer networking is an alternative approach to network communication. To understand howP2P differs from the standard approach to network communication it is helpful to take a stepbackward and look at client-server communications. Client-server communications are ubiquitous innetworked applications today. c45.indd 111/11/09 5:35:13 PM </li></ul><p> 2. 2 CHAPTER 45 PEER-TO -PEER NETWORKINGClient-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 infor- mation that you require. If you want to download a le, you do so directly fromServer the web server. Similarly, desktop applications that include local or wide area network connectiv- ity will typically connect to a single server, for example, a database server or a Request Response server that hosts other services. This simple form of client-server architecture is illustrated in Figure 45-1. There is nothing inherently wrong with the client-server architecture, and indeed Client in many cases it will be exactly what you want. However, there is a scalability problem. Figure 45-2 shows how the client-server architecture scales with addi-FIGURE 45-1 tional clients. ServerClients FIGURE 45-2With 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 trafc 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 exible, 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 cli- ents, 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 le to appear. When the 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. c45.indd 2 11/11/09 5:35:15 PM 3. Peer-to - Peer Networking Overview 3You could use P2P technology to prevent this web server collapse. Instead of sending the le directly fromthe server to all the clients, you send the le to just a few clients. A few of the remaining clients then down-load the le from the clients that already have it, a few more clients download it from those second-levelclients, and so on. In fact, this process is made even faster by splitting the le into chunks and dividingthese chunks among clients, some of whom download it directly from the server, and some whom downloadchunks from other clients. This is how le-sharing technologies such as BitTorrent work, and is illustrated inFigure 45-3. Server ClientsFIGURE 45-3P2P Architectural ChallengesThere are still problems to solve in the le - sharing architecture discussed here. For a start, how doclients detect that other clients exist, and how do they locate chunks of the le that other clients mighthave? Also, how can you ensure optimal communication between clients that may be separated by entirecontinents?Every client participating in a P2P network application must be able to perform the following operations toovercome 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 soclients can obtain this list and contact other clients (known as peers), or you can use an infrastructure (forexample PNRP, covered in the next section) that enables clients to nd each other directly. Most le-sharing c45.indd 3 11/11/09 5:35:16 PM 4. 4 CHAPTER 45 PEER-TO -PEER NETWORKINGsystems use the list on a server solution by using servers known as trackers. Also, in le-sharing systems any client may act as a server as shown in Figure 45-3, by declaring that it has a le available and registering it with a tracker. In fact, a pure P2P network needs no servers at all, just peers. 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 topol- ogy of the connections between these clients can become extremely complex. You can often improve per- formance 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). Discovery, connection, and communication are central to any P2P implementation. The implementa- tion you look at in this chapter is to use the System.Net.PeerToPeer types with PNM for discovery and PNRP for connection. As you see in subsequent sections, these technologies cover all three of these operations. 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 otherpeer 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 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 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 specic 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- de ned pathways for queries and data 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 le-sharing applications discussed earlier Collaboration applications, such as desktop sharing and shared whiteboard applications c45.indd 411/11/09 5:35:16 PM 5. Microsoft Windows Peer-to - Peer Networking 5 Multi-user communication applications that allow users to communicate and exchange data directlyrather than through a server Distributed processing applications, as an alternative to supercomputing applications that processenormous amounts of data Web 2.0 applications that combine some or all the above in dynamic, next- generation webapplicationsMICROSOFT WINDOWS PEER-TO -PEER NETWORKINGThe Microsoft Windows Peer- to - Peer Networking platform is Microsoft s implementation of P2Ptechnology. It is part of Windows XP SP2, Windows Vista, and Windows 7, and is also available as anadd- on for Windows XP SP1. It includes two technologies that you can use when creating .NET P2Papplications: 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 andWindows 7 only)In this section you learn about these technologies. Peer Name Resolution Protocol (PNRP)You can of course use any protocol at your disposal to implement a P2P application, but if you are work-ing in a Microsoft Windows environment (and, let s face it, if youre reading this book you probably are)it makes sense to at least consider PNRP. There have been two versions of PNRP released to date. PNRPversion 1 was included in Windows XP SP2, Windows XP Professional x64 Edition, and Windows XPSP1 with the Advanced Networking Pack for Windows XP. PNRP version 2 was released with WindowsVista, and was made available to Windows XP SP2 users through a separate download (see KB920342 atsupport.microsoft.com/kb/920342). Windows 7 also uses version 2. Version 1 and version 2 of PNRPare not compatible, and this chapter covers only version 2.In itself, PNRP doesnt give you everything you need to create a P2P application. Rather, it is one of theunderlying 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 encapsu-lated in a PNRP ID. A peer that discovers the PNRP ID is able to use PNRP to resolve it to the actual peername, and can then communicate directly with the associated client.For example, you might de ne a peer name that represents a WCF service endpoint. You could use PNRPto register this peer name in a cloud as a PNRP ID. A peer running a suitable client application that uses adiscovery mechanism that can identify peer names for the service you are exposing might then discover thisPNRP ID. Once discovered, the peer would use PNRP to locate the endpoint of the WCF service and thenuse that service. An important point is that PNRP makes no assumptions about what a peer name actually represents. It is up to peers to decide how to use them when discovered. The information a peer receives from PNRP when resolving a PNRP ID includes the IPv6 (and usually also the IPv4) address of the publisher of the ID, along with a port number and optionally a small amount of additional data. Unless the peer knows what the peer name means it is unlikely to be able to do anything useful with this information. c45.indd 511/11/09 5:35:17 PM 6. 6 CHAPTER 45 PEER-TO -PEER NETWORKING PNRP IDs PNRP IDs are 256 -bit identi ers. 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 identi es the peer name. The hashed public key (known as the authority) combined with this string (the classi er) 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 45- 4.PNRP ID128-bit hashed P2P ID128-bit service location P2P IDAuthority (hashed public key) Classier (peer name identier) FIGURE 45-4The 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 elsewhere 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. A...</p>