diff options
Diffstat (limited to 'cpp/src/Ice/ObjectAdapterI.cpp')
-rw-r--r-- | cpp/src/Ice/ObjectAdapterI.cpp | 324 |
1 files changed, 324 insertions, 0 deletions
diff --git a/cpp/src/Ice/ObjectAdapterI.cpp b/cpp/src/Ice/ObjectAdapterI.cpp new file mode 100644 index 00000000000..2a5ecee51ad --- /dev/null +++ b/cpp/src/Ice/ObjectAdapterI.cpp @@ -0,0 +1,324 @@ +// ********************************************************************** +// +// 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/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(), + voidMemFun(& ::IceInternal::CollectorFactory::activate)); +} + +void +Ice::ObjectAdapterI::hold() +{ + JTCSyncT<JTCMutex> sync(*this); + + if (_collectorFactories.empty()) + { + throw ObjectAdapterDeactivatedException(__FILE__, __LINE__); + } + + for_each(_collectorFactories.begin(), _collectorFactories.end(), + voidMemFun(& ::IceInternal::CollectorFactory::hold)); +} + +void +Ice::ObjectAdapterI::deactivate() +{ + JTCSyncT<JTCMutex> sync(*this); + + if (_collectorFactories.empty()) + { + throw ObjectAdapterDeactivatedException(__FILE__, __LINE__); + } + + for_each(_collectorFactories.begin(), _collectorFactories.end(), + voidMemFun(& ::IceInternal::CollectorFactory::destroy)); + _collectorFactories.clear(); + _objects.clear(); +} + +void +Ice::ObjectAdapterI::add(const ObjectPtr& object, const string& identity) +{ + JTCSyncT<JTCMutex> sync(*this); + + if (_collectorFactories.empty()) + { + throw ObjectAdapterDeactivatedException(__FILE__, __LINE__); + } + + _objects.insert(make_pair(identity, 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 + + _objects.insert(make_pair(s.str(), object)); +} + +void +Ice::ObjectAdapterI::remove(const string& identity) +{ + JTCSyncT<JTCMutex> sync(*this); + + if (_collectorFactories.empty()) + { + throw ObjectAdapterDeactivatedException(__FILE__, __LINE__); + } + + _objects.erase(identity); +} + +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& identity) +{ + JTCSyncT<JTCMutex> sync(*this); + + map<string, ObjectPtr>::const_iterator p = _objects.find(identity); + if (p == _objects.end()) + { + return 0; + } + + return p->second; +} + +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 = _objects.begin(); p != _objects.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& identity) +{ + JTCSyncT<JTCMutex> sync(*this); + + if (_collectorFactories.empty()) + { + throw ObjectAdapterDeactivatedException(__FILE__, __LINE__); + } + + vector<EndpointPtr> endpoints; + transform(_collectorFactories.begin(), _collectorFactories.end(), back_inserter(endpoints), + constMemFun(&CollectorFactory::endpoint)); + + ReferencePtr reference = new Reference(_instance, identity, 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) +{ + string s(endpts); + transform(s.begin(), s.end(), s.begin(), tolower); + + string::size_type beg = 0; + string::size_type end; + + 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); + try + { + // 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, the only CollectorFactoryPtr reference for + // this object will be the one that is temporarily + // constructed for passing the "this" parameter below. And + // then this temporary reference is destroyed, this object + // would be deleted if we don't set the "no delete" flag. + __setNoDelete(true); + _collectorFactories.push_back(new CollectorFactory(instance, this, endp)); + __setNoDelete(false); + } + catch(...) + { + __setNoDelete(false); + throw; + } + + if (end == s.length()) + { + break; + } + + beg = end + 1; + } + + if (!_collectorFactories.size()) + { + throw EndpointParseException(__FILE__, __LINE__); + } +} + +Ice::ObjectAdapterI::~ObjectAdapterI() +{ + if (!_collectorFactories.empty()) + { + deactivate(); + } +} |