RSB  0.19.0
MetaData.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSB project.
4  *
5  * Copyright (C) 2011 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "MetaData.h"
28 
29 #include <stdexcept>
30 
31 #include <rsc/misc/langutils.h>
32 #include <rsc/runtime/ContainerIO.h>
33 
34 using namespace std;
35 
36 namespace rsb {
37 
38 const boost::posix_time::ptime MetaData::UNIX_EPOCH = boost::posix_time::ptime(
39  boost::gregorian::date(1970, boost::date_time::Jan, 1));
40 
41 MetaData::MetaData() :
42  senderId(false), createTime(rsc::misc::currentTimeMicros()), sendTime(
43  0), receiveTime(0), deliverTime(0) {
44 }
45 
47 }
48 
49 string MetaData::getClassName() const {
50  return "MetaData";
51 }
52 
53 void MetaData::printContents(std::ostream& stream) const {
54  stream << "senderId = " << senderId << ", creationTime = " << createTime
55  << ", sendTime = " << sendTime << ", receiveTime = " << receiveTime
56  << ", deliverTime = " << deliverTime << ", userTimes = "
57  << userTimes << ", userInfos = " << userInfos;
58 }
59 
60 rsc::misc::UUID MetaData::getSenderId() const {
61  return senderId;
62 }
63 
64 void MetaData::setSenderId(const rsc::misc::UUID& senderId) {
65  this->senderId = senderId;
66 }
67 
68 boost::uint64_t MetaData::getCreateTime() const {
69  return createTime;
70 }
71 
72 void MetaData::checkedTimeStampSet(boost::uint64_t& timestamp,
73  const double& proposedValue) {
74  if (proposedValue < 0) {
75  throw invalid_argument("Timestamps must be >= 0");
76  }
77  if (proposedValue < 0.000001) {
78  timestamp = rsc::misc::currentTimeMicros();
79  } else {
80  timestamp = boost::uint64_t((proposedValue * 1000000) + 0.5);
81  }
82 }
83 
84 void MetaData::checkedTimeStampSet(boost::uint64_t& timestamp,
85  const boost::uint64_t& proposedValue) {
86  if (proposedValue == 0) {
87  timestamp = rsc::misc::currentTimeMicros();
88  } else {
89  timestamp = proposedValue;
90  }
91 }
92 
93 void MetaData::checkedTimeStampSet(boost::uint64_t& timestamp,
94  const boost::posix_time::ptime& proposedValue) {
95  boost::posix_time::time_duration delta = proposedValue - UNIX_EPOCH;
96  // TODO convert time if not UTC time zone
97  if (delta.is_negative()) {
98  throw invalid_argument("Specified time is not a unix timestamp");
99  }
100  boost::uint64_t converted = delta.total_microseconds();
101  timestamp = converted;
102 }
103 
104 void MetaData::setCreateTime(const boost::uint64_t& time) {
106 }
107 
108 void MetaData::setCreateTime(const double& time) {
110 }
111 
112 void MetaData::setCreateTime(const boost::posix_time::ptime& time) {
114 }
115 
116 boost::uint64_t MetaData::getSendTime() const {
117  return sendTime;
118 }
119 
120 void MetaData::setSendTime(const boost::uint64_t& time) {
122 }
123 
124 void MetaData::setSendTime(const double& time) {
126 }
127 
128 void MetaData::setSendTime(const boost::posix_time::ptime& time) {
130 }
131 
132 boost::uint64_t MetaData::getReceiveTime() const {
133  return receiveTime;
134 }
135 
136 void MetaData::setReceiveTime(const boost::uint64_t& time) {
138 }
139 
140 void MetaData::setReceiveTime(const double& time) {
142 }
143 
144 void MetaData::setReceiveTime(const boost::posix_time::ptime& time) {
146 }
147 
148 boost::uint64_t MetaData::getDeliverTime() const {
149  return deliverTime;
150 }
151 
152 void MetaData::setDeliverTime(const boost::uint64_t& time) {
154 }
155 
156 void MetaData::setDeliverTime(const double& time) {
158 }
159 
160 void MetaData::setDeliverTime(const boost::posix_time::ptime& time) {
162 }
163 
164 set<string> MetaData::userTimeKeys() const {
165  set<string> keys;
166  for (map<string, boost::uint64_t>::const_iterator it = userTimes.begin();
167  it != userTimes.end(); ++it) {
168  keys.insert(it->first);
169  }
170  return keys;
171 }
172 
173 bool MetaData::hasUserTime(const string& key) const {
174  return this->userTimes.find(key) != this->userTimes.end();
175 }
176 
177 boost::uint64_t MetaData::getUserTime(const string& key) const {
178  map<string, boost::uint64_t>::const_iterator it
179  = this->userTimes.find(key);
180  if (it == this->userTimes.end()) {
181  throw invalid_argument("There is no user time with key '" + key + "'.");
182  }
183  return it->second;
184 }
185 
186 void MetaData::setUserTime(const string& key, const boost::uint64_t& time) {
187  userTimes.erase(key);
188  checkedTimeStampSet(userTimes[key], time);
189 }
190 
191 void MetaData::setUserTime(const string& key, const double& time) {
192  userTimes.erase(key);
193  checkedTimeStampSet(userTimes[key], time);
194 }
195 
196 void MetaData::setUserTime(const string& key,
197  const boost::posix_time::ptime& time) {
198  userTimes.erase(key);
199  checkedTimeStampSet(userTimes[key], time);
200 }
201 
202 map<string, boost::uint64_t>::const_iterator MetaData::userTimesBegin() const {
203  return userTimes.begin();
204 }
205 
206 map<string, boost::uint64_t>::const_iterator MetaData::userTimesEnd() const {
207  return userTimes.end();
208 }
209 
210 set<string> MetaData::userInfoKeys() const {
211  set<string> keys;
212  for (map<string, string>::const_iterator it = userInfos.begin();
213  it != userInfos.end(); ++it) {
214  keys.insert(it->first);
215  }
216  return keys;
217 }
218 
219 bool MetaData::hasUserInfo(const string& key) const {
220  return this->userInfos.find(key) != this->userInfos.end();
221 }
222 
223 string MetaData::getUserInfo(const string& key) const {
224  map<string, string>::const_iterator it = this->userInfos.find(key);
225  if (it == this->userInfos.end()) {
226  throw invalid_argument(
227  "No meta info registered under key '" + key + "'");
228  }
229  return it->second;
230 }
231 
232 void MetaData::setUserInfo(const string& key, const string& value) {
233  userInfos.erase(key);
234  userInfos[key] = value;
235 }
236 
237 map<string, string>::const_iterator MetaData::userInfosBegin() const {
238  return userInfos.begin();
239 }
240 
241 map<string, string>::const_iterator MetaData::userInfosEnd() const {
242  return userInfos.end();
243 }
244 
245 bool MetaData::operator==(const MetaData& other) const {
246  return (senderId == other.senderId) && (createTime == other.createTime)
247  && (sendTime == other.sendTime)
248  && (receiveTime == other.receiveTime)
249  && (deliverTime == other.deliverTime)
250  && (userTimes == other.userTimes) && (userInfos == other.userInfos);
251 }
252 
253 ostream& operator<<(ostream& stream, const MetaData& meta) {
254  meta.print(stream);
255  return stream;
256 }
257 
258 }
void setReceiveTime(const boost::uint64_t &time=0)
Sets the time at which an event is received by listener in its encoded form.
Definition: MetaData.cpp:136
void setDeliverTime(const boost::uint64_t &time=0)
Sets the time at which an event was decoded and will be dispatched to the client as soon as possible ...
Definition: MetaData.cpp:152
std::map< std::string, std::string >::const_iterator userInfosBegin() const
Definition: MetaData.cpp:237
bool operator==(const MetaData &other) const
Definition: MetaData.cpp:245
static const boost::posix_time::ptime UNIX_EPOCH
Definition: MetaData.h:261
boost::uint64_t getDeliverTime() const
Returns the time at which an event was decoded and will be dispatched to the client as soon as possib...
Definition: MetaData.cpp:148
STL namespace.
bool hasUserTime(const std::string &key) const
Checks whether a user-provided timestamp with the given key exists.
Definition: MetaData.cpp:173
void setSendTime(const boost::uint64_t &time=0)
Sets the time at which the generated notification for an event was sent on the bus (after serializati...
Definition: MetaData.cpp:120
boost::uint64_t getUserTime(const std::string &key) const
Returns the user timestamp stored under the provided key.
Definition: MetaData.cpp:177
boost::uint64_t createTime
Definition: MetaData.h:263
boost::uint64_t getReceiveTime() const
Returns the time at which an event is received by listener in its encoded form.
Definition: MetaData.cpp:132
boost::uint64_t deliverTime
Definition: MetaData.h:266
std::map< std::string, boost::uint64_t >::const_iterator userTimesBegin() const
Definition: MetaData.cpp:202
void checkedTimeStampSet(boost::uint64_t &timestamp, const boost::uint64_t &proposedValue)
Definition: MetaData.cpp:84
const boost::posix_time::ptime UNIX_EPOCH
void setCreateTime(const boost::uint64_t &time=0)
Sets the time stamp that is automatically filled with the time the event instance was created by the ...
Definition: MetaData.cpp:104
std::string getClassName() const
Definition: MetaData.cpp:49
Framework-supplied meta data attached to each event that give information e.g.
Definition: MetaData.h:55
bool hasUserInfo(const std::string &key) const
Checks whether a user info exists under the provided key.
Definition: MetaData.cpp:219
std::map< std::string, boost::uint64_t > userTimes
Definition: MetaData.h:268
ostream & operator<<(ostream &stream, const MetaData &meta)
Definition: MetaData.cpp:253
void setUserInfo(const std::string &key, const std::string &value)
Sets a user info with the specified key and value or replaces and already existing one...
Definition: MetaData.cpp:232
boost::uint64_t getCreateTime() const
Returns a time stamp that is automatically filled with the time the event instance was created by the...
Definition: MetaData.cpp:68
virtual ~MetaData()
Definition: MetaData.cpp:46
void printContents(std::ostream &stream) const
Definition: MetaData.cpp:53
boost::uint64_t getSendTime() const
Returns the time at which the generated notification for an event was sent on the bus (after serializ...
Definition: MetaData.cpp:116
boost::uint64_t sendTime
Definition: MetaData.h:264
std::set< std::string > userInfoKeys() const
Returns all keys of user-defined infos.
Definition: MetaData.cpp:210
void setUserTime(const std::string &key, const boost::uint64_t &time=0)
Sets a user timestamp and replaces existing entries.
Definition: MetaData.cpp:186
std::map< std::string, std::string > userInfos
Definition: MetaData.h:269
boost::uint64_t receiveTime
Definition: MetaData.h:265
std::map< std::string, std::string >::const_iterator userInfosEnd() const
Definition: MetaData.cpp:241
std::string getUserInfo(const std::string &key) const
Returns the user-defined string for the given key.
Definition: MetaData.cpp:223
std::map< std::string, boost::uint64_t >::const_iterator userTimesEnd() const
Definition: MetaData.cpp:206
rsc::misc::UUID senderId
Definition: MetaData.h:255
std::set< std::string > userTimeKeys() const
Returns the keys of all available user times.
Definition: MetaData.cpp:164