-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWatchdog.cs
372 lines (355 loc) · 15 KB
/
Watchdog.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//___________________________________________________________________________________
//
// Copyright (C) 2020, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GITTER: https://gitter.im/mpostol/OPC-UA-OOI
//___________________________________________________________________________________
using CAS.CommServer.ProtocolHub.Communication.Properties;
using System;
using System.Diagnostics;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using UAOOI.ProcessObserver.RealTime.Processes;
namespace CAS.Lib.RTLib.Processes
{
/// <summary>
/// Watchdog implementation - To apply deadline the class method call add this attribute
/// and inherit from <see cref=" ContextBoundObject"/>
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class WatchdogAttribute : Attribute, IContextAttribute
{
#region private
private string m_ObjectName;
private readonly TimeSpan m_deadline;
private string GetName(IConstructionCallMessage ctorMsg, string defaultName)
{
string id = "";
if (ctorMsg.ArgCount > 0)
for (int ix = 0; ix < ctorMsg.ArgCount; ix++)
if (ctorMsg.GetInArg(ix) is string)
id += (string)ctorMsg.GetInArg(ix) + " ";
if (id.Length < 2)
id = defaultName;
return id;
}
#endregion
#region ContextAttribute
/// <summary>
/// Returns a Boolean value indicating whether the context parameter meets the context attribute's requirements.
/// </summary>
/// <param name="ctx">The context in which to check.</param>
/// <param name="ctorMsg">The <see cref="T:System.Runtime.Remoting.Activation.IConstructionCallMessage"></see> to which to add the context property.</param>
/// <returns>
/// true if the passed in context is okay; otherwise, false.
/// </returns>
/// <exception cref="T:System.ArgumentNullException">Either ctx or ctorMsg is null. </exception>
/// <PermissionSet><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure"/></PermissionSet>
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
public bool IsContextOK(Context ctx, IConstructionCallMessage ctorMsg)
{
// Force new context.
return false;
}
/// <summary>
/// Adds the current context property to the given message.
/// </summary>
/// <param name="ctorMsg">The <see cref="T:System.Runtime.Remoting.Activation.IConstructionCallMessage"></see>
/// to which to add the context property.
/// </param>
/// <exception cref="T:System.ArgumentNullException">The ctorMsg parameter is null. </exception>
/// <PermissionSet><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure"/>
/// </PermissionSet>
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
public void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
{
m_ObjectName = GetName(ctorMsg, m_ObjectName);
ctorMsg.ContextProperties.Add(new WatchdogProperty(m_ObjectName, m_deadline));
}
#endregion
#region constructor
/// <summary>
/// Initializes a new instance of the <see cref="WatchdogAttribute"/> class.
/// </summary>
public WatchdogAttribute() : this("WatchdogAttribute", 5 * 60) { }
/// <summary>
/// Initializes a new instance of the <see cref="WatchdogAttribute"/> class.
/// </summary>
/// <param name="objectName">Name of the object.</param>
/// <param name="deadlineInS">The deadline in S.</param>
public WatchdogAttribute(string objectName, int deadlineInS)
{
m_ObjectName = objectName;
m_deadline = new TimeSpan(0, 0, deadlineInS);
}
#endregion
}
/// <summary>
/// Implementation of the interface for a message sink.
/// </summary>
public class WatchdogSink : IMessageSink
{
#region private
private IMessageSink m_Next;
private WatchdogProperty m_Property; //obiekt na rzecz kt¡rego nale¬y wywo-aŠ metodà
#endregion
#region IMessageSink Members
/// <summary>
/// Asynchronously processes the given message.
/// </summary>
/// <param name="msg">The message to process.</param>
/// <param name="replySink">The reply sink for the reply message.</param>
/// <returns>
/// Returns an <see cref="T:System.Runtime.Remoting.Messaging.IMessageCtrl"></see> interface that provides a way to control asynchronous messages after they have been dispatched.
/// </returns>
/// <exception cref="T:System.Security.SecurityException">The immediate caller makes the call through a reference to the interface and does not have infrastructure permission. </exception>
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
IMessageCtrl IMessageSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
return m_Next.AsyncProcessMessage(msg, replySink);
}
/// <summary>
/// Gets the next message sink in the sink chain.
/// </summary>
/// <value></value>
/// <returns>The next message sink in the sink chain.</returns>
/// <exception cref="T:System.Security.SecurityException">The immediate caller makes the call through a reference to the interface and does not have infrastructure permission. </exception>
/// <PermissionSet><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure"/></PermissionSet>
//[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
IMessageSink IMessageSink.NextSink { get { return m_Next; } }
/// <summary>
/// Synchronously processes the given message.
/// </summary>
/// <param name="msg">The message to process.</param>
/// <returns>
/// A reply message in response to the request.
/// </returns>
/// <exception cref="T:System.Security.SecurityException">The immediate caller makes the call through a reference to the interface and does not have infrastructure permission. </exception>
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
IMessage IMessageSink.SyncProcessMessage(IMessage msg)
{
IMethodMessage methodMessage = (IMethodMessage)msg;
m_Property.EnterWatchdog(new MethodCall(msg));
IMessage retMsg = m_Next.SyncProcessMessage(msg);
m_Property.ExitWatchdog();
return retMsg;
}
#endregion
#region constructor
/// <summary>
/// Initializes a new instance of the <see cref="WatchdogSink"/> class.
/// </summary>
/// <param name="ims">The message sink.</param>
/// <param name="property">The property <see cref="WatchdogProperty"/> owner of this object.</param>
internal WatchdogSink(IMessageSink ims, WatchdogProperty property)
{
m_Next = ims;
m_Property = property;
}
#endregion
}
/// <summary>
/// Watchdog property to be added to the guarded by the Watchdog object
/// </summary>
public class WatchdogProperty : IContextProperty, IContributeObjectSink
{
#region private
private string MethodCallMessageToString(IMethodCallMessage msg)
{
if (msg != null)
{
StringBuilder mDescription = new StringBuilder();
if (msg.MethodName != null)
{
mDescription.AppendFormat("Method = {0}(", msg.MethodName);
}
for (int ix = 0; ix < msg.ArgCount; ix++)
{
mDescription.AppendFormat(" {0}=", msg.GetArgName(ix));
string val = "null";
if (msg.GetArg(ix) != null)
val = msg.GetArg(ix).ToString();
mDescription.AppendFormat(" {0} ;", val);
}
mDescription.Append(")");
return mDescription.ToString();
}
else
return "Null message";
}
private const string PropName = "WatchdogProperty";
private string m_ObjectName;
private readonly TimeSpan m_deadline;
private TimeSpan m_maxDelay = new TimeSpan();
private IMethodCallMessage m_lastCall;
private System.Diagnostics.Stopwatch stopwatch;
private System.Threading.Timer timer;
private int InsideWatchdogCoutner = 0;
private void MaxDelayMessage(string SourceName)
{
if (stopwatch.Elapsed > m_maxDelay)
{
m_maxDelay = stopwatch.Elapsed;
string message = String.Format(Resources.MaxDelayMessageFormat, m_maxDelay.TotalMilliseconds.ToString(), m_ObjectName, SourceName + ":" + MethodCallMessageToString(m_lastCall));
AssemblyTraceEvent.Trace(TraceEventType.Information, 267, "WatchdogProperty", message);
}
}
private void Pendulum(object state)
{
if (!Monitor.TryEnter(this)) // instead of lock (this)
return; // we are exiting, because the lock is already acquired (this is not the problem because it will be soon launched again by the Timer)
try
{
if (stopwatch.Elapsed > m_deadline)
{
MaxDelayMessage("Pendulum");
stopwatch.Reset();
string message = String.Format(Resources.RestartMessageFormat, m_ObjectName, MethodCallMessageToString(m_lastCall));
AssemblyTraceEvent.Trace(TraceEventType.Critical, 162, "WatchdogProperty", message);
#if DEBUG
MarkRestart(message);
//NUnit.Framework.Assert.Fail
//( "I am about to reboot the system, but reboot is now switched off because of debug mode", "Processes.MonitoredThread" );
#else
CAS.Lib.RTLib.Processes.Manager.ForceReboot();
#endif
}
}
finally
{
Monitor.Exit(this);
}
}
#endregion
#region IContributeObjectSink Members
/// <summary>
/// Chains the message sink of the provided server object in front of the given sink chain.
/// </summary>
/// <param name="obj">The server object which provides the message sink that is to be chained in front of the given chain.</param>
/// <param name="nextSink">The chain of sinks composed so far.</param>
/// <returns>The composite sink chain.</returns>
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
IMessageSink IContributeObjectSink.GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)
{
return new WatchdogSink(nextSink, this);
}
#endregion
#region IContextProperty Members
/// <summary>
/// Called when the context is frozen.
/// </summary>
/// <param name="newContext">The context to freeze.</param>
/// <PermissionSet><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure"/></PermissionSet>
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
void IContextProperty.Freeze(Context newContext) { }
/// <summary>
/// Returns a Boolean value indicating whether the context property is compatible with the new context.
/// </summary>
/// <param name="newCtx">The new context in which the <see cref="T:System.Runtime.Remoting.Contexts.ContextProperty"></see> has been created.</param>
/// <returns>
/// true if the context property can coexist with the other context properties in the given context; otherwise, false.
/// </returns>
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
bool IContextProperty.IsNewContextOK(Context newCtx)
{
// We could also inspect the other properties for the context to make sure none conflict,
// but for this, we just find out if the new context has a WatchdogProperty property. If not, reject it
WatchdogProperty newContextLogProperty = newCtx.GetProperty(PropName) as WatchdogProperty;
if (newContextLogProperty == null)
{
Debug.Assert(false);
return false;
}
return true;
}
/// <summary>
/// Gets the name of the property under which it will be added to the context.
/// </summary>
/// <value></value>
/// <returns>The name of the property.</returns>
/// <PermissionSet><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure"/></PermissionSet>
//[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
string IContextProperty.Name
{
get
{
return (PropName);
}
}
#endregion
#region public
/// <summary>
/// Enters the guarded by the watchdog object.
/// </summary>
/// <param name="lastCall">The call message.</param>
public void EnterWatchdog(IMethodCallMessage lastCall)
{
lock (this)
{
if (stopwatch.Elapsed > TimeSpan.Zero)
{
string message = String.Format(Resources.WatchdogStopwatchIsNotZeroMessageFormat, stopwatch.Elapsed.Milliseconds);
AssemblyTraceEvent.Trace(TraceEventType.Warning, 286, "WatchdogProperty", message);
}
if (InsideWatchdogCoutner != 0)
{
string message = String.Format(Resources.InsideWatchdogMessageFormat, InsideWatchdogCoutner);
AssemblyTraceEvent.Trace(TraceEventType.Warning, 291, "WatchdogProperty", message);
}
#if DEBUG
string meth = MethodCallMessageToString(lastCall);
#endif
m_lastCall = lastCall;
stopwatch.Start();
InsideWatchdogCoutner++;
}
}
/// <summary>
/// Exits the guarded by the watchdog object.
/// </summary>
public void ExitWatchdog()
{
lock (this)
{
MaxDelayMessage("ExitWatchdog");
stopwatch.Reset();
InsideWatchdogCoutner--;
}
}
#endregion
#region constructor
/// <summary>
/// Initializes a new instance of the <see cref="WatchdogProperty"/> class.
/// </summary>
/// <param name="objectName">Name of the object.</param>
/// <param name="deadline">The deadline.</param>
public WatchdogProperty(string objectName, TimeSpan deadline)
{
stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Reset();
timer = new System.Threading.Timer(new TimerCallback(Pendulum), null, 0, 1000);
m_ObjectName = objectName;
m_deadline = deadline;
}
#endregion
#if DEBUG
/// <summary>
/// counts the number of times when restart should occurs
/// </summary>
public static byte count = 0;
/// <summary>
/// Marks the restart.
/// </summary>
[Conditional("DEBUG")]
private static void MarkRestart(string message)
{
Console.WriteLine(message);
count++;
}
#endif
}
}