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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
|
// **********************************************************************
//
// Copyright (c) 2001
// Mutable Realms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#include <IceUtil/UUID.h>
#include <Ice/ObjectAdapterI.h>
#include <Ice/ServantLocator.h>
#include <Ice/Instance.h>
#include <Ice/Proxy.h>
#include <Ice/ProxyFactory.h>
#include <Ice/ReferenceFactory.h>
#include <Ice/Endpoint.h>
#include <Ice/EndpointFactoryManager.h>
#include <Ice/ConnectionFactory.h>
#include <Ice/RouterInfo.h>
#include <Ice/LocalException.h>
#include <Ice/Properties.h>
#include <Ice/Functional.h>
#include <Ice/LocatorInfo.h>
#include <Ice/Locator.h>
#include <Ice/LoggerUtil.h>
#ifdef _WIN32
# include <sys/timeb.h>
#else
# include <sys/time.h>
#endif
using namespace std;
using namespace Ice;
using namespace IceInternal;
string
Ice::ObjectAdapterI::getName()
{
return _name; // _name is immutable
}
CommunicatorPtr
Ice::ObjectAdapterI::getCommunicator()
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
return _instance->communicator();
}
void
Ice::ObjectAdapterI::activate()
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
if(!_printAdapterReadyDone)
{
if(_locatorInfo)
{
Identity ident;
ident.name = "dummy";
//
// TODO: This might throw if we can't connect to the
// locator. Shall we raise a special exception for the
// activate operation instead of a non obvious network
// exception?
//
_locatorInfo->getLocatorRegistry()->addAdapter(_name, newDirectProxy(ident));
}
}
for_each(_incomingConnectionFactories.begin(), _incomingConnectionFactories.end(),
Ice::voidMemFun(&IncomingConnectionFactory::activate));
if(!_printAdapterReadyDone)
{
if(_instance->properties()->getPropertyAsInt("Ice.PrintAdapterReady") > 0)
{
cout << _name << " ready" << endl;
}
_printAdapterReadyDone = true;
}
}
void
Ice::ObjectAdapterI::hold()
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
for_each(_incomingConnectionFactories.begin(), _incomingConnectionFactories.end(),
Ice::voidMemFun(&IncomingConnectionFactory::hold));
}
void
Ice::ObjectAdapterI::deactivate()
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
//
// Ignore deactivation requests if the Object Adapter has
// already been deactivated.
//
return;
}
for_each(_incomingConnectionFactories.begin(), _incomingConnectionFactories.end(),
Ice::voidMemFun(&IncomingConnectionFactory::destroy));
_instance->outgoingConnectionFactory()->removeAdapter(this);
_activeServantMap.clear();
_activeServantMapHint = _activeServantMap.end();
for_each(_locatorMap.begin(), _locatorMap.end(),
Ice::secondVoidMemFun<string, ServantLocator>(&ServantLocator::deactivate));
_locatorMap.clear();
_locatorMapHint = _locatorMap.end();
_instance = 0;
}
void
Ice::ObjectAdapterI::waitForDeactivate()
{
//
// _incommingConnectionFactories is immutable, thus no mutex lock
// is necessary. (A mutex lock wouldn't work here anyway, as there
// would be a deadlock with upcalls.)
//
for_each(_incomingConnectionFactories.begin(), _incomingConnectionFactories.end(),
Ice::voidMemFun(&IncomingConnectionFactory::waitUntilFinished));
}
ObjectPrx
Ice::ObjectAdapterI::add(const ObjectPtr& object, const Identity& ident)
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
_activeServantMapHint = _activeServantMap.insert(_activeServantMapHint, make_pair(ident, object));
return newProxy(ident);
}
ObjectPrx
Ice::ObjectAdapterI::addWithUUID(const ObjectPtr& object)
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
Identity ident;
ident.name = IceUtil::generateUUID();
_activeServantMapHint = _activeServantMap.insert(_activeServantMapHint, make_pair(ident, object));
return newProxy(ident);
}
void
Ice::ObjectAdapterI::remove(const Identity& ident)
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
_activeServantMap.erase(ident);
_activeServantMapHint = _activeServantMap.end();
}
void
Ice::ObjectAdapterI::addServantLocator(const ServantLocatorPtr& locator, const string& prefix)
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
_locatorMapHint = _locatorMap.insert(_locatorMapHint, make_pair(prefix, locator));
}
void
Ice::ObjectAdapterI::removeServantLocator(const string& prefix)
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
map<string, ServantLocatorPtr>::iterator p = _locatorMap.end();
if(_locatorMapHint != _locatorMap.end())
{
if(_locatorMapHint->first == prefix)
{
p = _locatorMapHint;
}
}
if(p == _locatorMap.end())
{
p = _locatorMap.find(prefix);
}
if(p != _locatorMap.end())
{
p->second->deactivate();
if(p == _locatorMapHint)
{
_locatorMap.erase(p++);
_locatorMapHint = p;
}
else
{
_locatorMap.erase(p);
}
}
}
ServantLocatorPtr
Ice::ObjectAdapterI::findServantLocator(const string& prefix)
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
map<string, ServantLocatorPtr>::iterator p = _locatorMap.end();
if(_locatorMapHint != _locatorMap.end())
{
if(_locatorMapHint->first == prefix)
{
p = _locatorMapHint;
}
}
if(p == _locatorMap.end())
{
p = _locatorMap.find(prefix);
}
if(p != _locatorMap.end())
{
_locatorMapHint = p;
return p->second;
}
else
{
return 0;
}
}
ObjectPtr
Ice::ObjectAdapterI::identityToServant(const Identity& ident)
{
IceUtil::Mutex::Lock sync(*this);
if(_activeServantMapHint != _activeServantMap.end())
{
if(_activeServantMapHint->first == ident)
{
return _activeServantMapHint->second;
}
}
ObjectDict::iterator p = _activeServantMap.find(ident);
if(p != _activeServantMap.end())
{
_activeServantMapHint = p;
return p->second;
}
else
{
return 0;
}
}
ObjectPtr
Ice::ObjectAdapterI::proxyToServant(const ObjectPrx& proxy)
{
ReferencePtr ref = proxy->__reference();
return identityToServant(ref->identity);
}
ObjectPrx
Ice::ObjectAdapterI::createProxy(const Identity& ident)
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
return newProxy(ident);
}
ObjectPrx
Ice::ObjectAdapterI::createDirectProxy(const Identity& ident)
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
return newDirectProxy(ident);
}
ObjectPrx
Ice::ObjectAdapterI::createReverseProxy(const Identity& ident)
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
//
// Create a reference and return a reverse proxy for this reference.
//
vector<EndpointPtr> endpoints;
ReferencePtr ref = _instance->referenceFactory()->create(ident, "", Reference::ModeTwoway, false, false,"",
endpoints, 0, 0, this);
return _instance->proxyFactory()->referenceToProxy(ref);
}
void
Ice::ObjectAdapterI::addRouter(const RouterPrx& router)
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
RouterInfoPtr routerInfo = _instance->routerManager()->get(router);
if(routerInfo)
{
//
// Add the router's server proxy endpoints to this object
// adapter.
//
ObjectPrx proxy = routerInfo->getServerProxy();
{
IceUtil::Mutex::Lock routerEndpointsSync(_routerEndpointsMutex);
copy(proxy->__reference()->endpoints.begin(), proxy->__reference()->endpoints.end(),
back_inserter(_routerEndpoints));
sort(_routerEndpoints.begin(), _routerEndpoints.end()); // Must be sorted.
_routerEndpoints.erase(unique(_routerEndpoints.begin(), _routerEndpoints.end()), _routerEndpoints.end());
}
//
// Associate this object adapter with the router. This way,
// new outgoing connections to the router's client proxy will
// use this object adapter for callbacks.
//
routerInfo->setAdapter(this);
//
// Also modify all existing outgoing connections to the
// router's client proxy to use this object adapter for
// callbacks.
//
_instance->outgoingConnectionFactory()->setRouter(routerInfo->getRouter());
//
// Creates proxies with endpoints instead of the adapter name
// when there is a router.
//
_useEndpointsInProxy = true;
}
}
void
Ice::ObjectAdapterI::setLocator(const LocatorPrx& locator)
{
IceUtil::Mutex::Lock sync(*this);
if(!_instance)
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
_locatorInfo = _instance->locatorManager()->get(locator);
if(_locatorInfo)
{
//
// If a locator is set, we create proxies with adapter names in
// the reference instead of endpoints. If it's not set, we create
// proxies with endpoints if there's at least one incoming
// connection factory or router endpoints.
//
_useEndpointsInProxy = false;
}
else
{
IceUtil::Mutex::Lock routerEndpointsSync(_routerEndpointsMutex);
_useEndpointsInProxy = !_incomingConnectionFactories.empty() || !_routerEndpoints.empty();
}
}
list<ConnectionPtr>
Ice::ObjectAdapterI::getIncomingConnections() const
{
//
// _incommingConnectionFactories is immutable, thus no mutex lock
// is necessary.
//
list<ConnectionPtr> connections;
vector<IncomingConnectionFactoryPtr>::const_iterator p;
for(p = _incomingConnectionFactories.begin(); p != _incomingConnectionFactories.end(); ++p)
{
list<ConnectionPtr> cons = (*p)->connections();
connections.splice(connections.end(), cons);
}
return connections;
}
Ice::ObjectAdapterI::ObjectAdapterI(const InstancePtr& instance, const string& name, const string& endpts) :
_instance(instance),
_printAdapterReadyDone(false),
_name(name),
_activeServantMapHint(_activeServantMap.end()),
_locatorMapHint(_locatorMap.end())
{
string s(endpts);
transform(s.begin(), s.end(), s.begin(), tolower);
__setNoDelete(true);
try
{
string::size_type beg;
string::size_type end = 0;
while(end < s.length())
{
static const string delim = " \t\n\r";
beg = s.find_first_not_of(delim, end);
if(beg == string::npos)
{
break;
}
end = s.find(':', beg);
if(end == string::npos)
{
end = s.length();
}
if(end == beg)
{
break;
}
string es = s.substr(beg, end - beg);
//
// Don't store the endpoint in the adapter. The Collector
// might change it, for example, to fill in the real port
// number if a zero port number is given.
//
EndpointPtr endp = _instance->endpointFactoryManager()->create(es);
_incomingConnectionFactories.push_back(new IncomingConnectionFactory(instance, endp, this));
end = end + 1;
}
}
catch(...)
{
deactivate();
__setNoDelete(false);
throw;
}
__setNoDelete(false);
//
// Create proxies with the adapter endpoints only if there's
// incoming connection factories. If there's no incoming
// connection factories we will create proxies with the adapter
// name in the reference (to allow collocation to work).
//
_useEndpointsInProxy = !_incomingConnectionFactories.empty();
//
// Object Adapters without incoming connection factories are
// permissible, as such Object Adapters can still be used with a
// router. (See addRouter.)
//
/*
if(_incomingConnectionFactories.empty())
{
throw EndpointParseException(__FILE__, __LINE__);
}
*/
}
Ice::ObjectAdapterI::~ObjectAdapterI()
{
if(_instance)
{
Warning out(_instance->logger());
out << "object adapter has not been deactivated";
}
}
ObjectPrx
Ice::ObjectAdapterI::newProxy(const Identity& ident) const
{
if(_useEndpointsInProxy)
{
return newDirectProxy(ident);
}
else
{
//
// Create a reference with the adapter id.
//
vector<EndpointPtr> endpoints;
ReferencePtr ref = _instance->referenceFactory()->create(ident, "", Reference::ModeTwoway, false, false, _name,
endpoints, 0, 0, 0);
//
// Return a proxy for the reference.
//
return _instance->proxyFactory()->referenceToProxy(ref);
}
}
ObjectPrx
Ice::ObjectAdapterI::newDirectProxy(const Identity& ident) const
{
vector<EndpointPtr> endpoints;
//
// First we add all endpoints from all incoming connection
// factories.
//
transform(_incomingConnectionFactories.begin(), _incomingConnectionFactories.end(), back_inserter(endpoints),
Ice::constMemFun(&IncomingConnectionFactory::endpoint));
//
// Now we also add the endpoints of the router's server proxy, if
// any. This way, object references created by this object adapter
// will also point to the router's server proxy endpoints.
//
{
IceUtil::Mutex::Lock routerEndpointsSync(_routerEndpointsMutex);
copy(_routerEndpoints.begin(), _routerEndpoints.end(), back_inserter(endpoints));
}
//
// Create a reference and return a proxy for this reference.
//
ReferencePtr ref = _instance->referenceFactory()->create(ident, "", Reference::ModeTwoway, false, false, "",
endpoints, 0, 0, 0);
return _instance->proxyFactory()->referenceToProxy(ref);
}
bool
Ice::ObjectAdapterI::isLocal(const ObjectPrx& proxy) const
{
ReferencePtr ref = proxy->__reference();
vector<EndpointPtr>::const_iterator p;
if(!ref->adapterId.empty())
{
//
// Proxy is local if the reference adapter id matches this
// adapter name.
//
return ref->adapterId == _name;
}
//
// Proxies which have at least one endpoint in common with the
// endpoints used by this object adapter's incoming connection
// factories are considered local.
//
for(p = ref->endpoints.begin(); p != ref->endpoints.end(); ++p)
{
vector<IncomingConnectionFactoryPtr>::const_iterator q;
for(q = _incomingConnectionFactories.begin(); q != _incomingConnectionFactories.end(); ++q)
{
if((*q)->equivalent(*p))
{
return true;
}
}
}
{
IceUtil::Mutex::Lock routerEndpointsSync(_routerEndpointsMutex);
//
// Proxies which have at least one endpoint in common with the
// router's server proxy endpoints (if any), are also considered
// local.
//
for(p = ref->endpoints.begin(); p != ref->endpoints.end(); ++p)
{
if(binary_search(_routerEndpoints.begin(), _routerEndpoints.end(), *p)) // _routerEndpoints is sorted.
{
return true;
}
}
}
return false;
}
|