Yet Another Robot Platform
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2019 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  */
9 #include <tcpros_carrier_api.h>
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>
17 #include <string>
19 // temporary slave
21 private:
23  std::string hostname;
24  int portnum;
26  bool verbose;
27  bool worked;
28 public:
29  RosSlave(bool verbose) :
30  portnum(-1),
31  done(0),
32  verbose(verbose),
33  worked(false)
34  {}
36  void start(const char *hostname, int portnum) {
37  this->hostname = hostname;
38  this->portnum = portnum;
39  slave.setVerbosity(-1);
40  slave.setReader(*this);
41  slave.open("...");
42  }
44  void stop() {
45  double delay = 0.1;
46  while (!done.check()) {
47  if (delay>1) {
48  worked = false;
49  break;
50  }
51  // Always use SystemClock for this delay
53  delay *= 2;
54  }
55  if (delay<=1) {
56  worked = true;
57  }
58  slave.close();
59  }
62  return slave.where();
63  }
65  bool isOk() {
66  return worked;
67  }
69  bool read(yarp::os::ConnectionReader& reader) override {
70  yarp::os::Bottle cmd, reply;
71  bool ok = cmd.read(reader);
72  if (!ok) return false;
73  if (verbose) printf("slave got request %s\n", cmd.toString().c_str());
74  reply.addInt32(1);
75  reply.addString("");
76  yarp::os::Bottle& lst = reply.addList();
77  lst.addString("TCPROS");
78  lst.addString(hostname.c_str());
79  lst.addInt32(portnum);
80  yarp::os::ConnectionWriter *writer = reader.getWriter();
81  if (writer==NULL) { return false; }
82  if (verbose) printf("replying with %s\n", reply.toString().c_str());
83  reply.write(*writer);
84  done.post();
85  return true;
86  }
87 };
void stop()
Definition: RosSlave.h:44
void setVerbosity(int level)
Set whether the port should issue messages about its operations.
Definition: Port.cpp:639
bool verbose
Definition: RosSlave.h:26
std::string hostname
Definition: RosSlave.h:23
RosSlave(bool verbose)
Definition: RosSlave.h:29
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:129
bool read(yarp::os::ConnectionReader &reader) override
Read this object from a network connection.
Definition: RosSlave.h:69
bool isOk()
Definition: RosSlave.h:65
yarp::os::Semaphore done
Definition: RosSlave.h:25
Contact where() const override
Returns information about how this port can be reached.
Definition: Port.cpp:413
A mini-server for network communication.
Definition: Port.h:49
int portnum
Definition: RosSlave.h:24
yarp::os::Port slave
Definition: RosSlave.h:22
bool check()
Decrement the counter, unless that would require waiting.
Definition: Semaphore.cpp:109
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:79
An interface for writing to a network connection.
void post()
Increment the counter.
Definition: Semaphore.cpp:114
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:27
bool worked
Definition: RosSlave.h:27
void start(const char *hostname, int portnum)
Definition: RosSlave.h:36
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition: Bottle.cpp:99
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition: Bottle.cpp:189
A simple collection of objects that can be described and transmitted in a portable way...
Definition: Bottle.h:72
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:28
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition: Bottle.cpp:141
yarp::os::Contact where()
Definition: RosSlave.h:61
An interface for reading from a network connection.
static void delaySystem(double seconds)
Definition: SystemClock.cpp:32
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:170
Represents how to reach a part of a YARP network.
Definition: Contact.h:38
void delay(double seconds)
Wait for a certain number of seconds.
Definition: Time.cpp:111
bool read(ConnectionReader &reader) override
Set the bottle&#39;s value based on input from a network connection.
Definition: Bottle.cpp:199
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition: Port.cpp:508
void close() override
Stop port activity.
Definition: Port.cpp:369