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(); }
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"); }