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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
// **********************************************************************
//
// Copyright (c) 2003-2010 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.
//
// **********************************************************************
#include <Glacier2/RequestQueue.h>
#include <Glacier2/Instance.h>
#include <Glacier2/SessionRouterI.h>
#include <set>
using namespace std;
using namespace Ice;
using namespace Glacier2;
namespace
{
//
// AMI base callback class for twoway/oneway requests
//
class IceInvokeI : public AMI_Array_Object_ice_invoke
{
public:
IceInvokeI(const AMD_Object_ice_invokePtr& amdCB, const InstancePtr& instance, const ConnectionPtr& connection) :
_amdCB(amdCB),
_instance(instance),
_connection(connection)
{
}
virtual void
ice_exception(const Exception& ex)
{
//
// If the connection has been lost, destroy the session.
//
if(_connection)
{
if(dynamic_cast<const Ice::SocketException*>(&ex) ||
dynamic_cast<const Ice::TimeoutException*>(&ex) ||
dynamic_cast<const Ice::ProtocolException*>(&ex))
{
try
{
_instance->sessionRouter()->destroySession(_connection);
}
catch(const Exception&)
{
}
}
}
if(_amdCB)
{
_amdCB->ice_exception(ex);
}
}
protected:
const AMD_Object_ice_invokePtr _amdCB;
const InstancePtr _instance;
const ConnectionPtr _connection;
};
class TwowayIceInvokeI : public IceInvokeI
{
public:
TwowayIceInvokeI(const AMD_Object_ice_invokePtr& amdCB, const InstancePtr& instance, const ConnectionPtr& con) :
IceInvokeI(amdCB, instance, con)
{
}
virtual void
ice_response(bool ok, const pair<const Byte*, const Byte*>& outParams)
{
_amdCB->ice_response(ok, outParams);
}
};
class OnewayIceInvokeI : public IceInvokeI, public Ice::AMISentCallback
{
public:
OnewayIceInvokeI(const AMD_Object_ice_invokePtr& amdCB, const InstancePtr& instance, const ConnectionPtr& con) :
IceInvokeI(amdCB, instance, con)
{
}
virtual void
ice_response(bool ok, const pair<const Byte*, const Byte*>& outParams)
{
assert(false);
}
virtual void
ice_sent()
{
_amdCB->ice_response(true, pair<const Byte*, const Byte*>(0, 0));
}
};
}
Glacier2::Request::Request(const ObjectPrx& proxy, const std::pair<const Byte*, const Byte*>& inParams,
const Current& current, bool forwardContext, const Ice::Context& sslContext,
const AMD_Object_ice_invokePtr& amdCB) :
_proxy(proxy),
_inParams(inParams.first, inParams.second),
_current(current),
_forwardContext(forwardContext),
_sslContext(sslContext),
_amdCB(amdCB)
{
//
// If this is a batch call, we can finish the AMD call right away.
//
if(_proxy->ice_isBatchOneway() || _proxy->ice_isBatchDatagram())
{
_amdCB->ice_response(true, pair<const Byte*, const Byte*>(0, 0));
}
Context::const_iterator p = current.ctx.find("_ovrd");
if(p != current.ctx.end())
{
const_cast<string&>(_override) = p->second;
}
}
bool
Glacier2::Request::invoke(const InstancePtr& instance, const Ice::ConnectionPtr& connection)
{
pair<const Byte*, const Byte*> inPair;
if(_inParams.size() == 0)
{
inPair.first = inPair.second = 0;
}
else
{
inPair.first = &_inParams[0];
inPair.second = inPair.first + _inParams.size();
}
if(_proxy->ice_isBatchOneway() || _proxy->ice_isBatchDatagram())
{
ByteSeq outParams;
if(_forwardContext)
{
if(_sslContext.size() > 0)
{
Ice::Context ctx = _current.ctx;
ctx.insert(_sslContext.begin(), _sslContext.end());
_proxy->ice_invoke(_current.operation, _current.mode, inPair, outParams, ctx);
}
else
{
_proxy->ice_invoke(_current.operation, _current.mode, inPair, outParams, _current.ctx);
}
}
else
{
if(_sslContext.size() > 0)
{
_proxy->ice_invoke(_current.operation, _current.mode, inPair, outParams, _sslContext);
}
else
{
_proxy->ice_invoke(_current.operation, _current.mode, inPair, outParams);
}
}
return true; // Batch invocation.
}
else
{
AMI_Array_Object_ice_invokePtr amiCB;
if(_proxy->ice_isTwoway())
{
amiCB = new TwowayIceInvokeI(_amdCB, instance, connection);
}
else
{
amiCB = new OnewayIceInvokeI(_amdCB, instance, connection);
}
bool sent;
if(_forwardContext)
{
if(_sslContext.size() > 0)
{
Ice::Context ctx = _current.ctx;
ctx.insert(_sslContext.begin(), _sslContext.end());
sent = _proxy->ice_invoke_async(amiCB, _current.operation, _current.mode, inPair, ctx);
}
else
{
sent = _proxy->ice_invoke_async(amiCB, _current.operation, _current.mode, inPair, _current.ctx);
}
}
else
{
if(_sslContext.size() > 0)
{
sent = _proxy->ice_invoke_async(amiCB, _current.operation, _current.mode, inPair, _sslContext);
}
else
{
sent = _proxy->ice_invoke_async(amiCB, _current.operation, _current.mode, inPair);
}
}
if(sent && !_proxy->ice_isTwoway())
{
_amdCB->ice_response(true, pair<const Byte*, const Byte*>(0, 0));
}
return false; // Not a batch invocation.
}
}
bool
Glacier2::Request::override(const RequestPtr& other) const
{
//
// Both override values have to be non-empty.
//
if(_override.empty() || other->_override.empty())
{
return false;
}
//
// Override does not work for twoways, because a response is
// expected for each request.
//
if(_proxy->ice_isTwoway() || other->_proxy->ice_isTwoway())
{
return false;
}
//
// We cannot override if the proxies differ.
//
if(_proxy != other->_proxy)
{
return false;
}
return _override == other->_override;
}
Glacier2::RequestQueue::RequestQueue(const RequestQueueThreadPtr& requestQueueThread,
const InstancePtr& instance,
const Ice::ConnectionPtr& connection) :
_requestQueueThread(requestQueueThread),
_instance(instance),
_connection(connection)
{
}
bool
Glacier2::RequestQueue::addRequest(const RequestPtr& request)
{
IceUtil::Mutex::Lock lock(*this);
if(request->hasOverride())
{
for(vector<RequestPtr>::iterator p = _requests.begin(); p != _requests.end(); ++p)
{
//
// If the new request overrides an old one, then abort the old
// request and replace it with the new request.
//
if(request->override(*p))
{
*p = request;
return true;
}
}
}
//
// No override, we add the new request.
//
if(_requests.empty())
{
_requestQueueThread->flushRequestQueue(this); // This might throw if the thread is destroyed.
}
_requests.push_back(request);
return false;
}
void
Glacier2::RequestQueue::flushRequests(set<Ice::ObjectPrx>& batchProxies)
{
IceUtil::Mutex::Lock lock(*this);
for(vector<RequestPtr>::const_iterator p = _requests.begin(); p != _requests.end(); ++p)
{
try
{
if((*p)->invoke(_instance, _connection)) // If batch invocation, add the proxy to the batch proxy set.
{
batchProxies.insert((*p)->getProxy());
}
}
catch(const Ice::LocalException&)
{
// Ignore, this can occur for batch requests.
}
}
_requests.clear();
}
Glacier2::RequestQueueThread::RequestQueueThread(const IceUtil::Time& sleepTime) :
IceUtil::Thread("Glacier2 request queue thread"),
_sleepTime(sleepTime),
_destroy(false),
_sleep(false)
{
}
Glacier2::RequestQueueThread::~RequestQueueThread()
{
assert(_destroy);
assert(_queues.empty());
}
void
Glacier2::RequestQueueThread::destroy()
{
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);
assert(!_destroy);
_destroy = true;
_sleep = false;
notify();
}
try
{
getThreadControl().join();
}
catch(const IceUtil::ThreadNotStartedException&)
{
// Expected if start() failed.
}
}
void
Glacier2::RequestQueueThread::flushRequestQueue(const RequestQueuePtr& queue)
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);
if(_destroy)
{
throw Ice::ObjectNotExistException(__FILE__, __LINE__);
}
if(_queues.empty() && !_sleep)
{
notify();
}
_queues.push_back(queue);
}
void
Glacier2::RequestQueueThread::run()
{
while(true)
{
vector<RequestQueuePtr> queues;
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);
//
// Wait indefinitely if there's no requests to
// send. If the queue is being destroyed we still need to
// wait until all the responses for twoway requests are
// received.
//
while(!_destroy && (_queues.empty() || _sleep))
{
if(_sleep)
{
IceUtil::Time now = IceUtil::Time::now(IceUtil::Time::Monotonic);
if(!timedWait(_sleepDuration))
{
_sleepDuration = IceUtil::Time();
}
else
{
_sleepDuration -= IceUtil::Time::now(IceUtil::Time::Monotonic) - now;
}
if(_sleepDuration <= IceUtil::Time())
{
_sleep = false;
}
}
else
{
wait();
}
}
//
// If the queue is being destroyed and there's no requests or responses
// to send, we're done.
//
if(_destroy && _queues.empty())
{
return;
}
assert(!_queues.empty() && !_sleep);
queues.swap(_queues);
if(_sleepTime > IceUtil::Time())
{
_sleep = true;
_sleepDuration = _sleepTime;
}
}
set<Ice::ObjectPrx> flushProxySet;
for(vector<RequestQueuePtr>::const_iterator p = queues.begin(); p != queues.end(); ++p)
{
(*p)->flushRequests(flushProxySet);
}
set<Ice::ConnectionPtr> flushConnectionSet;
for(set<Ice::ObjectPrx>::const_iterator q = flushProxySet.begin(); q != flushProxySet.end(); ++q)
{
//
// As an optimization, we only flush the proxy batch requests if we didn't
// already flush the requests of a proxy which is using the same connection.
//
Ice::ConnectionPtr connection = (*q)->ice_getCachedConnection();
if(!connection || flushConnectionSet.find(connection) == flushConnectionSet.end())
{
class FlushCB : public AMI_Object_ice_flushBatchRequests
{
public:
virtual void ice_exception(const Ice::Exception&) { } // Ignore.
};
(*q)->ice_flushBatchRequests_async(new FlushCB());
if(connection)
{
flushConnectionSet.insert(connection);
}
}
}
}
}
|