<commit_before>﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Firefly.Owin;
using Firefly.Utils;

[assembly: ServerFactory]
namespace Firefly.Owin
{
    using AppFunc = Func<IDictionary<string, object>, Task>;

    public class ServerFactory : Attribute
    {
        public static void Initialize(IDictionary<string, object> properties)
        {
            if (Addresses(properties) == null)
            {
                properties["server.Addresses"] = new List<IDictionary<string, object>>();
            }
        }

        public static IDisposable Create(AppFunc app, IDictionary<string, object> properties)
        {
            var factory = new Http.ServerFactory();

            var created = new List<IDisposable>();

            var addresses = Addresses(properties);
            if (addresses != null)
            {
                foreach (var address in addresses)
                {
                    var port = Port(address);
                    var hostname = Host(address);
                    if (hostname == null || hostname == "*" || hostname == "+")
                    {
                        created.Add(factory.Create(app, port));
                    }
                    else
                    {
                        created.Add(factory.Create(app, port, hostname));
                    }
                }
            }
            return new Disposable(
                () =>
                {
                    foreach (var disposable in created)
                    {
                        disposable.Dispose();
                    }
                });
        }

        static IList<IDictionary<string, object>> Addresses(IDictionary<string, object> properties)
        {
            object value;
            if (properties.TryGetValue("server.Addresses", out value) && value is IList<IDictionary<string, object>>)
            {
                return (IList<IDictionary<string, object>>)value;
            }
            return null;
        }

        static string Host(IDictionary<string, object> address)
        {
            object value;
            return address.TryGetValue("host", out value) ? Convert.ToString(value) : null;<commit_msg>Fixes for MonkeySpace demo<commit_after>﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Firefly.Owin;
using Firefly.Utils;

[assembly: ServerFactory]
namespace Firefly.Owin
{
    using AppFunc = Func<IDictionary<string, object>, Task>;

    public class ServerFactory : Attribute
    {
        public static void Initialize(IDictionary<string, object> properties)
        {
            if (Addresses(properties) == null)
            {
                properties["host.Addresses"] = new List<IDictionary<string, object>>();
            }
        }

        public static IDisposable Create(AppFunc app, IDictionary<string, object> properties)
        {
            var factory = new Http.ServerFactory();

            var created = new List<IDisposable>();

            var addresses = Addresses(properties);
            if (addresses != null)
            {
                foreach (var address in addresses)
                {
                    var port = Port(address);
                    var hostname = Host(address);
                    if (hostname == null || hostname == "*" || hostname == "+")
                    {
                        created.Add(factory.Create(app, port));

                        Kickstart(new IPEndPoint(IPAddress.Loopback,port));
                    }
                    else
                    {
                        created.Add(factory.Create(app, port, hostname));

                        Kickstart(new DnsEndPoint(hostname, port));
                    }
                }
            }
            return new Disposable(
                () =>
                {
                    foreach (var disposable in created)
                    {
                        disposable.Dispose();
                    }
                });
        }

        private static void Kickstart(EndPoint endPoint)
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            socket.Connect(endPoint);
            socket.Close();
        }

        static IList<IDictionary<string, object>> Addresses(IDictionary<string, object> properties)
        {
            object value;
            if (properties.TryGetValue("host.Addresses", out value) && value is IList<IDictionary<string, object>>)
            {
                return (IList<IDictionary<string, object>>)value;
            }
            return null;
        }

        static string Host(IDictionary<string, object> address)
        {
            object value;
            return address.TryGetValue("host", out value) ? Convert.ToString(value) : null;
{'fraction_non_alphanumeric': 0.09532520325203252, 'fraction_numerical': 0.0, 'mean_word_length': 1.4790931989924434, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, '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': '3384721', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1137, 'n_words': 270}