YARP
Yet Another Robot Platform
RobotDescriptionClient.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #include "RobotDescriptionClient.h"
11 #include <yarp/os/Log.h>
12 #include <yarp/os/LogComponent.h>
13 #include <yarp/os/LogStream.h>
14 
17 using namespace yarp::dev;
18 using namespace yarp::os;
19 using namespace yarp::sig;
20 
21 
22 namespace {
23 YARP_LOG_COMPONENT(ROBOTDESCRIPTIONCLIENT, "yarp.device.robotDescriptionClient")
24 }
25 
26 
27 //------------------------------------------------------------------------------------------------------------------------------
28 
30 {
31  m_local_name.clear();
32  m_remote_name.clear();
33 
34  m_local_name = config.find("local").asString();
35  m_remote_name = config.find("remote").asString();
36 
37  if (m_local_name == "")
38  {
39  yCError(ROBOTDESCRIPTIONCLIENT, "open(): Invalid local name");
40  return false;
41  }
42 
43  if (m_remote_name == "")
44  {
45  yCError(ROBOTDESCRIPTIONCLIENT, "open(): Invalid remote name");
46  return false;
47  }
48 
49  std::string local_rpc, remote_rpc;
50 
51  local_rpc = m_local_name + "/rpc";
52  remote_rpc = m_remote_name + "/rpc";
53 
54  if (!m_rpc_port.open(local_rpc))
55  {
56  yCError(ROBOTDESCRIPTIONCLIENT, "open(): Could not open rpc port %s, check network", local_rpc.c_str());
57  return false;
58  }
59 
60 
61  bool ok = true;
62 
63  ok = Network::connect(local_rpc, remote_rpc);
64  if (!ok)
65  {
66  yCError(ROBOTDESCRIPTIONCLIENT, "open(): Could not connect to %s", remote_rpc.c_str());
67  return false;
68  }
69 
70 
71  return true;
72 }
73 
75 {
76  m_rpc_port.close();
77 
78  return true;
79 }
80 
81 bool RobotDescriptionClient::getAllDevicesByType(const std::string &type, std::vector<DeviceDescription>& dev_list)
82 {
84  yarp::os::Bottle resp;
85  dev_list.clear();
86 
90  b.addString(type);
91  bool ret = m_rpc_port.write(b, resp);
92  if (ret)
93  {
94  if (resp.get(0).asVocab() != VOCAB_OK)
95  {
96  yCError(ROBOTDESCRIPTIONCLIENT) << "getAllDevices(): Received error from server";
97  return false;
98  }
99  else
100  {
101  Bottle *b = resp.get(1).asList();
102  for (size_t i = 0; i < b->size(); i += 2)
103  {
104  DeviceDescription desc;
105  desc.device_name = b->get(i).asString();
106  desc.device_type = b->get(i + 1).asString();
107  dev_list.push_back(desc);
108  }
109  return true;
110  }
111  }
112  else
113  {
114  yCError(ROBOTDESCRIPTIONCLIENT) << "Error on writing on rpc port";
115  return false;
116  }
117  return true;
118 }
119 
120 bool RobotDescriptionClient::unregisterDevice(const std::string& device_name)
121 {
123  yarp::os::Bottle resp;
124 
128  b.addString(device_name);
129  bool ret = m_rpc_port.write(b, resp);
130  if (ret)
131  {
132  if (resp.get(0).asVocab() != VOCAB_OK)
133  {
134  yCError(ROBOTDESCRIPTIONCLIENT) << "unregisterDevice(): Received error from server";
135  return false;
136  }
137  }
138  else
139  {
140  yCError(ROBOTDESCRIPTIONCLIENT) << "Error on writing on rpc port";
141  return false;
142  }
143  return true;
144 }
145 
147 {
149  yarp::os::Bottle resp;
150 
154  b.addString(dev.device_name);
155  b.addString(dev.device_type);
156  bool ret = m_rpc_port.write(b, resp);
157  if (ret)
158  {
159  if (resp.get(0).asVocab() != VOCAB_OK)
160  {
161  yCError(ROBOTDESCRIPTIONCLIENT) << "registerDevice(): Received error from server";
162  return false;
163  }
164  }
165  else
166  {
167  yCError(ROBOTDESCRIPTIONCLIENT) << "Error on writing on rpc port";
168  return false;
169  }
170  return true;
171 }
172 
173 bool RobotDescriptionClient::getAllDevices(std::vector<DeviceDescription>& dev_list)
174 {
176  yarp::os::Bottle resp;
177  dev_list.clear();
178 
182  bool ret = m_rpc_port.write(b, resp);
183  if (ret)
184  {
185  if (resp.get(0).asVocab() != VOCAB_OK)
186  {
187  yCError(ROBOTDESCRIPTIONCLIENT) << "getAllDevices(): Received error from server";
188  return false;
189  }
190  else
191  {
192  Bottle *b = resp.get(1).asList();
193  for (size_t i = 0; i < b->size();i+=2)
194  {
195  DeviceDescription desc;
196  desc.device_name = b->get(i).asString();
197  desc.device_type = b->get(i+1).asString();
198  dev_list.push_back(desc);
199  }
200  return true;
201  }
202  }
203  else
204  {
205  yCError(ROBOTDESCRIPTIONCLIENT) << "Error on writing on rpc port";
206  return false;
207  }
208  return true;
209 }
LogStream.h
VOCAB_IROBOT_BY_TYPE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_BY_TYPE
Definition: IRobotDescription.h:74
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Value::asVocab
virtual std::int32_t asVocab() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:231
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::os::Bottle::size
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
yarp::sig
Signal processing.
Definition: Image.h:25
VOCAB_IROBOT_DEVICE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DEVICE
Definition: IRobotDescription.h:73
RobotDescriptionClient::getAllDevices
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.
Definition: RobotDescriptionClient.cpp:173
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
RobotDescriptionClient::close
bool close() override
Close the DeviceDriver.
Definition: RobotDescriptionClient.cpp:74
yarp::dev::DeviceDescription::device_type
std::string device_type
Definition: IRobotDescription.h:23
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
RobotDescriptionClient::unregisterDevice
bool unregisterDevice(const std::string &device_name) override
Unregister a running yarp device from a robot description server.
Definition: RobotDescriptionClient.cpp:120
RobotDescriptionClient::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: RobotDescriptionClient.cpp:29
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
VOCAB_IROBOT_DESCRIPTION
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DESCRIPTION
Definition: IRobotDescription.h:68
Log.h
yarp::dev::DeviceDescription
Definition: IRobotDescription.h:21
yarp::os::Bottle::get
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
VOCAB_IROBOT_SET
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_SET
Definition: IRobotDescription.h:70
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
RobotDescriptionClient.h
yarp::dev::DeviceDescription::device_name
std::string device_name
Definition: IRobotDescription.h:22
yarp::os::Searchable::find
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
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
VOCAB_IROBOT_DELETE
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_DELETE
Definition: IRobotDescription.h:71
yarp::os::Bottle::addVocab
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:167
LogComponent.h
VOCAB_IROBOT_GET
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_GET
Definition: IRobotDescription.h:69
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
RobotDescriptionClient::getAllDevicesByType
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.
Definition: RobotDescriptionClient.cpp:81
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
RobotDescriptionClient::registerDevice
bool registerDevice(const yarp::dev::DeviceDescription &dev) override
Register a new running yarp device into a robot description server.
Definition: RobotDescriptionClient.cpp:146
yarp::os::Value::asList
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:243
VOCAB_IROBOT_ALL
constexpr yarp::conf::vocab32_t VOCAB_IROBOT_ALL
Definition: IRobotDescription.h:72
VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_OK
Definition: GenericVocabs.h:18