Ejemplo n.º 1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="packet">The switch packet.</param>
        internal SwitchEventReceivedArgs(SwitchPacket packet)
        {
            ArgCollection properties;
            ArgCollection variables;
            string        eventName;

            this.TimeUtc = packet.Headers.Get("Event-Date-GMT", DateTime.MinValue);

            if (packet.Headers.TryGetValue("Event-Name", out eventName))
            {
                this.EventName = eventName;
                this.EventCode = SwitchHelper.ParseEventCode(eventName);
            }
            else
            {
                // I don't think this should ever happen.

                SysLog.LogWarning("SwitchConnection received an event without an [Event-Name] property.");

                this.EventName = string.Empty;
                this.EventCode = default(SwitchEventCode);
            }

            SwitchHelper.ProcessEventProperties(packet.Headers, out properties, out variables);

            this.Properties  = properties;
            this.Variables   = variables;
            this.ContentType = packet.ContentType;
            this.Content     = packet.Content;
            this.ContentText = packet.ContentText;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles the parsing of the event properties and subcontent from the packet content.  The packet's
        /// <see cref="Headers"/> dictionary, as well as the <see cref="ContentType"/>, <see cref="Content" />,
        /// and <see cref="ContentText" /> property values will be overwritten with the parsed event
        /// properties.
        /// </summary>
        private void ParseEvent()
        {
            Assertion.Test(PacketType == SwitchPacketType.Event);

            if (Content == null)
            {
                SysLog.LogWarning("Encountered switch event packet with no content.");
                PacketType = SwitchPacketType.Unknown;
                return;
            }

            string headers;
            int    cbContent;
            int    pos;

            string[] lines;
            string[] fields;
            string   name;
            string   value;

            pos = Helper.IndexOf(Content, LFLF);
            if (pos == -1)
            {
                SysLog.LogWarning("Ignoring a switch event packet with headers not terminated by LFLF.");
                PacketType = SwitchPacketType.Unknown;
                return;
            }

            // Parse the event headers.

            headers      = Helper.ASCIIEncoding.GetString(Content, 0, pos);
            this.Headers = new ArgCollection(ArgCollectionType.Unconstrained);
            lines        = headers.Split('\n');

            foreach (var line in lines)
            {
                fields = line.Split(new char[] { ':' }, 2);
                if (fields.Length < 2)
                {
                    continue;
                }

                name          = fields[0].Trim();
                value         = Helper.UrlDecode(fields[1].Trim());
                Headers[name] = value;
            }

            if (!Headers.TryGetValue("Event-Name", out value))
            {
                SysLog.LogWarning("Ignoring a switch event packet without an [Event-Name] header.");
                PacketType = SwitchPacketType.Unknown;
                return;
            }

            EventCode = SwitchHelper.ParseEventCode(value);

            // Extract the event content data.

            pos += 2;   // Skip over the LFLF after the headers.

            ContentType = null;
            ContentText = null;

            if (Headers.TryGetValue("Content-Length", out value))
            {
                cbContent = int.Parse(value);
                Content   = Helper.Extract(Content, pos);

                if (Content.Length != cbContent)
                {
                    SysLog.LogWarning("Ignoring malformed switch event packet. Expected [{0}] bytes of event content but received [{1}] bytes.", cbContent, Content.Length);
                    PacketType = SwitchPacketType.Unknown;
                    return;
                }

                if (Headers.TryGetValue("Content-Type", out value))
                {
                    ContentType = value.ToLower();
                    if (ContentType != null && ContentType.StartsWith("text"))
                    {
                        ContentText = Helper.ASCIIEncoding.GetString(Content);
                    }
                }
            }
            else
            {
                Content = null;
            }
        }