Beispiel #1
0
        private void RefreshButton_Click(object sender, RoutedEventArgs e)
        {
            // Get local peers
            PeerNearMeCollection peersNearMe = PeerCollaboration.GetPeersNearMe();

            // Prepare for new peers
            PeerList.Items.Clear();

            // Examine peers
            foreach (PeerNearMe peerNearMe in peersNearMe)
            {
                PeerList.Items.Add(
                    new PeerEntry
                {
                    PeerNearMe     = peerNearMe,
                    PresenceStatus = peerNearMe.GetPresenceInfo(peerNearMe.PeerEndPoints[0]).PresenceStatus,
                    DisplayString  = peerNearMe.Nickname
                });
            }

            // Add failure message if necessary
            if (PeerList.Items.Count == 0)
            {
                PeerList.Items.Add(
                    new PeerEntry
                {
                    DisplayString = "No peers found."
                });
            }
        }
Beispiel #2
0
        private void btnGet_Click(object sender, RoutedEventArgs e)
        {
            // 取得目前所有登入的Peer端點
            PeerNearMeCollection pnmc = PeerCollaboration.GetPeersNearMe();

            String str = "";

            foreach (PeerNearMe pnm in pnmc)
            {
                // 取得Peer端點的暱稱
                str = pnm.Nickname;

                // 取得Peer端點的位置
                PeerEndPointCollection pepc = pnm.PeerEndPoints;

                foreach (PeerEndPoint pep in pepc)
                {
                    // 回傳System.Net.IPEndPoint物件
                    IPEndPoint ipEndPoint = pep.EndPoint;

                    // 取得Peer端點的IP位址
                    str = str + ", " + ipEndPoint.Address.ToString() + ":";
                    // 取得Peer端點的通訊埠
                    str = str + ipEndPoint.Port.ToString();
                }

                lstPeer.Items.Add(str);
            }
        }
Beispiel #3
0
        private void buttonLoad_Click(object sender, EventArgs e)
        {
            PeerNearMeCollection peerNearMeCollection = PeerCollaboration.GetPeersNearMe();

            foreach (PeerNearMe peerNearMe in peerNearMeCollection)
            {
                peerNearMe.RefreshDataAsync("Load");
            }
        }
Beispiel #4
0
        //------------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------------
        // <Snippet2>
        //This function retrieves the peers near me as a PeerNearMeCollection.
        public static PeerNearMeCollection GetPeersNearMe()
        {
            PeerNearMeCollection peers = null;

            try
            {
                peers = PeerCollaboration.GetPeersNearMe();
                if (peers == null ||
                    peers.Count == 0)
                {
                    Console.WriteLine("There are no peers near me.");
                }
                foreach (PeerNearMe pnm in peers)
                {
                    Console.WriteLine("Getting the peers near me: {0}, nickname {1}, isOnline {2}",
                                      pnm.ToString(),
                                      pnm.Nickname,
                                      pnm.IsOnline);
                }
            }
            catch (PeerToPeerException p2pEx)
            {
                Console.WriteLine("Could not obtain an enumeration of the peers near me: {0}", p2pEx.Message);
            }
            catch (InvalidOperationException ioEx)
            {
                Console.WriteLine("The application is no longer signed into the Peer Collaboration Infrastructure: {0}",
                                  ioEx.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unexpected exception caught when trying to enumerate the peers near me: {0}",
                                  ex.Message);
            }

            return(peers);
        }
Beispiel #5
0
        private void buttonStartOrUpdate_Click(object sender, EventArgs e)
        {
            if (PeerOpened)
            {
                AddLog("UpdateSend", LogType.System);
                SetSendData();
            }
            else
            {
                try
                {
                    PeerCollaboration.SignIn(PeerScope);
                }
                catch (PeerToPeerException ex)
                {
                    AddLog("SignIn:" + ex.Message, LogType.System);
                    return;
                }
                SetupPeer();
                UpdateUI();

                PeerNearMe.PeerNearMeChanged += PeerNearMe_PeerNearMeChanged;

                AddLog("StartPeer", LogType.System);
                SetSendData();

                //buttonLoad.PerformClick();

                PeerNearMeCollection peerNearMeCollection = PeerCollaboration.GetPeersNearMe();
                foreach (PeerNearMe peerNearMe in peerNearMeCollection)
                {
                    peerNearMe.RefreshDataCompleted += peerNearMe_RefreshDataCompleted;
                    peerNearMe.RefreshDataAsync("Setup");
                }
            }
        }
        public static PeerNearMeCollection GetPeersNearMe()
        {
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Entering GetPeersNearMeGetPeersNearMe.");
            CollaborationHelperFunctions.Initialize();
            PeerCollaborationPermission.UnrestrictedPeerCollaborationPermission.Demand();

            PeerNearMeCollection pnmc = new PeerNearMeCollection();

            SafeCollabEnum handlePeerEnum = null;
            UInt32 pnmCount = 0;
            int errorCode = 0;

            try{
                //
                // Call native to get the enumeration of peers near
                //
                errorCode = UnsafeCollabNativeMethods.PeerCollabEnumPeopleNearMe(out handlePeerEnum);

                if (errorCode != 0){
                    Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabEnumPeopleNearMe returned with errorcode {0}", errorCode);
                    throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetPeersNearMeFailed), errorCode);
                }

                errorCode = UnsafeCollabNativeMethods.PeerGetItemCount(handlePeerEnum, ref pnmCount);
                if (errorCode != 0){
                    Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerGetItemCount returned with errorcode {0}", errorCode);
                    throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetPeersNearMeFailed), errorCode);
                }

                if (pnmCount == 0){
                    Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "No peers found. \nLeaving GetPeersNearMe()");
                    return pnmc;
                }

                unsafe{
                    SafeCollabData appArray;
                    errorCode = UnsafeCollabNativeMethods.PeerGetNextItem(handlePeerEnum, ref pnmCount, out appArray);

                    IntPtr pPEER_PEOPLE_NEAR_ME = appArray.DangerousGetHandle();
                    IntPtr* pPeersNearMe = (IntPtr*)pPEER_PEOPLE_NEAR_ME;

                    //
                    // Loops through individual native structures and makes peers from them 
                    //
                    for (ulong i = 0; i < pnmCount; i++){
                        IntPtr pContactPtr = (IntPtr)pPeersNearMe[i];
                        PEER_PEOPLE_NEAR_ME pnm = (PEER_PEOPLE_NEAR_ME)Marshal.PtrToStructure(pContactPtr, typeof(PEER_PEOPLE_NEAR_ME));

                        PeerNearMe peerNearMe = new PeerNearMe();
                        peerNearMe.Id = CollaborationHelperFunctions.ConvertGUIDToGuid(pnm.id);
                        peerNearMe.Nickname = Marshal.PtrToStringUni(pnm.pwzNickname); ;

                        PEER_ENDPOINT pe = pnm.endpoint;
                        peerNearMe.PeerEndPoints.Add(CollaborationHelperFunctions.ConvertPEER_ENDPOINTToPeerEndPoint(pe));

                        pnmc.Add(peerNearMe);
                    }
                }
            }
            finally{
                if (handlePeerEnum != null) handlePeerEnum.Dispose();
            }

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving GetPeersNearMeGetPeersNearMe with {0} peers.", pnmc.Count);

            return pnmc;
        }
Beispiel #7
0
        //------------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------------
        //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;
        }