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