SegmentedMemory.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package MemoryX::Core
17* @author ALexey Kozlov ( kozlov at kit dot edu)
18* @date 2013
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#include "SegmentedMemory.h"
24
25#include <mutex>
26
28#include <ArmarXCore/interface/core/Log.h>
29
30namespace memoryx
31{
35
39
40 AbstractMemorySegmentPrx
41 SegmentedMemory::addSegment(const std::string& segmentName,
42 const AbstractMemorySegmentPtr& segment,
43 const ::Ice::Current& c)
44 {
45 std::unique_lock lock(segmentsMutex);
46 // check if a segment with the same name already exists in memory
47 MemorySegmentMap::const_iterator it = segments.find(segmentName);
48
49 if (it != segments.end())
50 {
51 throw SegmentAlreadyExistsException();
52 }
53
54 // assign name
55 segment->setSegmentName(segmentName);
56 segment->setParentMemory(this);
57
58 auto id = segment->getIceId();
59 // register proxy
60 AbstractMemorySegmentPrx segmentProxy =
61 AbstractMemorySegmentPrx::uncheckedCast(getObjectAdapter()->add(segment, id));
62
63 // insert into known segments
64 MemorySegmentEntry segmentEntry;
65 segmentEntry.proxy = segmentProxy;
66 segmentEntry.pointer = segment;
67
68 segments[segmentName] = segmentEntry;
69
70 return segmentProxy;
71 }
72
73 bool
74 SegmentedMemory::hasSegment(const std::string& segmentName, const ::Ice::Current& c) const
75 {
76 std::unique_lock lock(segmentsMutex);
77 return segments.find(segmentName) != segments.end();
78 }
79
80 AbstractMemorySegmentPrx
81 SegmentedMemory::getSegment(const std::string& segmentName, const ::Ice::Current& c) const
82 {
83 std::unique_lock lock(segmentsMutex);
84
85 // find segment
86 MemorySegmentMap::const_iterator iter = segments.find(segmentName);
87
88 if (iter == segments.end())
89 {
90 ARMARX_ERROR_S << "Could not find segment with name " << segmentName << std::endl;
91 throw InvalidEntityException();
92 }
93
94 return iter->second.proxy;
95 }
96
97 void
98 SegmentedMemory::removeSegment(const std::string& segmentName, const ::Ice::Current& c)
99 {
100 std::unique_lock lock(segmentsMutex);
101
102 MemorySegmentMap::iterator iter = segments.find(segmentName);
103
104 if (iter != segments.end())
105 {
106 AbstractMemorySegmentPrx proxy = iter->second.proxy;
107
108 // remove from adapter
109 Ice::Identity id = proxy->ice_getIdentity();
110 getObjectAdapter()->remove(id);
111
112 // remove from known segments
113 segments.erase(iter);
114 }
115 }
116
117 NameList
118 SegmentedMemory::getSegmentNames(const ::Ice::Current&) const
119 {
120 std::unique_lock lock(segmentsMutex);
121
122 NameList result;
123
124 for (MemorySegmentMap::const_iterator it = segments.begin(); it != segments.end(); ++it)
125 {
126 result.push_back(it->first);
127 }
128
129 return result;
130 }
131
132 AbstractMemorySegmentPtr
133 SegmentedMemory::getSegmentPtr(const std::string& segmentName)
134 {
135 std::unique_lock lock(segmentsMutex);
136
137 // find segment
138 MemorySegmentMap::iterator iter = segments.find(segmentName);
139
140 if (iter == segments.end())
141 {
142 ARMARX_ERROR_S << "Could not find segment with name " << segmentName << std::endl;
143 throw InvalidEntityException();
144 }
145
146 return iter->second.pointer;
147 }
148
149 EntityBasePtr
150 memoryx::SegmentedMemory::findEntityById(const std::string& entityId, const Ice::Current& c)
151 {
152 NameList names = getSegmentNames();
153
154 for (auto name : names)
155 {
156 EntityMemorySegmentInterfacePtr segment =
157 memoryx::EntityMemorySegmentInterfacePtr::dynamicCast(getSegmentPtr(name));
158
159 if (!segment)
160 {
161 continue;
162 }
163
164 auto entity = segment->getEntityById(entityId);
165
166 if (entity)
167 {
168 return entity;
169 }
170 }
171
172 return NULL;
173 }
174
175 EntityRefBasePtr
176 memoryx::SegmentedMemory::findEntityRefById(const std::string& entityId, const Ice::Current& c)
177 {
178 NameList names = getSegmentNames();
179
180 for (auto name : names)
181 {
182 EntityMemorySegmentInterfacePtr segment =
183 memoryx::EntityMemorySegmentInterfacePtr::dynamicCast(getSegmentPtr(name));
184
185 if (!segment)
186 {
187 continue;
188 }
189
190 auto entity = segment->getEntityRefById(entityId);
191
192 if (entity)
193 {
194 return entity;
195 }
196 }
197
198 return NULL;
199 }
200} // namespace memoryx
constexpr T c
AbstractMemorySegmentPrx addSegment(const std::string &segmentName, const AbstractMemorySegmentPtr &segment, const ::Ice::Current &=Ice::emptyCurrent) override
virtual Ice::ObjectAdapterPtr getObjectAdapter() const =0
NameList getSegmentNames(const ::Ice::Current &=Ice::emptyCurrent) const override
MemorySegmentMap segments
EntityRefBasePtr findEntityRefById(const std::string &entityId, const Ice::Current &c=Ice::emptyCurrent) override
AbstractMemorySegmentPtr getSegmentPtr(const std::string &segmentName)
void removeSegment(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) override
EntityBasePtr findEntityById(const std::string &entityId, const Ice::Current &c=Ice::emptyCurrent) override
AbstractMemorySegmentPrx getSegment(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) const override
std::shared_mutex segmentsMutex
bool hasSegment(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) const override
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
VirtualRobot headers.
AbstractMemorySegmentPrx proxy
AbstractMemorySegmentPtr pointer