comparison Messaging/Server/EndPoints/MessageEndPoint.cs @ 31:7d9de5746f18

Working version
author adminsh@apollo
date Thu, 22 Mar 2012 08:09:41 +0000
parents 9919ee227c93
children
comparison
equal deleted inserted replaced
30:8d574f2d4174 31:7d9de5746f18
1 using System; 1 using System;
2 using System.Threading.Tasks; 2 using System.Threading.Tasks;
3 using Common.Messages; 3 using Common.Messages;
4 using GalaSoft.MvvmLight; 4 using GalaSoft.MvvmLight;
5 using GalaSoft.MvvmLight.Messaging; 5 using GalaSoft.MvvmLight.Messaging;
6 using Server.Interfaces; 6 using Server.Listeners;
7 7
8 namespace Server.EndPoints 8 namespace Server.EndPoints
9 { 9 {
10 public class MessageEndPoint<T, TV> : ObservableObject 10 public class MessageEndPoint<T, TY> : ObservableObject
11 where T : MessageBase 11 where T : ClientMessage
12 where TV : LogMessage 12 where TY : LogMessage
13
14 { 13 {
15 #region Fields 14 #region Fields
16 15
17 private readonly IListener _listener; 16 private readonly IListener _listener;
17 private readonly object _locker;
18 18
19 #endregion 19 #endregion
20 20
21 #region Constructor 21 #region Constructor
22 22
23 public MessageEndPoint(IListener listener) 23 public MessageEndPoint(IListener listener)
24 { 24 {
25 _listener = listener; 25 _listener = listener;
26 _locker = new object();
26 Init(); 27 Init();
27 } 28 }
28 29
29 #endregion 30 #endregion
30 31
32 #region Messaging
33
31 public void Init() 34 public void Init()
32 { 35 {
33 Messenger.Default.Register<T>(this, ClientMessageReceived); 36 Messenger.Default.Register<T>(this, ClientMessageReceived);
34 Messenger.Default.Register<TV>(this, LogMessageReceived); 37 Messenger.Default.Register<TY>(this, LogMessageReceived);
38 Messenger.Default.Register<OverrideSwitchMessage>(this, SwitchMessageReceived);
35 } 39 }
36 40
37 protected void LogMessageReceived(TV message) 41 private void SwitchMessageReceived(OverrideSwitchMessage message)
42 {
43 if (message.Switch == Switch.Off)
44 {
45 IsListening = false;
46 }
47 else
48 {
49 IsListening = true;
50 }
51 }
52
53 protected void LogMessageReceived(TY message)
38 { 54 {
39 this.DisplayLog = DisplayLog + Environment.NewLine + message.Body; 55 this.DisplayLog = DisplayLog + Environment.NewLine + message.Body;
40 } 56 }
41 57
42 protected void ClientMessageReceived(T message) 58 protected void ClientMessageReceived(T message)
43 { 59 {
44 this.DisplayCount++; 60 this.DisplayCount++;
45 } 61 }
62
63 #endregion
46 64
47 #region Properties 65 #region Properties
48 66
49 #region IsListening 67 #region IsListening
50 68
82 #endregion 100 #endregion
83 101
84 #region DisplayCount 102 #region DisplayCount
85 103
86 public const string DisplayCountPropertyName = "DisplayCount"; 104 public const string DisplayCountPropertyName = "DisplayCount";
87 private volatile int _displayCount; 105 private int _displayCount;
88 public int DisplayCount 106 public int DisplayCount
89 { 107 {
90 get { return _displayCount; } 108 get { return _displayCount; }
91 set 109 set
92 { 110 {
93 if (_displayCount == value) return; 111 lock (_locker)
94 _displayCount = value; 112 {
95 RaisePropertyChanged(() => DisplayCount); 113 if (_displayCount == value) return;
114 _displayCount = value;
115 RaisePropertyChanged(() => DisplayCount);
116 }
96 } 117 }
97 } 118 }
98 #endregion 119 #endregion
99 120
100 #region DisplayLog 121 #region DisplayLog
151 172
152 #endregion 173 #endregion
153 174
154 #region Commands 175 #region Commands
155 176
156
157 private void ToggleListener() 177 private void ToggleListener()
158 { 178 {
159 if (null == _listener) return; 179 if (null == _listener) return;
160 180
161 if (_listener.IsListening) 181 if (_listener.IsListening)
162 {
163 Task.Factory.StartNew(_listener.Stop); 182 Task.Factory.StartNew(_listener.Stop);
164 }
165 else 183 else
166 {
167 Task.Factory.StartNew(_listener.Start); 184 Task.Factory.StartNew(_listener.Start);
168 }
169 } 185 }
170 186
171 #endregion 187 #endregion
172 } 188 }
173 } 189 }