YARP
Yet Another Robot Platform
Stamp.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2019 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 
19 yarp::os::Stamp::Stamp(int count, double time)
20 {
21  sequenceNumber = count;
22  timeStamp = time;
23 }
24 
26 {
27  sequenceNumber = -1;
28  timeStamp = 0;
29 }
30 
32 {
33  return sequenceNumber;
34 }
35 
37 {
38  return timeStamp;
39 }
40 
42 {
43  return sequenceNumber >= 0;
44 }
45 
47 {
48  if (connection.isTextMode()) {
49  std::string stampStr = connection.expectText();
50  int seqNum;
51  double ts;
52  int ret = std::sscanf(stampStr.c_str(), "%d %lg\n", &seqNum, &ts);
53  if (ret != 2) {
54  sequenceNumber = -1;
55  timeStamp = 0;
56  return false;
57  }
58  sequenceNumber = seqNum;
59  timeStamp = ts;
60  } else {
61  connection.convertTextMode();
62  std::int32_t header = connection.expectInt32();
63  if (header != BOTTLE_TAG_LIST) {
64  return false;
65  }
66  std::int32_t len = connection.expectInt32();
67  if (len != 2) {
68  return false;
69  }
70  std::int32_t code = connection.expectInt32();
71  if (code != BOTTLE_TAG_INT32) {
72  return false;
73  }
74  sequenceNumber = connection.expectInt32();
75  code = connection.expectInt32();
76  if (code != BOTTLE_TAG_FLOAT64) {
77  return false;
78  }
79  timeStamp = connection.expectFloat64();
80  if (connection.isError()) {
81  sequenceNumber = -1;
82  timeStamp = 0;
83  return false;
84  }
85  }
86  return !connection.isError();
87 }
88 
90 {
91  if (connection.isTextMode()) {
92  char buf[512];
93  std::snprintf(buf, 512, "%d %.*g", sequenceNumber, DBL_DIG, timeStamp);
94  connection.appendText(buf);
95  } else {
96  connection.appendInt32(BOTTLE_TAG_LIST); // nested structure
97  connection.appendInt32(2); // with two elements
98  connection.appendInt32(BOTTLE_TAG_INT32);
99  connection.appendInt32(sequenceNumber);
100  connection.appendInt32(BOTTLE_TAG_FLOAT64);
101  connection.appendFloat64(timeStamp);
102  connection.convertTextMode();
103  }
104  return !connection.isError();
105 }
106 
108 {
109  // a very conservative maximum
110  return 32767;
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
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition: Stamp.cpp:113
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:336
#define BOTTLE_TAG_INT32
Definition: Bottle.h:23
bool ret
bool isValid() const
Check if this Stamp is valid.
Definition: Stamp.cpp:41
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:334
bool read(ConnectionReader &connection) override
Read this object from a network connection.
Definition: Stamp.cpp:46
virtual ~Stamped()
Destructor.
virtual bool isError() const =0
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:335
Stamp()
Construct an invalid Stamp.
Definition: Stamp.cpp:25
virtual bool convertTextMode()=0
Reads in a standard description in text mode, and converts it to a standard description in binary...
An interface for writing to a network connection.
virtual void appendInt32(std::int32_t data)=0
Send a representation of a 32-bit integer to the network connection.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:121
virtual bool isTextMode() const =0
Check if the connection is text mode.
virtual yarp::conf::float64_t expectFloat64()=0
Read a 64-bit floating point number from the network connection.
virtual bool isError() const =0
virtual std::string expectText(const char terminatingChar='\n')=0
Read some text from the network connection.
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:27
virtual std::int32_t expectInt32()=0
Read a 32-bit integer from the network connection.
virtual bool isTextMode() const =0
Check if the connection is text mode.
int getMaxCount() const
Get the maximum sequence number, after which an incrementing sequence should return to zero...
Definition: Stamp.cpp:107
An interface for reading from a network connection.
virtual void appendText(const std::string &str, const char terminate='\n')=0
Send a terminated string to the network connection.
bool write(ConnectionWriter &connection) const override
Write this object to a network connection.
Definition: Stamp.cpp:89
double getTime() const
Get the time stamp.
Definition: Stamp.cpp:36
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
#define BOTTLE_TAG_LIST
Definition: Bottle.h:30
int getCount() const
Get the sequence number.
Definition: Stamp.cpp:31
virtual void appendFloat64(yarp::conf::float64_t data)=0
Send a representation of a 64-bit floating point number to the network connection.