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
|
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice-E is licensed to you under the terms described in the
// ICEE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceE/IncomingConnectionFactory.h>
#include <IceE/Connection.h>
#include <IceE/Instance.h>
#include <IceE/LoggerUtil.h>
#include <IceE/TraceLevels.h>
#include <IceE/DefaultsAndOverrides.h>
#include <IceE/Properties.h>
#include <IceE/Transceiver.h>
#include <IceE/Acceptor.h>
#include <IceE/Endpoint.h>
#include <IceE/LocalException.h>
#include <IceE/Functional.h>
using namespace std;
using namespace Ice;
using namespace IceInternal;
IceUtil::Shared* IceInternal::upCast(IncomingConnectionFactory* p) { return p; }
void
IceInternal::IncomingConnectionFactory::activate()
{
IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
setState(StateActive);
}
void
IceInternal::IncomingConnectionFactory::hold()
{
IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
setState(StateHolding);
}
void
IceInternal::IncomingConnectionFactory::destroy()
{
IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
setState(StateClosed);
}
void
IceInternal::IncomingConnectionFactory::waitUntilHolding() const
{
list<ConnectionPtr> connections;
{
IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
//
// First we wait until the connection factory itself is in holding
// state.
//
while(_state < StateHolding)
{
wait();
}
//
// We want to wait until all connections are in holding state
// outside the thread synchronization.
//
connections = _connections;
}
//
// Now we wait until each connection is in holding state.
//
for_each(connections.begin(), connections.end(), Ice::constVoidMemFun(&Connection::waitUntilHolding));
}
void
IceInternal::IncomingConnectionFactory::waitUntilFinished()
{
IceUtil::ThreadPtr threadPerIncomingConnectionFactory;
list<ConnectionPtr> connections;
{
IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
//
// First we wait until the factory is destroyed. If we are using
// an acceptor, we also wait for it to be closed.
//
while(_state != StateClosed || _acceptor)
{
wait();
}
threadPerIncomingConnectionFactory = _threadPerIncomingConnectionFactory;
_threadPerIncomingConnectionFactory = 0;
//
// We want to wait until all connections are finished outside the
// thread synchronization.
//
connections.swap(_connections);
}
if(threadPerIncomingConnectionFactory)
{
threadPerIncomingConnectionFactory->getThreadControl().join();
}
for_each(connections.begin(), connections.end(), Ice::voidMemFun(&Connection::waitUntilFinished));
}
EndpointPtr
IceInternal::IncomingConnectionFactory::endpoint() const
{
// No mutex protection necessary, _endpoint is immutable.
return _endpoint;
}
list<ConnectionPtr>
IceInternal::IncomingConnectionFactory::connections() const
{
IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
list<ConnectionPtr> result;
//
// Only copy connections which have not been destroyed.
//
remove_copy_if(_connections.begin(), _connections.end(), back_inserter(result),
Ice::constMemFun(&Connection::isDestroyed));
return result;
}
void
IceInternal::IncomingConnectionFactory::flushBatchRequests()
{
list<ConnectionPtr> c = connections(); // connections() is synchronized, so no need to synchronize here.
for(list<ConnectionPtr>::const_iterator p = c.begin(); p != c.end(); ++p)
{
try
{
(*p)->flushBatchRequests();
}
catch(const LocalException&)
{
// Ignore.
}
}
}
string
IceInternal::IncomingConnectionFactory::toString() const
{
IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
if(_transceiver)
{
return _transceiver->toString();
}
assert(_acceptor);
return _acceptor->toString();
}
IceInternal::IncomingConnectionFactory::IncomingConnectionFactory(const InstancePtr& instance,
const EndpointPtr& endpoint,
const ObjectAdapterPtr& adapter) :
_instance(instance),
_endpoint(endpoint),
_adapter(adapter),
_warn(_instance->initializationData().properties->getPropertyAsInt("Ice.Warn.Connections") > 0),
_state(StateHolding)
{
if(_instance->defaultsAndOverrides()->overrideTimeout)
{
const_cast<EndpointPtr&>(_endpoint) =
_endpoint->timeout(_instance->defaultsAndOverrides()->overrideTimeoutValue);
}
_acceptor = _endpoint->acceptor(const_cast<EndpointPtr&>(_endpoint));
assert(_acceptor);
_acceptor->listen();
__setNoDelete(true);
try
{
//
// If we are in thread per connection mode, we also use one
// thread per incoming connection factory, that accepts new
// connections on this endpoint.
//
_threadPerIncomingConnectionFactory = new ThreadPerIncomingConnectionFactory(this);
_threadPerIncomingConnectionFactory->start(_instance->threadPerConnectionStackSize());
}
catch(const Ice::Exception& ex)
{
{
Error out(_instance->initializationData().logger);
out << "cannot create thread for incoming connection factory:\n" << ex.toString();
}
try
{
_acceptor->close();
}
catch(const LocalException&)
{
// Here we ignore any exceptions in close().
}
_state = StateClosed;
_acceptor = 0;
_threadPerIncomingConnectionFactory = 0;
__setNoDelete(false);
ex.ice_throw();
}
__setNoDelete(false);
}
IceInternal::IncomingConnectionFactory::~IncomingConnectionFactory()
{
IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
assert(_state == StateClosed);
assert(!_acceptor);
assert(_connections.empty());
assert(!_threadPerIncomingConnectionFactory);
}
void
IceInternal::IncomingConnectionFactory::setState(State state)
{
if(_state == state) // Don't switch twice.
{
return;
}
switch(state)
{
case StateActive:
{
if(_state != StateHolding) // Can only switch from holding to active.
{
return;
}
for_each(_connections.begin(), _connections.end(), Ice::voidMemFun(&Connection::activate));
break;
}
case StateHolding:
{
if(_state != StateActive) // Can only switch from active to holding.
{
return;
}
for_each(_connections.begin(), _connections.end(), Ice::voidMemFun(&Connection::hold));
break;
}
case StateClosed:
{
if(_acceptor)
{
//
// Connect to our own acceptor, which unblocks our
// thread per incoming connection factory stuck in accept().
//
_acceptor->connectToSelf();
}
#ifdef _STLP_BEGIN_NAMESPACE
// voidbind2nd is an STLport extension for broken compilers in IceE/Functional.h
for_each(_connections.begin(), _connections.end(),
voidbind2nd(Ice::voidMemFun1(&Connection::destroy), Connection::ObjectAdapterDeactivated));
#else
for_each(_connections.begin(), _connections.end(),
bind2nd(Ice::voidMemFun1(&Connection::destroy), Connection::ObjectAdapterDeactivated));
#endif
break;
}
}
_state = state;
notifyAll();
}
void
IceInternal::IncomingConnectionFactory::run()
{
assert(_acceptor);
while(true)
{
//
// We must accept new connections outside the thread
// synchronization, because we use blocking accept.
//
TransceiverPtr transceiver;
try
{
transceiver = _acceptor->accept();
}
catch(const SocketException&)
{
// Ignore socket exceptions.
}
catch(const TimeoutException&)
{
// Ignore timeouts.
}
catch(const LocalException& ex)
{
// Warn about other Ice local exceptions.
if(_warn)
{
Warning out(_instance->initializationData().logger);
out << "connection exception:\n" << ex.toString() << "\n" << _acceptor->toString();
}
}
ConnectionPtr connection;
{
IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
while(_state == StateHolding)
{
wait();
}
if(_state == StateClosed)
{
if(transceiver)
{
try
{
transceiver->close();
}
catch(const LocalException&)
{
// Here we ignore any exceptions in close().
}
}
try
{
_acceptor->close();
}
catch(const LocalException& ex)
{
_acceptor = 0;
notifyAll();
ex.ice_throw();
}
_acceptor = 0;
notifyAll();
return;
}
assert(_state == StateActive);
//
// Reap connections for which destruction has completed.
//
_connections.erase(remove_if(_connections.begin(), _connections.end(),
Ice::constMemFun(&Connection::isFinished)),
_connections.end());
//
// Create a connection object for the connection.
//
if(transceiver)
{
try
{
connection = new Connection(_instance, transceiver, _endpoint, _adapter);
}
catch(const LocalException&)
{
return;
}
_connections.push_back(connection);
}
}
//
// In thread per connection mode, the connection's thread will
// take care of connection validation and activation. We don't
// want to block this thread waiting until validation is
// complete because it is the only thread that can accept
// connections with this factory's acceptor. Therefore we
// don't call validate() and activate() from the connection
// factory in thread per connection mode.
//
}
}
IceInternal::IncomingConnectionFactory::ThreadPerIncomingConnectionFactory::ThreadPerIncomingConnectionFactory(
const IncomingConnectionFactoryPtr& factory) :
_factory(factory)
{
}
void
IceInternal::IncomingConnectionFactory::ThreadPerIncomingConnectionFactory::run()
{
try
{
_factory->run();
}
catch(const Exception& ex)
{
Error out(_factory->_instance->initializationData().logger);
out << "exception in thread per incoming connection factory:\n" << _factory->toString() << ex.toString();
}
catch(const std::exception& ex)
{
Error out(_factory->_instance->initializationData().logger);
out << "std::exception in thread per incoming connection factory:\n" << _factory->toString() << ex.what();
}
catch(...)
{
Error out(_factory->_instance->initializationData().logger);
out << "unknown exception in thread per incoming connection factory:\n" << _factory->toString();
}
_factory = 0; // Resolve cyclic dependency.
}
|