|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
48 using Waiters = std::list<std::pair<double, Semaphore*>>;
74 auto waiter_it = waiters->begin();
75 while (waiter_it != waiters->end()) {
76 Semaphore* waiterSemaphore = waiter_it->second;
77 waiter_it = waiters->erase(waiter_it);
78 if (waiterSemaphore !=
nullptr) {
79 waiterSemaphore->
post();
87 NetworkBase::disconnect(clockName, port.getName(), style);
93 bool ok = bot.
read(reader);
100 if (!ok && !closing) {
101 yCError(NETWORKCLOCK,
"Error reading clock port");
108 _time = sec + (nsec * 1e-9);
113 auto waiter_it = waiters->begin();
114 while (waiter_it != waiters->end()) {
115 if (waiter_it->first - _time < 1E-12) {
116 Semaphore* waiterSemaphore = waiter_it->second;
117 waiter_it = waiters->erase(waiter_it);
118 if (waiterSemaphore !=
nullptr) {
119 waiterSemaphore->
post();
130 NetworkClock::NetworkClock() :
137 yCWarning(NETWORKCLOCK,
"Destroying network clock");
143 mPriv->port.setReadOnly();
144 mPriv->port.setReader(*mPriv);
145 if (!clockSourcePortName.empty()) {
146 mPriv->clockName = clockSourcePortName;
153 if (localPortName.empty()) {
154 const int MAX_STRING_SIZE = 255;
155 char hostName[MAX_STRING_SIZE];
162 localPortName += std::string(hostName) +
"/" + processInfo.
name +
"/" + std::string(std::to_string(processInfo.
pid)) +
"/clock:i";
166 bool ret = mPriv->port.open(localPortName);
177 yCError(NETWORKCLOCK,
"Cannot find time port \"%s\" or a time topic \"%s@\"\n", mPriv->clockName.c_str(), mPriv->clockName.c_str());
186 mPriv->timeMutex.lock();
187 double result = mPriv->_time;
188 mPriv->timeMutex.unlock();
194 if (seconds <= 1E-12) {
198 mPriv->listMutex.lock();
199 if (mPriv->closing) {
202 mPriv->listMutex.unlock();
207 std::pair<double, Semaphore*> waiter(
now() + seconds,
new Semaphore(0));
208 mPriv->waiters->push_back(waiter);
209 mPriv->listMutex.unlock();
211 waiter.second->wait();
212 delete waiter.second;
213 waiter.second =
nullptr;
218 return mPriv->initted;
A simple collection of objects that can be described and transmitted in a portable way.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
A class for thread synchronization and mutual exclusion.
#define yCWarning(component,...)
bool isValid() const override
Check if time is valid (non-zero).
std::list< std::pair< double, Semaphore * > > Waiters
bool read(ConnectionReader &reader) override
Read this object from a network connection.
void delay(double seconds) override
Wait for a certain number of seconds.
A mini-server for network communication.
static Contact queryName(const std::string &name)
Find out information about a registered name.
double now() override
Return the current time in seconds, relative to an arbitrary starting point.
void post()
Increment the counter.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
static void delaySystem(double seconds)
bool open(const std::string &clockSourcePortName, std::string localPortName="")
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 ProcessInfo getProcessInfo(int pid=0)
gets the operating system process information given by its PID.
void gethostname(char *hostname, size_t size)
Portable wrapper for the gethostname() function.
An interface for reading from a network connection.
#define yCError(component,...)
virtual std::int32_t asInt32() const
Get 32-bit integer value.
The ProcessInfo struct provides the operating system process information.
An interface to the operating system, including Port based communication.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.