public static void Main()
        {
            var horn             = new ConsoleHorn();
            var topicBasedPubSub = new TopicBasedPubSub();

            var wireUpResult = WireUp(topicBasedPubSub);

            wireUpResult.Startables.ForEach(x => x.Start());
            StartPrintingQueueStats(horn, wireUpResult.Trackables, wireUpResult.StatsProjection);

            topicBasedPubSub.SubscribeByType(wireUpResult.MidgetHouse);

            var ordersCount = 10;

            for (var i = 0; i < ordersCount; ++i)
            {
                var isDrinker = i % 2 == 0;
                var orderId   = Guid.NewGuid();
                topicBasedPubSub.SubscribeByCorellationId(orderId, new MessageTracer(orderId.ToString("N")));
                wireUpResult.Waiter
                .PlaceNewOrder(orderId, new Dictionary <string, int>
                {
                    { isDrinker?GoodsMenu.Drinkables.Vodka: GoodsMenu.Eatables.Meat, 2 }
                });

                horn.Say($"[outer user]: got order handle {orderId}.");
            }
            horn.Say("[outer user]: placed all orders.");
            Console.ReadKey(false);
        }
        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 WireUpResult WireUp(TopicBasedPubSub topicBasedPubSub)
        {
            var horn = PrintDetails ? (IHorn) new ConsoleHorn() : new SilentHorn();

            var cookNames = new[]
            {
                "Joe",
                "Greg",
                "Bro"
            };
            var printer = new Printer(horn);

            var cashier = AsQueueable(nameof(Cashier), new Cashier(topicBasedPubSub, horn));

            var assistantManager = AsQueueable(nameof(AssistantManager), new AssistantManager(topicBasedPubSub, horn));

            var random = new Random();
            var cooks  = cookNames
                         .Select(
                cookName =>
                AsQueueable($"{nameof(Cook)}-{cookName}",
                            new DroppingHandler <CookFood>(new Cook(topicBasedPubSub, horn, cookName, random.Next(0, 10000)))))
                         .ToList();

            var megaCook = AsQueueable("CookDispatcher", new MoreFairDispatcher <CookFood>(cooks));

            var waiter = new Waiter(topicBasedPubSub, horn);

            topicBasedPubSub.SubscribeByType <PrintReceipt>(printer);
            topicBasedPubSub.SubscribeByType <TakePayment>(cashier);
            topicBasedPubSub.SubscribeByType <PriceOrder>(assistantManager);
            topicBasedPubSub.SubscribeByType <CookFood>(megaCook);

            var midgetHouse = AsQueueable(nameof(MidgetHouse), new MidgetHouse(topicBasedPubSub));

            var items = cooks.Concat(new object[]
            {
                megaCook,
                cashier,
                assistantManager,
                midgetHouse
            }).ToList();

            var statsProjection = new StatsProjection();

            topicBasedPubSub.SubscribeByType <OrderPlaced>(statsProjection);
            topicBasedPubSub.SubscribeByType <OrderFinalized>(statsProjection);

            return(new WireUpResult
            {
                Waiter = waiter,
                Startables = items
                             .Cast <IStartable>()
                             .ToList(),
                Trackables = items
                             .Cast <ITrackable>()
                             .ToList(),
                MidgetHouse = midgetHouse,
                StatsProjection = statsProjection
            });
        }
Пример #4
0
 public static IMidget Create(bool isDodgyCustomer, TopicBasedPubSub topicBasedPubSub)
 {
     return(isDodgyCustomer
         ? (IMidget) new ZimbabweanMidget(topicBasedPubSub)
         : new LithuanianMidget(topicBasedPubSub));
 }
Пример #5
0
 public MidgetHouse(TopicBasedPubSub topicBasedPubSub)
 {
     _topicBasedPubSub = topicBasedPubSub;
     _midgets          = new Dictionary <Guid, IMidget>();
 }
Пример #6
0
        private static void Main()
        {
            var publisher = new TopicBasedPubSub();

            var alarmClock         = new AlarmClock(publisher);
            var midgetHouse        = new MidgetHouse(publisher);
            var midgetHouseHandler = new QueuedHandler <Message>("MidgetHouse", midgetHouse);

            midgetHouse.QueuedHandler = midgetHouseHandler;

            var cashier          = new Cashier(publisher);
            var cashierQueue     = new QueuedHandler <TakePayment>("Cashier", cashier);
            var assistantManager = new QueuedHandler <PriceOrdered>("AssistantManager", new FuzzyHandler <PriceOrdered>(new AssistantManager(publisher), 0, 20));

            var seed = new Random(DateTime.Now.Millisecond);

            var cook1 = new QueuedHandler <CookFood>(
                "BogdanQueue",
                new TTLHandler <CookFood>(new FuzzyHandler <CookFood>(new Cook(seed.Next(1000), publisher), 10, 10)));
            var cook2 = new QueuedHandler <CookFood>(
                "RomanQueue",
                new TTLHandler <CookFood>(new FuzzyHandler <CookFood>(new Cook(seed.Next(1000), publisher), 95, 50)));
            var cook3 = new QueuedHandler <CookFood>(
                "WaclawQueue",
                new TTLHandler <CookFood>(new FuzzyHandler <CookFood>(new Cook(seed.Next(1000), publisher), 10, 20)));

            var dispatcher = new QueuedHandler <CookFood>(
                "MFDispatcher",
                new TTLHandler <CookFood>(
                    new MFDispatcher <CookFood>(
                        new List <QueuedHandler <CookFood> >
            {
                cook1,
                cook2,
                cook3
            })));

            StartQueues(
                new List <IStartable>
            {
                assistantManager,
                cashierQueue,
                dispatcher,
                cook1,
                cook2,
                cook3,
                midgetHouseHandler,
                alarmClock
            });
            StartQueuePrinter(
                new List <IPrintable>
            {
                assistantManager,
                cashierQueue,
                dispatcher,
                cook1,
                cook2,
                cook3,
                midgetHouseHandler
            });

            var waiter = new Waiter(publisher);

            publisher.Subscribe(dispatcher);
            publisher.Subscribe(assistantManager);
            publisher.Subscribe(cashier);
            publisher.Subscribe(alarmClock);
            publisher.Subscribe <OrderPlaced>(midgetHouse);

            PlaceOrders(waiter);

            HandlePays(cashier);

            Console.ReadKey();
        }