diff --git a/source/RobotAPI/libraries/armem/client/MemoryNameSystem.cpp b/source/RobotAPI/libraries/armem/client/MemoryNameSystem.cpp index 8e60a93726e057532bdaf24caa3dd525afa2840d..f879ed5a349cfccb511315371aad7e72ab4363af 100644 --- a/source/RobotAPI/libraries/armem/client/MemoryNameSystem.cpp +++ b/source/RobotAPI/libraries/armem/client/MemoryNameSystem.cpp @@ -61,11 +61,32 @@ namespace armarx::armem::client // Compare ice identities, update if it changed. auto foo = [](auto & oldProxy, const auto & newProxy) { - if (oldProxy->ice_getIdentity() != newProxy->ice_getIdentity()) + try { - // Replace old proxy by new one. + newProxy->ice_ping(); + } + catch (const Ice::Exception& e) + { + // The new proxy must be reachable + throw error::MemoryNameSystemQueryFailed(e.what()); + } + + try + { + oldProxy->ice_ping(); + + if (oldProxy->ice_getIdentity() != newProxy->ice_getIdentity()) + { + // Replace old proxy by new one. This is the case if the identity changed. + oldProxy = newProxy; + } + } + catch (const Ice::Exception&) + { + // if there is any exception, replace the old proxy by the new one oldProxy = newProxy; } + }; foo(it->second.reading, server.reading); foo(it->second.writing, server.writing); diff --git a/source/RobotAPI/libraries/armem/mns/Registry.cpp b/source/RobotAPI/libraries/armem/mns/Registry.cpp index 2c0a92da8bb31a2adac56da11d9dd31bd873312f..b11b822285d84ba557e481a0b18ec85469688702 100644 --- a/source/RobotAPI/libraries/armem/mns/Registry.cpp +++ b/source/RobotAPI/libraries/armem/mns/Registry.cpp @@ -54,7 +54,7 @@ namespace armarx::armem::mns info.name = input.name; info.server = input.server; info.timeRegistered = armem::Time::Now(); - ARMARX_DEBUG << "Registered memory '" << info.name << "'."; + ARMARX_INFO << "Registered memory '" << info.name << "'."; result.success = true; return result; diff --git a/source/RobotAPI/libraries/armem/server/plugins/Plugin.cpp b/source/RobotAPI/libraries/armem/server/plugins/Plugin.cpp index 193ca45e3c646bb372c684ea36644e7c5f08e450..6443b38874f43fe13c0ca56ad4868625d2f9de72 100644 --- a/source/RobotAPI/libraries/armem/server/plugins/Plugin.cpp +++ b/source/RobotAPI/libraries/armem/server/plugins/Plugin.cpp @@ -76,15 +76,21 @@ namespace armarx::armem::server::plugins } void - Plugin::postOnConnectComponent() + Plugin::preOnConnectComponent() { Component& parent = this->parent<Component>(); - // register to MNS + // register new server info to MNS if (clientPlugin->isMemoryNameSystemEnabled() and clientPlugin->getMemoryNameSystemClient()) { - registerServer(parent); + registerServer(parent); } + } + + void + Plugin::postOnConnectComponent() + { + Component& parent = this->parent<Component>(); parent.getTopic(memoryTopic, memoryTopicName); iceAdapter.setMemoryListener(memoryTopic); diff --git a/source/RobotAPI/libraries/armem/server/plugins/Plugin.h b/source/RobotAPI/libraries/armem/server/plugins/Plugin.h index 0712e4d9f2281adf2c435a7585a019b97907c9d7..0bb86b71baa485fea1a5f6c959ffff1647136445 100644 --- a/source/RobotAPI/libraries/armem/server/plugins/Plugin.h +++ b/source/RobotAPI/libraries/armem/server/plugins/Plugin.h @@ -31,6 +31,7 @@ namespace armarx::armem::server::plugins virtual void preOnInitComponent() override; virtual void postOnInitComponent() override; + virtual void preOnConnectComponent() override; virtual void postOnConnectComponent() override; virtual void preOnDisconnectComponent() override; diff --git a/source/RobotAPI/libraries/armem_objects/client/instance/ObjectReader.cpp b/source/RobotAPI/libraries/armem_objects/client/instance/ObjectReader.cpp index 131fdc655569a16b5d80488289ad2e0fa0d90983..05e850869fac0ce879b1a9b186b5acea99f98890 100644 --- a/source/RobotAPI/libraries/armem_objects/client/instance/ObjectReader.cpp +++ b/source/RobotAPI/libraries/armem_objects/client/instance/ObjectReader.cpp @@ -26,25 +26,34 @@ namespace armarx::armem::obj::instance { const std::string prefix = propertyPrefix; - def->optional(p.memoryName, prefix + "MemoryName"); + def->optional(properties.memoryName, prefix + "MemoryName"); } void Reader::connect(armem::client::MemoryNameSystem& memoryNameSystem) { // Wait for the memory to become available and add it as dependency. - ARMARX_IMPORTANT << "Waiting for memory '" << p.memoryName << "' ..."; + ARMARX_IMPORTANT << "Waiting for memory '" << properties.memoryName << "' ..."; try { // simply wait until memory is ready. Do nothing with reader but get prx - auto r = memoryNameSystem.useReader(p.memoryName); + auto r = memoryNameSystem.useReader(properties.memoryName); + + // cast MemoryPrx to objPoseStoragePrx --> NOT WORKING DUE TO SOME ICE CACHING OR SO. + // Explanation: + // When the readingPrx disconnects and reconnects, the cast fails as it uses internally + // a ice_isA call which results in connection refused. The reason is, that for some + // reason the mns thinks that the reconnected prx is similar to the old info + // (check ice_identity) so it returnes the cached prx in its server map. Could be fixed + // by always querying the mns component for new proxies, but this may slow the system + // down. + //this->objPoseStorage = + // objpose::ObjectPoseStorageInterfacePrx::uncheckedCast(r.readingPrx); + // Current fix: Get prx from mns: + this->readingPrx = r.readingPrx; + + ARMARX_INFO << "Connected to Memory '" << properties.memoryName << "'"; - // cast MemoryPrx to objPoseStoragePrx - this->objPoseStorage = - objpose::ObjectPoseStorageInterfacePrx::checkedCast(r.readingPrx); - - ARMARX_IMPORTANT << "Connected to Memory and ObjectPoseStorage '" << p.memoryName - << "'"; } catch (const armem::error::CouldNotResolveMemoryServer& e) { @@ -58,7 +67,7 @@ namespace armarx::armem::obj::instance const armarx::core::time::Duration& until) { std::map<std::string, bool> ret; - auto providers = objPoseStorage->getAvailableProvidersInfo(); + auto providers = getObjectPoseStorage()->getAvailableProvidersInfo(); for (const auto& [k, p] : providers) { // TODO: check supported objects? @@ -81,7 +90,7 @@ namespace armarx::armem::obj::instance req.request.objectIDs = {requestObject}; req.request.relativeTimeoutMS = until.toMilliSeconds(); - auto requestResult = objPoseStorage->requestObjects(req); + auto requestResult = getObjectPoseStorage()->requestObjects(req); if (requestResult.results.count(requestObject)) { @@ -95,7 +104,7 @@ namespace armarx::armem::obj::instance { // TODO: Shall we throw an exception if no instance index is set? - auto objectPoses = objPoseStorage->getObjectPoses(); + auto objectPoses = getObjectPoseStorage()->getObjectPoses(); for (const auto& pose : objectPoses) { ObjectID oid; @@ -114,7 +123,7 @@ namespace armarx::armem::obj::instance Reader::queryLatestObjectInstances() { std::map<std::string, objpose::ObjectPose> ret; - auto objectPoses = objPoseStorage->getObjectPoses(); + auto objectPoses = getObjectPoseStorage()->getObjectPoses(); for (const auto& pose : objectPoses) { ObjectID oid; @@ -128,7 +137,7 @@ namespace armarx::armem::obj::instance Reader::queryLatestObjectInstances(const ObjectID& classId) { std::map<std::string, objpose::ObjectPose> ret; - auto objectPoses = objPoseStorage->getObjectPoses(); + auto objectPoses = getObjectPoseStorage()->getObjectPoses(); for (const auto& pose : objectPoses) { ObjectID oid; diff --git a/source/RobotAPI/libraries/armem_objects/client/instance/ObjectReader.h b/source/RobotAPI/libraries/armem_objects/client/instance/ObjectReader.h index 18f52a6111d96525c0b5940b2e03084b8ff29a96..41769504c5bcf3096371dd7c4da774b3133ff7e6 100644 --- a/source/RobotAPI/libraries/armem_objects/client/instance/ObjectReader.h +++ b/source/RobotAPI/libraries/armem_objects/client/instance/ObjectReader.h @@ -34,6 +34,7 @@ // The object pose provider. As long as the provider is not connected to armem we need the second proxy #include <RobotAPI/interface/objectpose/ObjectPoseProvider.h> #include <RobotAPI/interface/objectpose/ObjectPoseStorageInterface.h> +#include <RobotAPI/interface/armem/server/MemoryInterface.h> namespace armarx::armem::obj::instance { @@ -71,21 +72,22 @@ namespace armarx::armem::obj::instance Properties getProperties() { - return this->p; + return this->properties; } objpose::ObjectPoseStorageInterfacePrx getObjectPoseStorage() const { - return objPoseStorage; + // TODO: see problem in source file. This performs a ice_isA every time this method is called + return objpose::ObjectPoseStorageInterfacePrx::checkedCast(this->readingPrx); } private: - Properties p; + Properties properties; const std::string propertyPrefix = "mem.obj.instance."; - objpose::ObjectPoseStorageInterfacePrx objPoseStorage; + armarx::armem::server::ReadingMemoryInterfacePrx readingPrx; };