RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 userTimes.count(key) > 0;
175 }
176 
177 boost::uint64_t MetaData::getUserTime(const string& key) const {
178  map<string, boost::uint64_t>::const_iterator it = userTimes.find(key);
179  if (it == userTimes.end()) {
180  throw invalid_argument("There is no user time with key '" + key + "'.");
181  } else {
182  return it->second;
183  }
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 userInfos.count(key);
221 }
222 
223 string MetaData::getUserInfo(const string& key) const {
224  if (userInfos.count(key)) {
225  return userInfos.find(key)->second;
226  } else {
227  throw invalid_argument(
228  "No meta info registered under key '" + key + "'");
229  }
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 }