From 9201a8811f320960000d93fcd45165f2c07dbf92 Mon Sep 17 00:00:00 2001 From: PsychoMark Date: Tue, 21 Jun 2016 11:43:51 +0200 Subject: [PATCH] Error handling, connection status and new icons --- App.xaml | 3 +- Icons.xaml | 483 ++++++++++++++++++++++++++++++ Images/Clear.svg | 56 ++++ Images/Clear.xaml | 60 ++++ Images/Connect.png | Bin 1606 -> 0 bytes Images/Connect.svg | 85 ++++++ Images/Connect.xaml | 203 +++++++++++++ Images/Disconnect.png | Bin 1727 -> 0 bytes Images/Disconnect.svg | 86 ++++++ Images/Disconnect.xaml | 203 +++++++++++++ Model/IConnection.cs | 25 ++ Model/RabbitMQClientConnection.cs | 45 ++- PettingZoo.csproj | 8 +- Properties/Resources.Designer.cs | 36 +++ Properties/Resources.resx | 12 + README.md | 12 + View/ConnectionWindow.xaml | 1 + View/ConnectionWindow.xaml.cs | 8 + View/MainWindow.xaml | 8 +- ViewModel/MainViewModel.cs | 51 +++- 20 files changed, 1366 insertions(+), 19 deletions(-) create mode 100644 Icons.xaml create mode 100644 Images/Clear.svg create mode 100644 Images/Clear.xaml delete mode 100644 Images/Connect.png create mode 100644 Images/Connect.svg create mode 100644 Images/Connect.xaml delete mode 100644 Images/Disconnect.png create mode 100644 Images/Disconnect.svg create mode 100644 Images/Disconnect.xaml create mode 100644 README.md diff --git a/App.xaml b/App.xaml index bffea81..a9025ab 100644 --- a/App.xaml +++ b/App.xaml @@ -5,7 +5,8 @@ - + + diff --git a/Icons.xaml b/Icons.xaml new file mode 100644 index 0000000..848e067 --- /dev/null +++ b/Icons.xaml @@ -0,0 +1,483 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Images/Clear.svg b/Images/Clear.svg new file mode 100644 index 0000000..a93f38f --- /dev/null +++ b/Images/Clear.svg @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Images/Clear.xaml b/Images/Clear.xaml new file mode 100644 index 0000000..85ad8ee --- /dev/null +++ b/Images/Clear.xaml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Images/Connect.png b/Images/Connect.png deleted file mode 100644 index 1fba021a0c726f92946fb104335948dcb6941be0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1606 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA1|-9oezpTC$r9IylHmNblJdl&R0hYC{G?O` z&)mfH)S%SFl*+=BsWuD@%u1Od5hW46K32*3xq68pHF_1f1wh>l3^w)^1&PVosU-?Y zsp*+{wo31J?^jaDOtDo8H}y5}EpSfF$n>ZxN)4{^3rViZPPR-@vbR&PsjvbXkegbP zs8ErclUHn2VXFi-*9yo63F|81#=KlDb#X~hD#E>34K5C;EJ)Q4 zN-fSWElLJPT$(b-ssbzLqSVBa{GyQj{2W*)24v)yHFb3| zws1Cr>2=9ZF3nBND}m`vLFhHbsTY(KatnYqyQCInmZhe+73JqDfW2&$iQ6qkIL(9V zO~LIJW1M>RfsWA!MJ!T8!-RmT2gHOYTObFX@Kf`Esl5o8tji=m^f53n?ecVS45_$v zWu|pTh@(hbxPbG44k4AUXNoEriHbtLPrF)UgH$uNcr0eA2^9-FEymRr;wkKs)6}{| zm{+q#rPWoZW}$>DPe)gp??S;(5iyJZAJ|zS@yzb~y_uWY_dR=eW^TQjeerqAn-eCB zA9vJ$!Bp)ad8Sc4LFUZ!mSbiG{C~I~=xcb1+~?WX(((MxM3sG_e+)YQFwdJ@>oa?Y zmSYWf#23NsvwL5&1h*BgU6&hd)L|uR@O+_J*_o#&XXg|vrQ9nENPPS}X6~P)Gn1cx zY&sh`DQWgUiT4#}o~^yol>6nu`yWx-+de+pWA=X{@5ktaPE)r979RP~XD2OPkrnYH zh3nDUI}Wy*H;$?7OM0zrwph1DmGz_6LhBso8rg_6?&{2fd6ynZFR*R=;k+$xo>}h4 z*AG^F^T{o6clr2wo8P^B=J~aoPAyWo{ow{no%@Nm`Wq}OuW!5fIIZJ{x#FYiv2$$t zof>}p&biYda{GgYaM3T}1c^OmnX|1<6^lsU{Jh!lRdlfC%%3N}2X%%k&DYv@@o(G; zr#-*2-<%Js5Ke9IJYvh-{N!({u5N(sd~dT)KU-TK-EThoCbsthg9S&~&NWNyngl=C zI_$B!%hp*t`}CLJh98U*nC}YxHjDGRBYx0s@}*y9e;MQb?4_38XcK=h*)Hko*Q)61 zYnms`;_evrRC@1^y)v)d^UZCO%g0YkFP7T)^~-Jc+<$&6YiH{|ka=nwH|N^3{aY0G zDLO7T&62dpNM`0YdUd;IlcL@H!251XkI0&t8LZQ_nbK6Tdg>MHZ6BnrZn`>e&(`eV zqgv55W~?8taes(! + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Images/Connect.xaml b/Images/Connect.xaml new file mode 100644 index 0000000..a31ac6f --- /dev/null +++ b/Images/Connect.xaml @@ -0,0 +1,203 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Images/Disconnect.png b/Images/Disconnect.png deleted file mode 100644 index e86d993c11ca9cb8c9d8735ca7056106e90dbf18..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1727 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA1|-9oezpTC$r9IylHmNblJdl&R0hYC{G?O` z&)mfH)S%SFl*+=BsWuD@%u1Od5hW46K32*3xq68pHF_1f1wh>l3^w)^1&PVosU-?Y zsp*+{wo31J?^jaDOtDo8H}y5}EpSfF$n>ZxN)4{^3rViZPPR-@vbR&PsjvbXkegbP zs8ErclUHn2VXFi-*9yo63F|81#=KlDb#X~hD#E>34K5C;EJ)Q4 zN-fSWElLJPT$(b-ssbzLqSVBa{GyQj{2W*)24v)y>p{tpbo29X{nWcr1iIJOu zqlF7huSMv>2~2MaLaz}{y`ZF!TL84#CABECEH%ZgC_h&L>}9J=oNh62!)YE= zZwhX=7~s^a4|I$^C}NQ!8YToxJs>7L*#bH6grAxROzlO$WIf07c0Mq-7<;-nhE&{o zGs`vOqH;gAb)3J8el;13YAk(kf!dqrd4vHc6#W-UuwR-RT?CbsQ?^&6k^ulG&2 zUbWpDoiJ$PUS`4Snp=2Sh)x&)_OQ@JfSg zJ~1FR;UX^6n^-DVXhx~dTPt$YN%PZr$2-;qGHV!9zr>q*$)fO&BD4x z|BN~=x>$B=)}Cus{Wq=ogZqNsRlR**y=&v!6#ua)|FV6-sBQb>w%X61uiS441-!eo z%uP^lmCx4m4Ocw>?fY4{MZRa}mP#GoXBv-TKnRz96DlbuTxUq;txX=U@=(4zJ4N{ir^E!i)A|o;Lvo!qHuqTU%qtd_SXSLWwe{43 zqpyr>`8NsutXg(A@8v)4sE(GsQ`ddJv~}hKtE+!|qJo0=q$a7xJ-x8s?9AcKR&F)H zQh%jqeoah@u#A5*>2uz-17G6h(~iCjV!J=t?Y^2@P4ta))9*_UvR{6&y<4zeLAu>{ z@uY?@`HAPdDiZIj3Rc^?+2y5N44O2dZu&Os#Uhob{j{30E_uxU#vvmlT`ZX>TT&3^ zv7B3dkwoS8xyw)Pv5HdZ;fU&g79G4=F!44=TTTmq$}iR<-G%m_L@x@?JMs7A7E!rH tk0>Ulm?{7O diff --git a/Images/Disconnect.svg b/Images/Disconnect.svg new file mode 100644 index 0000000..34bc7c2 --- /dev/null +++ b/Images/Disconnect.svg @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Images/Disconnect.xaml b/Images/Disconnect.xaml new file mode 100644 index 0000000..66d8822 --- /dev/null +++ b/Images/Disconnect.xaml @@ -0,0 +1,203 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Model/IConnection.cs b/Model/IConnection.cs index a0967e8..394a721 100644 --- a/Model/IConnection.cs +++ b/Model/IConnection.cs @@ -2,6 +2,29 @@ namespace PettingZoo.Model { + public enum ConnectionStatus + { + Disconnected, + Connecting, + Connected, + Error + } + + + public class StatusChangedEventArgs : EventArgs + { + public ConnectionStatus Status { get; private set; } + public string Context { get; private set; } + + + public StatusChangedEventArgs(ConnectionStatus status, string context) + { + Status = status; + Context = context; + } + } + + public class MessageReceivedEventArgs : EventArgs { public MessageInfo MessageInfo { get; private set; } @@ -14,8 +37,10 @@ namespace PettingZoo.Model } + public interface IConnection : IDisposable { + event EventHandler StatusChanged; event EventHandler MessageReceived; } } diff --git a/Model/RabbitMQClientConnection.cs b/Model/RabbitMQClientConnection.cs index 2978baf..0b9c4b5 100644 --- a/Model/RabbitMQClientConnection.cs +++ b/Model/RabbitMQClientConnection.cs @@ -12,11 +12,14 @@ namespace PettingZoo.Model { public class RabbitMQClientConnection : IConnection { + private const int ConnectRetryDelay = 5000; + private readonly CancellationTokenSource connectionTaskToken; private RabbitMQ.Client.IConnection connection; private IModel model; + public event EventHandler StatusChanged; public event EventHandler MessageReceived; @@ -44,6 +47,9 @@ namespace PettingZoo.Model connection.Dispose(); connection = null; } + + StatusChanged = null; + MessageReceived = null; } @@ -58,18 +64,34 @@ namespace PettingZoo.Model Password = connectionInfo.Password }; - // ToDo exception handling - connection = factory.CreateConnection(); - model = connection.CreateModel(); + var statusContext = String.Format(@"{0}:{1}{2}", connectionInfo.Host, connectionInfo.Port, connectionInfo.VirtualHost); - var queueName = model.QueueDeclare().QueueName; - model.QueueBind(queueName, connectionInfo.Exchange, connectionInfo.RoutingKey); + while (!cancellationToken.IsCancellationRequested) + { + DoStatusChanged(ConnectionStatus.Connecting, statusContext); + try + { + connection = factory.CreateConnection(); + model = connection.CreateModel(); + + var queueName = model.QueueDeclare().QueueName; + model.QueueBind(queueName, connectionInfo.Exchange, connectionInfo.RoutingKey); - var consumer = new EventingBasicConsumer(model); - consumer.Received += ClientReceived; + var consumer = new EventingBasicConsumer(model); + consumer.Received += ClientReceived; - model.BasicConsume(queueName, true, consumer); + model.BasicConsume(queueName, true, consumer); + DoStatusChanged(ConnectionStatus.Connected, statusContext); + + break; + } + catch (Exception e) + { + DoStatusChanged(ConnectionStatus.Error, e.Message); + Task.Delay(ConnectRetryDelay, cancellationToken).Wait(cancellationToken); + } + } } @@ -90,6 +112,13 @@ namespace PettingZoo.Model } + private void DoStatusChanged(ConnectionStatus status, string context = null) + { + if (StatusChanged != null) + StatusChanged(this, new StatusChangedEventArgs(status, context)); + } + + private static Dictionary ConvertProperties(IBasicProperties basicProperties) { var properties = new Dictionary(); diff --git a/PettingZoo.csproj b/PettingZoo.csproj index cc251c1..ed207e9 100644 --- a/PettingZoo.csproj +++ b/PettingZoo.csproj @@ -92,6 +92,10 @@ MSBuild:Compile Designer + + Designer + MSBuild:Compile + Designer MSBuild:Compile @@ -177,10 +181,6 @@ false - - - - diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs index abbcb26..ba0c262 100644 --- a/Properties/Resources.Designer.cs +++ b/Properties/Resources.Designer.cs @@ -212,5 +212,41 @@ namespace PettingZoo.Properties { return ResourceManager.GetString("PropertyValue", resourceCulture); } } + + /// + /// Looks up a localized string similar to Connected. + /// + public static string StatusConnected { + get { + return ResourceManager.GetString("StatusConnected", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Connecting to {0}.... + /// + public static string StatusConnecting { + get { + return ResourceManager.GetString("StatusConnecting", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Disconnected. + /// + public static string StatusDisconnected { + get { + return ResourceManager.GetString("StatusDisconnected", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Error: {0}. + /// + public static string StatusError { + get { + return ResourceManager.GetString("StatusError", resourceCulture); + } + } } } diff --git a/Properties/Resources.resx b/Properties/Resources.resx index 4758649..e9c49e4 100644 --- a/Properties/Resources.resx +++ b/Properties/Resources.resx @@ -168,4 +168,16 @@ Value + + Connected + + + Connecting to {0}... + + + Disconnected + + + Error: {0} + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..bfe49bb --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# Petting Zoo +##### A RabbitMQ live message viewer + +ToDo: explain how it brings you coffee, fame and world peace. Or maybe just makes watching the messages flow slightly more comfortable. + + +#### Icons + +Icons are from the Interaction Assets pack by Madebyoliver + + +Designed by Freepik and distributed by Flaticon \ No newline at end of file diff --git a/View/ConnectionWindow.xaml b/View/ConnectionWindow.xaml index 29b3776..700f928 100644 --- a/View/ConnectionWindow.xaml +++ b/View/ConnectionWindow.xaml @@ -40,6 +40,7 @@