45 using Waiters = std::list<std::pair<double, Semaphore*>>;
71 auto waiter_it = waiters->begin();
72 while (waiter_it != waiters->end()) {
73 Semaphore* waiterSemaphore = waiter_it->second;
74 waiter_it = waiters->erase(waiter_it);
75 if (waiterSemaphore !=
nullptr) {
76 waiterSemaphore->
post();
84 NetworkBase::disconnect(clockName, port.getName(), style);
90 bool ok = bot.
read(reader);
97 if (!ok && !closing) {
98 yCError(NETWORKCLOCK,
"Error reading clock port");
103 double oldTime = _time;
106 _time = sec + (nsec * 1e-9);
111 auto waiter_it = waiters->begin();
112 if (oldTime > _time) {
115 waiter_it->first = _time + (waiter_it->first - oldTime);
118 while (waiter_it != waiters->end())
120 if (waiter_it->first - _time < 1E-12) {
121 Semaphore* waiterSemaphore = waiter_it->second;
122 waiter_it = waiters->erase(waiter_it);
123 if (waiterSemaphore !=
nullptr) {
124 waiterSemaphore->
post();
135 NetworkClock::NetworkClock() :
142 yCWarning(NETWORKCLOCK,
"Destroying network clock");
148 mPriv->port.setReadOnly();
149 mPriv->port.setReader(*mPriv);
150 if (!clockSourcePortName.empty()) {
151 mPriv->clockName = clockSourcePortName;
158 if (localPortName.empty()) {
159 const int MAX_STRING_SIZE = 255;
160 char hostName[MAX_STRING_SIZE];
167 localPortName += std::string(hostName) +
"/" + processInfo.
name +
"/" + std::string(
std::to_string(processInfo.
pid)) +
"/clock:i";
171 bool ret = mPriv->port.open(localPortName);
182 yCError(NETWORKCLOCK,
"Cannot find time port \"%s\" or a time topic \"%s@\"\n", mPriv->clockName.c_str(), mPriv->clockName.c_str());
191 mPriv->timeMutex.lock();
192 double result = mPriv->_time;
193 mPriv->timeMutex.unlock();
199 if (seconds <= 1E-12) {
203 mPriv->listMutex.lock();
204 if (mPriv->closing) {
207 mPriv->listMutex.unlock();
212 std::pair<double, Semaphore*> waiter(
now() + seconds,
new Semaphore(0));
213 mPriv->waiters->push_back(waiter);
214 mPriv->listMutex.unlock();
216 waiter.second->wait();
217 delete waiter.second;
218 waiter.second =
nullptr;
223 return mPriv->initted;
std::list< std::pair< double, Semaphore * > > Waiters
bool read(ConnectionReader &reader) override
Read this object from a network connection.
A simple collection of objects that can be described and transmitted in a portable way.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
An interface for reading from a network connection.
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
static Contact queryName(const std::string &name)
Find out information about a registered name.
void delay(double seconds) override
Wait for a certain number of seconds.
bool isValid() const override
Check if time is valid (non-zero).
bool open(const std::string &clockSourcePortName, std::string localPortName="")
double now() override
Return the current time in seconds, relative to an arbitrary starting point.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
A mini-server for network communication.
A class for thread synchronization and mutual exclusion.
void post()
Increment the counter.
static void delaySystem(double seconds)
static ProcessInfo getProcessInfo(int pid=0)
gets the operating system process information given by its PID.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
std::string to_string(IntegerType x)
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
void gethostname(char *hostname, size_t size)
Portable wrapper for the gethostname() function.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
The ProcessInfo struct provides the operating system process information.