<commit_before>﻿using System.Collections.Concurrent;
using System.Net;
using System.Threading.Tasks;

using m.Config;
using m.Http;
using m.Http.Extensions;
using m.Logging;

namespace m.Sample
{
    class WebSocketService
    {
        readonly LoggingProvider.ILogger logger = LoggingProvider.GetLogger(typeof(WebSocketService));

        readonly HttpResponse Index = new RedirectResponse("/web/index.html");
        readonly ConcurrentDictionary<long, IWebSocketSession> sessions;

        public WebSocketService()
        {
            sessions = new ConcurrentDictionary<long, IWebSocketSession>();
        }

        public HttpResponse Redirect(IHttpRequest req)
        {
            string userAgent;
            req.Headers.TryGetValue(HttpHeader.UserAgent, out userAgent);
            logger.Debug("Incoming request for index from:[{0}] with useragent:[{1}]", req.RemoteEndPoint, userAgent);
            return Index;
        }

        public WebSocketUpgradeResponse HandleUpgradeRequest(IWebSocketUpgradeRequest upgradeRequest)
        {
            return upgradeRequest.AcceptUpgrade(session => Task.Run(() => HandleWebSocketSession(session)));
        }

        async Task HandleWebSocketSession(IWebSocketSession session)
        {
            sessions[session.Id] = session;

            try
            {
                using (session)
                {
                    BroadcastMessage(string.Format("*** session-{0} connected ({1} online)", session.Id, sessions.Count));

                    while (session.IsOpen)
                    {
                        var message = await session.ReadNextMessageAsync();

                        switch (message.MessageType)
                        {
                            case WebSocketMessage.Type.Text:
                                var text = (WebSocketMessage.Text)message;
                                BroadcastMessage(string.Format("<session-{0}> {1}", session.Id, text.Payload));
                                break;

                            case WebSocketMessage.Type.Close:
                                session.CloseSession();
                                break;

                            case WebSocketMessage.Type.Ping:
                                session.SendPong();
                                break;

                            case WebSocketMessage.Type.Pong:
                                break;
                        }
                    }
                }
            }
            finally
            {
                sessions.TryRemove(session.Id, out session);
                BroadcastMessage(string.Format("*** session-{0} disconnected", session.Id));
            }
        }

        void BroadcastMessage(string message)
        {
            try
            {
                foreach (var s in sessions.Values)
                {
                    s.SendText(message);
                }
            }
            catch
            {
                return;
            }
        }
    }

    class Program
    {
        class ServerConfig : IConfigurable
        {
            [EnvironmentVariable("httpListenPort")]
            public int ListenPort { get; set; }

            public ServerConfig()
            {
                ListenPort = 8080; // Default
            }
        }

        public static void Main(string[] args)
        {
            LoggingProvider.Use(LoggingProvider.NullLoggingProvider);

            var config = ConfigManager.Load<ServerConfig>();

            var wsService = new WebSocketService();

            var server = new HttpBackend(IPAddress.Any, config.ListenPort);
            var routeTable = new RouteTable(
                //Route.Post("/API/*").With((req) => new HttpResponse(HttpStatusCode.Accepted)),
                Route.ServeDirectory("/web/*", "./web/"),
                Route.Get("/").With(wsService.Redirect),
                Route.GetWebSocketUpgrade("/ws").With(wsService.HandleUpgradeRequest),
                Route.Get("/metrics").With(Lift.ToJsonHandler(server.GetMetricsReport)
                                               .FilterResponse(Filters.GZip))
                                     .LimitRate(100)
            );

            server.Start(routeTable);
        }
    }
}
<commit_msg>Put sample app back as it was.<commit_after>﻿using System.Collections.Concurrent;
using System.Net;
using System.Threading.Tasks;

using m.Config;
using m.Http;
using m.Http.Extensions;
using m.Logging;

namespace m.Sample
{
    class WebSocketService
    {
        readonly LoggingProvider.ILogger logger = LoggingProvider.GetLogger(typeof(WebSocketService));

        readonly HttpResponse Index = new RedirectResponse("/web/index.html");
        readonly ConcurrentDictionary<long, IWebSocketSession> sessions;

        public WebSocketService()
        {
            sessions = new ConcurrentDictionary<long, IWebSocketSession>();
        }

        public HttpResponse Redirect(IHttpRequest req)
        {
            string userAgent;
            req.Headers.TryGetValue(HttpHeader.UserAgent, out userAgent);
            logger.Debug("Incoming request for index from:[{0}] with useragent:[{1}]", req.RemoteEndPoint, userAgent);
            return Index;
        }

        public WebSocketUpgradeResponse HandleUpgradeRequest(IWebSocketUpgradeRequest upgradeRequest)
        {
            return upgradeRequest.AcceptUpgrade(session => Task.Run(() => HandleWebSocketSession(session)));
        }

        async Task HandleWebSocketSession(IWebSocketSession session)
        {
            sessions[session.Id] = session;

            try
            {
                using (session)
                {
                    BroadcastMessage(string.Format("*** session-{0} connected ({1} online)", session.Id, sessions.Count));

                    while (session.IsOpen)
                    {
                        var message = await session.ReadNextMessageAsync();

                        switch (message.MessageType)
                        {
                            case WebSocketMessage.Type.Text:
                                var text = (WebSocketMessage.Text)message;
                                BroadcastMessage(string.Format("<session-{0}> {1}", session.Id, text.Payload));
                                break;

                            case WebSocketMessage.Type.Close:
                                session.CloseSession();
                                break;

                            case WebSocketMessage.Type.Ping:
                                session.SendPong();
                                break;

                            case WebSocketMessage.Type.Pong:
                                break;
                        }
                    }
                }
            }
            finally
            {
                sessions.TryRemove(session.Id, out session);
                BroadcastMessage(string.Format("*** session-{0} disconnected", session.Id));
            }
        }

        void BroadcastMessage(string message)
        {
            try
            {
                foreach (var s in sessions.Values)
                {
                    s.SendText(message);
                }
            }
            catch
            {
                return;
            }
        }
    }

    class Program
    {
        class ServerConfig : IConfigurable
        {
            [EnvironmentVariable("httpListenPort")]
            public int ListenPort { get; set; }

            public ServerConfig()
            {
                ListenPort = 8080; // Default
            }
        }

        public static void Main(string[] args)
        {
            LoggingProvider.Use(LoggingProvider.ConsoleLoggingProvider);

            var config = ConfigManager.Load<ServerConfig>();

            var wsService = new WebSocketService();

            var server = new HttpBackend(IPAddress.Any, config.ListenPort);
            var routeTable = new RouteTable(
                Route.ServeDirectory("/web/*", "./web/"),
                Route.Get("/").With(wsService.Redirect),
                Route.GetWebSocketUpgrade("/ws").With(wsService.HandleUpgradeRequest),
                Route.Get("/metrics").With(Lift.ToJsonHandler(server.GetMetricsReport)
                                               .FilterResponse(Filters.GZip))
                                     .LimitRate(100)
            );

            server.Start(routeTable);
        }
    }
}

{'fraction_non_alphanumeric': 0.08910309157164688, 'fraction_numerical': 0.0032914070765252147, 'mean_word_length': 1.3457402812241521, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1037702', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1900, 'n_words': 361}