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