annotate Messaging/Server/EndPoints/MessageEndPoint.cs @ 29:9919ee227c93

msmq added
author adminsh@apollo
date Wed, 21 Mar 2012 22:09:18 +0000
parents
children 7d9de5746f18
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;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
6 using Server.Interfaces;
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 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
10 public class MessageEndPoint<T, TV> : ObservableObject
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
11 where T : MessageBase
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
12 where TV : LogMessage
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
13
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
14 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
15 #region Fields
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
16
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
17 private readonly IListener _listener;
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;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
26 Init();
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
27 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
28
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
29 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
30
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
31 public void Init()
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
32 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
33 Messenger.Default.Register<T>(this, ClientMessageReceived);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
34 Messenger.Default.Register<TV>(this, LogMessageReceived);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
35 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
36
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
37 protected void LogMessageReceived(TV message)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
38 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
39 this.DisplayLog = DisplayLog + Environment.NewLine + message.Body;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
40 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
41
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
42 protected void ClientMessageReceived(T message)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
43 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
44 this.DisplayCount++;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
45 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
46
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
47 #region Properties
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
48
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
49 #region IsListening
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
50
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
51 public const string IsListeningPropertyName = "IsListening";
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
52 private bool _isListening;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
53 public bool IsListening
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
54 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
55 get { return _isListening; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
56 set
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
57 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
58 if (_isListening == value) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
59 _isListening = value;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
60 RaisePropertyChanged(() => IsListening);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
61 ToggleListener();
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
62 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
63 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
64
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
65 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
66
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
67 #region DisplayText
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
68
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
69 public const string DisplayTextPropertyName = "DisplayText";
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
70 private string _displayText;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
71 public string DisplayText
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
72 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
73 get { return _displayText; }
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 (_displayText == value) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
77 _displayText = value;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
78 RaisePropertyChanged(() => DisplayText);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
79 }
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 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
83
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
84 #region DisplayCount
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
85
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
86 public const string DisplayCountPropertyName = "DisplayCount";
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
87 private volatile int _displayCount;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
88 public int DisplayCount
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
89 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
90 get { return _displayCount; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
91 set
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
92 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
93 if (_displayCount == value) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
94 _displayCount = value;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
95 RaisePropertyChanged(() => DisplayCount);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
96 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
97 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
98 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
99
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
100 #region DisplayLog
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
101
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
102 public const string DisplayLogPropertyName = "DisplayLog";
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
103 private string _displayLog = string.Empty;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
104 public string DisplayLog
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
105 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
106 get { return _displayLog; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
107 set
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
108 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
109 if (_displayLog == value) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
110 _displayLog = value;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
111 RaisePropertyChanged(() => DisplayLog);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
112 IsLogChanged = true;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
113 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
114 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
115
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
116 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
117
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
118 #region IsLogChanged
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
119
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
120 public const string IsLogChangedPropertyName = "IsLogChanged";
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
121 private bool _isLogChanged;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
122 public bool IsLogChanged
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
123 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
124 get { return _isLogChanged; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
125 set
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
126 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
127 if (_isLogChanged == value) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
128 _isLogChanged = value;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
129 RaisePropertyChanged(() => IsLogChanged);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
130 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
131 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
132
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
133 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
134
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
135 #region ToolTip
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
136
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
137 public const string ToolTipPropertyName = "ToolTip";
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
138 private string _toolTip = string.Empty;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
139 public string ToolTip
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
140 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
141 get { return _toolTip; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
142 set
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
143 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
144 if (_toolTip == value) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
145 _toolTip = value;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
146 RaisePropertyChanged(() => ToolTip);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
147 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
148 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
149
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
150 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
151
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
152 #endregion
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
153
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
154 #region Commands
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
155
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
156
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
157 private void ToggleListener()
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
158 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
159 if (null == _listener) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
160
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
161 if (_listener.IsListening)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
162 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
163 Task.Factory.StartNew(_listener.Stop);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
164 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
165 else
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
166 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
167 Task.Factory.StartNew(_listener.Start);
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 }