<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;   

namespace Pequot
{
    class PequotServer
    {
        //off, error, warning, info, verbose
        public static TraceSwitch verbosity = new TraceSwitch("Verbosity", "Verbosity of program");
        //default port = 80
        public static int Port = 80;
        public static DirectoryInfo Dir;
        private DirectoryInfo serverDirectory;
        public static string phpLocation;
        public static string[] phpFiles;
        public static string ipToListenAt = "";

        public const string kDefaultSettings = "PequotConfig.xml";

        static void Main(string[] args)
        {
            SetupTrace();
            verbosity.Level = TraceLevel.Info;   //default is Info (changed on load from config file)
            //todo: eventually put in log file command-line switch detection code

            PequotServer server = new PequotServer();
            Trace.WriteLine("Pequot Server " + server.GetType().Assembly.GetName().Version.ToString(3));
            server.LoadProperties();

            TcpListener listener = null;
            bool listening = true;
            //is this the best way to get the address?
            //TODO:fix this
            IPAddress host = IPAddress.None;
            if (ipToListenAt.Length <= 0 || !IPAddress.TryParse(ipToListenAt, out host))
            {
                foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName()))
                {
                    host = ip;
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                        break;
                }
            }
            listener = new TcpListener(host, Port);
            listener.Start();

            if (listening)
                Trace.WriteLine("Server listening at IP " + host.ToString() + " on port " + Port);

            while (listening)
            {
                //spawn new thread when we get a connection
                new ServerThread(listener.AcceptTcpClient()).Run();
            }
            listener.Stop();
        }

        private static void SetupTrace()
        {
            Trace.AutoFlush = true;
            TextWriterTraceListener consoleListener = new TextWriterTraceListener(Console.Out);
            Trace.Listeners.Add(consoleListener);
            //TODO: implement log file:
            //TextWriterTraceListener fileListener = new TextWriterTraceListener(fileName);
            //Trace.Listeners.Add(fileListener);
        }

        private void LoadProperties()
        {
            //defaults
            serverDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
            phpLocation = "";//@"C:\Program Files\PHP\php-cgi.exe";   //empty string = no php
            //default location from install = "C:\Program Files\PHP\php-cgi.exe"

            AppSettings.Load(kDefaultSettings);
            Dir = new DirectoryInfo(AppSettings.Get("Directory", Path.Combine(serverDirectory.FullName, "files")));
            Port = int.Parse(AppSettings.Get("Port", "80"));
            phpLocation = AppSettings.Get("PHP Location", "");
            phpFiles = AppSettings.Get("PHP File Extensions", ".php").Split(',');
            ipToListenAt = AppSettings.Get("IP To Listen At", "");
            try
            {
                verbosity.Level = (TraceLevel)TraceLevel.Parse(typeof(TraceLevel), (AppSettings.Get(verbosity.DisplayName, verbosity.Level.ToString())));
            }
            catch (Exception ex)
            {
                // Yes, it's amusing that we're writing an error about verbosity that hinges on verbosity.
                Trace.WriteLineIf(verbosity.TraceError, "Error parsing verbosity level: " + ex.Message +
                                  Environment.NewLine + "Defaulted to " + verbosity.Level.ToString() + ".");
            }
            SaveProperties();

            if (!Dir.Exists)
                Dir.Create();

        }

        private void SaveProperties()
        {
            AppSettings.Set("Directory", Dir.FullName);
            AppSettings.Set("Port", Port.ToString());
            AppSettings.Set("PHP Location", phpLocation);
            AppSettings.Set("PHP File Extensions", String.Join(",", phpFiles));
            if(ipToListenAt.Length>0)
                AppSettings.Set("IP To Listen At", ipToListenAt);<commit_msg>Allow binding to all addresses & default to IpAddress.Any<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;   

namespace Pequot
{
    class PequotServer
    {
        //off, error, warning, info, verbose
        public static TraceSwitch verbosity = new TraceSwitch("Verbosity", "Verbosity of program");
        //default port = 80
        public static int Port = 80;
        public static DirectoryInfo Dir;
        private DirectoryInfo serverDirectory;
        public static string phpLocation;
        public static string[] phpFiles;
        public static string ipToListenAt = "";

        public const string kDefaultSettings = "PequotConfig.xml";

        static void Main(string[] args)
        {
            SetupTrace();
            verbosity.Level = TraceLevel.Info;   //default is Info (changed on load from config file)
            //todo: eventually put in log file command-line switch detection code

            PequotServer server = new PequotServer();
            Trace.WriteLine("Pequot Server " + server.GetType().Assembly.GetName().Version.ToString(3));
            server.LoadProperties();

            TcpListener listener = null;
            bool listening = true;

            IPAddress host = IPAddress.Any;
            // try parsing the address and silently fail to Any if it is invalid
            IPAddress.TryParse(ipToListenAt, out host);

            listener = new TcpListener(host, Port);
            try
            {
                listener.Start();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Could not start server.  Error: " + ex.Message);
                listening = false;
            }

            if (listening)
                Trace.WriteLine("Server listening at IP " + host.ToString() + " on port " + Port);

            while (listening)
            {
                //spawn new thread when we get a connection
                new ServerThread(listener.AcceptTcpClient()).Run();
            }
            listener.Stop();
        }

        private static void SetupTrace()
        {
            Trace.AutoFlush = true;
            TextWriterTraceListener consoleListener = new TextWriterTraceListener(Console.Out);
            Trace.Listeners.Add(consoleListener);
            //TODO: implement log file:
            //TextWriterTraceListener fileListener = new TextWriterTraceListener(fileName);
            //Trace.Listeners.Add(fileListener);
        }

        private void LoadProperties()
        {
            //defaults
            serverDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
            phpLocation = "";//@"C:\Program Files\PHP\php-cgi.exe";   //empty string = no php
            //default location from install = "C:\Program Files\PHP\php-cgi.exe"

            AppSettings.Load(kDefaultSettings);
            Dir = new DirectoryInfo(AppSettings.Get("Directory", Path.Combine(serverDirectory.FullName, "files")));
            Port = int.Parse(AppSettings.Get("Port", "80"));
            phpLocation = AppSettings.Get("PHP Location", "");
            phpFiles = AppSettings.Get("PHP File Extensions", ".php").Split(',');
            ipToListenAt = AppSettings.Get("IP To Listen At", IPAddress.Any.ToString()); // default is Any (0.0.0.0)
            try
            {
                verbosity.Level = (TraceLevel)TraceLevel.Parse(typeof(TraceLevel), (AppSettings.Get(verbosity.DisplayName, verbosity.Level.ToString())));
            }
            catch (Exception ex)
            {
                // Yes, it's amusing that we're writing an error about verbosity that hinges on verbosity.
                Trace.WriteLineIf(verbosity.TraceError, "Error parsing verbosity level: " + ex.Message +
                                  Environment.NewLine + "Defaulted to " + verbosity.Level.ToString() + ".");
            }
            SaveProperties();

            if (!Dir.Exists)
                Dir.Create();

        }

        private void SaveProperties()
        {
            AppSettings.Set("Directory", Dir.FullName);
            AppSettings.Set("Port", Port.ToString());
            AppSettings.Set("PHP Location", phpLocation);
            AppSettings.Set("PHP File Extensions", String.Join(",", phpFiles));
            AppSettings.Set("IP To Listen At", ipToListenAt);
{'fraction_non_alphanumeric': 0.10342128801431127, 'fraction_numerical': 0.0022361359570661895, 'mean_word_length': 1.9472817133443163, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1642050', 'n_tokens_mistral': 2258, 'n_tokens_neox': 2165, 'n_words': 578}