RemoteObjectNode.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore
19  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl.txt
22  * GNU General Public License
23  */
24 #include <ArmarXCore/core/ArmarXManager.h> // for ArmarXManager
25 #include <ArmarXCore/core/ComponentFactories.h> // for ComponentFactory
26 #include <Ice/Object.h> // for Object
27 #include <Ice/ObjectAdapter.h> // for ObjectAdapter
28 #include <Ice/PropertiesAdmin.h> // for PropertyDict
29 #include <IceUtil/Handle.h> // for HandleBase, Handle
30 
31 #include <algorithm> // for move, max
32 #include <ostream> // for operator<<, etc
33 #include <thread> // for thread
34 #include <utility> // for pair
35 #include <thread>
36 
37 #include "ArmarXCore/core/Component.h" // for Component
38 #include "ArmarXCore/core/logging/LogSender.h" // for LogSender
39 #include "ArmarXCore/core/logging/Logging.h" // for ARMARX_DEBUG_S, etc
41 #include "ArmarXCore/interface/core/RemoteObjectNode.h"
42 #include "ArmarXCore/interface/core/UserException.h"
43 #include "ArmarXCore/interface/core/util/distributed/RemoteHandle/RemoteHandleControlBlock.h"
44 #include "RemoteObjectNode.h"
45 
46 
47 namespace armarx
48 {
49  /**
50  * @brief The processor thread count according to std. (in libstd shipped with gcc 4.6 this is 0)
51  */
52  static const unsigned CORE_COUNT_STD = std::thread::hardware_concurrency();
53  /**
54  * @brief The minimal used processor thread count. (in case both boost and std return 0)
55  */
56  static const unsigned CORE_COUNT_MIN = 1;
57  /**
58  * @brief Used core count. (in case both boost and std return 0 we will still use 1)
59  */
60  static const unsigned CORE_COUNT_DEFAULT = std::max(CORE_COUNT_STD, CORE_COUNT_MIN);
61 
63  {
64  return CORE_COUNT_DEFAULT;
65  }
66 
67  void RemoteObjectNode::setCoreCount(int newCount)
68  {
69  coreCountUsed = newCount < 1 ? getDefaultCoreCount() : newCount;
70  ARMARX_DEBUG_S << "processor core count according to (std/requested from user) : "
71  << CORE_COUNT_STD << "/" << newCount;
72  ARMARX_VERBOSE_S << "processor core count used: " << coreCountUsed;
73  }
74 
76  {
77  assert(!shuttingDown);
79  }
80 
82  {
83  std::lock_guard<std::mutex> lock {dataMutex};
85  }
87  {
88  std::lock_guard<std::mutex> lock {dataMutex};
90  }
92  {
93  std::lock_guard<std::mutex> lock {dataMutex};
94  return remoteHandledObjects.size();
95  }
96  Ice::StringSeq RemoteObjectNode::getKnownComponentFactories(const Ice::Current&) const
97  {
99  }
100 
101  Ice::ObjectPrx RemoteObjectNode::createPersistentComponent(const std::string& componentFactoryName, const std::string& registrationName, const ComponentParameter& params, const Ice::Current&)
102  {
103  if (shuttingDown)
104  {
105  throw ServerShuttingDown {};
106  }
107  return doRegisterPersistentManagedIceObjectAtRON(setupComponent(componentFactoryName, registrationName, params));
108  }
109  ClientSideRemoteHandleControlBlockBasePtr RemoteObjectNode::createRemoteHandledComponent(const std::string& componentFactoryName, const std::string& registrationName, const ComponentParameter& params, const Ice::Current&)
110  {
111  if (shuttingDown)
112  {
113  throw ServerShuttingDown {};
114  }
115  return doRegisterRemoteHandledManagedIceObjectAtRON(setupComponent(componentFactoryName, registrationName, params));
116  }
117 
118  Ice::ObjectPrx RemoteObjectNode::registerPersistentObjectWithIdentity(const Ice::Identity& ident, const Ice::ObjectPtr& registree, const Ice::Current&)
119  {
120  if (shuttingDown)
121  {
122  throw ServerShuttingDown {};
123  }
124  auto mioPtr = ManagedIceObjectPtr::dynamicCast(registree);
125 
126  if (mioPtr)
127  {
128  return doRegisterPersistentManagedIceObjectAtRON(setupManagedIceObject(std::move(mioPtr), ident.category + ident.name));
129  }
130  return doRegisterPersistentIceObjectAtRON(setupIceObject(registree, ident));
131  }
132  ClientSideRemoteHandleControlBlockBasePtr RemoteObjectNode::registerRemoteHandledObjectWithIdentity(const Ice::Identity& ident, const Ice::ObjectPtr& registree, const Ice::Current&)
133  {
134  if (shuttingDown)
135  {
136  throw ServerShuttingDown {};
137  }
138  auto mioPtr = ManagedIceObjectPtr::dynamicCast(registree);
139 
140  if (mioPtr)
141  {
142  return doRegisterRemoteHandledManagedIceObjectAtRON(setupManagedIceObject(std::move(mioPtr), ident.category + ident.name));
143  }
144  return doRegisterRemoteHandledIceObjectAtRON(setupIceObject(registree, ident));
145  }
146 
147  Ice::ObjectPrx RemoteObjectNode::registerPersistentObject(const std::string& registrationName, const Ice::ObjectPtr& registree, const Ice::Current&)
148  {
149  if (shuttingDown)
150  {
151  throw ServerShuttingDown {};
152  }
153  auto mioPtr = ManagedIceObjectPtr::dynamicCast(registree);
154 
155  if (mioPtr)
156  {
157  return doRegisterPersistentManagedIceObjectAtRON(setupManagedIceObject(std::move(mioPtr), registrationName));
158  }
159  Ice::Identity ident;
160  ident.name = registrationName;
161  return doRegisterPersistentIceObjectAtRON(setupIceObject(registree, std::move(ident)));
162  }
163  ClientSideRemoteHandleControlBlockBasePtr RemoteObjectNode::registerRemoteHandledObject(const std::string& registrationName, const Ice::ObjectPtr& registree, const Ice::Current&)
164  {
165  if (shuttingDown)
166  {
167  throw ServerShuttingDown {};
168  }
169  auto mioPtr = ManagedIceObjectPtr::dynamicCast(registree);
170 
171  if (mioPtr)
172  {
173  return doRegisterRemoteHandledManagedIceObjectAtRON(setupManagedIceObject(std::move(mioPtr), registrationName));
174  }
175  Ice::Identity ident;
176  ident.name = registrationName;
177  return doRegisterRemoteHandledIceObjectAtRON(setupIceObject(registree, std::move(ident)));
178  }
179 
181  {
182  getArmarXManager()->addObject(mioPtr, true, registrationName);
183  //the self proxy could still be null => wait for it
184  auto mioPrx = mioPtr->getProxy(-1);
185  return {std::move(mioPtr), std::move(mioPrx)};
186  }
187  RemoteObjectNode::ManagedIceObjectPtrAndPrx RemoteObjectNode::setupComponent(const std::string& componentFactoryName, const std::string& registrationName, const ComponentParameter& params)
188  {
189  if (!ComponentFactory::has(componentFactoryName))
190  {
191  throw NoSuchComponentFactory {"There is no component factory for the name " + componentFactoryName};
192  }
193  return setupManagedIceObject(ComponentFactory::get(componentFactoryName)(params.prop, params.configName, params.configDomain), registrationName);
194  }
196  {
197  auto ioPrx = getArmarXManager()->getAdapter()->add(ptr, ident);
198  return {std::move(ident), std::move(ioPrx)};
199  }
200 
202  {
203  auto mioPtr = std::move(mio.ptr);
204  auto axManager = getArmarXManager();
205 
206  auto id = nextRemoteHandledObjectId++;
207  RemoteObjectNodePtr ron = this;
208 
210  axManager,
211  std::move(mio.prx),
212  [axManager, mioPtr, ron, id]
213  {
214  axManager->removeObjectNonBlocking(mioPtr);
215  ron->removeRemoteHandledObject(id);
216  }
217  );
218  {
219  std::lock_guard<std::mutex> lock(dataMutex);
220  if (shuttingDown)
221  {
222  mioRH.directHandle->forceDeletion();
223  throw ServerShuttingDown {};
224  }
225  remoteHandledObjects[id] = std::move(mioRH.directHandle);
226  }
227  return mioRH.clientSideRemoteHandleControlBlock;
228  }
230  {
231  auto ioIdent = std::move(io.ident);
232  auto objAdapter = getArmarXManager()->getAdapter();
233 
234  auto id = nextRemoteHandledObjectId++;
235  RemoteObjectNodePtr ron = this;
236 
239  std::move(io.prx),
240  [objAdapter, ioIdent, ron, id]
241  {
242  objAdapter->remove(ioIdent);
243  ron->removeRemoteHandledObject(id);
244  }
245  );
246  {
247  std::lock_guard<std::mutex> lock(dataMutex);
248  if (shuttingDown)
249  {
250  ioRH.directHandle->forceDeletion();
251  throw ServerShuttingDown {};
252  }
253  remoteHandledObjects[id] = std::move(ioRH.directHandle);
254  }
255  return ioRH.clientSideRemoteHandleControlBlock;
256  }
258  {
259  {
260  std::lock_guard<std::mutex> lock(dataMutex);
261  if (shuttingDown)
262  {
263  getArmarXManager()->removeObjectBlocking(mio.ptr);
264  throw ServerShuttingDown {};
265  }
266  persistentManagedIceObjects.emplace_back(std::move(mio.ptr));
267  }
268  return mio.prx;
269  }
271  {
272  {
273  std::lock_guard<std::mutex> lock(dataMutex);
274  if (shuttingDown)
275  {
276  getArmarXManager()->getAdapter()->remove(io.ident);
277  throw ServerShuttingDown {};
278  }
279  persistentIceObjectIdentities.emplace_back(std::move(io.ident));
280  }
281  return io.prx;
282  }
283 
285  {
286  std::lock_guard<std::mutex> lock(dataMutex);
287  auto it = remoteHandledObjects.find(id);
288  if (it == remoteHandledObjects.end())
289  {
290  ARMARX_ERROR_S << "RON " << getName() << " has no object with id " << id;
291  return;
292  }
293  //just to be safe
294  it->second->forceDeletion();
295  remoteHandledObjects.erase(it);
296  }
297 
299  {
300  shuttingDown = true;
301  std::lock_guard<std::mutex> lock(dataMutex);
302  for (auto& elem : remoteHandledObjects)
303  {
304  elem.second->forceDeletion();
305  }
306 
307  auto axManager = getArmarXManager();
308  for (auto& obj : persistentManagedIceObjects)
309  {
310  axManager->removeObjectBlocking(obj);
311  }
313 
314  auto adapter = axManager->getAdapter();
315  for (auto& ident : persistentIceObjectIdentities)
316  {
317  adapter->remove(ident);
318  }
320  }
321 }
armarx::RemoteObjectNode::getKnownComponentFactories
Ice::StringSeq getKnownComponentFactories(const Ice::Current &=Ice::emptyCurrent) const override
Definition: RemoteObjectNode.cpp:96
armarx::Registrar::get
static const RegisteredType & get(const KeyType &key)
Returns the registered object for the given key.
Definition: Registrar.h:84
armarx::RemoteObjectNode::setupManagedIceObject
ManagedIceObjectPtrAndPrx setupManagedIceObject(ManagedIceObjectPtr mioPtr, std::string registrationName)
Definition: RemoteObjectNode.cpp:180
armarx::Registrar::has
static bool has(const KeyType &key)
Definition: Registrar.h:94
ArmarXManager.h
armarx::RemoteObjectNode::persistentIceObjectIdentities
Ice::IdentitySeq persistentIceObjectIdentities
The identities of persistent ice objects.
Definition: RemoteObjectNode.h:207
armarx::RemoteObjectNode::registerPersistentObject
Ice::ObjectPrx registerPersistentObject(const std::string &registrationName, const Ice::ObjectPtr &registree, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteObjectNode.cpp:147
armarx::ManagedIceObject::getArmarXManager
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
Definition: ManagedIceObject.cpp:348
armarx::RemoteObjectNode::IceObjectIdentityAndPrx::prx
Ice::ObjectPrx prx
Definition: RemoteObjectNode.h:177
armarx::RemoteObjectNode::nextRemoteHandledObjectId
std::atomic< Ice::Long > nextRemoteHandledObjectId
Definition: RemoteObjectNode.h:193
armarx::RemoteObjectNode::getNumberOfRemoteHandledObjects
Ice::Long getNumberOfRemoteHandledObjects(const Ice::Current &=Ice::emptyCurrent) const override
Definition: RemoteObjectNode.cpp:91
armarx::RemoteObjectNode::IceObjectIdentityAndPrx::ident
Ice::Identity ident
Definition: RemoteObjectNode.h:176
armarx::RemoteObjectNode::ManagedIceObjectPtrAndPrx::ptr
ManagedIceObjectPtr ptr
Definition: RemoteObjectNode.h:166
armarx::RemoteObjectNode::doRegisterRemoteHandledManagedIceObjectAtRON
ClientSideRemoteHandleControlBlockBasePtr doRegisterRemoteHandledManagedIceObjectAtRON(ManagedIceObjectPtrAndPrx mio)
Definition: RemoteObjectNode.cpp:201
RemoteObjectNode.h
armarx::RemoteObjectNode::persistentManagedIceObjects
std::vector< ManagedIceObjectPtr > persistentManagedIceObjects
The persistent managed ice objects.
Definition: RemoteObjectNode.h:203
armarx::RemoteObjectNode::getDefaultCoreCount
static unsigned getDefaultCoreCount()
Definition: RemoteObjectNode.cpp:62
IceInternal::Handle< ManagedIceObject >
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::Registrar::getKeys
static std::vector< KeyType > getKeys()
Retrieves the list of all registered elements.
Definition: Registrar.h:115
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:198
armarx::RemoteObjectNode::onExitComponent
void onExitComponent() override
Removes all remote objects.
Definition: RemoteObjectNode.cpp:75
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
RemoteHandleControlBlock.h
armarx::RemoteObjectNode::ManagedIceObjectPtrAndPrx::prx
Ice::ObjectPrx prx
Definition: RemoteObjectNode.h:167
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::RemoteObjectNode::setupComponent
ManagedIceObjectPtrAndPrx setupComponent(const std::string &componentFactoryName, const std::string &registrationName, const ComponentParameter &params)
Definition: RemoteObjectNode.cpp:187
armarx::RemoteObjectNode::doRegisterPersistentIceObjectAtRON
Ice::ObjectPrx doRegisterPersistentIceObjectAtRON(IceObjectIdentityAndPrx io)
Definition: RemoteObjectNode.cpp:270
armarx::RemoteObjectNode::getNumberOfPersistentObjects
Ice::Long getNumberOfPersistentObjects(const Ice::Current &=Ice::emptyCurrent) const override
Definition: RemoteObjectNode.cpp:86
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::RemoteObjectNode::registerRemoteHandledObjectWithIdentity
ClientSideRemoteHandleControlBlockBasePtr registerRemoteHandledObjectWithIdentity(const Ice::Identity &ident, const Ice::ObjectPtr &registree, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteObjectNode.cpp:132
armarx::RemoteObjectNode::getNumberOfObjects
Ice::Long getNumberOfObjects(const Ice::Current &=Ice::emptyCurrent) const override
Definition: RemoteObjectNode.cpp:81
armarx::RemoteObjectNode::coreCountUsed
std::size_t coreCountUsed
The nuber of cores returned, when querried for it.
Definition: RemoteObjectNode.h:217
Component.h
armarx::RemoteObjectNode::IceObjectIdentityAndPrx
Definition: RemoteObjectNode.h:174
armarx::RemoteObjectNode::shutdownAndCleanup
void shutdownAndCleanup()
Definition: RemoteObjectNode.cpp:298
armarx::RemoteObjectNode::setupIceObject
IceObjectIdentityAndPrx setupIceObject(const Ice::ObjectPtr &ptr, Ice::Identity ident)
Definition: RemoteObjectNode.cpp:195
armarx::RemoteObjectNode::createPersistentComponent
Ice::ObjectPrx createPersistentComponent(const std::string &componentFactoryName, const std::string &registrationName, const ComponentParameter &params, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteObjectNode.cpp:101
armarx::RemoteObjectNode::setCoreCount
virtual void setCoreCount(int newCount)
Definition: RemoteObjectNode.cpp:67
armarx::RemoteObjectNode::shuttingDown
std::atomic_bool shuttingDown
Definition: RemoteObjectNode.h:194
armarx::RemoteObjectNode::doRegisterRemoteHandledIceObjectAtRON
ClientSideRemoteHandleControlBlockBasePtr doRegisterRemoteHandledIceObjectAtRON(IceObjectIdentityAndPrx io)
Definition: RemoteObjectNode.cpp:229
armarx::RemoteObjectNode::registerRemoteHandledObject
ClientSideRemoteHandleControlBlockBasePtr registerRemoteHandledObject(const std::string &registrationName, const Ice::ObjectPtr &registree, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteObjectNode.cpp:163
armarx::RemoteObjectNode::dataMutex
std::mutex dataMutex
mutex to protect the map
Definition: RemoteObjectNode.h:212
armarx::RemoteObjectNode::createRemoteHandledComponent
ClientSideRemoteHandleControlBlockBasePtr createRemoteHandledComponent(const std::string &componentFactoryName, const std::string &registrationName, const ComponentParameter &params, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteObjectNode.cpp:109
armarx::RemoteObjectNode::removeRemoteHandledObject
void removeRemoteHandledObject(Ice::Long id)
Definition: RemoteObjectNode.cpp:284
LogSender.h
ComponentFactories.h
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:200
armarx::RemoteObjectNode::ManagedIceObjectPtrAndPrx
Definition: RemoteObjectNode.h:164
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
Logging.h
armarx::RemoteObjectNode::doRegisterPersistentManagedIceObjectAtRON
Ice::ObjectPrx doRegisterPersistentManagedIceObjectAtRON(ManagedIceObjectPtrAndPrx mio)
Definition: RemoteObjectNode.cpp:257
armarx::RemoteObjectNode::remoteHandledObjects
std::unordered_map< Ice::Long, RemoteHandleControlBlockPtr > remoteHandledObjects
the map of running objects
Definition: RemoteObjectNode.h:199
armarx::RemoteObjectNode::registerPersistentObjectWithIdentity
Ice::ObjectPrx registerPersistentObjectWithIdentity(const Ice::Identity &ident, const Ice::ObjectPtr &registree, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteObjectNode.cpp:118
armarx::RemoteHandleControlBlock::create
static ManagementData create(ArmarXManagerPtr manager, Ice::ObjectPrx managedObjectPrx, Deleter deleter)
Creates a new RemoteHandleControlBlock.
Definition: RemoteHandleControlBlock.h:215
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28