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
|
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#include <Ice/ObjectAdapterI.h>
#include <Ice/ServantLocator.h>
#include <Ice/Instance.h>
#include <Ice/Proxy.h>
#include <Ice/ProxyFactory.h>
#include <Ice/Reference.h>
#include <Ice/Endpoint.h>
#include <Ice/ConnectionFactory.h>
#include <Ice/Exception.h>
#include <Ice/Properties.h>
#include <Ice/Functional.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()
{
return _instance->communicator(); // _instance is immutable
}
void
Ice::ObjectAdapterI::activate()
{
IceUtil::Mutex::Lock sync(*this);
if (_collectorFactories.empty())
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
for_each(_collectorFactories.begin(), _collectorFactories.end(), Ice::voidMemFun(&IncomingConnectionFactory::activate));
}
void
Ice::ObjectAdapterI::hold()
{
IceUtil::Mutex::Lock sync(*this);
if (_collectorFactories.empty())
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
for_each(_collectorFactories.begin(), _collectorFactories.end(), Ice::voidMemFun(&IncomingConnectionFactory::hold));
}
void
Ice::ObjectAdapterI::deactivate()
{
IceUtil::Mutex::Lock sync(*this);
if (_collectorFactories.empty())
{
//
// Ignore deactivation requests if the Object Adapter has
// already been deactivated.
//
return;
}
for_each(_collectorFactories.begin(), _collectorFactories.end(), Ice::voidMemFun(&IncomingConnectionFactory::destroy));
_collectorFactories.clear();
_activeServantMap.clear();
_activeServantMapHint = _activeServantMap.end();
for_each(_locatorMap.begin(), _locatorMap.end(),
Ice::secondVoidMemFun<string, ServantLocator>(&ServantLocator::deactivate));
_locatorMap.clear();
_locatorMapHint = _locatorMap.end();
}
ObjectPrx
Ice::ObjectAdapterI::add(const ObjectPtr& object, const Identity& ident)
{
IceUtil::Mutex::Lock sync(*this);
if (_collectorFactories.empty())
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
_activeServantMapHint = _activeServantMap.insert(_activeServantMapHint, make_pair(ident, object));
return newProxy(ident);
}
ObjectPrx
Ice::ObjectAdapterI::addTemporary(const ObjectPtr& object)
{
IceUtil::Mutex::Lock sync(*this);
if (_collectorFactories.empty())
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
ostringstream s;
#ifdef WIN32
struct _timeb tb;
_ftime(&tb);
s << hex << '.' << tb.time << '.' << tb.millitm << '.' << rand();
#else
timeval tv;
gettimeofday(&tv, 0);
s << hex << '.' << tv.tv_sec << '.' << tv.tv_usec / 1000 << '.' << rand();
#endif
Identity ident;
ident.name = s.str();
_activeServantMapHint = _activeServantMap.insert(_activeServantMapHint, make_pair(ident, object));
return newProxy(ident);
}
void
Ice::ObjectAdapterI::remove(const Identity& ident)
{
IceUtil::Mutex::Lock sync(*this);
if (_collectorFactories.empty())
{
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 (_collectorFactories.empty())
{
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 (_collectorFactories.empty())
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
map<string, ::Ice::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();
_locatorMap.erase(p);
_locatorMapHint = _locatorMap.end();
}
}
ServantLocatorPtr
Ice::ObjectAdapterI::findServantLocator(const string& prefix)
{
IceUtil::Mutex::Lock sync(*this);
if (_collectorFactories.empty())
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
map<string, ::Ice::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 (_collectorFactories.empty())
{
throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
}
return newProxy(ident);
}
Ice::ObjectAdapterI::ObjectAdapterI(const InstancePtr& instance, const string& name, const string& endpts) :
_instance(instance),
_name(name),
_activeServantMapHint(_activeServantMap.end()),
_locatorMapHint(_locatorMap.end())
{
string s(endpts);
transform(s.begin(), s.end(), s.begin(), tolower);
string::size_type beg = 0;
string::size_type end;
__setNoDelete(true);
try
{
while (true)
{
end = s.find(':', beg);
if (end == string::npos)
{
end = s.length();
}
if (end == beg)
{
throw EndpointParseException(__FILE__, __LINE__);
}
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 = Endpoint::endpointFromString(instance, es);
_collectorFactories.push_back(new IncomingConnectionFactory(instance, endp, this));
if (end == s.length())
{
break;
}
beg = end + 1;
}
}
catch(...)
{
if (!_collectorFactories.empty())
{
deactivate();
}
__setNoDelete(false);
throw;
}
__setNoDelete(false);
if (_collectorFactories.empty())
{
throw EndpointParseException(__FILE__, __LINE__);
}
string value = _instance->properties()->getProperty("Ice.PrintAdapterReady");
if (atoi(value.c_str()) >= 1)
{
cout << _name << " ready" << endl;
}
}
Ice::ObjectAdapterI::~ObjectAdapterI()
{
if (!_collectorFactories.empty())
{
deactivate();
}
}
ObjectPrx
Ice::ObjectAdapterI::newProxy(const Identity& ident)
{
vector<EndpointPtr> endpoints;
transform(_collectorFactories.begin(), _collectorFactories.end(), back_inserter(endpoints),
Ice::constMemFun(&IncomingConnectionFactory::endpoint));
ReferencePtr reference = new Reference(_instance, ident, "", Reference::ModeTwoway, false, endpoints, endpoints);
return _instance->proxyFactory()->referenceToProxy(reference);
}
bool
Ice::ObjectAdapterI::isLocal(const ObjectPrx& proxy)
{
ReferencePtr ref = proxy->__reference();
vector<EndpointPtr>::const_iterator p;
for (p = ref->endpoints.begin(); p != ref->endpoints.end(); ++p)
{
vector<IncomingConnectionFactoryPtr>::const_iterator q;
for (q = _collectorFactories.begin(); q != _collectorFactories.end(); ++q)
{
if ((*q)->equivalent(*p))
{
return true;
}
}
}
return false;
}
|