Ejemplo n.º 1
0
        internal EventMessage(BasicMessage basicMessage)
        {
            if (basicMessage.ContentType != ContentTypes.EventPlain)
            {
                /*
                 * Special Case:
                 * When an Outbound Socket sends the "connect" command, FreeSwitch responds with a Command-Reply.
                 * This Command-Reply message contains a CHANNEL_DATA event message buried in its headers.
                 * In this case, we can hydrate an event message from a message of content type Command-Reply.
                 */
                if (basicMessage.Headers.ContainsKey(HeaderNames.EventName))
                {
                    Headers  = basicMessage.Headers;
                    BodyText = basicMessage.BodyText;
                    return;
                }

                /* otherwise, we'll throw an exception if we get the wrong content-type message passed in*/
                throw new InvalidOperationException("Expected content type event/plain, got {0} instead.".Fmt(basicMessage.ContentType));
            }

            // normally, the content of the event will be in the BasicMessage's body text and will need to be parsed to produce an EventMessage
            if (string.IsNullOrEmpty(basicMessage.BodyText))
            {
                throw new ArgumentException("Message did not contain an event body.");
            }

            try
            {
                var delimiterIndex = basicMessage.BodyText.IndexOf("\n\n", StringComparison.Ordinal);
                if (delimiterIndex == -1 || delimiterIndex == basicMessage.BodyText.Length - 2)
                {
                    // body text consists of key-value-pair event headers, no body
                    Headers  = basicMessage.BodyText.ParseKeyValuePairs(": ");
                    BodyText = null;
                }
                else
                {
                    // ...but some Event Messages also carry a body payload, eg. a BACKGROUND_JOB event
                    // which is a message carried inside an EventMessage carried inside a BasicMessage..
                    Headers = basicMessage.BodyText.Substring(0, delimiterIndex).ParseKeyValuePairs(": ");
                    var contentLength = int.Parse(Headers[HeaderNames.ContentLength]);

                    var body = basicMessage.BodyText.Substring(delimiterIndex + 2, contentLength);

                    //remove any \n\n if any
                    var index = body.IndexOf("\n\n", System.StringComparison.Ordinal);
                    this.BodyText = index > 0 ? body.Substring(0, index) : body;
                }
            }
            catch (Exception ex)
            {
                Log.ErrorException("Failed to parse body of event", ex);
                Log.Error(BodyText);
                throw;
            }
        }
Ejemplo n.º 2
0
        internal CommandReply(BasicMessage basicMessage)
        {
            if (basicMessage.ContentType != ContentTypes.CommandReply)
            {
                throw new ArgumentException("Expected content type command/reply, got {0} instead.".Fmt(basicMessage.ContentType));
            }

            Headers = basicMessage.Headers;
            BodyText = basicMessage.BodyText;
        }
Ejemplo n.º 3
0
        internal ApiResponse(BasicMessage basicMessage)
        {
            if (basicMessage.ContentType != ContentTypes.ApiResponse)
            {
                throw new ArgumentException("Expected content type api/response, got {0} instead.".Fmt(basicMessage.ContentType));
            }

            Headers  = basicMessage.Headers;
            BodyText = basicMessage.BodyText;
        }
Ejemplo n.º 4
0
        public CommandReply(BasicMessage basicMessage)
        {
            if (basicMessage.ContentType != ContentTypes.CommandReply)
            {
                throw new ArgumentException("Expected content type command/reply, got {0} instead.".Fmt(basicMessage.ContentType));
            }

            Headers  = basicMessage.Headers;
            BodyText = basicMessage.BodyText;
        }
