-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAMQPDemoForm.cs
More file actions
617 lines (537 loc) · 20.1 KB
/
Copy pathAMQPDemoForm.cs
File metadata and controls
617 lines (537 loc) · 20.1 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/**
* Copyright (c) 2007-2014, Kaazing Corporation. All rights reserved.
*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using Kaazing.HTML5;
using Kaazing.Security;
using Kaazing.AMQP;
namespace Kaazing.AMQP.Demo
{
public partial class AMQPDemoForm : Form
{
private const int LOG_LIMIT = 50;
private AmqpClient client = null;
private BasicChallengeHandler basicHandler;
private AmqpChannel publishChannel = null;
private AmqpChannel consumeChannel = null;
private AmqpChannel txnPublishChannel = null;
private AmqpChannel txnConsumeChannel = null;
private string queueName = "queue" + new Random().Next();
private string txnQueueName = "txnqueue" + new Random().Next();
private string exchangeName = "demo_exchange";
private string txnExchangeName = "demo_txn_exchange";
private string routingKey = "broadcastkey";
private bool terminated = false;
private Queue<String> logLines = new Queue<String>();
private delegate void InvokeDelegate();
public AMQPDemoForm()
{
InitializeComponent();
UpdateUI(false);
//setup ChallengeHandler to handler Basic/Application Basic authentications
this.basicHandler = BasicChallengeHandler.Create();
basicHandler.LoginHandler = new LoginHandlerDemo(this);
}
private void AMQPDemoForm_Load(object sender, EventArgs e)
{
}
/*
* Button click handler for connect button
*/
private void ConnectButton_Click(object sender, EventArgs e)
{
ConnectButton.Enabled = false;
client = new AmqpClient();
client.ChallengeHandler = basicHandler;
client.OpenEvent += new AmqpEventHandler(ConnectedHandler);
client.CloseEvent += new AmqpEventHandler(CloseHandler);
client.ErrorEvent += new AmqpEventHandler(ConnectionErrorHandler);
ConnectionStatusValueLabel.Text = "CONNECTING";
Log("\n");
Log("CONNECTING: " + LocationText.Text );
UpdateTextBoxes(false);
string virtualHost = VirtualHostText.Text;
string locText = LocationText.Text;
if ((locText == null) || (locText.Length == 0))
{
locText = "wss://demos.kaazing.com/amqp";
}
try
{
client.Connect(locText, virtualHost, "guest", "guest");
}
catch (Exception ex)
{
UpdateUI(false);
Log("Exception: " + ex.Message);
}
}
private void CreateChannel()
{
Log("OPEN: Publish Channel");
publishChannel = client.OpenChannel();
publishChannel.OpenEvent += PublishChannelOpenHandler;
publishChannel.DeclareExchangeEvent += DeclareExchangeOkHandler;
publishChannel.CloseEvent += PublishChannelCloseHandler;
publishChannel.ErrorEvent += PublishChannelErrorHandler;
Log("OPEN: Consume Channel");
consumeChannel = client.OpenChannel();
consumeChannel.OpenEvent += ConsumeChannelOpenHandler;
consumeChannel.DeclareQueueEvent += DeclareQueueOkHandler;
consumeChannel.BindQueueEvent += BindQueueOkHandler;
consumeChannel.ConsumeEvent += BasicConsumeOkHandler;
consumeChannel.MessageEvent += MessageHandler;
consumeChannel.FlowEvent += FlowOkHandler;
consumeChannel.CancelEvent += CancelOkBasicHandler;
consumeChannel.CloseEvent += ChannelCloseHandler;
txnPublishChannel = client.OpenChannel();
txnPublishChannel.OpenEvent += TxnPublishChannelOpenHandler;
txnPublishChannel.CommitTransactionEvent += CommitOkTxHandler;
txnPublishChannel.RollbackTransactionEvent += RollbackOkTxHandler;
txnPublishChannel.SelectTransactionEvent += SelectOkTxHandler;
txnConsumeChannel = client.OpenChannel();
txnConsumeChannel.OpenEvent += TxnConsumeChannelOpenHandler;
txnConsumeChannel.MessageEvent += TxnMessageHandler;
}
/*
* Button click handler for disconnect button!
*/
private void CloseButton_Click(object sender, EventArgs e)
{
Log("DISCONNECT");
// close the connection...
publishChannel.CloseChannel(0, "", 0, 0);
consumeChannel.CloseChannel(0, "", 0, 0);
txnPublishChannel.CloseChannel(0, "", 0, 0);
txnConsumeChannel.CloseChannel(0, "", 0, 0);
Disconnect();
}
private void Disconnect()
{
terminated = true;
if (client != null)
{
client.Disconnect();
}
client = null;
}
/*
* Button click handler for Publish(non-txn) button
*/
private void Publish_Click(object sender, EventArgs e)
{
if (MessageText.Text == null || MessageText.Text.Length == 0)
{
Log("Please enter valid text for AMQP message");
return;
}
PublishBasic(MessageText.Text);
}
/*
* Button click handler for Flow On button!
*/
private void Flow_On_Click(object sender, EventArgs e)
{
consumeChannel.FlowChannel(true);
}
/*
* Button click handler for Flow Off button!
*/
private void Flow_Off_Click(object sender, EventArgs e)
{
consumeChannel.FlowChannel(false);
}
/*
* Button click handler for Clear Log button!
*/
private void ClearLogButton_Click(object sender, EventArgs e)
{
logLines.Clear();
Output.Text = "";
}
/*
* Button click handler for Select button!
*/
private void SelectTx_Click(object sender, EventArgs e)
{
Log("TXN SELECT/START");
txnPublishChannel.SelectTx();
}
/*
* Button click handler for Publish(transactional) button!
*/
private void PublishTx_Click(object sender, EventArgs e)
{
if (TransactionMessageText.Text == null || TransactionMessageText.Text.Length == 0)
{
Log("Please enter valid text for AMQP message");
return;
}
TxPublishBasic(TransactionMessageText.Text);
}
/*
* Button click handler for Commit transaction button!
*/
private void CommitTx_Click(object sender, EventArgs e)
{
Log("TXN COMMIT");
txnPublishChannel.CommitTx();
}
/*
* Button click handler for Rollback transaction button!
*/
private void RollbackTx_Click(object sender, EventArgs e)
{
Log("TXN ROLLBACK");
txnPublishChannel.RollbackTx();
}
/*
* All registered Event Handlers
*/
private void CloseHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
if (!terminated)
{
Disconnect();
}
ConnectionStatusValueLabel.Text = "DISCONNECTED";
terminated = false;
UpdateUI(false);
Log("DISCONNECTED");
ExchangeText.ReadOnly = false;
TransactionExchangeText.ReadOnly = false;
}));
}
private void ConnectedHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("CONNECTED");
ConnectionStatusValueLabel.Text = "CONNECTED";
exchangeName = ExchangeText.Text;
txnExchangeName = TransactionExchangeText.Text;
CreateChannel();
// Ready to process messages
UpdateUI(true);
ExchangeText.ReadOnly = true;
TransactionExchangeText.ReadOnly = true;
}));
}
private void ConnectionErrorHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
ConnectionStatusValueLabel.Text = "ERROR";
Log("ERROR:" + e.Message);
}));
}
/*
* Publish Channel Handlers
*/
private void PublishChannelOpenHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("OPENED: Publish Channel");
publishChannel.DeclareExchange(exchangeName, "fanout", false, false, false, null);
}));
}
private void PublishChannelErrorHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("ERROR: PUBLISH CHANNEL: " + e.Message);
}));
}
private void PublishChannelCloseHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("CLOSED: PUBLISH CHANNEL");
}));
}
/*
* Txn Publish Channel Handler
*/
private void TxnPublishChannelOpenHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
txnPublishChannel.DeclareExchange(txnExchangeName, "fanout", false, false, false, null);
}));
}
private void PublishBasic(string text)
{
ByteBuffer buffer = new ByteBuffer();
buffer.PutString(text, System.Text.Encoding.UTF8);
buffer.Flip();
AmqpProperties amqpProperties = new AmqpProperties();
amqpProperties.MessageId = "abcdxyz1234pqr";
amqpProperties.CorrelationId = "23456";
amqpProperties.UserId =UserIdText.Text;
amqpProperties.ContentType = AmqpProperties.TEXT_PLAIN;
amqpProperties.DeliveryMode = 1;
amqpProperties.Priority = 6;
amqpProperties.Timestamp = DateTime.Now.ToLocalTime();
AmqpArguments customHeaders = new AmqpArguments();
customHeaders.AddInteger("KZNG_AMQP_KEY1", 100);
customHeaders.AddLongString("KZNG_AMQP_KEY2", "Custom Header Value");
amqpProperties.Headers = customHeaders;
publishChannel.PublishBasic(buffer, amqpProperties, exchangeName, routingKey, false, false);
Log(amqpProperties.ToString());
Log("Published Message Properties:");
Log("MESSAGE PUBLISHED: " + text);
}
private void TxPublishBasic(string text)
{
ByteBuffer buffer = new ByteBuffer();
buffer.PutString(text, System.Text.Encoding.UTF8);
buffer.Flip();
AmqpProperties amqpProperties = new AmqpProperties();
txnPublishChannel.PublishBasic(buffer, amqpProperties, txnExchangeName, routingKey, false, false);
Log("TXN MESSAGE PUBLISHED: " + text);
}
/*
* Consume Channel Handlers
*/
private void ConsumeChannelOpenHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("OPENED: Consume Channel");
consumeChannel.DeclareQueue(queueName, false, false, false, false, false, null)
.BindQueue(queueName, exchangeName, routingKey, false, null)
.ConsumeBasic(queueName, routingKey, false, false, false, false, null);
}));
}
/*
* Txn Consume Channel Handlers
*/
private void TxnConsumeChannelOpenHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
txnConsumeChannel.DeclareQueue(txnQueueName, false, false, false, false, false, null)
.BindQueue(txnQueueName, txnExchangeName, routingKey, false, null)
.ConsumeBasic(txnQueueName, routingKey, false, false, false, false, null);
}));
}
private void DeclareExchangeOkHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("EXCHANGE DECLARED: " + exchangeName);
}));
}
private void DeclareQueueOkHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("QUEUE DECLARED: " + queueName);
}));
}
private void BindQueueOkHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("QUEUE BOUND: " + exchangeName + " - " + queueName);
}));
}
private void BasicConsumeOkHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("READY TO CONSUME FROM QUEUE: " + queueName);
}));
}
private void ChannelErrorHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("ERROR: " + e.Message);
}));
}
private void ChannelCloseHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("CHANNEL CLOSED");
}));
}
private void CancelOkBasicHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("BASIC CANCELLED");
}));
}
private void FlowOkHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("FLOW: " + (e.IsFlowActive ? "ON" : "OFF"));
}));
}
private void CommitOkTxHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("TXN COMMITTED");
UpdateTransactionButtons(false, true);
}));
}
private void RollbackOkTxHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("TXN ROLLEDBACK");
UpdateTransactionButtons(false, true);
}));
}
private void SelectOkTxHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
Log("TXN SELECTED/STARTED");
UpdateTransactionButtons(true, true);
}));
}
private void MessageHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
ByteBuffer buf = e.Body;
string message = buf.GetString(System.Text.Encoding.UTF8);
Log(e.AmqpProperties.ToString());
Log("Consumed Message Properties:");
Log("MESSAGE CONSUMED: " + message);
// Explicitly acknowledge the message as we are passing
// 'false' as the value of the 'noAck' parameter in
// the consumeChannel.ConsumeBasic() call.
long dt = Convert.ToInt64(e.Arguments["deliveryTag"]);
((AmqpChannel)sender).AckBasic(dt, true);
}));
}
private void TxnMessageHandler(object sender, AmqpEventArgs e)
{
this.BeginInvoke((InvokeDelegate)(() =>
{
ByteBuffer buf = e.Body;
string message = buf.GetString(System.Text.Encoding.UTF8);
Log("TXN MESSAGE CONSUMED: " + message);
// Explicitly acknowledge the message as we are passing
// 'false' as the value of the 'noAck' parameter in
// the txnConsumeChannel.ConsumeBasic() call.
long dt = Convert.ToInt64(e.Arguments["deliveryTag"]);
((AmqpChannel)sender).AckBasic(dt, true);
}));
}
/*
* All helper/convenience methods.
*/
private void UpdateTextBoxes(bool enabled)
{
LocationText.Enabled = enabled;
VirtualHostText.Enabled = enabled;
UserIdText.Enabled = enabled;
}
private void UpdateUI(bool connected)
{
UpdateTextBoxes(!connected);
ConnectButton.Enabled = !connected;
DisconnectButton.Enabled = connected;
SubscribeButton.Enabled = connected;
SendButton.Enabled = connected;
UnsubscribeButton.Enabled = connected;
UpdateTransactionButtons(false, connected);
}
private void UpdateTransactionButtons(bool transacted, bool connected)
{
if (connected)
{
SelectButton.Enabled = !transacted;
CommitButton.Enabled = transacted;
PublishTransactionButton.Enabled = transacted;
RollbackButton.Enabled = transacted;
}
else
{
SelectButton.Enabled = false;
CommitButton.Enabled = false;
PublishTransactionButton.Enabled = false;
RollbackButton.Enabled = false;
}
}
/*
* Console output
*/
private void Log(String arg)
{
logLines.Enqueue(arg);
if (logLines.Count > LOG_LIMIT)
{
logLines.Dequeue();
}
String[] o = logLines.ToArray();
Array.Reverse(o);
Output.Text = String.Join("\r\n", o);
}
///
/// Handle server authentication challenge request,
/// Popup a login window for username/password
///
public PasswordAuthentication AuthenticationHandler()
{
PasswordAuthentication credentials = null;
LoginDemoForm loginForm = new LoginDemoForm();
AutoResetEvent userInputCompleted = new AutoResetEvent(false);
//
//Popup login window on new Task. Note: please use Task to do parallel jobs
//
Task t = Task.Factory.StartNew(() =>
{
if (loginForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
credentials = new PasswordAuthentication(loginForm.Username, loginForm.Password.ToCharArray());
}
userInputCompleted.Set();
});
// wait user click 'OK' or 'Cancel' on login window
userInputCompleted.WaitOne();
return credentials;
}
private void checkConnectionData()
{
if (LocationText.Text.Length == 0 || VirtualHostText.Text.Length == 0 || UserIdText.Text.Length == 0 || ExchangeText.Text.Length==0)
{
ConnectButton.Enabled = false;
}
else
{
ConnectButton.Enabled = true;
}
}
private void LocationText_TextChanged(object sender, EventArgs e)
{
checkConnectionData();
}
private void VirtualHostText_TextChanged(object sender, EventArgs e)
{
checkConnectionData();
}
private void UserIdText_TextChanged(object sender, EventArgs e)
{
checkConnectionData();
}
private void ExchangeText_TextChanged(object sender, EventArgs e)
{
checkConnectionData();
}
}
}