// </Snippet7> //------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------ // <Snippet8> // Getting the presence info for the contacts in the Contact Collection. private static void GetPresenceInformation() { Console.WriteLine("Getting presence information..."); { PeerContactCollection peerContacts = PeerCollaboration.ContactManager.GetContacts(); if (null == peerContacts) { Console.WriteLine("Unable to enumerate contacts."); return; } // If there are no contacts available to watch, notify the console if (peerContacts.Count == 0) { Console.WriteLine("No contacts for which to obtain presence information."); return; } Console.WriteLine("Printing out all contacts in your contact store..."); PeerEndPoint peerEndPoint = null; foreach (PeerContact pc in peerContacts) { try { Console.WriteLine("The contact display name is: \"{0}\" and the peer name is \"{1}\".", pc.DisplayName, pc.PeerName); // In case there are multiple endpoints, pick an endpoint for this contact peerEndPoint = PickEndpointForContact(pc); Console.WriteLine("The presence information is: {0}.", pc.GetPresenceInfo(peerEndPoint).PresenceStatus.ToString()); } catch (ArgumentException argEx) { Console.WriteLine("The provided endpoint is null or is not valid: {0}", argEx.Message); } catch (PeerToPeerException p2pEx) { Console.WriteLine("The Peer Collaboration Infrastructure could not obtain presence data for the endpoint: {0}", p2pEx.Message); } catch (Exception ex) { Console.WriteLine("Error enumerating the contacts: {0}", ex); } } } return; }
//------------------------------------------------------------------------------------------------------ // <Snippet6> //Enumerating the contacts and letting the user choose which one to delete. public static void DeleteContact() { PeerContactCollection pcc = null; string contactToDelete = ""; try { pcc = EnumContacts(); if (pcc == null || pcc.Count == 0) { Console.WriteLine("Contact list is empty -- no such contact exists."); return; } Console.Write("Please enter the nickname of the contact you wish to delete: "); contactToDelete = Console.ReadLine(); foreach (PeerContact pc in pcc) { if (pc.Nickname.Equals(contactToDelete)) { PeerCollaboration.ContactManager.DeleteContact(pc); Console.WriteLine("Contact {0} successfully deleted!", contactToDelete); return; } } Console.WriteLine("Contact {0} could not be found in the contact collection.", contactToDelete); } catch (ArgumentNullException argNullEx) { Console.WriteLine("The supplied contact is null: {0}", argNullEx.Message); } catch (ArgumentException argEx) { Console.WriteLine("The supplied contact \"{0}\" could not be found in the Contact Manager: {1}", contactToDelete, argEx.Message); } catch (PeerToPeerException p2pEx) { Console.WriteLine("The Peer Collaboration Infrastructure could not delete \"{0}\": {1}", contactToDelete, p2pEx.Message); } catch (Exception ex) { Console.WriteLine("Unexpected exception when trying to delete a contact : {0}", ex); } return; }
// </Snippet2> //------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------ // <Snippet3> // 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; }
// </Snippet5> //------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------ // Printing the user menu. private static PeerContactCollection EnumContacts() { PeerContactCollection peerContacts = null; Console.WriteLine("Enumerating your contacts..."); try { peerContacts = PeerCollaboration.ContactManager.GetContacts(); // The following method displays an enumeration of all available contacts. DisplayContacts(peerContacts); } catch (PeerToPeerException p2pEx) { Console.WriteLine("The Peer Collaboration Infrastructure could not return an enumeration of the available contacts: {0}", p2pEx.Message); } catch (Exception ex) { Console.WriteLine("Error enumerating the contacts: {0}", ex); } return(peerContacts); }
//------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------ //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; }
public PeerContactCollection GetContacts() { PeerContactCollection peerContactColl = new PeerContactCollection(); SafeCollabEnum handlePeerEnum = null; UInt32 contactCount = 0; int errorCode = 0; if (m_Disposed) throw new ObjectDisposedException(this.GetType().FullName); PeerCollaborationPermission.UnrestrictedPeerCollaborationPermission.Demand(); Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Entering GetContacts()"); try{ // // Get contacts array from native // errorCode = UnsafeCollabNativeMethods.PeerCollabEnumContacts(out handlePeerEnum); if (errorCode != 0){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabEnumContacts returned with errorcode {0}", errorCode); throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetContactsFailed), errorCode); } errorCode = UnsafeCollabNativeMethods.PeerGetItemCount(handlePeerEnum, ref contactCount); if (errorCode != 0){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerGetItemCount returned with errorcode {0}", errorCode); throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetContactsFailed), errorCode); } if (contactCount == 0){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "No contacts found. \nLeaving GetContacts()"); return peerContactColl; } unsafe{ SafeCollabData contactArray = null; try{ errorCode = UnsafeCollabNativeMethods.PeerGetNextItem(handlePeerEnum, ref contactCount, out contactArray); if (errorCode != 0){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerGetNextItem returned with errorcode {0}", errorCode); throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetContactsFailed), errorCode); } // // Loop through the contacts array to build PeerContact collection // IntPtr pPEER_CONTACT = contactArray.DangerousGetHandle(); IntPtr* pContacts = (IntPtr*)pPEER_CONTACT; for (ulong i = 0; i < contactCount; i++){ IntPtr pContactPtr = (IntPtr)pContacts[i]; PEER_CONTACT pc = (PEER_CONTACT)Marshal.PtrToStructure(pContactPtr, typeof(PEER_CONTACT)); PeerContact peerContact = CollaborationHelperFunctions.ConvertPEER_CONTACTToPeerContact(pc); peerContactColl.Add(peerContact); } } finally{ contactArray.Dispose(); } } } finally{ handlePeerEnum.Dispose(); } Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Returning collections with {0} contacts. \nLeaving GetContacts()"); return peerContactColl; }