/
ConnectivityDetector.cs
302 lines (245 loc) · 9.03 KB
/
ConnectivityDetector.cs
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
using System;
using System.Collections;
using System.Configuration;
using System.Diagnostics;
using System.Threading;
using System.Net;
namespace Vs.Provider.RtpLib.MSR.LST.Net.Rtp
{
public class ConnectivityDetector
{
#region Statics
private static string ipAddress = "233.4.12.3";
private static ushort port = 5004;
private static int updateInterval = 10000;
static ConnectivityDetector()
{
string setting;
if((setting = ConfigurationManager.AppSettings[AppConfig.CD_IPAddress]) != null)
{
ipAddress = setting;
}
if((setting = ConfigurationManager.AppSettings[AppConfig.CD_Port]) != null)
{
port = ushort.Parse(setting);
}
if((setting = ConfigurationManager.AppSettings[AppConfig.CD_UpdateIntervalSeconds]) != null)
{
updateInterval = ushort.Parse(setting) * 1000;
}
}
#endregion Statics
#region Members
private RtpEL eventLog = new RtpEL(RtpEL.Source.ConnectivityDetector);
private string cName; // "ConnectivityDetector@" + HostName, set in constructor
private const string name = "CD";
private RtpParticipant rtpParticipant;
private RtpSession rtpSession;
private RtpSender rtpSender;
private bool networkReceive = false;
private bool networkSend = false;
private bool wellKnownHost = false;
private ArrayList wellKnownHosts = new ArrayList();
private Timer updateThread;
private object classLock = new object();
private bool disposed = false;
private DateTime lastSend = DateTime.Now;
#endregion
#region Constructors
public ConnectivityDetector()
{
try
{
cName = "ConnectivityDetector@" + HostName;
InitializeWellKnownHosts();
InitializeNetwork();
// Wait N seconds, and only call this once
updateThread = new Timer(new TimerCallback(UpdateThread), null, updateInterval, updateInterval);
}
catch (Exception e)
{
eventLog.WriteEntry(e.ToString(), EventLogEntryType.Error, 0);
}
}
public void Dispose()
{
lock(classLock)
{
try
{
disposed = true;
// Unhook events
RtpEvents.ReceiverReport -= new RtpEvents.ReceiverReportEventHandler(RtpReceiverReport);
RtpEvents.DuplicateCNameDetected -= new RtpEvents.DuplicateCNameDetectedEventHandler(DuplicateCNameDetected);
if(updateThread != null)
{
updateThread.Dispose();
updateThread = null;
}
if(rtpSession != null)
{
rtpSession.Dispose();
rtpSession = null;
rtpSender = null;
}
}
catch(Exception e)
{
eventLog.WriteEntry("Error disposing ConnectivityDetector. \n" + e.ToString(),
EventLogEntryType.Error, 0);
}
}
}
#endregion
#region Public
public string MulticastIPAddress
{
get{return ipAddress;}
}
public int MulticastPort
{
get{return port;}
}
public string LocalMulticastInterface
{
get{return MSR.LST.Net.Utility.GetLocalMulticastInterface().ToString();}
}
public string HostName
{
get{return Dns.GetHostEntry(Dns.GetHostName()).HostName;}
}
#endregion
#region Private
private void InitializeWellKnownHosts()
{
try
{
wellKnownHosts.Add("ConnectivityDetector@venues.conferencexp.net");
wellKnownHosts.Add("ConnectivityDetector@disc1.cs.washington.edu");
wellKnownHosts.Add("ConnectivityDetector@holodeck.eecs.berkeley.edu");
}
catch (Exception e)
{
eventLog.WriteEntry(e.ToString(), EventLogEntryType.Error, (int)RtpEL.ID.Error);
}
}
private void InitializeNetwork()
{
RtpEvents.ReceiverReport += new RtpEvents.ReceiverReportEventHandler(RtpReceiverReport);
RtpEvents.DuplicateCNameDetected += new RtpEvents.DuplicateCNameDetectedEventHandler(DuplicateCNameDetected);
// Create participant
rtpParticipant = new RtpParticipant(cName, name);
rtpParticipant.SetTool(true);
// Create session with Participant and Rtp data
rtpSession = new RtpSession(new IPEndPoint(IPAddress.Parse(ipAddress), port), rtpParticipant, true, true);
// Create RtpSender
rtpSender = rtpSession.CreateRtpSender(HostName, PayloadType.PipecleanerSignal, null, null);
}
private void UpdateThread(object state)
{
lock(classLock)
{
if(!disposed)
{
try
{
CheckForOthers();
CheckForI2();
RaiseConnectivityEvent();
ClearFlags();
SendRtpData();
}
catch (Exception e)
{
eventLog.WriteEntry(e.ToString(),EventLogEntryType.Error, (int)RtpEL.ID.Error);
}
}
}
}
private void RtpReceiverReport(object sender, RtpEvents.ReceiverReportEventArgs ea)
{
// We'll take a receiver report from any RtpSession
if(ea.SenderCName != cName)
{
if (ea.SourceCName == cName)
{
networkSend = true;
}
}
}
private void DuplicateCNameDetected (object sender, RtpEvents.DuplicateCNameDetectedEventArgs ea)
{
if (sender == rtpSession)
{
string msg = string.Format("A duplicate CNAME was detected between {0} and {1}. Connectivity testing terminating.",
ea.IPAddresses[0].ToString(), ea.IPAddresses[1].ToString());
eventLog.WriteEntry(msg, EventLogEntryType.Error, 0);
System.Windows.Forms.MessageBox.Show(msg);
Dispose();
}
}
private void CheckForOthers()
{
if (rtpSession.Participants.Count > 1)
{
networkReceive = true;
}
}
private void CheckForI2()
{
// Look for well-known hosts (Internet2 hosts)
foreach(string wkh in wellKnownHosts)
{
if(rtpSession.Participants.ContainsKey(wkh))
{
wellKnownHost = true;
break;
}
}
}
private string Details()
{
string details = "ConferenceXP Nodes Detected:\n";
foreach (RtpParticipant p in new ArrayList(rtpSession.Participants.Values))
{
details += string.Format(" {0}, {1}\n", p.CName, p.Tool);
}
return details;
}
private void RaiseConnectivityEvent()
{
try
{
if (Connectivity != null)
{
Connectivity(networkReceive, networkSend, wellKnownHost, Details());
}
}
catch (Exception e)
{
eventLog.WriteEntry(e.ToString(), EventLogEntryType.Error, 0);
}
}
private void ClearFlags()
{
networkReceive = false;
networkSend = false;
wellKnownHost = false;
}
private void SendRtpData()
{
// Only send data once per minute
if(DateTime.Now.Subtract(lastSend).Minutes >= 1)
{
rtpSender.Send((BufferChunk)name);
lastSend = DateTime.Now;
}
}
#endregion
#region Events
public delegate void ConnectivityEventHandler(bool networkReceiveDetected,
bool networkSendDetected, bool wellKnownHostDetected, string details);
public event ConnectivityEventHandler Connectivity;
#endregion
}
}