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.

1348 ❘ ChaPTer 45 peer-tO-peer netwOrkinG<br />

➤<br />

➤<br />

Cloud.AllLinkLocal — This static field gets a cloud that contains all the link local clouds available<br />

to the peer.<br />

Cloud.Available — This static field gets a cloud that contains all the clouds that are available to the<br />

peer, which includes link local clouds <strong>and</strong> (if available) the global cloud.<br />

When created, you can set the Comment <strong>and</strong> Data properties if you want. Be aware of the limitations of<br />

these properties, though. You will receive a PeerToPeerException if you try to set Comment to a string<br />

greater than 39 Unicode characters or an ArgumentOutOfRangeException if you try to set Data to a<br />

byte[] greater than 4,096 bytes. You can also add endpoints by using the EndPointCollection property.<br />

This property is a System.Net.IPEndPointCollection collection of System.Net.IPEndPoint objects.<br />

If you use the EndPointCollection property you might also want to set the UseAutoEndPointSelection<br />

property to false to prevent automatic generation of endpoints.<br />

When you are ready to register the peer name you can call the PeerNameRegistration.Start() method.<br />

To remove a peer name registration from the PNRP service you use the PeerNameRegistration.Stop()<br />

method.<br />

The following code registers a secured peer name with a comment:<br />

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

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

pnr.Comment = "Get pizza here";<br />

pnr.Start();<br />

resolving Peer names<br />

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

1. Generate a peer name from a known P2P ID or a P2P ID obtained through a discovery technique.<br />

2. Use a resolver to resolve the peer name <strong>and</strong> obtain a collection of peer name records. You can limit the<br />

resolver to a particular cloud <strong>and</strong>/or a maximum number of results to return.<br />

3. For any peer name records that you obtain, obtain peer name, endpoint, comment, <strong>and</strong> additional data<br />

information as required.<br />

This process starts with a PeerName object similar to a peer name registration. The difference here is that<br />

you use a peer name that is registered by one or more remote peers. The simplest way to get a list of active<br />

peers in your link local cloud is for each peer to register an unsecured peer name with the same classifier<br />

<strong>and</strong> to use the same peer name in the resolving phase. However, this is not a recommended strategy for<br />

global clouds because unsecured peer names are easily spoofed.<br />

To resolve peer names you use the PeerNameResolver class. When you have an instance of this class you<br />

can choose to resolve peer names synchronously by using the Resolve() method, or asynchronously using<br />

the ResolveAsync() method.<br />

You can call the Resolve() method with a single PeerName parameter, but you can also pass an optional<br />

Cloud instance to resolve in, an int maximum number of peers to return, or both. This method returns<br />

a PeerNameRecordCollection instance, which is a collection of PeerNameRecord objects. For example,<br />

the following code resolves an unsecured peer name in all link local clouds <strong>and</strong> returns a maximum of<br />

5 results:<br />

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

PeerNameResolver pnres = new PeerNameResolver();<br />

PeerNameRecordCollection pnrc = pnres.Resolve(pn, Cloud.AllLinkLocal, 5);<br />

The ResolveAsync() method uses a st<strong>and</strong>ard asynchronous method call pattern. You pass a unique<br />

userState object to the method <strong>and</strong> listen for ResolveProgressChanged events for peers being found <strong>and</strong><br />

the ResolveCompleted event when the method terminates. You can cancel a pending asynchronous request<br />

with the ResolveAsyncCancel() method.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!