Example #1
0
        /// <summary>
        /// Serialize the soap message
        /// </summary>
        /// <param name="envelope">The soap envelope</param>
        /// <returns>Serialized value</returns>
        public static string Serialize(SoapEnvelope envelope)
        {
            string result = null;

            XmlAttributes attrs = new XmlAttributes();

            XmlElementAttribute attr = new XmlElementAttribute();

            attr.ElementName = "Body";
            attr.Type        = envelope.body.GetType();
            attrs.XmlElements.Add(attr);

            XmlAttributes       attrs2 = new XmlAttributes();
            XmlElementAttribute attr2  = new XmlElementAttribute();

            attr2.ElementName = "Header";
            attr2.Type        = envelope.header.GetType();

            attrs2.XmlElements.Add(attr2);

            XmlAttributeOverrides attros = new XmlAttributeOverrides();

            attros.Add(typeof(SoapEnvelope), "Body", attrs);
            attros.Add(typeof(SoapEnvelope), "Header", attrs2);

            XmlSerializer a = new XmlSerializer(typeof(SoapEnvelope), attros);

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            ns.Add("soap", "http://www.w3.org/2003/05/soap-envelope");
            ns.Add("wsa", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
            ns.Add("wsd", "http://schemas.xmlsoap.org/ws/2005/04/discovery");
            ns.Add("PeerDist", "http://schemas.microsoft.com/p2p/2007/09/PeerDistributionDiscovery");

            using (MemoryStream ms = new MemoryStream())
            {
                XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8);
                writer.Formatting = Formatting.None;
                a.Serialize(writer, envelope, ns);
                writer.Close();
                result = Encoding.UTF8.GetString(ms.ToArray());
            }

            return(result);
        }
        /// <summary>
        /// receive message handler
        /// </summary>
        /// <param name="sender">The sender</param>
        /// <param name="message">The soap envelope message</param>
        private void Server_ReceiveProbeMessage(IPEndPoint sender, SoapEnvelope message)
        {
            WsdHeader header = (WsdHeader)message.Header;
            ProbeType probe = ((ProbeOp)message.Body).Probe;
            if (this.receive && (!this.returned.Contains(header.MessageID.Value)))
            {
                this.CaptureHeaderRequirements(header);
                this.CaptureProbeRequirements(probe);
                PccrdBothRoleCaptureCode.CaptureCommonRequirements(Site);
                PccrdBothRoleCaptureCode.CaptureTypesElementRequirements(probe.Types, Site);
                PccrdBothRoleCaptureCode.CaptureScopesElementRequirements(probe.Scopes, Site);

                ProbeMsg probeMsg = new ProbeMsg(header.MessageID.Value, probe.Types, probe.Scopes.Text[0]);
                this.ReceiveProbeMessage(sender, probeMsg);
            }
        }
        /// <summary>
        /// Receive probe message.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="message">The probe message.</param>
        private void Server_ReceiveProbeMessage(IPEndPoint sender, SoapEnvelope message)
        {
            WsdHeader header = (WsdHeader)message.Header;
            ProbeType probe = ((ProbeOp)message.Body).Probe;
            if (!this.isReceived && (!this.returned.Contains(header.MessageID.Value)))
            {
                this.isReceived = true;

                ProbeMsg probeMsg = new ProbeMsg(header.MessageID.Value, probe.Types, probe.Scopes.Text[0]);
                this.ReceiveProbeMessage(sender, probeMsg);
            }
        }
 /// <summary>
 /// receive message
 /// </summary>
 /// <param name="sender">The sender</param>
 /// <param name="message">The soap envelope message</param>
 public void HandleRequest(IPEndPoint sender, SoapEnvelope message)
 {
     this.ReceiveProbeMessage(sender, message);
 }
 /// <summary>
 /// send unicast message
 /// </summary>
 /// <param name="message">The soap envelope message</param>
 /// <param name="ipaddress">The remote ipaddress</param>
 /// <param name="port">The used port number for transporting</param>
 public void SendUnicast(SoapEnvelope message, string ipaddress, int port)
 {
     this.service.SendUnicast(message, ipaddress, port);
 }
        /// <summary>
        /// Send multicast message
        /// </summary>
        /// <param name="message">The multicast message</param>
        public void SendMulticast(SoapEnvelope message)
        {
            if (null == message)
            {
                throw new ArgumentNullException("message", "The input parameter \"message\" is null.");
            }

            this.transport.SendBytes(Encoding.UTF8.GetBytes(SoapEnvelope.Serialize(message)), MultiAddress, MultiPort);
        }
        /// <summary>
        /// Send unicast message
        /// </summary>
        /// <param name="message">The unicast message</param>
        /// <param name="address">Destination IP address</param>
        /// <param name="port">Destination port</param>
        public void SendUnicast(SoapEnvelope message, string address, int port)
        {
            if (null == message)
            {
                throw new ArgumentNullException("message", "The input parameter \"message\" is null.");
            }

            if (string.IsNullOrEmpty(address))
            {
                throw new ArgumentNullException("ipAddress", "The input parameter \"ipAddress\" is null or empty.");
            }

            if (0 == port)
            {
                throw new ArgumentNullException("ipAddress", "The input parameter \"ipAddress\" is 0.");
            }

            this.transport.SendBytes(Encoding.UTF8.GetBytes(SoapEnvelope.Serialize(message)), address, port);
        }
        /// <summary>
        /// Serialize the soap message
        /// </summary>
        /// <param name="envelope">The soap envelope</param>
        /// <returns>Serialized value</returns>
        public static string Serialize(SoapEnvelope envelope)
        {
            string result = null;

            XmlAttributes attrs = new XmlAttributes();

            XmlElementAttribute attr = new XmlElementAttribute();
            attr.ElementName = "Body";
            attr.Type = envelope.body.GetType();
            attrs.XmlElements.Add(attr);

            XmlAttributes attrs2 = new XmlAttributes();
            XmlElementAttribute attr2 = new XmlElementAttribute();
            attr2.ElementName = "Header";
            attr2.Type = envelope.header.GetType();

            attrs2.XmlElements.Add(attr2);

            XmlAttributeOverrides attros = new XmlAttributeOverrides();
            attros.Add(typeof(SoapEnvelope), "Body", attrs);
            attros.Add(typeof(SoapEnvelope), "Header", attrs2);

            XmlSerializer a = new XmlSerializer(typeof(SoapEnvelope), attros);

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("soap", "http://www.w3.org/2003/05/soap-envelope");
            ns.Add("wsa", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
            ns.Add("wsd", "http://schemas.xmlsoap.org/ws/2005/04/discovery");
            ns.Add("PeerDist", "http://schemas.microsoft.com/p2p/2007/09/PeerDistributionDiscovery");

            using (MemoryStream ms = new MemoryStream())
            {
                XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8);
                writer.Formatting = Formatting.None;
                a.Serialize(writer, envelope, ns);
                writer.Close();
                result = Encoding.UTF8.GetString(ms.ToArray());
            }

            return result;
        }
        /// <summary>
        /// Receive the pccrd messages
        /// </summary>
        /// <param name="sender">The sender</param>
        /// <param name="message">The soap envelope message</param>
        private void Client_ReceivePccrdMessage(IPEndPoint sender, SoapEnvelope message)
        {
            Status status = new Status();
            status.ErrorCode = string.Empty;
            status.FaultType = FaultType.OK;

            WsdHeader header = (WsdHeader)message.Header;
            if (this.startReceiving && (!this.returned.Contains(header.MessageID.Value)))
            {
                this.returned.Add(header.MessageID.Value);
                ProbeMatchesType matches = ((ProbeMatchOp)message.Body).ProbeMatches;
                this.CaptureHeaderRequirements(header);
                this.CaptureProbeMatchesRequirements(matches);
                PccrdBothRoleCaptureCode.CaptureCommonRequirements(Site);
                foreach (ProbeMatchType match in matches.ProbeMatch)
                {
                    PccrdBothRoleCaptureCode.CaptureTypesElementRequirements(match.Types, Site);
                    PccrdBothRoleCaptureCode.CaptureScopesElementRequirements(match.Scopes, Site);
                }

                ProbeMatchMsg probeMatchMsg = new ProbeMatchMsg();
                probeMatchMsg.InstanceId = header.AppSequence.InstanceId.ToString();
                probeMatchMsg.MessageNumber = header.AppSequence.MessageNumber;
                PeerProperties[] result = new PeerProperties[matches.ProbeMatch.Length];

                for (int i = 0; i < matches.ProbeMatch.Length; i++)
                {
                    result[i] = new PeerProperties();
                    result[i].Address = matches.ProbeMatch[i].EndpointReference.Address.Value;
                    result[i].BlockCount = matches.ProbeMatch[i].Any[0].InnerText;
                    result[i].MetadataVersion = matches.ProbeMatch[i].MetadataVersion;
                    result[i].Scopes = matches.ProbeMatch[i].Scopes.Text[0];
                    result[i].Types = matches.ProbeMatch[i].Types;
                    result[i].XAddrs = matches.ProbeMatch[i].XAddrs;
                }

                probeMatchMsg.Matches = result;
                this.ReceiveProbeMatchMessage(status, probeMatchMsg);
            }
        }
 /// <summary>
 /// receive message
 /// </summary>
 /// <param name="sender">The sender</param>
 /// <param name="message">The soap envelope message</param>
 private void ReceiveMessage(IPEndPoint sender, SoapEnvelope message)
 {
     this.ReceivePccrdMessage(sender, message);
 }
 /// <summary>
 /// Sends message
 /// </summary>
 /// <param name="reqMsg">out message</param>
 public void SendMessage(SoapEnvelope reqMsg)
 {
     this.discoveryClient.SendMulticast(reqMsg);
 }
 /// <summary>
 /// Create a probe match message
 /// </summary>
 /// <param name="relatesTo">The URI of relates to</param>
 /// <param name="instanceId">Instance ID</param>
 /// <param name="messageNumber">Message number</param>
 /// <param name="matches">Probe match messages</param>
 /// <returns>The response message</returns>
 public SoapEnvelope CreateProbeMatchMessage(string relatesTo, string instanceId, uint messageNumber, ProbeMatchType[] matches)
 {
     WsdHeader header = new WsdHeader();
     header.Action.Value = ProtocolStrings.ProbeMatchesAction;
     header.RelatesTo = new AttributedURI();
     header.RelatesTo.Value = relatesTo;
     header.AppSequence = new AppSequenceType();
     header.AppSequence.MessageNumber = 1;
     header.AppSequence.InstanceId = (uint)(this.serviceStartTime - this.baseTime).Seconds;
     header.To = new AttributedURI();
     header.To.Value = "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous";
     ProbeMatchOp match = new ProbeMatchOp();
     match.ProbeMatches = new ProbeMatchesType();
     match.ProbeMatches.ProbeMatch = matches;
     SoapEnvelope resp = new SoapEnvelope(SoapMessageVersion.Soap11Wsa10, header, match);
     return resp;
 }