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
|
// **********************************************************************
//
// 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 <Ice/OutgoingAsync.h>
#include <Ice/Object.h>
#include <Ice/ConnectionI.h>
#include <Ice/Reference.h>
#include <Ice/Instance.h>
#include <Ice/LocalException.h>
#include <Ice/Properties.h>
#include <Ice/LoggerUtil.h>
#include <Ice/LocatorInfo.h>
#include <Ice/ProxyFactory.h>
#include <Ice/RouterInfo.h>
#include <Ice/Outgoing.h> // For LocalExceptionWrapper.
#include <Ice/Protocol.h>
using namespace std;
using namespace Ice;
using namespace IceInternal;
void IceInternal::incRef(OutgoingAsync* p) { p->__incRef(); }
void IceInternal::decRef(OutgoingAsync* p) { p->__decRef(); }
void IceInternal::incRef(AMI_Object_ice_invoke* p) { p->__incRef(); }
void IceInternal::decRef(AMI_Object_ice_invoke* p) { p->__decRef(); }
IceInternal::OutgoingAsync::OutgoingAsync() :
__is(0),
__os(0)
{
}
IceInternal::OutgoingAsync::~OutgoingAsync()
{
assert(!__is);
assert(!__os);
}
void
IceInternal::OutgoingAsync::__finished(BasicStream& is)
{
IceUtil::Monitor<IceUtil::RecMutex>::Lock sync(_monitor);
DispatchStatus status;
try
{
__is->swap(is);
Byte b;
__is->read(b);
status = static_cast<DispatchStatus>(b);
switch(status)
{
case DispatchOK:
case DispatchUserException:
{
__is->startReadEncaps();
break;
}
case DispatchObjectNotExist:
case DispatchFacetNotExist:
case DispatchOperationNotExist:
{
Identity ident;
ident.__read(__is);
//
// For compatibility with the old FacetPath.
//
vector<string> facetPath;
__is->read(facetPath);
string facet;
if(!facetPath.empty())
{
if(facetPath.size() > 1)
{
throw MarshalException(__FILE__, __LINE__);
}
facet.swap(facetPath[0]);
}
string operation;
__is->read(operation);
auto_ptr<RequestFailedException> ex;
switch(static_cast<DispatchStatus>(status))
{
case DispatchObjectNotExist:
{
ex.reset(new ObjectNotExistException(__FILE__, __LINE__));
break;
}
case DispatchFacetNotExist:
{
ex.reset(new FacetNotExistException(__FILE__, __LINE__));
break;
}
case DispatchOperationNotExist:
{
ex.reset(new OperationNotExistException(__FILE__, __LINE__));
break;
}
default:
{
assert(false);
break;
}
}
ex->id = ident;
ex->facet = facet;
ex->operation = operation;
ex->ice_throw();
}
case DispatchUnknownException:
case DispatchUnknownLocalException:
case DispatchUnknownUserException:
{
string unknown;
__is->read(unknown);
auto_ptr<UnknownException> ex;
switch(static_cast<DispatchStatus>(status))
{
case DispatchUnknownException:
{
ex.reset(new UnknownException(__FILE__, __LINE__));
break;
}
case DispatchUnknownLocalException:
{
ex.reset(new UnknownLocalException(__FILE__, __LINE__));
break;
}
case DispatchUnknownUserException:
{
ex.reset(new UnknownUserException(__FILE__, __LINE__));
break;
}
default:
{
assert(false);
break;
}
}
ex->unknown = unknown;
ex->ice_throw();
}
default:
{
throw UnknownReplyStatusException(__FILE__, __LINE__);
}
}
}
catch(const LocalException& ex)
{
__finished(ex);
return;
}
assert(status == DispatchOK || status == DispatchUserException);
try
{
__response(status == DispatchOK);
}
catch(const Exception& ex)
{
warning(ex);
}
catch(const std::exception& ex)
{
warning(ex);
}
catch(...)
{
warning();
}
cleanup();
}
void
IceInternal::OutgoingAsync::__finished(const LocalException& exc)
{
IceUtil::Monitor<IceUtil::RecMutex>::Lock sync(_monitor);
if(__os) // Don't retry if cleanup() was already called.
{
//
// A CloseConnectionException indicates graceful server
// shutdown, and is therefore always repeatable without
// violating "at-most-once". That's because by sending a close
// connection message, the server guarantees that all
// outstanding requests can safely be repeated. Otherwise, we
// can also retry if the operation mode is Nonmutating or
// Idempotent.
//
// An ObjectNotExistException can always be retried as
// well without violating "at-most-once".
//
if(_mode == Nonmutating || _mode == Idempotent || dynamic_cast<const CloseConnectionException*>(&exc) ||
dynamic_cast<const ObjectNotExistException*>(&exc))
{
try
{
_proxy->__handleException(exc, _cnt);
__send();
return;
}
catch(const LocalException&)
{
}
}
}
try
{
ice_exception(exc);
}
catch(const Exception& ex)
{
warning(ex);
}
catch(const std::exception& ex)
{
warning(ex);
}
catch(...)
{
warning();
}
cleanup();
}
void
IceInternal::OutgoingAsync::__prepare(const ObjectPrx& prx, const string& operation, OperationMode mode,
const Context& context)
{
IceUtil::Monitor<IceUtil::RecMutex>::Lock sync(_monitor);
try
{
//
// We must first wait for other requests to finish.
//
while(__os)
{
_monitor.wait();
}
//
// Can't call async via a oneway proxy.
//
prx->__checkTwowayOnly(operation);
_proxy = prx;
_cnt = 0;
_mode = mode;
ReferencePtr ref = _proxy->__reference();
assert(!__is);
__is = new BasicStream(ref->getInstance().get());
assert(!__os);
__os = new BasicStream(ref->getInstance().get());
__os->writeBlob(requestHdr, sizeof(requestHdr));
ref->getIdentity().__write(__os);
//
// For compatibility with the old FacetPath.
//
if(ref->getFacet().empty())
{
__os->write(static_cast<string*>(0), static_cast<string*>(0));
}
else
{
string facet = ref->getFacet();
__os->write(&facet, &facet + 1);
}
__os->write(operation);
__os->write(static_cast<Byte>(_mode));
__os->writeSize(Int(context.size()));
Context::const_iterator p;
for(p = context.begin(); p != context.end(); ++p)
{
__os->write(p->first);
__os->write(p->second);
}
__os->startWriteEncaps();
}
catch(const LocalException& ex)
{
cleanup();
ex.ice_throw();
}
}
void
IceInternal::OutgoingAsync::__send()
{
IceUtil::Monitor<IceUtil::RecMutex>::Lock sync(_monitor);
try
{
while(true)
{
bool compress;
Ice::ConnectionIPtr connection = _proxy->__getDelegate()->__getConnection(compress);
try
{
connection->sendAsyncRequest(__os, this, compress);
//
// Don't do anything after sendAsyncRequest() returned
// without an exception. I such case, there will be
// callbacks, i.e., calls to the __finished()
// functions. Since there is no mutex protection, we
// cannot modify state here and in such callbacks.
//
return;
}
catch(const LocalExceptionWrapper& ex)
{
_proxy->__handleExceptionWrapper(ex);
}
catch(const LocalException& ex)
{
_proxy->__handleException(ex, _cnt);
}
}
}
catch(const LocalException& ex)
{
__finished(ex);
}
}
void
IceInternal::OutgoingAsync::warning(const Exception& ex) const
{
if(__os) // Don't print anything if cleanup() was already called.
{
ReferencePtr ref = _proxy->__reference();
if(ref->getInstance()->properties()->getPropertyAsIntWithDefault("Ice.Warn.AMICallback", 1) > 0)
{
Warning out(ref->getInstance()->logger());
out << "Ice::Exception raised by AMI callback:\n" << ex;
}
}
}
void
IceInternal::OutgoingAsync::warning(const std::exception& ex) const
{
if(__os) // Don't print anything if cleanup() was already called.
{
ReferencePtr ref = _proxy->__reference();
if(ref->getInstance()->properties()->getPropertyAsIntWithDefault("Ice.Warn.AMICallback", 1) > 0)
{
Warning out(ref->getInstance()->logger());
out << "std::exception raised by AMI callback:\n" << ex.what();
}
}
}
void
IceInternal::OutgoingAsync::warning() const
{
if(__os) // Don't print anything if cleanup() was already called.
{
ReferencePtr ref = _proxy->__reference();
if(ref->getInstance()->properties()->getPropertyAsIntWithDefault("Ice.Warn.AMICallback", 1) > 0)
{
Warning out(ref->getInstance()->logger());
out << "unknown exception raised by AMI callback";
}
}
}
void
IceInternal::OutgoingAsync::cleanup()
{
delete __is;
__is = 0;
delete __os;
__os = 0;
_monitor.notify();
}
void
Ice::AMI_Object_ice_invoke::__invoke(const ObjectPrx& prx, const string& operation, OperationMode mode,
const vector<Byte>& inParams, const Context& context)
{
try
{
__prepare(prx, operation, mode, context);
__os->writeBlob(inParams);
__os->endWriteEncaps();
}
catch(const LocalException& ex)
{
__finished(ex);
return;
}
__send();
}
void
Ice::AMI_Object_ice_invoke::__response(bool ok) // ok == true means no user exception.
{
vector<Byte> outParams;
try
{
Int sz = __is->getReadEncapsSize();
__is->readBlob(outParams, sz);
}
catch(const LocalException& ex)
{
__finished(ex);
return;
}
ice_response(ok, outParams);
}
|