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
|
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
namespace IceInternal
{
using System.Diagnostics;
using System.Collections.Generic;
public sealed class ACMConfig : System.ICloneable
{
internal ACMConfig(bool server)
{
timeout = 60 * 1000;
heartbeat = Ice.ACMHeartbeat.HeartbeatOnInvocation;
close = server ? Ice.ACMClose.CloseOnInvocation : Ice.ACMClose.CloseOnInvocationAndIdle;
}
public ACMConfig(Ice.Properties p, Ice.Logger l, string prefix, ACMConfig dflt)
{
Debug.Assert(prefix != null);
string timeoutProperty;
if((prefix.Equals("Ice.ACM.Client") || prefix.Equals("Ice.ACM.Server")) &&
p.getProperty(prefix + ".Timeout").Length == 0)
{
timeoutProperty = prefix; // Deprecated property.
}
else
{
timeoutProperty = prefix + ".Timeout";
};
timeout = p.getPropertyAsIntWithDefault(timeoutProperty, dflt.timeout / 1000) * 1000;
int hb = p.getPropertyAsIntWithDefault(prefix + ".Heartbeat", (int)dflt.heartbeat);
if(hb >= (int)Ice.ACMHeartbeat.HeartbeatOff && hb <= (int)Ice.ACMHeartbeat.HeartbeatAlways)
{
heartbeat = (Ice.ACMHeartbeat)hb;
}
else
{
l.warning("invalid value for property `" + prefix + ".Heartbeat" +
"', default value will be used instead");
heartbeat = dflt.heartbeat;
}
int cl = p.getPropertyAsIntWithDefault(prefix + ".Close", (int)dflt.close);
if(cl >= (int)Ice.ACMClose.CloseOff && cl <= (int)Ice.ACMClose.CloseOnIdleForceful)
{
close = (Ice.ACMClose)cl;
}
else
{
l.warning("invalid value for property `" + prefix + ".Close" +
"', default value will be used instead");
close = dflt.close;
}
}
public object Clone()
{
return this.MemberwiseClone();
}
public int timeout;
public Ice.ACMHeartbeat heartbeat;
public Ice.ACMClose close;
};
public interface ACMMonitor : TimerTask
{
void add(Ice.ConnectionI con);
void remove(Ice.ConnectionI con);
void reap(Ice.ConnectionI con);
ACMMonitor acm(Ice.Optional<int> timeout, Ice.Optional<Ice.ACMClose> c, Ice.Optional<Ice.ACMHeartbeat> h);
Ice.ACM getACM();
};
class FactoryACMMonitor : ACMMonitor
{
internal class Change
{
internal Change(Ice.ConnectionI connection, bool remove)
{
this.connection = connection;
this.remove = remove;
}
public readonly Ice.ConnectionI connection;
public readonly bool remove;
};
internal FactoryACMMonitor(Instance instance, ACMConfig config)
{
_instance = instance;
_config = config;
}
internal void destroy()
{
lock(this)
{
if(_instance == null)
{
return;
}
_instance = null;
_connections.Clear();
_changes.Clear();
}
}
public void add(Ice.ConnectionI connection)
{
if(_config.timeout == 0)
{
return;
}
lock(this)
{
if(_connections.Count == 0)
{
_connections.Add(connection);
_instance.timer().scheduleRepeated(this, _config.timeout / 2);
}
else
{
_changes.Add(new Change(connection, false));
}
}
}
public void remove(Ice.ConnectionI connection)
{
if(_config.timeout == 0)
{
return;
}
lock(this)
{
Debug.Assert(_instance != null);
_changes.Add(new Change(connection, true));
}
}
public void reap(Ice.ConnectionI connection)
{
lock(this)
{
_reapedConnections.Add(connection);
}
}
public ACMMonitor acm(Ice.Optional<int> timeout, Ice.Optional<Ice.ACMClose> c, Ice.Optional<Ice.ACMHeartbeat> h)
{
Debug.Assert(_instance != null);
ACMConfig config = (ACMConfig)_config.Clone();
if(timeout.HasValue)
{
config.timeout = timeout.Value * 1000; // To milliseconds
}
if(c.HasValue)
{
config.close = c.Value;
}
if(h.HasValue)
{
config.heartbeat = h.Value;
}
return new ConnectionACMMonitor(this, _instance.timer(), config);
}
public Ice.ACM getACM()
{
Ice.ACM acm = new Ice.ACM();
acm.timeout = _config.timeout / 1000;
acm.close = _config.close;
acm.heartbeat = _config.heartbeat;
return acm;
}
internal List<Ice.ConnectionI> swapReapedConnections()
{
lock(this)
{
if(_reapedConnections.Count == 0)
{
return null;
}
List<Ice.ConnectionI> connections = _reapedConnections;
_reapedConnections = new List<Ice.ConnectionI>();
return connections;
}
}
public void runTimerTask()
{
lock(this)
{
if(_instance == null)
{
return;
}
foreach(Change change in _changes)
{
if(change.remove)
{
_connections.Remove(change.connection);
}
else
{
_connections.Add(change.connection);
}
}
_changes.Clear();
if(_connections.Count == 0)
{
_instance.timer().cancel(this);
return;
}
}
//
// Monitor connections outside the thread synchronization, so
// that connections can be added or removed during monitoring.
//
long now = Time.currentMonotonicTimeMillis();
foreach(Ice.ConnectionI connection in _connections)
{
try
{
connection.monitor(now, _config);
}
catch(System.Exception ex)
{
handleException(ex);
}
}
}
internal void handleException(System.Exception ex)
{
lock(this)
{
if(_instance == null)
{
return;
}
_instance.initializationData().logger.error("exception in connection monitor:\n" + ex);
}
}
private Instance _instance;
readonly private ACMConfig _config;
private HashSet<Ice.ConnectionI> _connections = new HashSet<Ice.ConnectionI>();
private List<Change> _changes = new List<Change>();
private List<Ice.ConnectionI> _reapedConnections = new List<Ice.ConnectionI>();
};
internal class ConnectionACMMonitor : ACMMonitor
{
internal ConnectionACMMonitor(FactoryACMMonitor parent, Timer timer, ACMConfig config)
{
_parent = parent;
_timer = timer;
_config = config;
}
public void add(Ice.ConnectionI connection)
{
lock(this)
{
Debug.Assert(_connection == null);
_connection = connection;
if(_config.timeout > 0)
{
_timer.scheduleRepeated(this, _config.timeout / 2);
}
}
}
public void remove(Ice.ConnectionI connection)
{
lock(this)
{
Debug.Assert(_connection == connection);
_connection = null;
if(_config.timeout > 0)
{
_timer.cancel(this);
}
}
}
public void reap(Ice.ConnectionI connection)
{
_parent.reap(connection);
}
public ACMMonitor acm(Ice.Optional<int> timeout, Ice.Optional<Ice.ACMClose> c, Ice.Optional<Ice.ACMHeartbeat> h)
{
return _parent.acm(timeout, c, h);
}
public Ice.ACM getACM()
{
Ice.ACM acm = new Ice.ACM();
acm.timeout = _config.timeout / 1000;
acm.close = _config.close;
acm.heartbeat = _config.heartbeat;
return acm;
}
public void runTimerTask()
{
Ice.ConnectionI connection;
lock(this)
{
if(_connection == null)
{
return;
}
connection = _connection;
}
try
{
connection.monitor(Time.currentMonotonicTimeMillis(), _config);
}
catch(System.Exception ex)
{
_parent.handleException(ex);
}
}
readonly private FactoryACMMonitor _parent;
readonly private Timer _timer;
readonly private ACMConfig _config;
private Ice.ConnectionI _connection;
};
}
|