YARP
Yet Another Robot Platform
RobotDescriptionClient.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
8 #include <yarp/os/Log.h>
9 #include <yarp/os/LogComponent.h>
10 #include <yarp/os/LogStream.h>
11 
14 using namespace yarp::dev;
15 using namespace yarp::os;
16 using namespace yarp::sig;
17 
18 
19 namespace {
20 YARP_LOG_COMPONENT(ROBOTDESCRIPTIONCLIENT, "yarp.device.robotDescriptionClient")
21 }
22 
23 
24 //------------------------------------------------------------------------------------------------------------------------------
25 
27 {
28  m_local_name.clear();
29  m_remote_name.clear();
30 
31  m_local_name = config.find("local").asString();
32  m_remote_name = config.find("remote").asString();
33 
34  if (m_local_name == "")
35  {
36  yCError(ROBOTDESCRIPTIONCLIENT, "open(): Invalid local name");
37  return false;
38  }
39 
40  if (m_remote_name == "")
41  {
42  yCError(ROBOTDESCRIPTIONCLIENT, "open(): Invalid remote name");
43  return false;
44  }
45 
46  std::string local_rpc, remote_rpc;
47 
48  local_rpc = m_local_name + "/rpc";
49  remote_rpc = m_remote_name + "/rpc";
50 
51  if (!m_rpc_port.open(local_rpc))
52  {
53  yCError(ROBOTDESCRIPTIONCLIENT, "open(): Could not open rpc port %s, check network", local_rpc.c_str());
54  return false;
55  }
56 
57 
58  bool ok = true;
59 
60  ok = Network::connect(local_rpc, remote_rpc);
61  if (!ok)
62  {
63  yCError(ROBOTDESCRIPTIONCLIENT, "open(): Could not connect to %s", remote_rpc.c_str());
64  return false;
65  }
66 
67 
68  return true;
69 }
70 
72 {
73  m_rpc_port.close();
74 
75  return true;
76 }
77 
78 bool RobotDescriptionClient::getAllDevicesByType(const std::string &type, std::vector<DeviceDescription>& dev_list)
79 {
81  yarp::os::Bottle resp;
82  dev_list.clear();
83 
87  b.addString(type);
88  bool ret = m_rpc_port.write(b, resp);
89  if (ret)
90  {
91  if (resp.get(0).asVocab32() != VOCAB_OK)
92  {
93  yCError(ROBOTDESCRIPTIONCLIENT) << "getAllDevices(): Received error from server";
94  return false;
95  }
96  else
97  {
98  Bottle *b = resp.get(1).asList();
99  for (size_t i = 0; i < b->size(); i += 2)
100  {
101  DeviceDescription desc;
102  desc.device_name = b->get(i).asString();
103  desc.device_type = b->get(i + 1).asString();
104  dev_list.push_back(desc);
105  }
106  return true;
107  }
108  }
109  else
110  {
111  yCError(ROBOTDESCRIPTIONCLIENT) << "Error on writing on rpc port";
112  return false;
113  }
114  return true;
115 }
116 
117 bool RobotDescriptionClient::unregisterDevice(const std::string& device_name)
118 {
120  yarp::os::Bottle resp;
121 
125  b.addString(device_name);
126  bool ret = m_rpc_port.write(b, resp);
127  if (ret)
128  {
129  if (resp.get(0).asVocab32() != VOCAB_OK)
130  {
131  yCError(ROBOTDESCRIPTIONCLIENT) << "unregisterDevice(): Received error from server";
132  return false;
133  }
134  }
135  else
136  {
137  yCError(ROBOTDESCRIPTIONCLIENT) << "Error on writing on rpc port";
138  return false;
139  }
140  return true;
141 }
142 
144 {
146  yarp::os::Bottle resp;
147 
151  b.addString(dev.device_name);
152  b.addString(dev.device_type);
153  bool ret = m_rpc_port.write(b, resp);
154  if (ret)
155  {
156  if (resp.get(0).asVocab32() != VOCAB_OK)
157  {
158  yCError(ROBOTDESCRIPTIONCLIENT) << "registerDevice(): Received error from server";
159  return false;
160  }
161  }
162  else
163  {
164  yCError(ROBOTDESCRIPTIONCLIENT) << "Error on writing on rpc port";
165  return false;
166  }
167  return true;
168 }
169 
170 bool RobotDescriptionClient::getAllDevices(std::vector<DeviceDescription>& dev_list)
171 {
173  yarp::os::Bottle resp;
174  dev_list.clear();
175 
179  bool ret = m_rpc_port.write(b, resp);
180  if (ret)
181  {
182  if (resp.get(0).asVocab32() != VOCAB_OK)
183  {
184  yCError(ROBOTDESCRIPTIONCLIENT) << "getAllDevices(): Received error from server";
185  return false;
186  }
187  else
188  {
189  Bottle *b = resp.get(1).asList();
190  for (size_t i = 0; i < b->size();i+=2)
191  {
192  DeviceDescription desc;
193  desc.device_name = b->get(i).asString();
194  desc.device_type = b->get(i+1).asString();
195  dev_list.push_back(desc);
196  }
197  return true;
198  }
199  }
200  else
201  {
202  yCError(ROBOTDESCRIPTIONCLIENT) << "Error on writing on rpc port";
203  return false;
204  }
205  return true;
206 }
constexpr yarp::conf::vocab32_t VOCAB_OK
Definition: GenericVocabs.h:15
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_SET
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DEVICE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DELETE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DESCRIPTION
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_BY_TYPE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_ALL
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_GET
bool ret
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool getAllDevices(std::vector< yarp::dev::DeviceDescription > &dev_list) override
Ask the complete list of all yarp device drivers registered by a robot description server.
bool registerDevice(const yarp::dev::DeviceDescription &dev) override
Register a new running yarp device into a robot description server.
bool unregisterDevice(const std::string &device_name) override
Unregister a running yarp device from a robot description server.
bool close() override
Close the DeviceDriver.
bool getAllDevicesByType(const std::string &type, std::vector< yarp::dev::DeviceDescription > &dev_list) override
Ask a list of all registered yarp device drivers whose type corresponds to the given param.
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:164
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:251
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:121
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:170
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:228
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:240
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
#define yCError(component,...)
Definition: LogComponent.h:154
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22