YARP
Yet Another Robot Platform
Stamp.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <yarp/os/Stamp.h>
8 
9 #include <yarp/os/Bottle.h>
12 #include <yarp/os/Time.h>
13 
14 #include <cfloat>
15 #include <limits>
16 
17 yarp::os::Stamp::Stamp(int count, double time)
18 {
19  sequenceNumber = count;
20  timeStamp = time;
21 }
22 
24 {
25  sequenceNumber = -1;
26  timeStamp = 0;
27 }
28 
30 {
31  return sequenceNumber;
32 }
33 
35 {
36  return timeStamp;
37 }
38 
40 {
41  return sequenceNumber >= 0;
42 }
43 
45 {
46  if (connection.isTextMode()) {
47  std::string stampStr = connection.expectText();
48  int seqNum;
49  double ts;
50  int ret = std::sscanf(stampStr.c_str(), "%d %lg\n", &seqNum, &ts);
51  if (ret != 2) {
52  sequenceNumber = -1;
53  timeStamp = 0;
54  return false;
55  }
56  sequenceNumber = seqNum;
57  timeStamp = ts;
58  } else {
59  connection.convertTextMode();
60  std::int32_t header = connection.expectInt32();
61  if (header != BOTTLE_TAG_LIST) {
62  return false;
63  }
64  std::int32_t len = connection.expectInt32();
65  // len should be 2 for Stamp, 3 for Header
66  if (len != 2 && len != 3) {
67  return false;
68  }
69  std::int32_t code = connection.expectInt32();
70  if (code != BOTTLE_TAG_INT32) {
71  return false;
72  }
73  sequenceNumber = connection.expectInt32();
74  code = connection.expectInt32();
75  if (code != BOTTLE_TAG_FLOAT64) {
76  return false;
77  }
78  timeStamp = connection.expectFloat64();
79  if (connection.isError()) {
80  sequenceNumber = -1;
81  timeStamp = 0;
82  return false;
83  }
84 
85  // Read frameId (unless receiving a Stamp), but just discard its value
86  if (len == 3) {
87  code = connection.expectInt32();
88  if (code != BOTTLE_TAG_STRING) {
89  sequenceNumber = -1;
90  timeStamp = 0;
91  return false;
92  }
93  std::string unused = connection.expectString();
94  YARP_UNUSED(unused);
95  }
96 
97  }
98  return !connection.isError();
99 }
100 
102 {
103  if (connection.isTextMode()) {
104  char buf[512];
105  std::snprintf(buf, 512, "%d %.*g", sequenceNumber, DBL_DIG, timeStamp);
106  connection.appendText(buf);
107  } else {
108  connection.appendInt32(BOTTLE_TAG_LIST); // nested structure
109  connection.appendInt32(2); // with two elements
110  connection.appendInt32(BOTTLE_TAG_INT32);
111  connection.appendInt32(sequenceNumber);
112  connection.appendInt32(BOTTLE_TAG_FLOAT64);
113  connection.appendFloat64(timeStamp);
114  connection.convertTextMode();
115  }
116  return !connection.isError();
117 }
118 
120 {
121  return std::numeric_limits<int>::max();
122 }
123 
125 {
126  double now = Time::now();
127 
128  sequenceNumber++;
129  if (sequenceNumber > getMaxCount() || sequenceNumber < 0) {
130  sequenceNumber = 0;
131  }
132  timeStamp = now;
133 }
134 
135 void yarp::os::Stamp::update(double time)
136 {
137  sequenceNumber++;
138  if (sequenceNumber > getMaxCount() || sequenceNumber < 0) {
139  sequenceNumber = 0;
140  }
141  timeStamp = time;
142 }
143 
144 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
147 yarp::os::Stamped::~Stamped() = default;
149 #endif // YARP_NO_DEPRECATED
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:25
#define BOTTLE_TAG_INT32
Definition: Bottle.h:21
#define BOTTLE_TAG_STRING
Definition: Bottle.h:26
#define BOTTLE_TAG_LIST
Definition: Bottle.h:28
bool ret
An interface for reading from a network connection.
virtual std::string expectString()
Read a string from the network connection.
virtual bool isTextMode() const =0
Check if the connection is text mode.
virtual std::int32_t expectInt32()=0
Read a 32-bit integer from the network connection.
virtual std::string expectText(const char terminatingChar='\n')=0
Read some text from the network connection.
virtual bool convertTextMode()=0
Reads in a standard description in text mode, and converts it to a standard description in binary.
virtual bool isError() const =0
virtual yarp::conf::float64_t expectFloat64()=0
Read a 64-bit floating point number from the network connection.
An interface for writing to a network connection.
virtual bool isError() const =0
virtual bool isTextMode() const =0
Check if the connection is text mode.
virtual void appendText(const std::string &str, const char terminate='\n')=0
Send a terminated string to the network connection.
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
virtual void appendInt32(std::int32_t data)=0
Send a representation of a 32-bit integer to the network connection.
virtual void appendFloat64(yarp::conf::float64_t data)=0
Send a representation of a 64-bit floating point number to the network connection.
int getMaxCount() const
Get the maximum sequence number, after which an incrementing sequence should return to zero.
Definition: Stamp.cpp:119
bool read(ConnectionReader &connection) override
Read this object from a network connection.
Definition: Stamp.cpp:44
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition: Stamp.cpp:124
double getTime() const
Get the time stamp.
Definition: Stamp.cpp:34
bool isValid() const
Check if this Stamp is valid.
Definition: Stamp.cpp:39
bool write(ConnectionWriter &connection) const override
Write this object to a network connection.
Definition: Stamp.cpp:101
Stamp()
Construct an invalid Stamp.
Definition: Stamp.cpp:23
int getCount() const
Get the sequence number.
Definition: Stamp.cpp:29
virtual ~Stamped()
Destructor.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:121
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:332
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:331
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:333
#define YARP_UNUSED(var)
Definition: api.h:162