summaryrefslogtreecommitdiff
path: root/cpp/src/Glacier2/RequestQueue.cpp
blob: 62b1c79a0747a5cb75c380a7f9e73b8f9e1500e5 (plain)
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
462
// **********************************************************************
//
// Copyright (c) 2003-2006 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 <set>

using namespace std;
using namespace Ice;
using namespace Glacier2;

namespace Glacier2
{

//
// AMI callback class for twoway requests
//
// NOTE: the received response isn't sent back directly with the AMD
// callback. Instead it's queued and the request queue thread is
// responsible for sending back the response. It's necessary because
// sending back the response might block.
//
class AMI_Array_Object_ice_invokeI : public AMI_Array_Object_ice_invoke
{
public:

    AMI_Array_Object_ice_invokeI(const RequestQueuePtr& requestQueue, const AMD_Array_Object_ice_invokePtr& amdCB) :
	_requestQueue(requestQueue),
	_amdCB(amdCB)
    {
	assert(_amdCB);
    }

    virtual void
    ice_response(bool ok, const pair<const Byte*, const Byte*>& outParams)
    {
	_requestQueue->addResponse(new Response(_amdCB, ok, outParams));
    }

    virtual void
    ice_exception(const Exception& ex)
    {
	_requestQueue->addResponse(new Response(_amdCB, ex));
    }

private:

    const RequestQueuePtr _requestQueue;
    const AMD_Array_Object_ice_invokePtr _amdCB;
};

}

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_Array_Object_ice_invokePtr& amdCB) :
    _proxy(proxy),
    _inParams(inParams.first, inParams.second),
    _current(current),
    _forwardContext(forwardContext),
    _sslContext(sslContext),
    _amdCB(amdCB)
{
    //
    // If this is not a twoway call, we can finish the AMD call right
    // away.
    //
    if(!_proxy->ice_isTwoway())
    {
	bool ok = true;
	pair<const Byte*, const Byte*> outParams(0, 0);
	_amdCB->ice_response(ok, outParams);
    }

    Context::const_iterator p = current.ctx.find("_ovrd");
    if(p != current.ctx.end())
    {
	const_cast<string&>(_override) = p->second;
    }
}


bool
Glacier2::Request::invoke(const RequestQueuePtr& requestQueue)
{
    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_isTwoway())
    {
	AMI_Array_Object_ice_invokePtr cb = new AMI_Array_Object_ice_invokeI(requestQueue, _amdCB);
	if(_forwardContext)
	{
	    if(_sslContext.size() > 0)
	    {
	        Ice::Context ctx = _current.ctx;
		ctx.insert(_sslContext.begin(), _sslContext.end());
	        _proxy->ice_invoke_async(cb, _current.operation, _current.mode, inPair, ctx);
	    }
	    else
	    {
	        _proxy->ice_invoke_async(cb, _current.operation, _current.mode, inPair, _current.ctx);
	    }
	}
	else
	{
	    if(_sslContext.size() > 0)
	    {
	        _proxy->ice_invoke_async(cb, _current.operation, _current.mode, inPair, _sslContext);
	    }
	    else
	    {
	        _proxy->ice_invoke_async(cb, _current.operation, _current.mode, inPair);
	    }
	}
	return true; // A twoway method is being dispatched.
    }
    else
    {
	try
	{
	    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);
		}
	    }
	}
	catch(const LocalException&)
	{
	}
	return false;
    }
}

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;
}

bool
Glacier2::Request::isBatch() const
{
    return _proxy->ice_isBatchOneway() || _proxy->ice_isBatchDatagram();
}

ConnectionPtr
Glacier2::Request::getConnection() const
{
    return _proxy->ice_getConnection();
}

Glacier2::Response::Response(const AMD_Array_Object_ice_invokePtr& amdCB, bool ok, 
			     const pair<const Byte*, const Byte*>& outParams) : 
    _amdCB(amdCB),
    _ok(ok),
    _outParams(outParams.first, outParams.second)
{
}

Glacier2::Response::Response(const AMD_Array_Object_ice_invokePtr& amdCB, const Exception& ex) : 
    _amdCB(amdCB),
    _ok(false),
    _exception(ex.ice_clone())
{
}

void
Glacier2::Response::invoke()
{
    if(_exception.get())
    {
	_amdCB->ice_exception(*_exception.get());
    }
    else
    {
        pair<const Byte*, const Byte*> outPair;
	if(_outParams.size() == 0)
	{
	    outPair.first = outPair.second = 0;
	}
	else
	{
	    outPair.first = &_outParams[0];
	    outPair.second = outPair.first + _outParams.size();
	}
	_amdCB->ice_response(_ok, outPair);
    }
}

