<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading;

namespace WebServer
{
    public class BroadcastedMessageEventArgs : EventArgs
    {
        public String Message { get; private set; }

        public BroadcastedMessageEventArgs(String message)
        {
            Message = message;
        }
    }

    public class LoggedMessageEventArgs : BroadcastedMessageEventArgs
    {
        public EventLogEntryType Type { get; private set; }

        public LoggedMessageEventArgs(EventLogEntryType type, String message)
            : base(message)
        {
            Type = type;
        }
    }

    public delegate void BroadcastedMessageHandler(Server server, BroadcastedMessageEventArgs e);
    public delegate void LoggedMessageHandler(Server server, LoggedMessageEventArgs e);

    public class Server
    {
        private readonly Dictionary<String, Type> _boundServlets;
        private readonly Dictionary<String, ScheduledJob> _scheduledJobs;

        private readonly HttpListener _listener;
        private readonly ManualResetEvent _stop;
        private bool _stopped;

        private Type _notFoundServlet;
        private Type _resourceServlet;
        
        public String ResourceRootUrl { get; set; }

        public event BroadcastedMessageHandler BroadcastedMessage;
        public event LoggedMessageHandler LoggedMessage;
        
        public bool IsListening
        {
            get { return _listener.IsListening; }
        }

        public Server()
        {
            _boundServlets = new Dictionary<String, Type>();
            _scheduledJobs = new Dictionary<String, ScheduledJob>();

            _listener = new HttpListener();

            _stop = new ManualResetEvent(false);
            _stopped = false;

            ResourceRootUrl = "/res";

            _notFoundServlet = typeof(Default404Servlet);
            _resourceServlet = typeof(DefaultResourceServlet);

            BindServletToURL<DefaultResourceServlet>("/favicon.ico");
        }

        public void Broadcast(String format, params object[] args)
        {
            if (BroadcastedMessage != null) {
                BroadcastedMessage(this, new BroadcastedMessageEventArgs(String.Format(format, args)));
            }
        }

        public void Log(EventLogEntryType type, String format, params object[] args)
        {
            if (LoggedMessage != null) {
                LoggedMessage(this, new LoggedMessageEventArgs(type, String.Format(format, args)));
            }
        }

        public virtual void Log(Exception e)
        {
            Log(EventLogEntryType.Error, "{0}: {1}", e.Message, e.StackTrace);
        }

        public virtual void Log(String format, params Object[] args)
        {
            Log(EventLogEntryType.Information, format, args);
        }

        public void Stop()
        {
            _stopped = true;
            _stop.Set();
            foreach (var job in _scheduledJobs) {
                job.Value.Cancel();
            }
        }

        public void AddPrefix(String uriPrefix)
        {
            _listener.Prefixes.Add(uriPrefix);
        }<commit_msg>Fixed a possible exception when logging / broadcasting messages with no format args.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading;

namespace WebServer
{
    public class BroadcastedMessageEventArgs : EventArgs
    {
        public String Message { get; private set; }

        public BroadcastedMessageEventArgs(String message)
        {
            Message = message;
        }
    }

    public class LoggedMessageEventArgs : BroadcastedMessageEventArgs
    {
        public EventLogEntryType Type { get; private set; }

        public LoggedMessageEventArgs(EventLogEntryType type, String message)
            : base(message)
        {
            Type = type;
        }
    }

    public delegate void BroadcastedMessageHandler(Server server, BroadcastedMessageEventArgs e);
    public delegate void LoggedMessageHandler(Server server, LoggedMessageEventArgs e);

    public class Server
    {
        private readonly Dictionary<String, Type> _boundServlets;
        private readonly Dictionary<String, ScheduledJob> _scheduledJobs;

        private readonly HttpListener _listener;
        private readonly ManualResetEvent _stop;
        private bool _stopped;

        private Type _notFoundServlet;
        private Type _resourceServlet;
        
        public String ResourceRootUrl { get; set; }

        public event BroadcastedMessageHandler BroadcastedMessage;
        public event LoggedMessageHandler LoggedMessage;
        
        public bool IsListening
        {
            get { return _listener.IsListening; }
        }

        public Server()
        {
            _boundServlets = new Dictionary<String, Type>();
            _scheduledJobs = new Dictionary<String, ScheduledJob>();

            _listener = new HttpListener();

            _stop = new ManualResetEvent(false);
            _stopped = false;

            ResourceRootUrl = "/res";

            _notFoundServlet = typeof(Default404Servlet);
            _resourceServlet = typeof(DefaultResourceServlet);

            BindServletToURL<DefaultResourceServlet>("/favicon.ico");
        }

        public void Broadcast(String format, params object[] args)
        {
            var message = args.Length == 0 ? format : String.Format(format, args);

            if (BroadcastedMessage != null) {
                BroadcastedMessage(this, new BroadcastedMessageEventArgs(message));
            }
        }

        public void Log(EventLogEntryType type, String format, params object[] args)
        {
            var message = args.Length == 0 ? format : String.Format(format, args);

            if (LoggedMessage != null) {
                LoggedMessage(this, new LoggedMessageEventArgs(type, message));
            }
        }

        public virtual void Log(Exception e)
        {
            Log(EventLogEntryType.Error, "{0}: {1}", e.Message, e.StackTrace);
        }

        public virtual void Log(String format, params Object[] args)
        {
            Log(EventLogEntryType.Information, format, args);
        }

        public void Stop()
        {
            _stopped = true;
            _stop.Set();
            foreach (var job in _scheduledJobs) {
                job.Value.Cancel();
            }
        }

        public void AddPrefix(String uriPrefix)
        {
            _listener.Prefixes.Add(uriPrefix);
        }
{'fraction_non_alphanumeric': 0.07439369141496802, 'fraction_numerical': 0.0017854485939592323, 'mean_word_length': 2.1543876114500233, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '1235925', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1609, 'n_words': 435}