Writer.cpp
Go to the documentation of this file.
1#include "Writer.h"
2
3#include <unistd.h> // For getpid()
4
7
8#include "../error.h"
9
11{
12
13 Writer::Writer(server::WritingMemoryInterfacePrx memory) : memory(memory)
14 {
15 // Automatically add PID to Ice context for client tracking
16 if (memory)
17 {
18 Ice::Context ctx;
19 ctx["clientPid"] = std::to_string(getpid());
20 this->memory = memory->ice_context(ctx);
21 }
22 }
23
24 data::AddSegmentResult
25 Writer::addSegment(const std::string& coreSegmentName,
26 const std::string& providerSegmentName,
27 bool clearWhenExists) const
28 {
29 data::AddSegmentInput input;
30 input.coreSegmentName = coreSegmentName;
31 input.providerSegmentName = providerSegmentName;
32 input.clearWhenExists = clearWhenExists;
33 return addSegment(input);
34 }
35
36 data::AddSegmentResult
37 Writer::addSegment(const MemoryID& providerSegmentID, bool clearWhenExists) const
38 {
39 return addSegment(providerSegmentID.coreSegmentName,
40 providerSegmentID.providerSegmentName,
41 clearWhenExists);
42 }
43
44 data::AddSegmentResult
45 Writer::addSegment(const std::pair<std::string, std::string>& names, bool clearWhenExists) const
46 {
47 return addSegment(names.first, names.second, clearWhenExists);
48 }
49
50 data::AddSegmentResult
51 Writer::addSegment(const data::AddSegmentInput& input) const
52 {
53 data::AddSegmentsResult results = addSegments({input});
54 ARMARX_CHECK_EQUAL(results.size(), 1);
55 return results.at(0);
56 }
57
58 data::AddSegmentsResult
59 Writer::addSegments(const data::AddSegmentsInput& inputs) const
60 {
62 data::AddSegmentsResult results = memory->addSegments(inputs);
63 ARMARX_CHECK_EQUAL(results.size(), inputs.size());
64 return results;
65 }
66
69 {
71
72 data::Commit commitIce;
73 toIce(commitIce, commit);
74
75 data::CommitResult resultIce = this->_commit(commitIce);
76
78 fromIce(resultIce, result);
79
80 return result;
81 }
82
83 data::CommitResult
84 Writer::commit(const data::Commit& _commit) const
85 {
86 data::Commit commit = _commit;
87 return this->_commit(commit);
88 }
89
91 Writer::commit(const EntityUpdate& update) const
92 {
94 commit.updates.push_back(update);
95
96 armem::CommitResult result = this->commit(commit);
97 ARMARX_CHECK_EQUAL(result.results.size(), 1);
98 return result.results.at(0);
99 }
100
102 Writer::commit(const MemoryID& entityID,
103 const std::vector<aron::data::DictPtr>& instancesData,
104 Time referencedTime,
105 const std::string& origin) const
106 {
107 EntityUpdate update;
108 update.entityID = entityID;
109 update.instancesData = instancesData;
110 update.referencedTime = referencedTime;
111 update.origin = origin;
112 return commit(update);
113 }
114
116 {
117 memory->clearWorkingMemory();
118 }
119
120 void
121 Writer::setWritingMemory(server::WritingMemoryInterfacePrx memory)
122 {
123 this->memory = memory;
124 }
125
126 data::CommitResult
127 Writer::_commit(data::Commit& commit) const
128 {
130
131 /*
132 * This function sets the `timeSent` of each `EntityUpdate` before
133 * sending the data. To allow that, `commit` needs to bo non-const.
134 * Otherwise, the function would need to make a copy of `commit`,
135 * which is not necessary in all cases; e.g. when called by
136 * `commit(Const Commit&)`, which converts the commit to ice types
137 * anyway.
138 */
139
140 const Time timeSent = armem::Time::Now();
141 for (data::EntityUpdate& update : commit.updates)
142 {
143 toIce(update.timeSent, timeSent);
144 }
145
146 data::CommitResult result;
147 auto handleError = [&commit, &result](const std::string& what)
148 {
149 for (const auto& _ : commit.updates)
150 {
151 (void)_;
152 data::EntityUpdateResult& r = result.results.emplace_back();
153 r.success = false;
154 r.errorMessage = "Memory component not registered.\n" + std::string(what);
155 }
156 };
157
158 try
159 {
160 result = memory->commit(commit);
161 }
162 catch (const Ice::ConnectionRefusedException& e)
163 {
164 handleError(e.what());
165 }
166 catch (const Ice::ConnectionLostException& e)
167 {
168 handleError(e.what());
169 }
170 catch (const Ice::NotRegisteredException& e)
171 {
172 handleError(e.what());
173 }
174
175 return result;
176 }
177} // namespace armarx::armem::client
178
179std::ostream&
180armarx::armem::data::operator<<(std::ostream& os, const AddSegmentInput& rhs)
181{
182 return os << "AddSegmentInput: "
183 << "\n- core segment: \t'" << rhs.coreSegmentName << "'"
184 << "\n- provider segment: \t'" << rhs.providerSegmentName << "'"
185 << "\n- clear when exists:\t" << rhs.clearWhenExists << ""
186 << "\n";
187}
188
189std::ostream&
190armarx::armem::data::operator<<(std::ostream& os, const AddSegmentsInput& rhs)
191{
192 for (size_t i = 0; i < rhs.size(); ++i)
193 {
194 os << "[" << i << "] " << rhs[i];
195 }
196 return os;
197}
198
199std::ostream&
200armarx::armem::data::operator<<(std::ostream& os, const AddSegmentResult& rhs)
201{
202 return os << "AddSegmentResult:"
203 << "\n- success: \t" << rhs.success << "\n- segment ID: \t'" << rhs.segmentID
204 << "'"
205 << "\n- error message: \t" << rhs.errorMessage << "\n";
206}
207
208std::ostream&
209armarx::armem::data::operator<<(std::ostream& os, const AddSegmentsResult& rhs)
210{
211 for (size_t i = 0; i < rhs.size(); ++i)
212 {
213 os << "[" << i << "] " << rhs[i];
214 }
215 return os;
216}
std::string coreSegmentName
Definition MemoryID.h:51
std::string providerSegmentName
Definition MemoryID.h:52
void clearWorkingMemory()
Clears the servers working memory (uses the WM-Clear feature)
Definition Writer.cpp:115
server::WritingMemoryInterfacePrx memory
Definition Writer.h:82
void setWritingMemory(server::WritingMemoryInterfacePrx memory)
Definition Writer.cpp:121
data::AddSegmentResult addSegment(const std::string &coreSegmentName, const std::string &providerSegmentName, bool clearWhenExists=false) const
Definition Writer.cpp:25
Writer(const Writer &)=default
Construct a memory writer.
CommitResult commit(const Commit &commit) const
Writes a Commit to the memory.
Definition Writer.cpp:68
data::AddSegmentsResult addSegments(const data::AddSegmentsInput &input) const
Definition Writer.cpp:59
static DateTime Now()
Definition DateTime.cpp:51
Brief description of class memory.
Definition memory.h:39
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
This file is part of ArmarX.
void fromIce(const armem::query::data::Input &ice, QueryInput &input)
Definition Query.cpp:56
void toIce(armem::query::data::Input &ice, const QueryInput &input)
Definition Query.cpp:49
std::ostream & operator<<(std::ostream &os, const AddSegmentInput &rhs)
Definition Writer.cpp:180
armarx::core::time::DateTime Time
Result of a Commit.
Definition Commit.h:111
std::vector< EntityUpdateResult > results
Definition Commit.h:112
A bundle of updates to be sent to the memory.
Definition Commit.h:90
Result of an EntityUpdate.
Definition Commit.h:75
An update of an entity for a specific point in time.
Definition Commit.h:26