-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
93 lines (74 loc) · 3.42 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace ClassLibrary4
{
internal static class Program
{
public static void Main(string[] args)
{
var dispatcher = new Dispatcher();
var cooks = new[]
{
new QueuedHandler<Messages.PrepareFood>(
new Chef(dispatcher, "Joe Bastianich", 1)),
new QueuedHandler<Messages.PrepareFood>(
new Chef(dispatcher, "Gordon Ramsay", 3)),
new QueuedHandler<Messages.PrepareFood>(
new KitchenNightmare(new Chef(dispatcher, "Amy's Baking Company", .5)))
};
var kitchenQueue = new QueuedHandler<Messages.PrepareFood>(
new TimeToLiveHandler<Messages.PrepareFood>(
new ShortestQueueDispatcher<Messages.PrepareFood>(cooks)));
kitchenQueue.Start();
var kitchen = new ThrottlingHandler<Messages.PrepareFood>(kitchenQueue);
var assistantManager = new AssistantManager(dispatcher);
var cashier = new Cashier(dispatcher);
var reporting = new EnterpriseExcel();
var alarmClock = new AlarmClock(dispatcher);
alarmClock.Start();
var storage = new RestaurantProcessStorage(kitchen, assistantManager, cashier, reporting, alarmClock);
var waiter = new Waiter("July M.", dispatcher);
dispatcher.Subscribe(Log<Messages.OrderPlaced>(dispatcher));
dispatcher.Subscribe(Log<Messages.SuspectOrderPlaced>(dispatcher));
dispatcher.Subscribe<Messages.OrderPlaced>(storage);
dispatcher.Subscribe<Messages.SuspectOrderPlaced>(storage);
dispatcher.Subscribe<Messages.PrepareFood>(storage);
dispatcher.Subscribe<Messages.FoodPrepared>(storage);
dispatcher.Subscribe<Messages.TotalOrder>(storage);
dispatcher.Subscribe<Messages.OrderTotalled>(storage);
dispatcher.Subscribe<Messages.OrderPaid>(storage);
dispatcher.Subscribe<Messages.ProcessCompleted>(storage);
dispatcher.Subscribe<Messages.Timeout<Message>>(alarmClock);
new Thread(
_ => PayLoop(cashier)).Start();
cooks.ForEach(x => x.Start());
var plates = new[] {"Pizza", "Chicharron", "Miguitas"};
for (var i = 0; i < 100000; i++)
{
waiter.PlaceOrder(i, 1, new Order.Item(plates[i%3], 1));
}
Console.ReadLine();
}
private static AdHocHandler<T> Log<T>(Dispatcher dispatcher) where T: Message
{
return new AdHocHandler<T>(
message => dispatcher.Subscribe(
message.CorrelationId.ToString(),
new AdHocHandler<Message>(Console.WriteLine)));
}
private static void PayLoop(Cashier cashier)
{
var outstandingOrders = new Queue<int>(Enumerable.Range(0, 100000));
while (outstandingOrders.Count > 0)
{
var nextOrder = outstandingOrders.Dequeue();
if (false == cashier.TryAcceptPayment(nextOrder))
{
outstandingOrders.Enqueue(nextOrder);
}
}
}
}
}