Beispiel #1
0
 private static void PlaceOrders(Waiter waiter)
 {
     for (var i = 0; i < 200; i++)
     {
         waiter.PlaceOrder(
             i,
             new List <string>
         {
             "pizza",
             "pizza",
             "pizza",
             "pizza",
             "pasta",
             "pasta",
             "pasta",
             "pasta",
             "wine",
             "wine",
             "wine",
             "wine",
             "wine",
             "wine"
         },
             i % 2 == 0);
     }
 }
        static void Main(string[] args)
        {
            var bus          = new TopicBasedPubSub();
            var orderPrinter = new OrderPrinter();

            var cashier         = new Cashier(bus);
            var assitantManager = new QueuedHandle <PriceOrder>("AssistantManager", new AssistantManager(bus));
            var db    = new ConcurrentDictionary <Guid, OrderDocument>();
            var jesse = new QueuedHandle <CookFood>("Cook:Jesse", new Cook("Jesse", 600, db, bus));
            var walt  = new QueuedHandle <CookFood>("Cook:Walt", new Cook("Walt", 700, db, bus));
            var gus   = new QueuedHandle <CookFood>("Cook:Gus", new Cook("Gus", 1500, db, bus));

            var dispatcher = new MoreFairDispatcher <CookFood>("FairDispatcher", new[] { jesse, walt, gus });

            var chaosMonkey = new ChaosMonkey <CookFood>(25, 25, dispatcher);
            var alarmClock  = new AlarmClock(bus);

            var startables = new List <IStartable> {
                assitantManager, jesse, walt, gus, dispatcher, alarmClock
            };
            var waiter = new Waiter("Heisenberg", bus);

            var house = new MinionHouse(bus);

            bus.Subscribe(chaosMonkey);
            bus.Subscribe(assitantManager);
            bus.Subscribe(cashier);
            bus.Subscribe <OrderPlaced>(house);
            bus.Subscribe <OrderCompleted>(house);
            bus.Subscribe(orderPrinter);
            bus.Subscribe(alarmClock);

            startables.ForEach(x => x.Start());

            for (var i = 0; i < 50; i++)
            {
                waiter.PlaceOrder(new LineItem("Crystal Meth", 3));
                Console.WriteLine("Order placed");
            }

            Console.WriteLine("Getting outstanding orders");

            Task.Run(() => GetOutstandingOrdersAndPay(cashier));

            Task.Run(() => StartMonitoringQueueDepthsAsync(startables));
            StartMonitoringMinionHouseAsync(house).Wait();
        }
        private static void Main(string[] args)
        {
            IList <IStartable> startables = new List <IStartable>();

            var bus              = new Bus();
            var cashier          = new ThreadedHandler <TakePayment>("Cashier", new Cashier(bus));
            var assistantManager = new ThreadedHandler <PriceOrder>("Assistant Manager",
                                                                    new AssistantManager(new MenuService(), bus));
            var cook1 = new ThreadedHandler <CookFood>("Cook (Chewie)",
                                                       new TtlHandler <CookFood>(new Cook("Chewie", 123, bus)));
            var cook2 = new ThreadedHandler <CookFood>("Cook (Luke)",
                                                       new TtlHandler <CookFood>(new Cook("Luke", 456, bus)));
            var cook3 = new ThreadedHandler <CookFood>("Cook (Darth)",
                                                       new TtlHandler <CookFood>(new Cook("Darth", 217, bus)));
            var mfdDispatcher = new MfdDispatcher <CookFood>(new[] { cook1, cook2, cook3 });
            var kitchen       = new ThreadedHandler <CookFood>("Kitchen", mfdDispatcher);
            var waiter        = new Waiter(new MenuService(), bus);

            bus.Subscribe(cashier);
            bus.Subscribe(assistantManager);
            bus.Subscribe(kitchen);

            var house = new ProcessManagerHouse(bus);

            bus.Subscribe <OrderPlaced>(house);
            bus.Subscribe <OrderPaid>(house);

            var alarmClock = new AlarmClock(bus);

            bus.Subscribe(alarmClock);

            startables.Add(cashier);
            startables.Add(assistantManager);
            startables.Add(cook1);
            startables.Add(cook2);
            startables.Add(cook3);
            startables.Add(kitchen);


            startables.Add(alarmClock);

            foreach (var startable in startables)
            {
                startable.StartListening();
            }

            Task.Factory.StartNew(() =>
            {
                while (!_stop)
                {
                    foreach (var startable in startables)
                    {
                        Console.WriteLine($"Queue: {startable.Name} Count: {startable.Count}");
                    }

                    Thread.Sleep(1000);
                }
            },
                                  TaskCreationOptions.LongRunning);

            for (var i = 0; i < 100; i++)
            {
                var isDodgy = false; //i%2 == 0;
                waiter.PlaceOrder(i, isDodgy, "Sausages", "Beans");
            }

            Console.ReadKey();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            var es = new AtomDispatcher();

            es.Start("az");
            // es.PostMessage(new OrderPlaced(order), "az");

            QueuedHandler          printOrderHandler = new QueuedHandler(new PrintOrderHandler());
            IEventHandler <IEvent> cashier           = new Cashier();
            QueuedHandler          cashierProxy      = new QueuedHandler(cashier);
            var assistingManager = new QueuedHandler(new AssistingManager("Diana"));
            var cook1            = new QueuedHandler(new TimeToLiveHandler(new Cook("John")));
            var cook2            = new QueuedHandler(new TimeToLiveHandler(new Cook("Peter")));
            var cook3            = new QueuedHandler(new TimeToLiveHandler(new Cook("Gregory")));

            var betterHandler = new QueuedHandler(new Dropper(new TimeToLiveHandler(new BetterDispatcher(new[] { cook1, cook2, cook3 }))));

            TelephoneOperator telephoneOperator = new TelephoneOperator("PhoneMan");
            Waiter            waiter            = new Waiter("Georgie");
            var house      = new MidgetHouse(Dispatcher.Instance);
            var houseQueue = new QueuedHandler(house);

            var clock = new AlarmClock();

            Dispatcher.Instance.Subscribe(typeof(OrderPlaced), houseQueue);
            Dispatcher.Instance.Subscribe(typeof(FoodCooked), houseQueue);
            Dispatcher.Instance.Subscribe(typeof(OrderPriced), houseQueue);
            Dispatcher.Instance.Subscribe(typeof(PaymentTaken), houseQueue);
            Dispatcher.Instance.Subscribe(typeof(FirstCookRetry), houseQueue);
            Dispatcher.Instance.Subscribe(typeof(SecondCookRetry), houseQueue);

            Dispatcher.Instance.Subscribe(typeof(CookFood), betterHandler);
            Dispatcher.Instance.Subscribe(typeof(PriceOrder), assistingManager);
            Dispatcher.Instance.Subscribe(typeof(TakePayment), cashierProxy);
            Dispatcher.Instance.Subscribe(typeof(PrintOrder), printOrderHandler);

            Dispatcher.Instance.Subscribe(typeof(SendToMeIn), clock);

            IEnumerable <QueuedHandler> allHandlers = new List <QueuedHandler> {
                houseQueue, betterHandler, cashierProxy, assistingManager, cook1, cook2, cook3, printOrderHandler
            };
            Monitor monitor = new Monitor(allHandlers, new [] { house });

            monitor.Start();

            foreach (IStartable handler in allHandlers)
            {
                handler.Start();
            }
            clock.Start();
            //waiter.PlaceOrder(new List<int> { 1, 2 });
            //telephoneOperator.PlaceOrder(new List<int> { 1, 2 });

            return;

            for (int i = 0; i < 1000; i++)
            {
                waiter.PlaceOrder(new List <int> {
                    1, 2
                });
                if (i % 3 == 0)
                {
                    Thread.Sleep(50);
                    telephoneOperator.PlaceOrder(new List <int> {
                        1, 2
                    });
                }
                Thread.Sleep(50);
            }
            while (true)
            {
                foreach (string orderId in ((Cashier)cashier).GetOutstandingOrders())
                {
                    Console.WriteLine("Got outstanding order to pay, id " + orderId);
                    ((Cashier)cashier).Pay(orderId, "123456");
                }
                Thread.Sleep(1);
            }
            //Console.ReadLine();
        }
