15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Building P2P applications ❘ 1347<br />

system.net.PeerToPeer<br />

The classes in the System.Net.PeerToPeer namespace encapsulate the API for PNRP <strong>and</strong> enable you to<br />

interact with the PNRP service. You will use these classes for two main tasks:<br />

➤<br />

➤<br />

Registering peer names<br />

Resolving peer names<br />

In the following sections, all the types referred to come from the System.Net.PeerToPeer namespace<br />

unless otherwise specified.<br />

registering Peer names<br />

To register a peer name you must carry out the following steps:<br />

1. Create a secured or unsecured peer name with a specified classifier.<br />

2. Configure a registration for the peer name, providing as much of the following optional information<br />

as you choose:<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

A TCP port number<br />

The cloud or clouds with which to register the peer name (if unspecified, PNRP will register<br />

the peer name in all available clouds)<br />

A comment of up to 39 characters<br />

Up to 4,096 bytes of additional data<br />

Whether to generate endpoints for the peer name automatically (the default behavior, where<br />

endpoints will be generated from the IP address or addresses of the peer <strong>and</strong>, if specified, the<br />

port number)<br />

A collection of endpoints<br />

3. Use the peer name registration to register the peer name with the local PNRP service.<br />

After Step 3 the peer name will be available to all peers in the selected cloud (or clouds). Peer registration<br />

continues until it is explicitly stopped, or until the process that registered the peer name is terminated.<br />

To create a peer name you use the PeerName class. You create an instance of this class from a string representation<br />

of a P2P ID in the form authority.classifier or from a classifier string <strong>and</strong> a PeerNameType.<br />

You can use PeerNameType.Secured or PeerNameType.Unsecured. For example:<br />

PeerName pn = new PeerName("Peer classifier", PeerNameType.Secured);<br />

Because an unsecured peer name uses an authority value of 0, the following lines of code are equivalent:<br />

PeerName pn = new PeerName("Peer classifier", PeerNameType.Unsecured);<br />

PeerName pn = new PeerName("0.Peer classifier");<br />

After you have a PeerName instance you can use it along with a port number to initialize a<br />

PeerNameRegistration object:<br />

PeerNameRegistration pnr = new PeerNameRegistration(pn, 8080);<br />

Alternatively, you can set the PeerName <strong>and</strong> (optionally) the Port properties on a PeerNameRegistration<br />

object created using its default parameter. You can also specify a Cloud instance as a third parameter of<br />

the PeerNameRegistration constructor, or through the Cloud property. You can obtain a Cloud instance<br />

from the cloud name or by using one of the following static members of Cloud:<br />

➤<br />

Cloud.Global — This static property obtains a reference to the global cloud. This may be a private<br />

global cloud depending on peer policy configuration.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!