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 ❘ 1353<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

PeerPresenceStatus.Busy — The peer is busy.<br />

PeerPresenceStatus.Idle — The peer isn’t active.<br />

PeerPresenceStatus.Offline — The peer is offline.<br />

PeerPresenceStatus.Online — The peer is online <strong>and</strong> available.<br />

PeerPresenceStatus.OnThePhone — The peer is busy with a phone call.<br />

PeerPresenceStatus.OutToLunch — The peer is away, but will be back after lunch.<br />

Discovering Peers<br />

You can obtain a list of peers near you if you are logged in to the link local cloud. You do this by using the<br />

PeerCollaboration.GetPeersNearMe() method. This returns a PeerNearMeCollection object containing<br />

PeerNearMe objects.<br />

You can use the Nickname property of PeerNearMe to obtain the name of a peer, IsOnline, to determine<br />

whether the peer is online, <strong>and</strong> (for lower-level operations) the PeerEndpoints property to determine endpoints<br />

related to the peer. PeerEndPoints is also necessary if you want to find out the online status of a<br />

PeerNearMe. You can pass an endpoint to the GetPresenceInfo() method to obtain a PeerPresenceInfo<br />

object, as described in the previous section.<br />

Managing Contacts <strong>and</strong> Detecting Peer Presence<br />

Contacts are a way in which you can remember peers. You can add a peer discovered through the People<br />

Near Me service <strong>and</strong> from then onward you can connect to them whenever you are both online. You can<br />

connect to a contact through link local or global clouds (assuming you have IPv6 connectivity to the<br />

Internet).<br />

You can add a contact from a peer that you have discovered by calling the PeerNearMe<br />

.AddToContactManager() method. When you call this method you can choose to associate a display name,<br />

nickname, <strong>and</strong> e-mail address with the contact. Typically, though, you will manage contacts by using the<br />

ContactManager class.<br />

However you manipulate contacts, you will be dealing with PeerContact objects. PeerContact, like<br />

PeerNearMe, inherits from the abstract Peer base class. PeerContact has more properties <strong>and</strong> methods<br />

than PeerNearMe. PeerContact includes DisplayName <strong>and</strong> EmailAddress properties that further describe<br />

a PNM peer, for example. Another difference between these two types is that PeerContact has a more<br />

explicit relationship with the System.Net.PeerToPeer.PeerName class. You can get a PeerName from a<br />

PeerContact through the PeerContact.PeerName property. After you have done this you can proceed to<br />

use techniques you looked at earlier to communicate with any endpoints the PeerName exposes.<br />

Information about the local peer is also accessible through the ContactManager class, through the static<br />

ContactManager.LocalContact property. This gets you a PeerContact property with details of the<br />

local peer.<br />

You can add PeerNearMe objects to the local list of contacts by using either the ContactManager<br />

.CreateContact() or CreateContactAsync() method, or PeerName objects by using the GetContact()<br />

method. You can remove contacts represented by a PeerNearMe or PeerName object with the<br />

DeleteContact() method.<br />

Finally, there are events that you can h<strong>and</strong>le to respond to changes to contacts. For example, you can use the<br />

PresenceChanged event to respond to changes of presence for any contacts known by the ContactManager.<br />

sample application<br />

There is a second sample application in the downloadable code for this chapter that illustrates the use of<br />

classes in the System.Net.PeerToPeer.Collaboration namespace. This application is similar to the other<br />

example, but much simpler. You need two computers that can both sign in to the PNM server to see this<br />

application in action, because it enumerates <strong>and</strong> displays PNM peers from the local subnet.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!