Beispiel #5
0
        private static void Main(string[] args)
        {
            var dispatcher = new Dispatcher();

            var waiter    = new Waiter(dispatcher);
            var assistant = new AssistantManager(dispatcher);
            var cashier   = new Cashier(dispatcher);

            var timer       = new TimerHandler(dispatcher);
            var house       = new RunnerHouse(dispatcher);
            var serialHouse =
                new ThreadedHandler <Event>(
                    "House",
                    house);

            // set up the kitchen
            var cookedOrders = new ConcurrentDictionary <Guid, bool>();
            var cooks        = new List <Cook> {
                new Cook("James", 100, cookedOrders, dispatcher),
                new Cook("Emily", 50, cookedOrders, dispatcher),
                new Cook("Sarah", 500, cookedOrders, dispatcher)
            };

            var threadedCooks = cooks.Select(c =>
                                             new ThreadedHandler <CookFood>(c.Name, c)).ToList();

            var kitchen =
                new ThreadedHandler <CookFood>(
                    "Kitchen",
                    new ScrewUp <CookFood>(
                        new MorefairDispatcher <CookFood>(
                            threadedCooks)));

            var numCompleted            = 0;
            var numFailedImmediately    = 0;
            var numFailedAfterRetry     = 0;
            var numSucceededImmediately = 0;
            var numSucceededAfterRetry  = 0;

            var onCompleted =
                new ThreadedHandler <OrderCompleted>(
                    "OnCompleted",
                    new Handler <OrderCompleted>(e => {
                numCompleted++;
                if (e.Success && e.Retried)
                {
                    numSucceededAfterRetry++;
                }
                if (e.Success && !e.Retried)
                {
                    numSucceededImmediately++;
                }
                if (!e.Success && e.Retried)
                {
                    numFailedAfterRetry++;
                }
                if (!e.Success && !e.Retried)
                {
                    numFailedImmediately++;
                }
            }));

            // subscribe everything
            dispatcher.Subscribe(kitchen);
            dispatcher.Subscribe(assistant);
            dispatcher.Subscribe(cashier);
            dispatcher.Subscribe(serialHouse);
            dispatcher.Subscribe(timer);
            dispatcher.Subscribe(onCompleted);

            // subscribe various printers
            //dispatcher.Subscribe(new Handler<CookFood>(e => Console.WriteLine($"{e.CorrelationId} Command: CookFood")));
            //dispatcher.Subscribe(new Handler<PriceOrder>(e => Console.WriteLine($"{e.CorrelationId} Command: PriceOrder")));
            //dispatcher.Subscribe(new Handler<TakePayment>(e => Console.WriteLine($"{e.CorrelationId} Command: TakePayment")));
            //dispatcher.Subscribe(new Handler<PrintOrder>(e => Console.WriteLine($"{e.CorrelationId} Command: PrintOrder")));
            //dispatcher.Subscribe(new Handler<Callback>(e => Console.WriteLine($"{e.CorrelationId} Command: Callback")));
            //dispatcher.Subscribe(new Handler<OrderPlaced>(e => Console.WriteLine($"{e.CorrelationId} Ordered")));
            //dispatcher.Subscribe(new Handler<FoodCooked>(e => Console.WriteLine($"{e.CorrelationId} Cooked")));
            //dispatcher.Subscribe(new Handler<OrderPriced>(e => Console.WriteLine($"{e.CorrelationId} Priced")));
            //dispatcher.Subscribe(new Handler<OrderPaid>(e => Console.WriteLine($"{e.CorrelationId} Paid")));
            //dispatcher.Subscribe(new Handler<OrderCompleted>(e => Console.WriteLine($"{e.CorrelationId} OrderCompleted")));
            //dispatcher.Subscribe(new Handler<CookFoodCalledBack>(e => Console.WriteLine($"{e.CorrelationId} CookFoodCalledBack")));
            //dispatcher.Subscribe(new Handler<FailedToContactTheKitchen>(e => Console.WriteLine($"{e.CorrelationId} Failed")));

            // start everything
            serialHouse.Start();
            kitchen.Start();
            foreach (var cook in threadedCooks)
            {
                cook.Start();
            }
            onCompleted.Start();

            var numOrders = 300;

            for (var i = 0; i < numOrders; i++)
            {
                waiter.PlaceOrder("Toby", 1, new List <Tuple <string, int> > {
                    Tuple.Create("Meat", 2),
                    Tuple.Create("Veg", 1)
                });
            }

            Action print = () => {
                Console.WriteLine($"-----------------------------------------");
                Console.WriteLine($"{kitchen.Name}: {kitchen.Count}");
                foreach (var cook in threadedCooks)
                {
                    Console.WriteLine($"{cook.Name}: {cook.Count}");
                }
                Console.WriteLine($"{onCompleted.Name}: {onCompleted.Count}");

                Console.WriteLine($"Timer {timer.Count}");
                Console.WriteLine($"Runners {house.CountRunners}");
                Console.WriteLine($"num Failed Immediately {numFailedImmediately}");
                Console.WriteLine($"num Failed AfterRetry {numFailedAfterRetry}");
                Console.WriteLine($"num Succeeded Immediately {numSucceededImmediately}");
                Console.WriteLine($"num Succeeded AfterRetry {numSucceededAfterRetry}");
                Console.WriteLine($"Complete {numCompleted}/{numOrders}");

                Thread.Sleep(1000);
            };

            while (numCompleted < numOrders)
            {
                print();
            }
            print();
            Console.WriteLine("All done");
        }