Export (0) Print
Expand All

ContactManager Class

Represents a collection of PeerContact objects which persist in a Windows Address Book.

System.Object
  System.Net.PeerToPeer.Collaboration.ContactManager

Namespace:  System.Net.PeerToPeer.Collaboration
Assembly:  System.Net (in System.Net.dll)

public sealed class ContactManager : IDisposable

The ContactManager type exposes the following members.

  NameDescription
Public propertyStatic memberLocalContactGets the PeerContact representing the local peer.
Public propertySynchronizingObjectWhen this property value is set, all events not fired as the result of an asynchronous operation will have the associated event handlers called back on the thread that created the specific SynchronizingObject.
Top

  NameDescription
Public methodAddContactAdds the specified PeerContact to the ContactManager of the local peer.
Public methodCreateContactCreates a PeerContact instance for the specified PeerNearMe object.
Public methodCreateContactAsyncCreates a contact instance for the specified PeerNearMe object.
Public methodDeleteContact(PeerContact)Removes the specified PeerContact from the ContactManager of the local peer.
Public methodDeleteContact(PeerName)Removes the PeerContact associated with the specified PeerName from the ContactManager of the local peer.
Public methodDisposeReleases all resources used by the ContactManager object.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetContactReturns the PeerContact object for the specified PeerName.
Public methodGetContactsReturns a PeerContactCollection that contains all contacts within the ContactManager of the remote peer.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodUpdateContactUpdates the data associated with the PeerContact.
Top

  NameDescription
Public eventApplicationChangedRaised whenever a PeerApplication associated with a PeerContact in the ContactManager has changed.
Public eventCreateContactCompletedRaised whenever a CreateContact method has completed.
Public eventNameChangedRaised whenever the PeerName associated with a PeerContact in the ContactManager has changed.
Public eventObjectChangedRaised whenever an object within a contact’s registered PeerObject objects has changed.
Public eventPresenceChangedRaised whenever the presence status of a PeerContact in the ContactManager has changed.
Public eventSubscriptionListChangedRaised when the list of subscribed contacts changes.
Top

This class has no public constructors, since a reference to it is returned by the PeerCollaboration class.

Address books that are associated with PeerContact management include those associated with the host peer and a remote peer. Either computer may collaborate with other peers; when a peer is local to the remote peer but not the host peer, it can be added to the ContactManager of the host peer. Certain operations on the ContactManager class, such as AddContact and DeleteContact, correlate to the associated operations being performed on the Windows Address book of the remote peer.

The host peer has access to this persistent storage for PeerContact information, which can be associated with the applications in which a peer intends to participate. A PeerApplication can identify itself as a user of a ContactManager persistent store on a computer.

The following example code illustrates how to enumerate a PeerNearMe and add it as a PeerContact to the local ContactManager:

// Displays all contacts and asssociated peer endpoints (PeerEndPoint) in the PeerContactCollection. 
private static void DisplayContacts(PeerContactCollection peerContactsCollection)
{
    if (peerContactsCollection == null ||
        peerContactsCollection.Count == 0)
    {
        Console.WriteLine("No contacts to display. To add a contact select option 0 from the menu.");
    }
    else
    {
        foreach (PeerContact pc in peerContactsCollection)
        {
            Console.WriteLine("The contact is: {0}", pc.DisplayName);
            DisplayEndpoints(pc.PeerEndPoints);
        }
    }
    return;
}

//------------------------------------------------------------------------------------------------------ 
//------------------------------------------------------------------------------------------------------ 
// Displays all peer end points (PeerEndPoint) in the PeerEndPointCollection. 

private static void DisplayEndpoints(PeerEndPointCollection endpointCollection)
{
    if (endpointCollection == null ||  endpointCollection.Count == 0)
        Console.WriteLine("No peer endpoints in the collection to display.");
    else
    {
        foreach (PeerEndPoint pep in endpointCollection)
        {
            Console.WriteLine("PeerEndPoint is: {0}", pep);
            Console.WriteLine("PeerEndPoint data is:\n  Name: {0}\n EndPoint IP address: {1}\n . Port: {2}\n",
                pep.Name,
                pep.EndPoint.Address,
                pep.EndPoint.Port);
        }
    }
    return;
}

//------------------------------------------------------------------------------------------------------ 
//------------------------------------------------------------------------------------------------------ 
//List PeerNearMe objects that may be added as contacts. 

private static void AddContact()
{
    PeerNearMeCollection pnmc = null;
    PeerContactCollection peerContacts = null;            
    bool peerNameFound = false;

    PeerApplication application = null;

    try
    {
        Console.WriteLine("Listing the existing contacts...");
        peerContacts = PeerCollaboration.ContactManager.GetContacts();
    }
    catch (PeerToPeerException p2pEx)
    {
        Console.WriteLine("The Peer Collaboration Infrastructure is not responding to the contact enumeration request: {0}", p2pEx.Message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("An unexpected error occurred while attempting to obtain the contact list: {0}", ex.Message);
    }

    DisplayContacts(peerContacts);

    try
    {
        //Adds one of the PeerNearMe objects as a contact.                               
        pnmc = GetPeersNearMe();
        Console.WriteLine("Please enter the nickname of the peer you wish to add as a contact:");
        string peerNameToAdd = Console.ReadLine();

        application = RegisterCollabApp();

        foreach (PeerNearMe pnm in pnmc)
        {
            PeerInvitationResponse res = null;
            if (pnm.Nickname.Contains(peerNameToAdd))
            {
                peerNameFound = true;
                if (!peerContacts.ToString().Contains(pnm.Nickname))
                {
                    Console.WriteLine("Adding peer {0} to the contact list.", pnm.Nickname);
                    pnm.AddToContactManager();
                }
                else
                {
                    Console.WriteLine("This peer already exists in your contact list.");
                    Console.WriteLine("Sending invitation using the Contact structure instead of the PeerNearMe.");
                    foreach (PeerContact pc in peerContacts)
                    {
                        if (pc.Nickname.Equals(pnm.Nickname))
                        {
                            res = pnm.Invite(application, "Peer Collaboration Sample", application.Data);
                            if (res.PeerInvitationResponseType == PeerInvitationResponseType.Accepted)
                            {
                                Console.WriteLine("Invitation to contact succeeded.");
                            }
                            else
                            {
                                Console.WriteLine("Invitation to contact {0}.", res.PeerInvitationResponseType);
                            }
                        }
                    }

                }
            }
        }

        if (!peerNameFound)
        {
            Console.WriteLine("No such peer exists near you. Cannot add to contacts.");
            return;
        }

        peerContacts = PeerCollaboration.ContactManager.GetContacts();

        Console.WriteLine("Listing the contacts again...");
        DisplayContacts(peerContacts);

    }
    catch (Exception ex)
    {
        Console.WriteLine("Error adding a contact: {0}", ex.Message);
    }
    finally
    {
        application.Dispose();
    }
    return;
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft