<commit_before>﻿using System;
using DotNetty.Transport.Channels;
using ModFreeSwitch.Common;
using ModFreeSwitch.Messages;
using NLog;

namespace ModFreeSwitch.Handlers.outbound {
    /// <summary>
    ///     Event Socket Client handler
    /// </summary>
    public abstract class EventSocketClientHandler : ChannelHandlerAdapter {
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();

        /// <summary>
        ///     Read data received from the freeSwitch mod_event_socket
        /// </summary>
        /// <param name="context">the channel context</param>
        /// <param name="message">the data received from socket</param>
        public override void ChannelRead(IChannelHandlerContext context, object message) {
            // We know that on good note the message object will be the EventSocketMessage after decoding.
            EventSocketMessage msg = message as EventSocketMessage;

            if (logger.IsDebugEnabled) logger.Debug("Message received [{0}]", msg);

            string contentType = msg?.ContentType();
            if (string.IsNullOrEmpty(contentType)) return;
            if (contentType.Equals(EventSocketHeadersValues.TextEventPlain)) {
                EventReceived?.Invoke(this, new EventSocketArgs(context, msg));
                return;
            }

            if (contentType.Equals(EventSocketHeadersValues.ApiResponse)) {
                ApiResponse?.Invoke(this, new EventSocketArgs(context, msg));
                return;
            }

            if (contentType.Equals(EventSocketHeadersValues.CommandReply)) {
                CommandReply?.Invoke(this, new EventSocketArgs(context, msg));
                return;
            }

            if (contentType.Equals(EventSocketHeadersValues.AuthRequest)) {
                AuthRequest?.Invoke(this, new EventSocketArgs(context, msg));
                return;
            }

            if (contentType.Equals(EventSocketHeadersValues.TextDisconnectNotice)) {
                DisconnectNotice?.Invoke(this, new EventSocketArgs(context, msg));
                return;
            }

            if (!contentType.Equals(EventSocketHeadersValues.TextRudeRejection)) return;
            RudeRejection?.Invoke(this, new EventSocketArgs(context, msg));
        }

        /// <summary>
        ///     Executed when the <see cref="ChannelRead" /> has finished its work
        /// </summary>
        /// <param name="context">the channel context</param>
        public override void ChannelReadComplete(IChannelHandlerContext context) { context.Flush(); }

        /// <summary>
        ///     This event is fired when an auth/request message is receiced. It  is very useful to complete connection to
        ///     freeSwitch
        ///     mod_event_socket
        /// </summary>
        protected event EventHandler<EventSocketArgs> AuthRequest = delegate { };

        /// <summary>
        ///     This event is fired whenever an event is received from freeSwitch mod_event_socket
        /// </summary>
        protected event EventHandler<EventSocketArgs> EventReceived = delegate { };

        /// <summary>
        ///     This event is fired when a disconnect notice is received.
        /// </summary>
        protected event EventHandler<EventSocketArgs> DisconnectNotice = delegate { };

        /// <summary>
        ///     Event fired when a rude rejection is received.
        /// </summary>
        protected event EventHandler<EventSocketArgs> RudeRejection = delegate { };

        /// <summary>
        ///     Fired whenever a command/reply is received
        /// </summary>
        protected event EventHandler<EventSocketArgs> CommandReply = delegate { };

        /// <summary>
        ///     Fired whenever a api/response is received
        /// </summary>
        protected event EventHandler<EventSocketArgs> ApiResponse = delegate { };
    }
}<commit_msg>Latest updates<commit_after>﻿using System;
using System.Threading.Tasks;
using DotNetty.Transport.Channels;
using ModFreeSwitch.Common;
using ModFreeSwitch.Messages;
using NLog;

namespace ModFreeSwitch.Handlers.outbound {
    /// <summary>
    ///     Event Socket Client handler
    /// </summary>
    public abstract class EventSocketClientHandler : ChannelHandlerAdapter {
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();

        /// <summary>
        ///     Read data received from the freeSwitch mod_event_socket
        /// </summary>
        /// <param name="context">the channel context</param>
        /// <param name="message">the data received from socket</param>
        public override void ChannelRead(IChannelHandlerContext context, object message) {
            // We know that on good note the message object will be the EventSocketMessage after decoding.
            EventSocketMessage msg = message as EventSocketMessage;

            if (logger.IsDebugEnabled) logger.Debug("Message received [{0}]", msg);

            string contentType = msg?.ContentType();
            if (string.IsNullOrEmpty(contentType)) return;
            if (contentType.Equals(EventSocketHeadersValues.TextEventPlain)) {
                EventReceived?.Invoke(this, new EventSocketArgs(context, msg));
                return;
            }

            if (contentType.Equals(EventSocketHeadersValues.ApiResponse)) {
                ApiResponse?.Invoke(this, new EventSocketArgs(context, msg));
                return;
            }

            if (contentType.Equals(EventSocketHeadersValues.CommandReply)) {
                CommandReply?.Invoke(this, new EventSocketArgs(context, msg));
                return;
            }

            if (contentType.Equals(EventSocketHeadersValues.AuthRequest)) {
                AuthRequest?.Invoke(this, new EventSocketArgs(context, msg));
                return;
            }

            if (contentType.Equals(EventSocketHeadersValues.TextDisconnectNotice)) {
                DisconnectNotice?.Invoke(this, new EventSocketArgs(context, msg));
                return;
            }

            if (!contentType.Equals(EventSocketHeadersValues.TextRudeRejection)) return;
            RudeRejection?.Invoke(this, new EventSocketArgs(context, msg));
        }

        /// <summary>
        ///     Executed when the <see cref="ChannelRead" /> has finished its work
        /// </summary>
        /// <param name="context">the channel context</param>
        public override void ChannelReadComplete(IChannelHandlerContext context) { context.Flush(); }

        /// <summary>
        ///     This event is fired when an auth/request message is receiced. It  is very useful to complete connection to
        ///     freeSwitch
        ///     mod_event_socket
        /// </summary>
        protected event EventHandler<EventSocketArgs> AuthRequest = delegate { };

        /// <summary>
        ///     This event is fired whenever an event is received from freeSwitch mod_event_socket
        /// </summary>
        protected event EventHandler<EventSocketArgs> EventReceived = delegate { };

        /// <summary>
        ///     This event is fired when a disconnect notice is received.
        /// </summary>
        protected event EventHandler<EventSocketArgs> DisconnectNotice = delegate { };

        /// <summary>
        ///     Event fired when a rude rejection is received.
        /// </summary>
        protected event EventHandler<EventSocketArgs> RudeRejection = delegate { };

        /// <summary>
        ///     Fired whenever a command/reply is received
        /// </summary>
        protected event EventHandler<EventSocketArgs> CommandReply = delegate { };

        /// <summary>
        ///     Fired whenever a api/response is received
        /// </summary>
        protected event EventHandler<EventSocketArgs> ApiResponse = delegate { };

        /// <summary>
        ///     Very useful to send
        /// </summary>
        /// <param name="context">the channel context</param>
        /// <param name="command">the command to send.</param>
        public async Task SendCommand(IChannelHandlerContext context, string command) { await context.WriteAndFlushAsync(command).ConfigureAwait(false); }
    }
}
{'fraction_non_alphanumeric': 0.100789313904068, 'fraction_numerical': 0.00024286581663630845, 'mean_word_length': 2.1984466019417477, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3399939', 'n_tokens_mistral': 1966, 'n_tokens_neox': 1904, 'n_words': 535}