<commit_before>﻿using System;
using System.Linq;
using System.Threading;
using Messages;
using NServiceBus;

namespace Sender
{
    internal class Program
    {
        private static IStartableBus _bus;
        private static AutoResetEvent _waitHandle;

        private static void Main(string[] args)
        {
            using (_bus = BusFactory.GetBus())
            using (_waitHandle = new AutoResetEvent(false))
            {
                var subscriberThread = new Thread(_ => Subscribe(_bus, _waitHandle))
                {
                    IsBackground = false,
                    Name = "Subscriber",
                };
                subscriberThread.Start(_waitHandle);

                var publisherThread = new Thread(_ => Publish(_bus))
                {
                    IsBackground = false,
                    Name = "Publisher"
                };
                publisherThread.Start();
                publisherThread.Join();

                _waitHandle.Set();
                subscriberThread.Join();
            }
        }

        private static void Subscribe(IStartableBus bus, WaitHandle waitHandle)
        {
            try
            {
                bus.Start();

                //Console.WriteLine("Waiting for messages.");
                waitHandle.WaitOne();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unhandled ex: {0}", ex);
            }
        }

        private static void Publish(ISendOnlyBus bus)
        { 
            while (Menu(_bus))
            {
            }
        }

        public static bool Menu(ISendOnlyBus bus)
        {
            int[] customers = Db.AllCustomers();

            using (Colr.Green())
            {
                Console.WriteLine(Environment.NewLine + "Menu:");
                Console.WriteLine("(n)ew customer");
                foreach (var customer in customers)
                {
                    Console.WriteLine("({0}) Customer {1}", customer, customer);
                }
                Console.WriteLine("(q) to quit");
                Console.Write(" > ");
            }

            char input = Console.ReadKey().KeyChar;
            switch (input)
            {
                case 'n':
                    int customerId = Db.NewCustomer();
                    while (CustomerMenu(bus, customerId))
                    {
                    }
                    return true;
                case 'q':
                    return false;
                default:
                    int i;
                    if (int.TryParse(input.ToString(), out i))
                        if (customers.Contains(i))
                        {
                            while (CustomerMenu(bus, i))
                            {
                            }
                            return true;
                        }

                    using (Colr.Red())
                        Console.WriteLine("Unknown input '{0}', please try again" + Environment.NewLine, input);

                    return true;
            }
        }

