Yet Another Robot Platform
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2018 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 <yarp/os/all.h>
11 #include <tcpros_carrier_api.h>
13 // temporary slave
15 private:
17  std::string hostname;
18  int portnum;
20  bool verbose;
21  bool worked;
22 public:
23  RosSlave(bool verbose) :
24  portnum(-1),
25  done(0),
26  verbose(verbose),
27  worked(false)
28  {}
30  void start(const char *hostname, int portnum) {
31  this->hostname = hostname;
32  this->portnum = portnum;
33  slave.setVerbosity(-1);
34  slave.setReader(*this);
35  slave.open("...");
36  }
38  void stop() {
39  double delay = 0.1;
40  while (!done.check()) {
41  if (delay>1) {
42  worked = false;
43  break;
44  }
45  // Always use SystemClock for this delay
47  delay *= 2;
48  }
49  if (delay<=1) {
50  worked = true;
51  }
52  slave.close();
53  }
56  return slave.where();
57  }
59  bool isOk() {
60  return worked;
61  }
63  virtual bool read(yarp::os::ConnectionReader& reader) override {
64  yarp::os::Bottle cmd, reply;
65  bool ok = cmd.read(reader);
66  if (!ok) return false;
67  if (verbose) printf("slave got request %s\n", cmd.toString().c_str());
68  reply.addInt32(1);
69  reply.addString("");
70  yarp::os::Bottle& lst = reply.addList();
71  lst.addString("TCPROS");
72  lst.addString(hostname.c_str());
73  lst.addInt32(portnum);
74  yarp::os::ConnectionWriter *writer = reader.getWriter();
75  if (writer==NULL) { return false; }
76  if (verbose) printf("replying with %s\n", reply.toString().c_str());
77  reply.write(*writer);
78  done.post();
79  return true;
80  }
81 };
void stop()
Definition: RosSlave.h:38
void setVerbosity(int level)
Set whether the port should issue messages about its operations.
Definition: Port.cpp:636
virtual bool read(yarp::os::ConnectionReader &reader) override
Read this object from a network connection.
Definition: RosSlave.h:63
bool verbose
Definition: RosSlave.h:20
std::string hostname
Definition: RosSlave.h:17
RosSlave(bool verbose)
Definition: RosSlave.h:23
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:129
bool isOk()
Definition: RosSlave.h:59
yarp::os::Semaphore done
Definition: RosSlave.h:19
Contact where() const override
Returns information about how this port can be reached.
Definition: Port.cpp:409
A mini-server for network communication.
Definition: Port.h:50
int portnum
Definition: RosSlave.h:18
yarp::os::Port slave
Definition: RosSlave.h:16
bool check()
Decrement the counter, unless that would require waiting.
Definition: Semaphore.cpp:110
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:115
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:21
void start(const char *hostname, int portnum)
Definition: RosSlave.h:30
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:70
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:55
An interface for reading from a network connection.
static void delaySystem(double seconds)
Definition: SystemClock.cpp:15
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:34
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:505
void close() override
Stop port activity.
Definition: Port.cpp:369