Ejemplo n.º 5
0
        internal ApiResponse(BasicMessage basicMessage)
        {
            if (basicMessage.ContentType != ContentTypes.ApiResponse)
            {
                throw new ArgumentException("Expected content type api/response, got {0} instead.".Fmt(basicMessage.ContentType));
            }

            Headers = basicMessage.Headers;
            BodyText = basicMessage.BodyText;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates an <see cref="OriginateResult"/> from either a BackgroundJobResult or an EventMessage
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>An <see cref="OriginateResult"/>.</returns>
        /// <exception cref="ArgumentException">If the wrong message type is passed.</exception>
        public static OriginateResult FromBackgroundJobResultOrChannelEvent(BasicMessage message)
        {
            var channelEvent = message as EventMessage;
            if (channelEvent != null)
            {
                return new OriginateResult(channelEvent);
            }

            var backgroundJobResult = message as BackgroundJobResult;
            if (backgroundJobResult != null)
            {
                return new OriginateResult(backgroundJobResult);
            }

            throw new ArgumentException("Message Type {0} is not valid to create an OriginateResult from.".Fmt(message.GetType()));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates an <see cref="OriginateResult"/> from either a BackgroundJobResult or an EventMessage
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>An <see cref="OriginateResult"/>.</returns>
        /// <exception cref="ArgumentException">If the wrong message type is passed.</exception>
        public static OriginateResult FromBackgroundJobResultOrChannelEvent(BasicMessage message)
        {
            var channelEvent = message as EventMessage;

            if (channelEvent != null)
            {
                return(new OriginateResult(channelEvent));
            }

            var backgroundJobResult = message as BackgroundJobResult;

            if (backgroundJobResult != null)
            {
                return(new OriginateResult(backgroundJobResult));
            }

            throw new ArgumentException("Message Type {0} is not valid to create an OriginateResult from.".Fmt(message.GetType()));
        }
Ejemplo n.º 8
0
 internal ChannelEvent(BasicMessage basicMessage) : base(basicMessage)
 {
 }
Ejemplo n.º 9
0
        internal EventMessage(BasicMessage basicMessage)
        {
            if (basicMessage is EventMessage)
            {
                Headers = basicMessage.Headers;
                BodyText = basicMessage.BodyText;
                return;
            }
            else if (basicMessage.ContentType != ContentTypes.EventPlain)
            {
                /* 
                 * Special Case:
                 * When an Outbound Socket sends the "connect" command, FreeSwitch responds with a Command-Reply.
                 * This Command-Reply message contains a CHANNEL_DATA event message buried in its headers.
                 * In this case, we can hydrate an event message from a message of content type Command-Reply.
                 */
                if (basicMessage.Headers.ContainsKey(HeaderNames.EventName))
                {
                    Headers = basicMessage.Headers;
                    BodyText = basicMessage.BodyText;
                    return;
                }

                /* otherwise, we'll throw an exception if we get the wrong content-type message passed in*/
                throw new InvalidOperationException("Expected content type event/plain, got {0} instead.".Fmt(basicMessage.ContentType));
            }

            // normally, the content of the event will be in the BasicMessage's body text and will need to be parsed to produce an EventMessage
            if (string.IsNullOrEmpty(basicMessage.BodyText))
            {
                throw new ArgumentException("Message did not contain an event body.");
            }

            try
            {
                var delimiterIndex = basicMessage.BodyText.IndexOf("\n\n", StringComparison.Ordinal);
                if (delimiterIndex == -1 || delimiterIndex == basicMessage.BodyText.Length - 2)
                {
                    // body text consists of key-value-pair event headers, no body
                    Headers = basicMessage.BodyText.ParseKeyValuePairs(": ");
                    BodyText = null;
                }
                else
                {
                    // ...but some Event Messages also carry a body payload, eg. a BACKGROUND_JOB event
                    // which is a message carried inside an EventMessage carried inside a BasicMessage..
                    Headers = basicMessage.BodyText.Substring(0, delimiterIndex).ParseKeyValuePairs(": ");
                    var contentLength = int.Parse(Headers[HeaderNames.ContentLength]);

                    var body = basicMessage.BodyText.Substring(delimiterIndex + 2, contentLength);

                    //remove any \n\n if any
                    var index = body.IndexOf("\n\n", System.StringComparison.Ordinal);
                    this.BodyText = index > 0 ? body.Substring(0, index) : body;
                }
            }
            catch (Exception ex)
            {
                Log.ErrorException("Failed to parse body of event", ex);
                Log.Error(BodyText);
                throw;
            }
        }