// ********************************************************************** // // Copyright (c) 2001 // MutableRealms, Inc. // Huntsville, AL, USA // // All Rights Reserved // // ********************************************************************** #include #include #include #include #include #include #include #include #include #include #include #ifdef WIN32 # include #else # include #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 sync(*this); if (_collectorFactories.empty()) { throw ObjectAdapterDeactivatedException(__FILE__, __LINE__); } for_each(_collectorFactories.begin(), _collectorFactories.end(), ::IceInternal::voidMemFun(&CollectorFactory::activate)); } void Ice::ObjectAdapterI::hold() { JTCSyncT sync(*this); if (_collectorFactories.empty()) { throw ObjectAdapterDeactivatedException(__FILE__, __LINE__); } for_each(_collectorFactories.begin(), _collectorFactories.end(), ::IceInternal::voidMemFun(&CollectorFactory::hold)); } void Ice::ObjectAdapterI::deactivate() { JTCSyncT sync(*this); if (_collectorFactories.empty()) { // // Ignore deactivation requests if the Object Adapter has // already been deactivated. // return; } for_each(_collectorFactories.begin(), _collectorFactories.end(), ::IceInternal::voidMemFun(&CollectorFactory::destroy)); _collectorFactories.clear(); _activeServantMap.clear(); _activeServantMapHint = _activeServantMap.begin(); if (_locator) { _locator->deactivate(); _locator = 0; } } ObjectPrx Ice::ObjectAdapterI::add(const ObjectPtr& object, const string& ident) { JTCSyncT 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) { JTCSyncT 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 _activeServantMapHint = _activeServantMap.insert(_activeServantMapHint, make_pair(s.str(), object)); return newProxy(s.str()); } void Ice::ObjectAdapterI::remove(const string& ident) { JTCSyncT sync(*this); if (_collectorFactories.empty()) { throw ObjectAdapterDeactivatedException(__FILE__, __LINE__); } _activeServantMap.erase(ident); _activeServantMapHint = _activeServantMap.begin(); } void Ice::ObjectAdapterI::setServantLocator(const ServantLocatorPtr& locator) { JTCSyncT sync(*this); _locator = locator; } ServantLocatorPtr Ice::ObjectAdapterI::getServantLocator() { JTCSyncT sync(*this); return _locator; } ObjectPtr Ice::ObjectAdapterI::identityToServant(const string& ident) { JTCSyncT sync(*this); map::const_iterator p = _activeServantMap.find(ident); if (p != _activeServantMap.end()) { 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 string& ident) { JTCSyncT 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.begin()) { 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(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(); } } ObjectPrx Ice::ObjectAdapterI::newProxy(const string& ident) { vector 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); } bool Ice::ObjectAdapterI::isLocal(const ObjectPrx& proxy) { ReferencePtr ref = proxy->__reference(); vector::const_iterator p; for (p = ref->endpoints.begin(); p != ref->endpoints.end(); ++p) { vector::const_iterator q; for (q = _collectorFactories.begin(); q != _collectorFactories.end(); ++q) { if ((*q)->equivalent(*p)) { return true; } } } return false; }