iCub-main
RawValuesPublisherClient.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2024 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
7 
8 // yarp includes
9 #include <yarp/os/LogComponent.h>
10 #include <yarp/os/Log.h>
11 #include <yarp/os/LogStream.h>
12 #include <yarp/os/Bottle.h>
13 #include <yarp/os/Network.h>
14 
15 namespace {
16  YARP_LOG_COMPONENT(RAWVALUESPUBLISHERCLIENT, "iCub.debugLibrary.rawvaluespublisherclient")
17 }
18 
19 #undef DEBUG_RAW_VALUES_MACRO
20 
21 using namespace yarp::os;
22 
23 // Static inline functions
24 static inline bool NOT_YET_IMPLEMENTED(const char* txt)
25 {
26  yCError(RAWVALUESPUBLISHERCLIENT) << txt << "has not yet been implemented";
27  return true;
28 }
29 
30 static inline bool DEPRECATED(const char* txt)
31 {
32  yCError(RAWVALUESPUBLISHERCLIENT) << txt << "has been deprecated";
33  return true;
34 }
35 
37 {
38  // saving read data from server output port in the local map
39  std::lock_guard<std::mutex> guard(dataMutex);
40  receivedRawDataMap = rawdata.vectorsMap;
41 }
42 
43 bool RawValuesPublisherClient::open(yarp::os::Searchable& config)
44 {
45  if (!parseParams(config))
46  {
47  return false;
48  }
49 
50  std::string localStreamingPortName = m_local + "/rawdata:i";
51  std::string remoteStreamingPortName = m_remote + "/rawdata:o";
52  std::string localRPCPortName = m_local + "/rpc:i";
53  std::string remoteRPCPortName = m_remote + "/rpc:o";
54 
55  // Open ports
56  // Open RPC port
57  if(!m_rpcPort.open(localRPCPortName))
58  {
59  yCError(RAWVALUESPUBLISHERCLIENT) << "Failure opening RPC port" << localStreamingPortName;
60  close();
61  return false;
62  }
63 
64  // Open streaming port
65  if(!m_streamingPort.open(localStreamingPortName))
66  {
67  yCError(RAWVALUESPUBLISHERCLIENT) << "Failure opening streaming port" << localStreamingPortName;
68  close();
69  return false;
70  }
71  m_streamingPort.useCallback();
72 
73  // Connect ports
74  if (!m_externalConnection)
75  {
76  // Connect RPC port
77  if (!yarp::os::Network::connect(localRPCPortName, remoteRPCPortName, m_carrier))
78  {
79  yCError(RAWVALUESPUBLISHERCLIENT) <<
80  "Failure in connecting remote port" << remoteRPCPortName <<
81  "to local port" << localRPCPortName;
82 
83  yCError(RAWVALUESPUBLISHERCLIENT) <<
84  "Check that the specified RawValuesPublisherServer is up. Closing.";
85 
86  close();
87  return false;
88  }
89 
90  // Connect Streaming port
91  if(!yarp::os::Network::connect(remoteStreamingPortName, localStreamingPortName, m_carrier))
92  {
93  yCError(RAWVALUESPUBLISHERCLIENT) <<
94  "Failure in connecting remote port" << remoteStreamingPortName <<
95  "to local port" << localStreamingPortName;
96 
97  yCError(RAWVALUESPUBLISHERCLIENT) <<
98  "Check that the specified RawValuesPublisherServer is up. Closing.";
99 
100  close();
101  return false;
102  }
103 
104  if (!m_RPCInterface.yarp().attachAsClient(m_rpcPort)) {
105  yCError(RAWVALUESPUBLISHERCLIENT, "Failure opening Thrift-based RPC interface.");
106  return false;
107  }
108  }
109  m_streamingPort.receivedRawDataMap = {};
110 
111  yCInfo(RAWVALUESPUBLISHERCLIENT) << "Open completes";
112  return true;
113 }
114 
116 {
117  m_streamingPort.close();
118  m_rpcPort.close();
119 
120  yCInfo(RAWVALUESPUBLISHERCLIENT) << "Close completes";
121 
122  return true;
123 }
124 
125 bool RawValuesPublisherClient::getRawDataMap(std::map<std::string, std::vector<std::int32_t>> &map)
126 {
127  std::lock_guard<std::mutex> guard(m_streamingPort.dataMutex);
128  map = m_streamingPort.receivedRawDataMap;
129 
130  return true;
131 }
132 
133 bool RawValuesPublisherClient::getRawData(std::string key, std::vector<std::int32_t> &data)
134 {
135  std::lock_guard<std::mutex> guard(m_streamingPort.dataMutex);
136  if(m_streamingPort.receivedRawDataMap.find(key) != m_streamingPort.receivedRawDataMap.end())
137  {
138  data = m_streamingPort.receivedRawDataMap[key];
139  }
140  else
141  {
142  yCError(RAWVALUESPUBLISHERCLIENT) << "Requested key:" << key << "does not exist in the raw data map.";
143  return false;
144  }
145 
146  return true;
147 }
148 
149 bool RawValuesPublisherClient::getKeys(std::vector<std::string> &keys)
150 {
151  return NOT_YET_IMPLEMENTED("getKeys");
152 }
153 
155 {
156  return NOT_YET_IMPLEMENTED("getNumberOfKeys()");
157 }
158 
160 {
161  metamap = m_RPCInterface.getMetadata();
162 
163  return true;
164 }
166 {
167  std::map<std::string, rawValuesKeyMetadata> metamap = (m_RPCInterface.getMetadata()).metadataMap;
168  if(metamap.find(key) != metamap.end())
169  {
170  meta = metamap[key];
171  }
172  else
173  {
174  yCError(RAWVALUESPUBLISHERCLIENT) << "Requested key" << key << "is not available in the map. Exiting";
175  return false;
176  }
177 
178  return true;
179 }
static bool NOT_YET_IMPLEMENTED(const char *txt)
static bool DEPRECATED(const char *txt)
@ data
virtual bool getKeyMetadata(std::string key, rawValuesKeyMetadata &meta) override
virtual bool getRawDataMap(std::map< std::string, std::vector< std::int32_t >> &map) override
virtual bool getMetadataMap(rawValuesKeyMetadataMap &metamap) override
virtual bool getKeys(std::vector< std::string > &keys) override
virtual int getNumberOfKeys() override
virtual bool getRawData(std::string key, std::vector< std::int32_t > &data) override
bool open(yarp::os::Searchable &config) override
void onRead(rawValuesDataVectorsMap &rawdata) override
rawValuesDataVectorsMap IDL struct of a map of vectors to store the raw value data sent by the device
std::map< std::string, std::vector< std::int32_t > > vectorsMap
contain a map of vectors of the raw data as <string, vector> the user wanna send from low to higher l...
yarp::sig::Vector & map(yarp::sig::Vector &v, double(op)(double))
Performs a unary operator inplace on each element of a vector.
Definition: Math.cpp:305