YARP
Yet Another Robot Platform
Map2D_nwc_yarp.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
8#include "Map2D_nwc_yarp.h"
9#include <yarp/os/Log.h>
11#include <yarp/os/LogStream.h>
12#include <mutex>
15
16using namespace yarp::dev;
17using namespace yarp::dev::Nav2D;
18using namespace yarp::os;
19using namespace yarp::sig;
20
21namespace {
22YARP_LOG_COMPONENT(MAP2D_NWC_YARP, "yarp.device.map2D_nwc_yarp")
23}
24
25//------------------------------------------------------------------------------------------------------------------------------
26
28{
29 m_local_name.clear();
30 m_map_server.clear();
31
32 m_local_name = config.find("local").asString();
33 m_map_server = config.find("remote").asString();
34 m_carrier = config.check("carrier", yarp::os::Value("tcp"), "the carrier used for the connection with the server").asString();
35
36 if (m_local_name.empty())
37 {
38 yCError(MAP2D_NWC_YARP, "open() error you have to provide valid local name");
39 return false;
40 }
41 if (m_map_server.empty())
42 {
43 yCError(MAP2D_NWC_YARP, "open() error you have to provide valid remote name");
44 return false;
45 }
46
47 std::string local_rpc1 = m_local_name;
48 local_rpc1 += "/mapClient_rpc";
49
50 std::string remote_rpc1 = m_map_server;
51 remote_rpc1 += "/rpc";
52
53 if (!m_rpcPort_to_Map2D_nws.open(local_rpc1))
54 {
55 yCError(MAP2D_NWC_YARP, "open() error could not open rpc port %s, check network", local_rpc1.c_str());
56 return false;
57 }
58
59 bool ok=false;
60 ok=Network::connect(local_rpc1, remote_rpc1, m_carrier);
61 if (!ok)
62 {
63 yCError(MAP2D_NWC_YARP, "open() error could not connect to %s", remote_rpc1.c_str());
64 return false;
65 }
66
68 {
69 yCError(MAP2D_NWC_YARP, "Error! Cannot attach the port as a client");
70 return false;
71 }
72
73 return true;
74}
75
77{
78 std::lock_guard <std::mutex> lg(m_mutex);
79 return m_map_RPC.store_map_RPC(map);
80}
81
82bool Map2D_nwc_yarp::get_map(std::string map_name, MapGrid2D& map)
83{
84 std::lock_guard <std::mutex> lg(m_mutex);
85 auto ret = m_map_RPC.get_map_RPC(map_name);
86 if (!ret.retval)
87 {
88 yCError(MAP2D_NWC_YARP, "Unable to get_map");
89 return false;
90 }
91 map = ret.themap;
92 return true;
93}
94
96{
97 std::lock_guard <std::mutex> lg(m_mutex);
99}
100
101bool Map2D_nwc_yarp::get_map_names(std::vector<std::string>& map_names)
102{
103 std::lock_guard <std::mutex> lg(m_mutex);
105 if (!ret.retval)
106 {
107 yCError(MAP2D_NWC_YARP, "Unable to get_map_names");
108 return false;
109 }
110 map_names = ret.map_names;
111 return true;
112}
113
114bool Map2D_nwc_yarp::remove_map(std::string map_name)
115{
116 std::lock_guard <std::mutex> lg(m_mutex);
117 return m_map_RPC.remove_map_RPC(map_name);
118}
119
120bool Map2D_nwc_yarp::storeLocation(std::string location_name, Map2DLocation loc)
121{
122 std::lock_guard <std::mutex> lg(m_mutex);
123 return m_map_RPC.store_location_RPC(location_name,loc);
124}
125
126bool Map2D_nwc_yarp::storeArea(std::string area_name, Map2DArea area)
127{
128 std::lock_guard <std::mutex> lg(m_mutex);
129 return m_map_RPC.store_area_RPC(area_name, area);
130}
131
132bool Map2D_nwc_yarp::storePath(std::string path_name, Map2DPath path)
133{
134 std::lock_guard <std::mutex> lg(m_mutex);
135 return m_map_RPC.store_path_RPC(path_name, path);
136}
137
138bool Map2D_nwc_yarp::getLocationsList(std::vector<std::string>& locations)
139{
140 std::lock_guard <std::mutex> lg(m_mutex);
142 if (!ret.retval)
143 {
144 yCError(MAP2D_NWC_YARP, "Unable to getLocationsList");
145 return false;
146 }
147 locations = ret.locations;
148 return true;
149}
150
151bool Map2D_nwc_yarp::getAreasList(std::vector<std::string>& areas)
152{
153 std::lock_guard <std::mutex> lg(m_mutex);
155 if (!ret.retval)
156 {
157 yCError(MAP2D_NWC_YARP, "Unable to getAreasList");
158 return false;
159 }
160 areas = ret.areas;
161 return true;
162}
163
164bool Map2D_nwc_yarp::getPathsList(std::vector<std::string>& paths)
165{
166 std::lock_guard <std::mutex> lg(m_mutex);
168 if (!ret.retval)
169 {
170 yCError(MAP2D_NWC_YARP, "Unable to getPathsList");
171 return false;
172 }
173 paths = ret.paths;
174 return true;
175}
176
177bool Map2D_nwc_yarp::getAllLocations(std::vector<yarp::dev::Nav2D::Map2DLocation>& locations)
178{
179 std::lock_guard <std::mutex> lg(m_mutex);
181 if (!ret.retval)
182 {
183 yCError(MAP2D_NWC_YARP, "Unable to getAllLocations");
184 return false;
185 }
186 locations = ret.locations;
187 return true;
188}
189
190bool Map2D_nwc_yarp::getAllAreas(std::vector<yarp::dev::Nav2D::Map2DArea>& areas)
191{
192 std::lock_guard <std::mutex> lg(m_mutex);
194 if (!ret.retval)
195 {
196 yCError(MAP2D_NWC_YARP, "Unable to getAllAreas");
197 return false;
198 }
199 areas = ret.areas;
200 return true;
201}
202
203bool Map2D_nwc_yarp::getAllPaths(std::vector<yarp::dev::Nav2D::Map2DPath>& paths)
204{
205 std::lock_guard <std::mutex> lg(m_mutex);
207 if (!ret.retval)
208 {
209 yCError(MAP2D_NWC_YARP, "Unable to getAllPaths");
210 return false;
211 }
212 paths = ret.paths;
213 return true;
214}
215
216bool Map2D_nwc_yarp::getLocation(std::string location_name, Map2DLocation& loc)
217{
218 std::lock_guard <std::mutex> lg(m_mutex);
219 auto ret = m_map_RPC.get_location_RPC(location_name);
220 if (!ret.retval)
221 {
222 yCError(MAP2D_NWC_YARP, "Unable to getLocation");
223 return false;
224 }
225 loc = ret.loc;
226 return true;
227}
228
229bool Map2D_nwc_yarp::getArea(std::string area_name, Map2DArea& area)
230{
231 std::lock_guard <std::mutex> lg(m_mutex);
232 auto ret = m_map_RPC.get_area_RPC(area_name);
233 if (!ret.retval)
234 {
235 yCError(MAP2D_NWC_YARP, "Unable to getArea");
236 return false;
237 }
238 area = ret.area;
239 return true;
240}
241
242bool Map2D_nwc_yarp::getPath(std::string path_name, Map2DPath& path)
243{
244 std::lock_guard <std::mutex> lg(m_mutex);
245 auto ret = m_map_RPC.get_path_RPC(path_name);
246 if (!ret.retval)
247 {
248 yCError(MAP2D_NWC_YARP, "Unable to getPath");
249 return false;
250 }
251 path = ret.path;
252 return true;
253}
254
255bool Map2D_nwc_yarp::deleteLocation(std::string location_name)
256{
257 std::lock_guard <std::mutex> lg(m_mutex);
258 return m_map_RPC.delete_location_RPC(location_name);
259}
260
261bool Map2D_nwc_yarp::renameLocation(std::string original_name, std::string new_name)
262{
263 std::lock_guard <std::mutex> lg(m_mutex);
264 return m_map_RPC.rename_location_RPC(original_name,new_name);
265}
266
267bool Map2D_nwc_yarp::deleteArea(std::string location_name)
268{
269 std::lock_guard <std::mutex> lg(m_mutex);
270 return m_map_RPC.delete_area_RPC(location_name);
271}
272
273bool Map2D_nwc_yarp::deletePath(std::string path_name)
274{
275 std::lock_guard <std::mutex> lg(m_mutex);
276 return m_map_RPC.delete_path_RPC(path_name);
277}
278
279bool Map2D_nwc_yarp::renameArea(std::string original_name, std::string new_name)
280{
281 std::lock_guard <std::mutex> lg(m_mutex);
282 return m_map_RPC.rename_area_RPC(original_name, new_name);
283}
284
285bool Map2D_nwc_yarp::renamePath(std::string original_name, std::string new_name)
286{
287 std::lock_guard <std::mutex> lg(m_mutex);
288 return m_map_RPC.rename_path_RPC(original_name, new_name);
289}
290
292{
293 std::lock_guard <std::mutex> lg(m_mutex);
295}
296
298{
299 std::lock_guard <std::mutex> lg(m_mutex);
301}
302
304{
305 std::lock_guard <std::mutex> lg(m_mutex);
307}
308
310{
311 std::lock_guard <std::mutex> lg(m_mutex);
313}
314
316{
317 std::lock_guard <std::mutex> lg(m_mutex);
319}
320
322{
323 return true;
324}
325
326bool Map2D_nwc_yarp::saveMapsCollection(std::string maps_collection)
327{
328 std::lock_guard <std::mutex> lg(m_mutex);
329 return m_map_RPC.save_maps_collection_RPC(maps_collection);
330}
331
332bool Map2D_nwc_yarp::loadMapsCollection(std::string maps_collection)
333{
334 std::lock_guard <std::mutex> lg(m_mutex);
335 return m_map_RPC.load_maps_collection_RPC(maps_collection);
336}
337
338bool Map2D_nwc_yarp::saveLocationsAndExtras(std::string locations_collection)
339{
340 std::lock_guard <std::mutex> lg(m_mutex);
341 return m_map_RPC.save_locations_and_extras_RPC(locations_collection);
342}
343
344bool Map2D_nwc_yarp::loadLocationsAndExtras(std::string locations_collection)
345{
346 std::lock_guard <std::mutex> lg(m_mutex);
347 return m_map_RPC.load_locations_and_extras_RPC(locations_collection);
348}
349
350bool Map2D_nwc_yarp::saveMapToDisk(std::string map_name, std::string file_name)
351{
352 std::lock_guard <std::mutex> lg(m_mutex);
353 return m_map_RPC.save_map_to_disk_RPC(map_name,file_name);
354}
355
356bool Map2D_nwc_yarp::loadMapFromDisk(std::string file_name)
357{
358 std::lock_guard <std::mutex> lg(m_mutex);
359 return m_map_RPC.load_map_from_disk_RPC(file_name);
360}
361
363{
364 std::lock_guard <std::mutex> lg(m_mutex);
366}
bool ret
virtual return_get_map_names get_map_names_RPC()
virtual bool rename_path_RPC(const std::string &original_name, const std::string &new_name)
virtual bool store_path_RPC(const std::string &path_name, const yarp::dev::Nav2D::Map2DPath &path)
virtual bool clear_all_areas_RPC()
virtual bool clear_all_paths_RPC()
virtual bool save_map_to_disk_RPC(const std::string &map_name, const std::string &file_name)
virtual bool store_map_RPC(const yarp::dev::Nav2D::MapGrid2D &themap)
virtual bool load_map_from_disk_RPC(const std::string &file_name)
virtual bool delete_location_RPC(const std::string &location_name)
virtual bool clear_all_maps_temporary_flags_RPC()
virtual return_get_all_areas get_all_areas_RPC()
virtual bool load_locations_and_extras_RPC(const std::string &locations_collection_file)
virtual bool clear_map_temporary_flags_RPC(const std::string &map_name)
virtual return_get_location get_location_RPC(const std::string &location_name)
virtual bool load_maps_collection_RPC(const std::string &maps_collection_file)
virtual bool delete_path_RPC(const std::string &path_name)
virtual return_get_areas_list get_areas_list_RPC()
virtual return_get_path get_path_RPC(const std::string &path_name)
virtual bool store_location_RPC(const std::string &location_name, const yarp::dev::Nav2D::Map2DLocation &loc)
virtual bool clear_all_maps_RPC()
virtual return_get_area get_area_RPC(const std::string &area_name)
virtual return_get_all_paths get_all_paths_RPC()
virtual return_get_map get_map_RPC(const std::string &map_name)
virtual return_get_paths_list get_paths_list_RPC()
virtual bool remove_map_RPC(const std::string &map_name)
virtual return_get_locations_list get_locations_list_RPC()
virtual bool rename_location_RPC(const std::string &original_name, const std::string &new_name)
virtual bool save_locations_and_extras_RPC(const std::string &locations_collection_file)
virtual return_get_all_locations get_all_locations_RPC()
virtual bool save_maps_collection_RPC(const std::string &maps_collection_file)
virtual bool rename_area_RPC(const std::string &original_name, const std::string &new_name)
virtual bool store_area_RPC(const std::string &area_name, const yarp::dev::Nav2D::Map2DArea &area)
virtual bool clear_all_locations_RPC()
virtual bool enable_maps_compression_RPC(const bool enable_compression)
virtual bool delete_area_RPC(const std::string &area_name)
bool getAllLocations(std::vector< yarp::dev::Nav2D::Map2DLocation > &locations) override
Get a list of all stored locations.
bool clearAllLocations() override
Delete all stored locations.
bool saveMapToDisk(std::string map_name, std::string file_name) override
Save a map to disk.
bool get_map(std::string map_name, yarp::dev::Nav2D::MapGrid2D &map) override
Gets a map from the map server.
bool renameArea(std::string original_name, std::string new_name) override
Searches for an area and renames it.
bool store_map(const yarp::dev::Nav2D::MapGrid2D &map) override
Stores a map into the map server.
bool getArea(std::string location_name, yarp::dev::Nav2D::Map2DArea &area) override
Retrieves an area.
bool storePath(std::string path_name, yarp::dev::Nav2D::Map2DPath path) override
Store a path.
bool loadMapFromDisk(std::string file_name) override
Load a map from disk.
bool getLocationsList(std::vector< std::string > &locations) override
Get a list of the names of all stored locations.
bool saveMapsCollection(std::string maps_collection_file) override
Save a collection of maps to disk.
bool renameLocation(std::string original_name, std::string new_name) override
Searches for a location and renames it.
bool getAllPaths(std::vector< yarp::dev::Nav2D::Map2DPath > &paths) override
Get a list of all stored paths.
bool getAllAreas(std::vector< yarp::dev::Nav2D::Map2DArea > &areas) override
Get a list of all stored areas.
bool remove_map(std::string map_name) override
Removes a map from the map server.
bool getLocation(std::string location_name, yarp::dev::Nav2D::Map2DLocation &loc) override
Retrieves a location specified by the user in the world reference frame.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::os::Port m_rpcPort_to_Map2D_nws
bool saveLocationsAndExtras(std::string locations_collection_file) override
Save a collection of locations/area/paths etc to disk.
std::string m_local_name
bool clearAllPaths() override
Delete all stored paths.
bool deleteArea(std::string location_name) override
Delete an area.
bool loadLocationsAndExtras(std::string locations_collection_file) override
Load a collection of locations/areas/paths etc from disk.
bool storeArea(std::string area_name, yarp::dev::Nav2D::Map2DArea area) override
Store an area.
bool clearAllMapsTemporaryFlags() override
Clear all temporary flags from all stored maps.
bool clearAllAreas() override
Delete all stored areas.
IMap2DMsgs m_map_RPC
bool storeLocation(std::string location_name, yarp::dev::Nav2D::Map2DLocation loc) override
Store a location specified by the user in the world reference frame.
std::string m_map_server
bool clearAllMaps() override
Removes all the registered maps from the server.
bool getPathsList(std::vector< std::string > &paths) override
Get a list of the names of all stored paths.
bool deletePath(std::string path_name) override
Delete a path.
bool clearMapTemporaryFlags(std::string map_name) override
Clear all temporary flags from a specific map.
bool loadMapsCollection(std::string maps_collection_file) override
Load a collection of maps from disk.
bool deleteLocation(std::string location_name) override
Delete a location.
std::mutex m_mutex
bool renamePath(std::string original_name, std::string new_name) override
Searches for a path and renames it.
bool getPath(std::string path_name, yarp::dev::Nav2D::Map2DPath &path) override
Retrieves a path.
bool close() override
Close the DeviceDriver.
bool get_map_names(std::vector< std::string > &map_names) override
Gets a list containing the names of all registered maps.
std::string m_carrier
bool getAreasList(std::vector< std::string > &locations) override
Get a list of the names of all stored areas.
bool enableMapsCompression(bool enable) override
99999999999
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:79
A base class for nested structures that can be searched.
Definition: Searchable.h:56
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
A single value (typically within a Bottle).
Definition: Value.h:43
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition: Wire.h:28
#define yCError(component,...)
Definition: LogComponent.h:213
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.