        public static bool CustomerMenu(ISendOnlyBus bus, int customerId)
        {
            Guid[] requests = Db.AllRequests(customerId);

            Console.WriteLine(Environment.NewLine);
            using (Colr.Yellow())
            {
                Console.WriteLine(Environment.NewLine + "Customer {0} Menu:", customerId);
                Console.WriteLine("(n)ew request");
                Console.WriteLine("(e)xtend all acceptance timeouts");
                Console.WriteLine("(r)educe all acceptance timeouts");
                for (var i = 0; i < requests.Length; i++)
                {
                    Console.WriteLine("({0}) Request {1}", i, requests[i]);
                }
                Console.WriteLine("(b)ack to previous menu");
                Console.Write(" > ");
            }

            char input = Console.ReadKey().KeyChar;
            switch (input)
            {
                case 'n':
                    
                    Console.WriteLine(Environment.NewLine);
                    int acceptIn = GetNumericValue("Number of seconds before auto accept or (0) to cancel: ");

                    if (acceptIn > 0)
                    {
                        while (RequestMenu(bus, customerId, CreateNewRequest(bus, customerId, acceptIn)))
                        {
                        }
                    }
                    else
                    {
                        using (Colr.Red())
                            Console.WriteLine("Canceled"); 
                    }
                    return true;
                case 'e':
                    Console.WriteLine(Environment.NewLine);
                    int extendBy = GetNumericValue("Number of seconds to extend by or (0) to cancel: ");

                    if (extendBy > 0)
                    {
                        bus.Send(new ExtendAllAcceptanceTimeouts {CustomerId = customerId, ExtendBySeconds = extendBy});
                    }
                    else<commit_msg>fixed a bug where extra requests were being created every time you selected an action from the request menu<commit_after>﻿using System;
using System.Linq;
using System.Threading;
using Messages;
using NServiceBus;

namespace Sender
{
    internal class Program
    {
        private static IStartableBus _bus;
        private static AutoResetEvent _waitHandle;

        private static void Main(string[] args)
        {
            using (_bus = BusFactory.GetBus())
            using (_waitHandle = new AutoResetEvent(false))
            {
                var subscriberThread = new Thread(_ => Subscribe(_bus, _waitHandle))
                {
                    IsBackground = false,
                    Name = "Subscriber",
                };
                subscriberThread.Start(_waitHandle);

                var publisherThread = new Thread(_ => Publish(_bus))
                {
                    IsBackground = false,
                    Name = "Publisher"
                };
                publisherThread.Start();
                publisherThread.Join();

                _waitHandle.Set();
                subscriberThread.Join();
            }
        }

        private static void Subscribe(IStartableBus bus, WaitHandle waitHandle)
        {
            try
            {
                bus.Start();

                //Console.WriteLine("Waiting for messages.");
                waitHandle.WaitOne();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unhandled ex: {0}", ex);
            }
        }

        private static void Publish(ISendOnlyBus bus)
        { 
            while (Menu(_bus))
            {
            }
        }

        public static bool Menu(ISendOnlyBus bus)
        {
            int[] customers = Db.AllCustomers();

            using (Colr.Green())
            {
                Console.WriteLine(Environment.NewLine + "Menu:");
                Console.WriteLine("(n)ew customer");
                foreach (var customer in customers)
                {
                    Console.WriteLine("({0}) Customer {1}", customer, customer);
                }
                Console.WriteLine("(q) to quit");
                Console.Write(" > ");
            }

            char input = Console.ReadKey().KeyChar;
            switch (input)
            {
                case 'n':
                    int customerId = Db.NewCustomer();
                    while (CustomerMenu(bus, customerId))
                    {
                    }
                    return true;
                case 'q':
                    return false;
                default:
                    int i;
                    if (int.TryParse(input.ToString(), out i))
                        if (customers.Contains(i))
                        {
                            while (CustomerMenu(bus, i))
                            {
                            }
                            return true;
                        }

                    using (Colr.Red())
                        Console.WriteLine("Unknown input '{0}', please try again" + Environment.NewLine, input);

                    return true;
            }
        }

        public static bool CustomerMenu(ISendOnlyBus bus, int customerId)
        {
            Guid[] requests = Db.AllRequests(customerId);

            Console.WriteLine(Environment.NewLine);
            using (Colr.Yellow())
            {
                Console.WriteLine(Environment.NewLine + "Customer {0} Menu:", customerId);
                Console.WriteLine("(n)ew request");
                Console.WriteLine("(e)xtend all acceptance timeouts");
                Console.WriteLine("(r)educe all acceptance timeouts");
                for (var i = 0; i < requests.Length; i++)
                {
                    Console.WriteLine("({0}) Request {1}", i, requests[i]);
                }
                Console.WriteLine("(b)ack to previous menu");
                Console.Write(" > ");
            }

            char input = Console.ReadKey().KeyChar;
            switch (input)
            {
                case 'n':
                    
                    Console.WriteLine(Environment.NewLine);
                    int acceptIn = GetNumericValue("Number of seconds before auto accept or (0) to cancel: ");

                    if (acceptIn > 0)
                    {
                        Guid requestId = CreateNewRequest(bus, customerId, acceptIn);
                        while (RequestMenu(bus, customerId, requestId))
                        {
                        }
                    }
                    else
                    {
                        using (Colr.Red())
                            Console.WriteLine("Canceled"); 
                    }
                    return true;
                case 'e':
                    Console.WriteLine(Environment.NewLine);
                    int extendBy = GetNumericValue("Number of seconds to extend by or (0) to cancel: ");

                    if (extendBy > 0)
                    {
                        bus.Send(new ExtendAllAcceptanceTimeouts {CustomerId = customerId, ExtendBySeconds = extendBy});
                    }
                    else
{'fraction_non_alphanumeric': 0.09045226130653267, 'fraction_numerical': 0.0023192887514495554, 'mean_word_length': 1.0727017824954936, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '3475649', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2221, 'n_words': 548}