Example #1
0
        public static PeerName CreateRelativePeerName(
            PeerName peerName,
            string classifier)
        {
            //-------------------------------------------------
            //Check arguments
            //-------------------------------------------------
            if (peerName == null)
            {
                throw new ArgumentNullException("peerName", SR.GetString(SR.Pnrp_PeerNameCantBeNull));
            }
            if (!peerName.IsSecured && (classifier == null || classifier.Length == 0))
            {
                throw new ArgumentException(SR.GetString(SR.Pnrp_InvalidClassifier), "classifier");
            }
            if (classifier != null && classifier.Length > PEER_MAX_CLASSIFIER_LENGTH)
            {
                throw new ArgumentException(SR.GetString(SR.Pnrp_InvalidClassifier), "classifier");
            }

            //--------------------------------------------------
            //Normalize using NFC
            //--------------------------------------------------
            if (classifier != null && classifier.Length > 0)
            {
                classifier = classifier.Normalize(NormalizationForm.FormC);
            }

            Int32        result;
            SafePeerData shNewPeerName = null;
            string       newPeerName   = null;

            try
            {
                //Here there is change made on the native side
                //when passing secured peer names, it takes string of the form [40hexdigits].claasisifer and a newclassifier
                //returns [40hexdigits.newclassifier]
                //But for unsecured peer names it does not take 0.clasfier and newclassfier to return 0.newclassfier.
                //It expects NULL as the first param. To satisfy this broken finctionality, we are passing null if the
                //peer name is unsecured.
                result = UnsafeP2PNativeMethods.PeerCreatePeerName(peerName.IsSecured? peerName.m_PeerName : null, classifier, out shNewPeerName);
                if (result != 0)
                {
                    throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Pnrp_CouldNotCreateRelativePeerName), result);
                }
                newPeerName = shNewPeerName.UnicodeString;
            }
            finally
            {
                if (shNewPeerName != null)
                {
                    shNewPeerName.Dispose();
                }
            }

            string authority;
            string newClassifier;

            WeakParsePeerName(newPeerName, out authority, out newClassifier);
            PeerName p = new PeerName(newPeerName, authority, newClassifier);

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "A new PeerName created from existing PeerName with a new classfier. Existing PeerName {0} Classifier {1} New PeerName {2}", peerName, classifier, p);
            return(p);
        }
Example #2
0
        public PeerName(string classifier, PeerNameType peerNameType)
        {
            //-------------------------------------------------
            //Check arguments
            //-------------------------------------------------
            if ((classifier == null || classifier.Length == 0) &&
                peerNameType == PeerNameType.Unsecured)
            {
                throw new ArgumentNullException("classifier");
            }
            if (classifier != null && classifier.Length > PEER_MAX_CLASSIFIER_LENGTH)
            {
                throw new ArgumentException(SR.GetString(SR.Pnrp_InvalidClassifier), "classifier");
            }
            //--------------------------------------------------
            //Normalize using NFC
            //--------------------------------------------------
            if (classifier != null && classifier.Length > 0)
            {
                classifier = classifier.Normalize(NormalizationForm.FormC);
            }
            //-------------------------------------------------
            //call the helper to create the PeerName
            //-------------------------------------------------
            Int32        result;
            SafePeerData shNewPeerName     = null;
            SafePeerData shDefaultIdentity = null;

            try
            {
                if (peerNameType == PeerNameType.Unsecured)
                {
                    result = UnsafeP2PNativeMethods.PeerCreatePeerName((string)null, classifier, out shNewPeerName);
                    if (result != 0)
                    {
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Pnrp_CouldNotCreateUnsecuredPeerName), result);
                    }
                    m_Authority = PEERNAME_UNSECURED_AUTHORITY;
                }
                else
                {
                    result = UnsafeP2PNativeMethods.PeerIdentityGetDefault(out shDefaultIdentity);
                    if (result != 0)
                    {
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Pnrp_CouldNotGetDefaultIdentity), result);
                    }
                    m_Authority = shDefaultIdentity.UnicodeString;
                    //}

                    result = UnsafeP2PNativeMethods.PeerCreatePeerName(m_Authority, classifier, out shNewPeerName);
                    if (result != 0)
                    {
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Pnrp_CouldNotCreateSecuredPeerName), result);
                    }
                }
                m_PeerName   = shNewPeerName.UnicodeString;
                m_Classifier = classifier;
            }
            finally
            {
                if (shNewPeerName != null)
                {
                    shNewPeerName.Dispose();
                }
                if (shDefaultIdentity != null)
                {
                    shDefaultIdentity.Dispose();
                }
            }
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "PeerName instance created - PeerName {0} Authority {1} Classfier {2}", m_PeerName, m_Authority, m_Classifier);
        }