annotate Messaging/Server/EndPoints/MessageEndPoint.cs @ 80:62889c14148e

clear rubbish
author Steven Hollidge <stevenhollidge@hotmail.com>
date Mon, 23 Apr 2012 23:07:33 +0100
parents 7d9de5746f18
children
rev   line source
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
1 using System;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
2 using System.Threading.Tasks;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
3 using Common.Messages;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
4 using GalaSoft.MvvmLight;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
5 using GalaSoft.MvvmLight.Messaging;
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
6 using Server.Listeners;
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
7
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
8 namespace Server.EndPoints
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
9 {
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
10 public class MessageEndPoint<T, TY> : ObservableObject
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
11 where T : ClientMessage
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
12 where TY : LogMessage
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
13 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
14 #region Fields
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
15
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
16 private readonly IListener _listener;
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
17 private readonly object _locker;
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
18
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
19 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
20
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
21 #region Constructor
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
22
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
23 public MessageEndPoint(IListener listener)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
24 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
25 _listener = listener;
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
26 _locker = new object();
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
27 Init();
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
28 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
30 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
31
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
32 #region Messaging
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
33
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
34 public void Init()
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
35 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
36 Messenger.Default.Register<T>(this, ClientMessageReceived);
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
37 Messenger.Default.Register<TY>(this, LogMessageReceived);
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
38 Messenger.Default.Register<OverrideSwitchMessage>(this, SwitchMessageReceived);
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
39 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
40
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
41 private void SwitchMessageReceived(OverrideSwitchMessage message)
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
42 {
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
43 if (message.Switch == Switch.Off)
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
44 {
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
45 IsListening = false;
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
46 }
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
47 else
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
48 {
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
49 IsListening = true;
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
50 }
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
51 }
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
52
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
53 protected void LogMessageReceived(TY message)
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
54 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
55 this.DisplayLog = DisplayLog + Environment.NewLine + message.Body;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
56 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
57
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
58 protected void ClientMessageReceived(T message)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
59 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
60 this.DisplayCount++;
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
61 }
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
62
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
63 #endregion
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
64
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
65 #region Properties
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
66
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
67 #region IsListening
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
68
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
69 public const string IsListeningPropertyName = "IsListening";
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
70 private bool _isListening;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
71 public bool IsListening
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
72 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
73 get { return _isListening; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
74 set
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
75 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
76 if (_isListening == value) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
77 _isListening = value;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
78 RaisePropertyChanged(() => IsListening);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
79 ToggleListener();
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
80 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
81 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
82
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
83 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
84
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
85 #region DisplayText
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
86
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
87 public const string DisplayTextPropertyName = "DisplayText";
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
88 private string _displayText;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
89 public string DisplayText
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
90 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
91 get { return _displayText; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
92 set
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
93 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
94 if (_displayText == value) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
95 _displayText = value;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
96 RaisePropertyChanged(() => DisplayText);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
97 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
98 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
99
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
100 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
101
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
102 #region DisplayCount
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
103
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
104 public const string DisplayCountPropertyName = "DisplayCount";
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
105 private int _displayCount;
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
106 public int DisplayCount
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
107 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
108 get { return _displayCount; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
109 set
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
110 {
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
111 lock (_locker)
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
112 {
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
113 if (_displayCount == value) return;
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
114 _displayCount = value;
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
115 RaisePropertyChanged(() => DisplayCount);
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
116 }
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
117 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
118 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
119 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
120
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
121 #region DisplayLog
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
122
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
123 public const string DisplayLogPropertyName = "DisplayLog";
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
124 private string _displayLog = string.Empty;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
125 public string DisplayLog
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
126 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
127 get { return _displayLog; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
128 set
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
129 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
130 if (_displayLog == value) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
131 _displayLog = value;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
132 RaisePropertyChanged(() => DisplayLog);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
133 IsLogChanged = true;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
134 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
135 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
136
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
137 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
138
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
139 #region IsLogChanged
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
140
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
141 public const string IsLogChangedPropertyName = "IsLogChanged";
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
142 private bool _isLogChanged;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
143 public bool IsLogChanged
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
144 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
145 get { return _isLogChanged; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
146 set
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
147 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
148 if (_isLogChanged == value) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
149 _isLogChanged = value;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
150 RaisePropertyChanged(() => IsLogChanged);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
151 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
152 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
153
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
154 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
155
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
156 #region ToolTip
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
157
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
158 public const string ToolTipPropertyName = "ToolTip";
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
159 private string _toolTip = string.Empty;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
160 public string ToolTip
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
161 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
162 get { return _toolTip; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
163 set
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
164 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
165 if (_toolTip == value) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
166 _toolTip = value;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
167 RaisePropertyChanged(() => ToolTip);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
168 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
169 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
170
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
171 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
172
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
173 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
174
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
175 #region Commands
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
176
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
177 private void ToggleListener()
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
178 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
179 if (null == _listener) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
180
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
181 if (_listener.IsListening)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
182 Task.Factory.StartNew(_listener.Stop);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
183 else
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
184 Task.Factory.StartNew(_listener.Start);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
185 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
186
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
187 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
188 }
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
189 }