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
|
// **********************************************************************
//
// Copyright (c) 2001
// Mutable Realms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#include <Ice/LocatorInfo.h>
#include <Ice/Locator.h>
#include <Ice/LocalException.h>
#include <Ice/Instance.h>
#include <Ice/TraceLevels.h>
#include <Ice/LoggerUtil.h>
#include <Ice/Endpoint.h>
#include <Ice/Reference.h>
#include <Ice/Functional.h>
#include <Ice/IdentityUtil.h>
#include <iterator>
using namespace std;
using namespace Ice;
using namespace IceInternal;
void IceInternal::incRef(LocatorManager* p) { p->__incRef(); }
void IceInternal::decRef(LocatorManager* p) { p->__decRef(); }
void IceInternal::incRef(LocatorInfo* p) { p->__incRef(); }
void IceInternal::decRef(LocatorInfo* p) { p->__decRef(); }
void IceInternal::incRef(LocatorAdapterTable* p) { p->__incRef(); }
void IceInternal::decRef(LocatorAdapterTable* p) { p->__decRef(); }
IceInternal::LocatorManager::LocatorManager() :
_tableHint(_table.end())
{
}
void
IceInternal::LocatorManager::destroy()
{
IceUtil::Mutex::Lock sync(*this);
for_each(_table.begin(), _table.end(), Ice::secondVoidMemFun<LocatorPrx, LocatorInfo>(&LocatorInfo::destroy));
_table.clear();
_tableHint = _table.end();
_adapterTables.clear();
}
LocatorInfoPtr
IceInternal::LocatorManager::get(const LocatorPrx& locator)
{
if(!locator)
{
return 0;
}
//
// TODO: reap unused locator info objects?
//
IceUtil::Mutex::Lock sync(*this);
map<LocatorPrx, LocatorInfoPtr>::iterator p = _table.end();
if(_tableHint != _table.end())
{
if(_tableHint->first == locator)
{
p = _tableHint;
}
}
if(p == _table.end())
{
p = _table.find(locator);
}
if(p == _table.end())
{
//
// Rely on locator identity for the adapter table. We want to
// have only one table per locator (not one per locator
// proxy).
//
map<Identity, LocatorAdapterTablePtr>::iterator t = _adapterTables.find(locator->ice_getIdentity());
if(t == _adapterTables.end())
{
t = _adapterTables.insert(_adapterTables.begin(),
make_pair(locator->ice_getIdentity(), new LocatorAdapterTable()));
}
_tableHint = _table.insert(_tableHint, make_pair(locator, new LocatorInfo(locator, t->second)));
}
else
{
_tableHint = p;
}
return _tableHint->second;
}
IceInternal::LocatorAdapterTable::LocatorAdapterTable()
{
}
bool
IceInternal::LocatorAdapterTable::get(const string& adapter, ::std::vector<EndpointPtr>& endpoints) const
{
IceUtil::Mutex::Lock sync(*this);
std::map<std::string, std::vector<EndpointPtr> >::const_iterator p = _adapterEndpointsMap.find(adapter);
if(p != _adapterEndpointsMap.end())
{
endpoints = p->second;
return true;
}
else
{
return false;
}
}
void
IceInternal::LocatorAdapterTable::clear()
{
IceUtil::Mutex::Lock sync(*this);
_adapterEndpointsMap.clear();
}
void
IceInternal::LocatorAdapterTable::add(const string& adapter, const ::std::vector<EndpointPtr>& endpoints)
{
IceUtil::Mutex::Lock sync(*this);
_adapterEndpointsMap.insert(make_pair(adapter, endpoints));
}
::std::vector<EndpointPtr>
IceInternal::LocatorAdapterTable::remove(const string& adapter)
{
IceUtil::Mutex::Lock sync(*this);
std::map<std::string, std::vector<EndpointPtr> >::iterator p = _adapterEndpointsMap.find(adapter);
if(p == _adapterEndpointsMap.end())
{
return std::vector<EndpointPtr>();
}
std::vector<EndpointPtr> endpoints = p->second;
_adapterEndpointsMap.erase(p);
return endpoints;
}
IceInternal::LocatorInfo::LocatorInfo(const LocatorPrx& locator, const LocatorAdapterTablePtr& adapterTable) :
_locator(locator),
_adapterTable(adapterTable)
{
assert(_locator);
assert(_adapterTable);
}
void
IceInternal::LocatorInfo::destroy()
{
IceUtil::Mutex::Lock sync(*this);
_locator = 0;
_locatorRegistry = 0;
_adapterTable->clear();
}
bool
IceInternal::LocatorInfo::operator==(const LocatorInfo& rhs) const
{
return _locator == rhs._locator;
}
bool
IceInternal::LocatorInfo::operator!=(const LocatorInfo& rhs) const
{
return _locator != rhs._locator;
}
bool
IceInternal::LocatorInfo::operator<(const LocatorInfo& rhs) const
{
return _locator < rhs._locator;
}
LocatorPrx
IceInternal::LocatorInfo::getLocator() const
{
//
// No mutex lock necessary, _locator is immutable.
//
return _locator;
}
LocatorRegistryPrx
IceInternal::LocatorInfo::getLocatorRegistry()
{
IceUtil::Mutex::Lock sync(*this);
if(!_locatorRegistry) // Lazy initialization.
{
_locatorRegistry = _locator->getRegistry();
}
return _locatorRegistry;
}
std::vector<EndpointPtr>
IceInternal::LocatorInfo::getEndpoints(const ReferencePtr& ref, bool& cached)
{
::std::vector<EndpointPtr> endpoints;
if(!ref->adapterId.empty())
{
cached = true;
if(!_adapterTable->get(ref->adapterId, endpoints))
{
cached = false;
//
// Search the adapter in the location service if we didn't
// find it in the cache.
//
try
{
ObjectPrx object = _locator->findAdapterByName(ref->adapterId);
if(object)
{
endpoints = object->__reference()->endpoints;
}
}
catch(const LocalException& ex)
{
//
// Just trace the failure. The proxy will most likely get empty
// endpoints and raise a NoEndpointException().
//
if(ref->instance->traceLevels()->location >= 1)
{
Trace out(ref->instance->logger(), ref->instance->traceLevels()->locationCat);
out << "couldn't contact the locator to retrieve adapter endpoints\n";
out << "adapter = " << ref->adapterId << "\n";
out << "reason = " << ex;
}
}
//
// Add to the cache.
//
if(!endpoints.empty())
{
_adapterTable->add(ref->adapterId, endpoints);
}
}
if(!endpoints.empty())
{
if(ref->instance->traceLevels()->location >= 1)
{
Trace out(ref->instance->logger(), ref->instance->traceLevels()->locationCat);
if(cached)
{
out << "found endpoints in locator table\n";
}
else
{
out << "retrieved endpoints from locator, adding to locator table\n";
}
out << "adapter = " << ref->adapterId << "\n";
const char* sep = endpoints.size() > 1 ? ":" : "";
ostringstream o;
transform(endpoints.begin(), endpoints.end(), ostream_iterator<string>(o, sep),
::Ice::constMemFun(&Endpoint::toString));
out << "endpoints = " << o.str();
}
}
}
else
{
cached = false;
}
return endpoints;
}
void
IceInternal::LocatorInfo::clearCache(const ReferencePtr& ref)
{
if(!ref->adapterId.empty())
{
std::vector<EndpointPtr> endpoints = _adapterTable->remove(ref->adapterId);
if(!endpoints.empty())
{
if(ref->instance->traceLevels()->location >= 1)
{
Trace out(ref->instance->logger(), ref->instance->traceLevels()->locationCat);
out << "removed endpoints from locator table\n";
out << "adapter = " << ref->adapterId;
const char* sep = endpoints.size() > 1 ? ":" : "";
ostringstream o;
transform(endpoints.begin(), endpoints.end(), ostream_iterator<string>(o, sep),
::Ice::constMemFun(&Endpoint::toString));
out << "endpoints = " << o.str();
}
}
}
}
|