YARP
Yet Another Robot Platform
RosSlave.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8#include <yarp/os/Bottle.h>
10#include <yarp/os/Port.h>
11#include <yarp/os/Semaphore.h>
12#include <yarp/os/SystemClock.h>
13
14#include <string>
15
16// temporary slave
17class RosSlave :
19{
20private:
21 yarp::os::Port slave;
22 std::string hostname;
23 int portnum;
25 bool worked;
26public:
28 portnum(-1),
29 done(0),
30 worked(false)
31 {}
32
33 void start(const char *hostname, int portnum) {
34 this->hostname = hostname;
35 this->portnum = portnum;
36 slave.setReader(*this);
37 slave.open("...");
38 }
39
40 void stop() {
41 double delay = 0.1;
42 while (!done.check()) {
43 if (delay>1) {
44 worked = false;
45 break;
46 }
47 // Always use SystemClock for this delay
49 delay *= 2;
50 }
51 if (delay<=1) {
52 worked = true;
53 }
54 slave.close();
55 }
56
58 return slave.where();
59 }
60
61 bool isOk() {
62 return worked;
63 }
64
65 bool read(yarp::os::ConnectionReader& reader) override {
66 yarp::os::Bottle cmd, reply;
67 bool ok = cmd.read(reader);
68 if (!ok) {
69 return false;
70 }
71 yCDebug(TCPROSCARRIER, "slave got request %s", cmd.toString().c_str());
72 reply.addInt32(1);
73 reply.addString("");
74 yarp::os::Bottle& lst = reply.addList();
75 lst.addString("TCPROS");
76 lst.addString(hostname.c_str());
77 lst.addInt32(portnum);
78 yarp::os::ConnectionWriter *writer = reader.getWriter();
79 if (writer==NULL) { return false; }
80 yCDebug(TCPROSCARRIER, "replying with %s", reply.toString().c_str());
81 reply.write(*writer);
82 done.post();
83 return true;
84 }
85};
const yarp::os::LogComponent & TCPROSCARRIER()
bool isOk()
Definition: RosSlave.h:61
yarp::os::Contact where()
Definition: RosSlave.h:57
void start(const char *hostname, int portnum)
Definition: RosSlave.h:33
RosSlave()
Definition: RosSlave.h:27
bool read(yarp::os::ConnectionReader &reader) override
Read this object from a network connection.
Definition: RosSlave.h:65
void stop()
Definition: RosSlave.h:40
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:64
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition: Bottle.cpp:182
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition: Bottle.cpp:240
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition: Bottle.cpp:230
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition: Bottle.cpp:140
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:170
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:211
An interface for reading from a network connection.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
An interface for writing to a network connection.
Represents how to reach a part of a YARP network.
Definition: Contact.h:33
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:24
A mini-server for network communication.
Definition: Port.h:46
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition: Port.cpp:511
Contact where() const override
Returns information about how this port can be reached.
Definition: Port.cpp:405
void close() override
Stop port activity.
Definition: Port.cpp:363
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:79
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:25
bool check()
Decrement the counter, unless that would require waiting.
Definition: Semaphore.cpp:106
void post()
Increment the counter.
Definition: Semaphore.cpp:111
static void delaySystem(double seconds)
Definition: SystemClock.cpp:29
#define yCDebug(component,...)
Definition: LogComponent.h:128
void delay(double seconds)
Wait for a certain number of seconds.
Definition: Time.cpp:111