iCub-main
Loading...
Searching...
No Matches
RawValuesPublisherServer.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/BufferedPort.h>
10#include <yarp/os/Bottle.h>
11#include <yarp/os/Log.h>
12#include <yarp/os/LogStream.h>
13#include <yarp/os/LogComponent.h>
14
15namespace {
16 YARP_LOG_COMPONENT(RAWVALUESPUBLISHERSERVER, "iCub.debugLibrary.rawvaluespublisherserver")
17}
18
19#undef DEBUG_RAW_VALUES_MACRO
20
21using namespace yarp::os;
22
23
27
29
30bool RawValuesPublisherServer::open(yarp::os::Searchable& config)
31{
32 if (!parseParams(config))
33 {
34 return false;
35 }
36 // set the period. m_period var is defined in the <device_name>_ParamsParser.h file
37 // and updates at config file parsing as per m_name var.
38 m_threadPeriodInS = m_period / 1000.0;
39
40 if(m_threadPeriodInS <= 0)
41 {
42 yCError(RAWVALUESPUBLISHERSERVER)
43 << "Period parameter is present with value:"
44 << m_threadPeriodInS << "but it is not a positive integer. Closing...";
45 return false;
46 }
47
48 // set streaming port
49 m_streamingPortName = m_name + "/rawdata:o";
50 // Open port
51 if(!m_streamingRawDataPort.open(m_streamingPortName))
52 {
53 yCError(RAWVALUESPUBLISHERSERVER)
54 << "Error opening streaming raw data port:" << m_streamingPortName;
55 close();
56 return false;
57 }
58
59 m_rpcPortName = m_name + "/rpc:o";
60
61 // Attach to port
62 if(!this->yarp().attachAsServer(m_rpcRawDataPort))
63 {
64 yCError(RAWVALUESPUBLISHERSERVER)
65 << "Failure in attaching RPC port to thrift RPC interface";
66 close();
67 return false;
68 }
69 // Open port
70 if(!m_rpcRawDataPort.open(m_rpcPortName))
71 {
72 yCError(RAWVALUESPUBLISHERSERVER)
73 << "Failure in opening rpc port:" << m_rpcPortName;
74 close();
75 return false;
76 }
77
78 yCInfo(RAWVALUESPUBLISHERSERVER) << "Open ports completes";
79 return true;
80}
81
83{
84 return(this->detachAll());
85}
86
91
92bool RawValuesPublisherServer::attachAll(const yarp::dev::PolyDriverList &p)
93{
94 if (p.size() != 1 )
95 {
96 yCError(RAWVALUESPUBLISHERSERVER)
97 << "Trying to expose" << p.size() << "device(s). Expected only one device to be exposed to YARP ports. Closing...";
98 close();
99 return false;
100 }
101
102 yarp::dev::PolyDriver* poly = p[0]->poly;
103
104 if(!poly)
105 {
106 yCError(RAWVALUESPUBLISHERSERVER)
107 << "NullPointerException when getting the polyDriver at attachAll.";
108 close();
109 return false;
110 }
111
112 // View all the interfaces
113 poly->view(m_iRawValuesPublisher);
114
115 // Set rate period
116 if(!this->setPeriod(m_threadPeriodInS))
117 {
118 yCError(RAWVALUESPUBLISHERSERVER)
119 << "Failure in setting periodic thread period";
120 close();
121 return false;
122 }
123
124 // Populate the RPC data to be served on the RPC port
125 if(!populateMetadata(m_mapOfMetadata))
126 {
127 yCError(RAWVALUESPUBLISHERSERVER, "Failure in getMetadata()");
128 close();
129 return false;
130 }
131
132 // Start periodic thread
133 if(!this->start())
134 {
135 yCError(RAWVALUESPUBLISHERSERVER)
136 << "Failure in starting periodic thread";
137 close();
138 return false;
139 }
140
141 yCInfo(RAWVALUESPUBLISHERSERVER) << "Attach completes";
142
143 return true;
144}
145
147{
148 // Stop periodicThread if running
149 if (this->isRunning())
150 {
151 this->stop();
152 }
153
154 m_rpcRawDataPort.close();
155 m_streamingRawDataPort.close();
156
157 yCInfo(RAWVALUESPUBLISHERSERVER) << "Detach complete";
158
159 return true;
160}
161
166
168{
169 rawValuesDataVectorsMap &rdm = m_streamingRawDataPort.prepare();
170 rdm.vectorsMap.clear();
171
172 if(!m_iRawValuesPublisher->getRawDataMap(rdm.vectorsMap))
173 {
174 m_streamingRawDataPort.unprepare();
175 return;
176 }
177
178 m_stamp.update();
179 m_streamingRawDataPort.setEnvelope(m_stamp);
180 m_streamingRawDataPort.write();
181}
182
183// Private nmethods
184bool RawValuesPublisherServer::populateMetadata(rawValuesKeyMetadataMap &metamap)
185{
186 if(!m_iRawValuesPublisher->getMetadataMap(metamap))
187 {
188 return false;
189 }
190
191 #ifdef DEBUG_RAW_VALUES_MACRO
192 for (auto [k,v] : metamap.metadataMap)
193 {
194 yCDebug(RAWVALUESPUBLISHERSERVER) << "Metadata at key:" << k << "with size:" << v.size;
195 for (size_t i = 0; i < v.size; i++)
196 {
197 yCDebug(RAWVALUESPUBLISHERSERVER) << "Metadata element:" << v.rawValueNames[i];
198 }
199
200 }
201 #endif
202
203
204 return true;
205}
bool parseParams(const yarp::os::Searchable &config)
bool open(yarp::os::Searchable &params) override
rawValuesKeyMetadataMap getMetadata() override
Read the rawvalues metadata necessary to configure the RawValuesPublisherClient device.
bool attachAll(const yarp::dev::PolyDriverList &p) override
virtual bool getMetadataMap(rawValuesKeyMetadataMap &metamap)=0
virtual bool getRawDataMap(std::map< std::string, std::vector< std::int32_t > > &map)=0
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...
Copyright (C) 2008 RobotCub Consortium.