RemoteHandleControlBlock.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 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
25 
26 namespace armarx
27 {
29  {
30  3000
31  };
32 
34  {
35  std::lock_guard<std::mutex> guard {mtx};
36  assert(useCount);
37  if (! --useCount)
38  {
39  lastTimeUseCountReachedZero = clock::now();
40  }
41  }
42 
43  ClientSideRemoteHandleControlBlockBasePtr RemoteHandleControlBlock::getClientSideRemoteHandleControlBlock(const Ice::Current&)
44  {
45  assert(managedObjectProxy);
46  assert(selfProxy);
47  return new ClientSideRemoteHandleControlBlock {selfProxy, managedObjectProxy};
48  }
49 
50  RemoteHandleControlBlock::RemoteHandleControlBlock(Ice::ObjectAdapterPtr objectAdapter, Ice::ObjectPrx managedObjectPrx, std::function<void ()> deleter):
51  objectAdapter {std::move(objectAdapter)},
52  deleter {std::move(deleter)},
53  managedObjectProxy {std::move(managedObjectPrx)}
54  {
55  if (!objectAdapter)
56  {
57  throw std::invalid_argument {"RemoteHandleControlBlock() called with a null object adapter."};
58  }
59  if (!managedObjectPrx)
60  {
61  throw std::invalid_argument {"RemoteHandleControlBlock() called with a null proxy."};
62  }
63 
64  Ice::Identity ident;
65  ident.name = "RemoteHandleControlBlock::" + managedObjectProxy->ice_getIdentity().name + "::" + IceUtil::generateUUID();
66  selfProxy = RemoteHandleControlBlockInterfacePrx::uncheckedCast(objectAdapter->add(this, std::move(ident)));
67  }
68 
69  SharedRemoteHandleState::SharedRemoteHandleState(unsigned int deletionDelayMs):
70  deletionDelay {deletionDelayMs},
71  sweeper {new PeriodicTaskType{this, &armarx::SharedRemoteHandleState::sweep, static_cast<int>(deletionDelayMs)}}
72  {
73  sweeper->start();
74  }
75 
77  {
78  std::lock_guard<std::mutex> stateGuard {stateMutex};
79  sweeper->stop();
80 
81  //delete all rhs
82  for (auto& rh : rhs)
83  {
84  rh->cleanup();
85  }
86  rhs.clear();
87  }
88 
90  {
91  std::lock_guard<std::mutex> stateGuard {stateMutex};
92  rhs.emplace(std::move(rh));
93  }
94 
95  void SharedRemoteHandleState::sweep()
96  {
97  auto deletionTime = RemoteHandleControlBlock::clock::now() - deletionDelay;
98  std::lock_guard<std::mutex> stateGuard {stateMutex};
99  auto blockIt = rhs.begin();
100  while (blockIt != rhs.end())
101  {
102  auto curBlockIt = blockIt++;
103  auto& block = *curBlockIt;
104 
105  std::unique_lock<std::mutex> blockGuard {block->mtx};
106  if (
107  //count is 0 and it reached zero more than deletionDelay ago
108  (!block->useCount && deletionTime > block->lastTimeUseCountReachedZero) ||
109  block->forcedDeletion
110  )
111  {
112  //clean up
113  block->cleanup();
114  //remove from set (blockIt stays valid)
115  rhs.erase(std::move(curBlockIt));
116  }
117  }
118  }
119 }
armarx::SharedRemoteHandleState::add
void add(RemoteHandleControlBlockPtr rh)
Definition: RemoteHandleControlBlock.cpp:89
armarx::SharedRemoteHandleState::~SharedRemoteHandleState
~SharedRemoteHandleState()
Definition: RemoteHandleControlBlock.cpp:76
IceInternal::Handle
Definition: forward_declarations.h:8
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::RemoteHandleControlBlock::decrementUseCount
void decrementUseCount(const Ice::Current &=Ice::emptyCurrent) override
decrements the useCount.
Definition: RemoteHandleControlBlock.cpp:33
RemoteHandleControlBlock.h
armarx::RemoteHandleControlBlock::getClientSideRemoteHandleControlBlock
ClientSideRemoteHandleControlBlockBasePtr getClientSideRemoteHandleControlBlock(const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteHandleControlBlock.cpp:43
armarx::ClientSideRemoteHandleControlBlock
The ClientSideRemoteHandleControlBlock is used at the client side for reference counting.
Definition: ClientSideRemoteHandleControlBlock.h:45
armarx::SharedRemoteHandleState::SharedRemoteHandleState
SharedRemoteHandleState(unsigned int deletionDelayMs)
Definition: RemoteHandleControlBlock.cpp:69
armarx::SharedRemoteHandleState::DEFAULT_DELETION_DELAY
static const unsigned int DEFAULT_DELETION_DELAY
The amount of time (in ms) required to pass after a RemoteHandleControlBlock's usecount has reacht ze...
Definition: RemoteHandleControlBlock.h:58
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28