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
|
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************
#include <Ice/RoutingTable.h>
#include <Glacier/Blobject.h>
using namespace std;
using namespace Ice;
using namespace Glacier;
Glacier::TwowayThrottle::TwowayThrottle(const CommunicatorPtr& communicator, bool reverse) :
_communicator(communicator),
_reverse(reverse),
_properties(_communicator->getProperties()),
_logger(_communicator->getLogger()),
_traceLevel(_properties->getPropertyAsInt("Glacier.Router.Trace.Throttle")),
_max(_reverse ?
_properties->getPropertyAsInt("Glacier.Router.Server.Throttle.Twoways") :
_properties->getPropertyAsInt("Glacier.Router.Client.Throttle.Twoways")),
_count(0)
{
}
Glacier::TwowayThrottle::~TwowayThrottle()
{
assert(_count == 0);
}
void
Glacier::TwowayThrottle::twowayStarted(const Ice::ObjectPrx& proxy, const Ice::Current& current)
{
if(_max <= 0)
{
return;
}
{
IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
assert(_count <= _max);
while(_count == _max)
{
if(_traceLevel >= 1)
{
Trace out(_logger, "Glacier");
out << "throttling ";
if(_reverse)
{
out << "reverse ";
}
out << "twoway call:";
out << "\nnumber of calls = " << _count;
out << "\nproxy = " << _communicator->proxyToString(proxy);
out << "\noperation = " << current.operation;
}
wait();
}
++_count;
}
}
void
Glacier::TwowayThrottle::twowayFinished()
{
if(_max <= 0)
{
return;
}
{
IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
assert(_count <= _max);
if(_count == _max)
{
notifyAll();
}
--_count;
}
}
#ifdef __HP_aCC
//
// Compiler bug!
// The conditional in Glacier::Blobject::Blobject below result in a
// std::exception "thread synchronization error" at runtime
// when using string litterals (looks like a RogueWave bug)
// The work around is to use static strings:
//
static const string traceServer = "Glacier.Router.Trace.Server";
static const string traceClient = "Glacier.Router.Trace.Client";
static const string serverForwardContext = "Glacier.Router.Server.ForwardContext";
static const string clientForwardContext = "Glacier.Router.Client.ForwardContext";
static const string serverSleepTime = "Glacier.Router.Server.SleepTime";
static const string clientSleepTime = "Glacier.Router.Client.SleepTime";
#endif
Glacier::Blobject::Blobject(const CommunicatorPtr& communicator, bool reverse) :
_communicator(communicator),
_reverse(reverse),
_properties(_communicator->getProperties()),
_logger(_communicator->getLogger()),
#ifdef __HP_aCC
//
// Compiler bug, see above
//
_traceLevel(_reverse ?
_properties->getPropertyAsInt(traceServer) :
_properties->getPropertyAsInt(traceClient)),
_forwardContext(_reverse ?
_properties->getPropertyAsInt(serverForwardContext) > 0 :
_properties->getPropertyAsInt(clientForwardContext) > 0),
_sleepTime(_reverse ?
IceUtil::Time::milliSeconds(_properties->getPropertyAsInt(serverSleepTime)) :
IceUtil::Time::milliSeconds(_properties->getPropertyAsInt(clientSleepTime))),
#else
_traceLevel(_reverse ?
_properties->getPropertyAsInt("Glacier.Router.Trace.Server") :
_properties->getPropertyAsInt("Glacier.Router.Trace.Client")),
_forwardContext(_reverse ?
_properties->getPropertyAsInt("Glacier.Router.Server.ForwardContext") > 0 :
_properties->getPropertyAsInt("Glacier.Router.Client.ForwardContext") > 0),
_sleepTime(_reverse ?
IceUtil::Time::milliSeconds(_properties->getPropertyAsInt("Glacier.Router.Server.SleepTime")) :
IceUtil::Time::milliSeconds(_properties->getPropertyAsInt("Glacier.Router.Client.SleepTime"))),
#endif
_twowayThrottle(_communicator, _reverse)
{
_requestQueue = new RequestQueue(_communicator, _traceLevel, _reverse, _sleepTime);
_requestQueueControl = _requestQueue->start();
}
Glacier::Blobject::~Blobject()
{
assert(!_communicator);
assert(!_requestQueue);
}
void
Glacier::Blobject::destroy()
{
//
// No mutex protection necessary, destroy is only called after all
// object adapters have shut down.
//
_communicator = 0;
_requestQueue->destroy();
_requestQueueControl.join();
_requestQueue = 0;
}
class GlacierCB : public AMI_Object_ice_invoke
{
public:
GlacierCB(const AMD_Object_ice_invokePtr& cb, TwowayThrottle& twowayThrottle) :
_cb(cb),
_twowayThrottle(twowayThrottle)
{
}
virtual
~GlacierCB()
{
_twowayThrottle.twowayFinished();
}
virtual void
ice_response(bool ok, const ::std::vector< ::Ice::Byte>& outParams)
{
_cb->ice_response(ok, outParams);
}
virtual void
ice_exception(const ::IceUtil::Exception& ex)
{
_cb->ice_exception(ex);
}
private:
AMD_Object_ice_invokePtr _cb;
TwowayThrottle& _twowayThrottle;
};
void
Glacier::Blobject::invoke(ObjectPrx& proxy, const AMD_Object_ice_invokePtr& amdCB, const vector<Byte>& inParams,
const Current& current)
{
try
{
bool missive = modifyProxy(proxy, current);
if(missive) // Batch routing?
{
vector<Byte> dummy;
amdCB->ice_response(true, dummy);
_requestQueue->addMissive(new Request(proxy, inParams, current, _forwardContext));
}
else // Regular routing.
{
AMI_Object_ice_invokePtr amiCB;
if(proxy->ice_isTwoway())
{
amiCB = new GlacierCB(amdCB, _twowayThrottle);
_twowayThrottle.twowayStarted(proxy, current);
}
else
{
vector<Byte> dummy;
amdCB->ice_response(true, dummy);
}
_requestQueue->addRequest(new Request(proxy, inParams, current, _forwardContext, amiCB));
}
}
catch(const Exception& ex)
{
if(_traceLevel >= 1)
{
Trace out(_logger, "Glacier");
if(_reverse)
{
out << "reverse ";
}
out << "routing exception:\n" << ex;
}
ex.ice_throw();
}
return;
}
bool
Glacier::Blobject::modifyProxy(ObjectPrx& proxy, const Current& current)
{
if(!current.facet.empty())
{
proxy = proxy->ice_newFacet(current.facet);
}
bool missive = false;
Context::const_iterator p = current.ctx.find("_fwd");
if(p != current.ctx.end())
{
for(unsigned int i = 0; i < p->second.length(); ++i)
{
char option = p->second[i];
switch(option)
{
case 't':
{
proxy = proxy->ice_twoway();
missive = false;
break;
}
case 'o':
{
proxy = proxy->ice_oneway();
missive = false;
break;
}
case 'd':
{
proxy = proxy->ice_datagram();
missive = false;
break;
}
case 'O':
{
proxy = proxy->ice_batchOneway();
missive = true;
break;
}
case 'D':
{
proxy = proxy->ice_batchDatagram();
missive = true;
break;
}
case 's':
{
proxy = proxy->ice_secure(true);
break;
}
case 'z':
{
proxy = proxy->ice_compress(true);
break;
}
default:
{
Warning out(_logger);
out << "unknown forward option `" << option << "'";
break;
}
}
}
}
return missive;
}
|