comparison Workflow/Client/MainWindow.xaml.cs @ 89:3c67e54e3a17

Working version
author stevenhollidge <stevenhollidge@hotmail.com>
date Mon, 30 Apr 2012 10:45:32 +0100
parents
children
comparison
equal deleted inserted replaced
88:e84dc4926a5a 89:3c67e54e3a17
1 using System;
2 using System.Threading.Tasks;
3 using System.Windows;
4 using System.Windows.Media.Animation;
5 using System.Windows.Threading;
6 using Burrow;
7 using Burrow.Extras;
8 using Client.Controls;
9 using Client.Messages;
10
11 namespace Client
12 {
13 public partial class MainWindow
14 {
15 private const string QUEUE_CONNECTION_STRING = "host=localhost;username=guest;password=guest";
16 private readonly ITunnel tunnel;
17 private readonly Subscription subscriptionOrder;
18 private readonly Subscription subscriptionPayment;
19 private readonly Subscription subscriptionDelivery;
20 private readonly Subscription subscriptionError;
21 private readonly Guid orderId;
22 private const string ORDER_ROUTING_KEY = "Order";
23 private const string PAYMENT_ROUTING_KEY = "Payment";
24 private const string DELIVERY_ROUTING_KEY = "Delivery";
25 private const string ORDER_STORYBOARD_KEY = "OrderStoryboard";
26 private const string PAYMENT_STORYBOARD_KEY = "PaymentStoryboard";
27 private const string DELIVERY_STORYBOARD_KEY = "DeliveryStoryboard";
28 private const string FADEINGIFT_STORYBOARD_KEY = "FadeInGiftStoryboard";
29 private readonly Storyboard orderStoryboard;
30 private readonly Storyboard paymentStoryboard;
31 private readonly Storyboard deliveryStoryboard;
32 private readonly Storyboard fadeInGiftStoryboard;
33
34 public MainWindow()
35 {
36 InitializeComponent();
37
38 // set up rabbitmq tunnel
39 tunnel = RabbitTunnel.Factory.Create(QUEUE_CONNECTION_STRING);
40 tunnel.SetSerializer(new JsonSerializer());
41
42 // set up a subscription to each message type
43 subscriptionOrder = tunnel.SubscribeAsync<OrderMessage>("Workflow",
44 (message, args) => Dispatcher.BeginInvoke(new Action(() => OnOrderReceived(message, args))));
45
46 subscriptionPayment = tunnel.SubscribeAsync<PaymentMessage>("Workflow",
47 (message, args) => Dispatcher.BeginInvoke(new Action(() => OnPaymentReceived(message, args))));
48
49 subscriptionDelivery = tunnel.SubscribeAsync<DeliveryMessage>("Workflow",
50 (message, args) => Dispatcher.BeginInvoke(new Action(() => OnDeliveryReceived(message, args))));
51
52 //subscriptionError = tunnel.SubscribeAsync<?????>("Workflow",
53 // (message, args) => Dispatcher.BeginInvoke(new Action(() => OnErrorReceived(message, args))));
54
55 // make sure all storyboards are available
56 orderStoryboard = (Storyboard)this.TryFindResource(ORDER_STORYBOARD_KEY);
57 paymentStoryboard = (Storyboard)this.TryFindResource(PAYMENT_STORYBOARD_KEY);
58 deliveryStoryboard = (Storyboard)this.TryFindResource(DELIVERY_STORYBOARD_KEY);
59 fadeInGiftStoryboard = (Storyboard)this.TryFindResource(FADEINGIFT_STORYBOARD_KEY);
60 if (orderStoryboard == null) throw new ResourceReferenceKeyNotFoundException("Cannot find OrderStoryboard resource", ORDER_STORYBOARD_KEY);
61 if (paymentStoryboard == null) throw new ResourceReferenceKeyNotFoundException("Cannot find PaymentStoryboard resource", PAYMENT_STORYBOARD_KEY);
62 if (deliveryStoryboard == null) throw new ResourceReferenceKeyNotFoundException("Cannot find DeliveryStoryboard resource", DELIVERY_STORYBOARD_KEY);
63 if (fadeInGiftStoryboard == null) throw new ResourceReferenceKeyNotFoundException("Cannot find FadeInGiftStoryboard resource", FADEINGIFT_STORYBOARD_KEY);
64
65 // create an orderid for the workflow
66 orderId = Guid.NewGuid();
67 }
68
69 private void NotifyUI(UIMessage ctrl, MessageType messageType, string text)
70 {
71 Dispatcher.BeginInvoke(new Action(() =>
72 {
73 ctrl.MessageType = messageType;
74 ctrl.Text = text;
75 ctrl.Show();
76 }));
77 }
78
79 #region EventHandlers
80
81 private void btnSubmit_Click(object sender, RoutedEventArgs e)
82 {
83 try
84 {
85 orderStoryboard.Begin();
86 NotifyUI(NotificationRight, MessageType.Success, "Order submitted");
87 }
88 catch (Exception ex)
89 {
90 NotifyUI(NotificationRight, MessageType.Error, ex.Message);
91 }
92 }
93
94 private void OnOrderStoryboardCompleted(object sender, EventArgs e)
95 {
96 try
97 {
98 tunnel.Publish(new OrderMessage
99 {
100 OrderId = orderId,
101 OrderDate = DateTime.Now,
102 ProductCode = "ABCDE",
103 Quantity = 10,
104 UnitPrice = 9.99m
105 }, ORDER_ROUTING_KEY);
106
107 NotifyUI(NotificationLeft, MessageType.Success, "Order published");
108 }
109 catch (Exception ex)
110 {
111 NotifyUI(NotificationRight, MessageType.Error, ex.Message);
112 }
113 }
114
115 private void OnOrderReceived(OrderMessage order, MessageDeliverEventArgs args)
116 {
117 try
118 {
119 NotifyUI(NotificationRight, MessageType.Success, "Order received");
120 paymentStoryboard.Begin();
121 }
122 catch (Exception ex)
123 {
124 NotifyUI(NotificationRight, MessageType.Error, ex.Message);
125 }
126 finally
127 {
128 if (subscriptionOrder != null) subscriptionOrder.Ack(args.DeliveryTag);
129 }
130 }
131
132 private void OnPaymentStoryboardCompleted(object sender, EventArgs e)
133 {
134 try
135 {
136 tunnel.Publish(new PaymentMessage()
137 {
138 OrderId = orderId,
139 IsConfirmed = true
140 }, PAYMENT_ROUTING_KEY);
141
142 NotifyUI(NotificationLeft, MessageType.Success, "Payment published");
143 }
144 catch (Exception ex)
145 {
146 NotifyUI(NotificationRight, MessageType.Error, ex.Message);
147 }
148 }
149
150 private void OnPaymentReceived(PaymentMessage payment, MessageDeliverEventArgs args)
151 {
152 try
153 {
154 NotifyUI(NotificationRight, MessageType.Success, "Payment received");
155 deliveryStoryboard.Begin();
156 }
157 catch (Exception ex)
158 {
159 NotifyUI(NotificationRight, MessageType.Error, ex.Message);
160 }
161 finally
162 {
163 if (subscriptionPayment != null) subscriptionPayment.Ack(args.DeliveryTag);
164 }
165 }
166
167 private void OnDeliveryStoryboardCompleted(object sender, EventArgs e)
168 {
169 try
170 {
171 tunnel.Publish(new DeliveryMessage()
172 {
173 OrderId = orderId,
174 Address = "123 Some Street, Somewhere"
175 }, DELIVERY_ROUTING_KEY);
176
177 NotifyUI(NotificationLeft, MessageType.Success, "Delivery published");
178 }
179 catch (Exception ex)
180 {
181 NotifyUI(NotificationRight, MessageType.Error, ex.Message);
182 }
183 }
184
185 private void OnDeliveryReceived(DeliveryMessage delivery, MessageDeliverEventArgs args)
186 {
187 try
188 {
189 NotifyUI(NotificationRight, MessageType.Success, "Delivery received");
190 fadeInGiftStoryboard.Begin();
191 }
192 catch (Exception ex)
193 {
194 NotifyUI(NotificationRight, MessageType.Error, ex.Message);
195 }
196 finally
197 {
198 if (subscriptionDelivery != null) subscriptionDelivery.Ack(args.DeliveryTag);
199 }
200 }
201
202 #endregion
203
204 private void Window_Closed(object sender, EventArgs e)
205 {
206 Application.Current.Shutdown();
207 }
208 }
209 }