Glacier2::RequestQueue::RequestQueue(const IceUtil::Time& sleepTime) :
    _sleepTime(sleepTime),
    _destroy(false),
    _sleep(false)
{
}

Glacier2::RequestQueue::~RequestQueue()
{
    IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);

    assert(_destroy);
    assert(_requests.empty());
    assert(_responses.empty());
}

void 
Glacier2::RequestQueue::destroy()
{
    {
	IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);
	
	assert(!_destroy);
	_destroy = true;
	_sleep = false;
	notify();
    }

    //
    // We don't want to wait for the RequestQueue thread, because this
    // destroy() operation is called when sessions expire or are
    // destroyed, in which case we do not want the session handler
    // thread to block here. Therefore we don't call join(), but
    // instead detach the thread right after we start it.
    //
    //getThreadControl().join();
}

bool
Glacier2::RequestQueue::addRequest(const RequestPtr& request)
{
    IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);
    
    if(_destroy)
    {
        throw ObjectNotExistException(__FILE__, __LINE__);
    }

    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.
    //
    _requests.push_back(request);
    if(!_sleep)
    {
	//
	// No need to notify if the request queue thread is sleeping,
	// once it wakes up it will check if there's requests to send.
	//
	notify();
    }
    return false;
}

void
Glacier2::RequestQueue::addResponse(const ResponsePtr& response)
{
    IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);
    _responses.push_back(response);
    notify();
}

void
Glacier2::RequestQueue::run()
{
    RequestQueuePtr self = this; // This is to avoid creating a temporary Ptr for each call to Request::invoke()
    ptrdiff_t dispatchCount = 0; // The dispatch count keeps track of the number of outstanding twoway requests.
    while(true)
    {
	vector<RequestPtr> requests;
	vector<ResponsePtr> responses;

        {
            IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);

	    //
	    // Wait indefinitely if there's no requests/responses to
	    // send. If the queue is being destroyed we still need to
	    // wait until all the responses for twoway requests are
	    // received.
	    //
            while((!_destroy || dispatchCount != 0) && _responses.empty() && (_requests.empty() || _sleep))
            {
		if(_sleep)
		{
		    IceUtil::Time now = IceUtil::Time::now();
		    if(!timedWait(_sleepDuration))
		    {
			_sleepDuration = IceUtil::Time();
		    }
		    else
		    {
			_sleepDuration -= IceUtil::Time::now() - 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 && _requests.empty() && _responses.empty())
            {
		assert(dispatchCount == 0); // We would have blocked in the wait() above otherwise.
                return;
            }

	    //
	    // If there's requests to sent and we're not sleeping,
	    // send the requests. If a sleep time is configured, we
	    // set the sleep duration and set the sleep flag to make
	    // sure we'll sleep again once we're done sending requests
	    // and responses.
	    //
	    if(!_requests.empty() && !_sleep)
	    {
		requests.swap(_requests);
		if(_sleepTime > IceUtil::Time())
		{
		    _sleep = true;
		    _sleepDuration = _sleepTime;
		}
	    }
	    if(!_responses.empty())
	    {
		responses.swap(_responses);
	    }
	}
        
        //
        // Send requests, flush batch requests, and sleep outside the
        // thread synchronization, so that new messages can be added
        // while this is being done.
        //

	set<ConnectionPtr> flushSet;
	
	for(vector<RequestPtr>::const_iterator p = requests.begin(); p != requests.end(); ++p)
	{
	    if((*p)->isBatch())
	    {
		try
		{
		    flushSet.insert((*p)->getConnection());
		}
		catch(const LocalException&)
		{
		    // Ignore.
		}
	    }
	    
	    //
	    // Invoke returns true if the request expects a response.
	    // If that's the case we increment the dispatch count to
	    // ensure that the thread won't be destroyed before the
	    // response is received.
	    //
	    if((*p)->invoke(self)) // Exceptions are caught within invoke().
	    {
		++dispatchCount;
	    }
	}

	for(set<ConnectionPtr>::const_iterator q = flushSet.begin(); q != flushSet.end(); ++q)
	{
	    try
	    {
		(*q)->flushBatchRequests();
	    }
	    catch(const LocalException&)
	    {
		// Ignore.
	    }
	}

	//
	// Send the responses and decrement the dispatch count.
	//
	for(vector<ResponsePtr>::const_iterator r = responses.begin(); r != responses.end(); ++r)
	{
	    (*r)->invoke();
	}
	dispatchCount -= responses.size();
    }
}