summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/ObjectAdapterI.cpp
blob: 323f6e45110b17acbdb9a7a2c75e22c76c6cb094 (plain)
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
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <Ice/ObjectAdapterI.h>
#include <Ice/Instance.h>
#include <Ice/Proxy.h>
#include <Ice/ProxyFactory.h>
#include <Ice/Reference.h>
#include <Ice/Endpoint.h>
#include <Ice/Collector.h>
#include <Ice/LocalException.h>
#include <Ice/Properties.h>
#include <Ice/Functional.h>
#include <sstream>

#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()
{
    JTCSyncT<JTCMutex> sync(*this);

    if (_collectorFactories.empty())
    {
	throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
    }

    for_each(_collectorFactories.begin(), _collectorFactories.end(),
	     ::IceInternal::voidMemFun(&CollectorFactory::activate));
}

void
Ice::ObjectAdapterI::hold()
{
    JTCSyncT<JTCMutex> sync(*this);

    if (_collectorFactories.empty())
    {
	throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
    }

    for_each(_collectorFactories.begin(), _collectorFactories.end(),
	     ::IceInternal::voidMemFun(& CollectorFactory::hold));
}

void
Ice::ObjectAdapterI::deactivate()
{
    JTCSyncT<JTCMutex> sync(*this);

    if (_collectorFactories.empty())
    {
	throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
    }

    for_each(_collectorFactories.begin(), _collectorFactories.end(),
	     ::IceInternal::voidMemFun(& CollectorFactory::destroy));
    _collectorFactories.clear();
    _aom.clear();
    _aomHint = _aom.begin();
}

void
Ice::ObjectAdapterI::add(const ObjectPtr& object, const string& ident)
{
    JTCSyncT<JTCMutex> sync(*this);

    if (_collectorFactories.empty())
    {
	throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
    }

    _aomHint = _aom.insert(_aomHint, make_pair(ident, object));
}

void
Ice::ObjectAdapterI::addTemporary(const ObjectPtr& object)
{
    JTCSyncT<JTCMutex> 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

    _aomHint = _aom.insert(_aomHint, make_pair(s.str(), object));
}

void
Ice::ObjectAdapterI::remove(const string& ident)
{
    JTCSyncT<JTCMutex> sync(*this);

    if (_collectorFactories.empty())
    {
	throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
    }

    _aom.erase(ident);
    _aomHint = _aom.begin();
}

void
Ice::ObjectAdapterI::setObjectLocator(const ObjectLocatorPtr& locator)
{
    JTCSyncT<JTCMutex> sync(*this);
    _locator = locator;
}

ObjectLocatorPtr
Ice::ObjectAdapterI::getObjectLocator()
{
    JTCSyncT<JTCMutex> sync(*this);
    return _locator;
}

ObjectPtr
Ice::ObjectAdapterI::identityToObject(const string& ident)
{
    JTCSyncT<JTCMutex> sync(*this);

    map<string, ObjectPtr>::const_iterator p = _aom.find(ident);
    if (p != _aom.end())
    {
	return p->second;
    }
    else
    {
	return 0;
    }
}

string
Ice::ObjectAdapterI::objectToIdentity(const ObjectPtr& object)
{
    JTCSyncT<JTCMutex> sync(*this);

    if (_collectorFactories.empty())
    {
	throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
    }

    map<string, ObjectPtr>::const_iterator p;
    for (p = _aom.begin(); p != _aom.end(); ++p)
    {
	if (object.get() == p->second.get())
	{
	    return p->first;
	}
    }

    return string();
}

ObjectPtr
Ice::ObjectAdapterI::proxyToObject(const ObjectPrx& proxy)
{
    //
    // We must first check whether at least one endpoint contained in
    // the proxy matches this object adapter.
    //
    ReferencePtr ref = proxy->__reference();
    vector<EndpointPtr>::const_iterator p;
    for (p = ref->endpoints.begin(); p != ref->endpoints.end(); ++p)
    {
	vector<CollectorFactoryPtr>::const_iterator q;
	for (q = _collectorFactories.begin(); q != _collectorFactories.end(); ++q)
	{
	    if ((*q)->equivalent(*p))
	    {
		//
		// OK, endpoints and object adapter match. Let's find
		// the object.
		//
		return identityToObject(ref->identity);
	    }
	}
    }

    throw WrongObjectAdapterException(__FILE__, __LINE__);
}

ObjectPrx
Ice::ObjectAdapterI::objectToProxy(const ObjectPtr& object)
{
    string identity = objectToIdentity(object);
    
    if (identity.empty())
    {
	throw WrongObjectAdapterException(__FILE__, __LINE__);
    }
    
    return identityToProxy(identity);
}

ObjectPrx
Ice::ObjectAdapterI::identityToProxy(const string& ident)
{
    JTCSyncT<JTCMutex> sync(*this);
    
    if (_collectorFactories.empty())
    {
	throw ObjectAdapterDeactivatedException(__FILE__, __LINE__);
    }
    
    vector<EndpointPtr> endpoints;
    transform(_collectorFactories.begin(), _collectorFactories.end(), back_inserter(endpoints),
	      ::IceInternal::constMemFun(&CollectorFactory::endpoint));

    ReferencePtr reference = new Reference(_instance, ident, Reference::ModeTwoway, false, endpoints, endpoints);
    return _instance->proxyFactory()->referenceToProxy(reference);
}

string
Ice::ObjectAdapterI::proxyToIdentity(const ObjectPrx& proxy)
{
    return proxy->__reference()->identity;
}

Ice::ObjectAdapterI::ObjectAdapterI(const InstancePtr& instance, const string& name, const string& endpts) :
    _instance(instance),
    _name(name),
    _aomHint(_aom.begin())
{
    string s(endpts);
    transform(s.begin(), s.end(), s.begin(), tolower);

    string::size_type beg = 0;
    string::size_type end;
    
    //
    // Set the "no delete" flag to true, meaning that this object will
    // not be deleted, even if the reference count drops to zero. This
    // is needed because if the constructor of the CollectorFactory
    // throws an exception, or if the CollectorFactories are destroyed
    // with "deactivate" from within this constructor, all
    // ObjectAdapterPtrs for this object will be destroyed, and thus
    // this object would be deleted if the "no delete" flag is not
    // set.
    //
    __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(es);
	    _collectorFactories.push_back(new CollectorFactory(instance, this, endp));
	    
	    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();
    }
}