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