YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
BottleZlibPortmonitor.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8#include <yarp/os/LogStream.h>
10#include <yarp/sig/Image.h>
11
12#include <cstring>
13#include <cmath>
14#include <algorithm>
15
16#include <zlib.h>
17#include <vector>
18
19using namespace yarp::os;
20using namespace yarp::sig;
21
22namespace {
24 "yarp.carrier.portmonitor.bottle_zlib",
28 nullptr)
29}
30
31
32void split(const std::string& s, char delim, std::vector<std::string>& elements)
33{
34 std::istringstream iss(s);
35 std::string item;
36 while (std::getline(iss, item, delim)) {
37 elements.push_back(item);
38 }
39}
40
42{
43 // Split command line string using '+' delimiter
44 std::vector<std::string> parameters;
45 split(carrierString, '+', parameters);
46
47 // Iterate over result strings
48 for (std::string param : parameters)
49 {
50 // If there is no '.', then the param is bad formatted, skip it.
51 auto pointPosition = param.find('.');
52 if (pointPosition == std::string::npos)
53 {
54 continue;
55 }
56
57 // Otherwise, separate key and value
58 std::string paramKey = param.substr(0, pointPosition);
60 std::string s = param.substr(pointPosition + 1, param.length());
61 paramValue.fromString(s.c_str());
62
63 //and append to the returned property
64 prop.put(paramKey, paramValue);
65 }
66 return;
67}
68
70{
71 std::string options_string=options.toString();
72 m_shouldCompress = (options.find("sender_side").asBool());
73
74 //parse the user parameters
76
77 std::string str = options.find("carrier").asString();
79 m_debug_compression_size = m_user_params.check("debug_compression_info");
80
81 return true;
82}
83
87
89{
90 return false;
91}
92
94{
95 return false;
96}
97
99{
100 /*
101 //FIXME by DrDanz using c++17
102 if(m_shouldCompress)
103 {
104 //sender side / compressor
105 auto* b = thing.cast_as<yarp::os::Portable>();
106 if(b == nullptr)
107 {
108 yCError(BOTTLE_ZLIB_MONITOR, "Expected type Bottle in sender side, but got wrong data type!");
109 return false;
110 }
111 }
112 else
113 {
114 //receiver side / decompressor
115 auto* b = thing.cast_as<yarp::os::Portable>();
116 if(b == nullptr)
117 {
118 yCError(BOTTLE_ZLIB_MONITOR, "Expected type Bottle in receiver side, but got wrong data type!");
119 return false;
120 }
121 }
122 */
123 return true;
124}
125
127{
128 if(m_shouldCompress)
129 {
130 //sender side / compressor
131 //auto zzz = thing.cast_as<yarp::os::Portable>(); //FIXME by DrDanz using c++17
132 yarp::os::Portable* pwrite = dynamic_cast<yarp::os::Portable*>(thing.getPortWriter());//.cast_as<yarp::os::Portable>();
135
136 size_t sizeUncompressed = 0;
137 const unsigned char* uncompressedData = (const unsigned char*)(b.toBinary(&sizeUncompressed));
138
139 size_t sizeCompressed = (sizeUncompressed * 1.1) + 12;
140 unsigned char* compressedData = (unsigned char*) malloc (sizeCompressed);
141
144 double end_time = yarp::os::Time::now();
145 if(!ret)
146 {
147 yCError(BOTTLE_ZLIB_MONITOR, "Failed to compress, exiting...");
149 return thing;
150 }
151
152 m_data.clear();
155 m_data.add(v);
156 m_th.setPortWriter(&m_data);
157
158 if (m_debug_compression_size)
159 {
160 yCDebug(BOTTLE_ZLIB_MONITOR) << "uncompressed size:" << sizeUncompressed
161 << "compressed size" << sizeCompressed
162 << "ratio:" << (double)sizeUncompressed/(double)sizeCompressed <<":1"
163 << "time:" << end_time - start_time;
164 }
165
167 }
168 else
169 {
170 //receiver side / decompressor
172
173 size_t sizeUncompressed = b->get(0).asInt32();
174 size_t sizeCompressed=b->get(1).asBlobLength();
175 const unsigned char* CompressedData = (const unsigned char*) b->get(1).asBlob();
176
177 unsigned char* uncompressedData = (unsigned char*)malloc(sizeUncompressed);
178
181 double end_time = yarp::os::Time::now();
182 if(!ret)
183 {
184 yCError(BOTTLE_ZLIB_MONITOR, "Failed to decompress, exiting...");
186 return thing;
187 }
188
189 m_data.fromBinary((const char*)(uncompressedData),sizeUncompressed);
190 m_th.setPortWriter(&m_data);
191
192 if (m_debug_compression_size)
193 {
194 yCDebug(BOTTLE_ZLIB_MONITOR) << "uncompressed size:" << sizeUncompressed
195 << "compressed size" << sizeCompressed
196 << "ratio:" << (double)sizeUncompressed / (double)sizeCompressed << ":1"
197 << "time:" << end_time - start_time;
198 }
199
201 }
202
203 return m_th;
204}
205
206int BottleZlibMonitorObject::compressData(const unsigned char* in, const size_t& in_size, unsigned char* out, size_t& out_size)
207{
208 int z_result = compress((Bytef*)out, (uLongf*)&out_size, (Bytef*)in, in_size);
209 switch (z_result)
210 {
211 case Z_OK:
212 break;
213
214 case Z_MEM_ERROR:
215 yCError(BOTTLE_ZLIB_MONITOR, "zlib compression: out of memory");
216 return false;
217 break;
218
219 case Z_BUF_ERROR:
220 yCError(BOTTLE_ZLIB_MONITOR, "zlib compression: output buffer wasn't large enough");
221 return false;
222 break;
223 }
224
225 return true;
226}
227
228int BottleZlibMonitorObject::decompressData(const unsigned char* in, const size_t& in_size, unsigned char* out, size_t& out_size)
229{
230 int z_result = uncompress((Bytef*)out, (uLongf*)&out_size, (const Bytef*)in, in_size);
231 switch (z_result)
232 {
233 case Z_OK:
234 break;
235
236 case Z_MEM_ERROR:
237 yCError(BOTTLE_ZLIB_MONITOR, "zlib compression: out of memory");
238 return false;
239 break;
240
241 case Z_BUF_ERROR:
242 yCError(BOTTLE_ZLIB_MONITOR, "zlib compression: output buffer wasn't large enough");
243 return false;
244 break;
245
246 case Z_DATA_ERROR:
247 yCError(BOTTLE_ZLIB_MONITOR, "zlib compression: file contains corrupted data");
248 return false;
249 break;
250 }
251
252 return true;
253}
void getParamsFromCommandLine(std::string carrierString, yarp::os::Property &prop)
void split(const std::string &s, char delim, std::vector< std::string > &elements)
bool ret
bool setparam(const yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are set via YARP admin port.
int decompressData(const unsigned char *in, const size_t &in_size, unsigned char *out, size_t &out_size)
int compressData(const unsigned char *in, const size_t &in_size, unsigned char *out, size_t &out_size)
void destroy() override
This will be called when the portmonitor object destroyes.
bool getparam(yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are requested via YARP admin port.
bool accept(yarp::os::Things &thing) override
This will be called when the data reach the portmonitor object.
yarp::os::Things & update(yarp::os::Things &thing) override
After data get accpeted in the accept() callback, an instance of that is given to the update function...
bool create(const yarp::os::Property &options) override
This will be called when the dll is properly loaded by the portmonitor carrier.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition Bottle.cpp:309
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 addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:140
const char * toBinary(size_t *size=nullptr)
Returns binary representation of bottle.
Definition Bottle.cpp:222
void fromBinary(const char *buf, size_t len)
Initializes bottle from a binary representation.
Definition Bottle.cpp:216
A mini-server for performing network communication in the background.
static LogCallback printCallback()
Get current print callback.
Definition Log.cpp:873
static LogType minimumPrintLevel()
Get current minimum print level.
Definition Log.cpp:833
@ LogTypeReserved
Definition Log.h:98
This is a base class for objects that can be both read from and be written to the YARP network.
Definition Portable.h:25
static bool copyPortable(const PortWriter &writer, PortReader &reader)
Copy one portable to another, via writing and reading.
Definition Portable.cpp:16
A class for storing options and configuration information.
Definition Property.h:33
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
std::string toString() const override
Return a standard text representation of the content of the object.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987
Base class for generic things.
Definition Things.h:18
yarp::os::PortWriter * getPortWriter()
Definition Things.cpp:31
T * cast_as()
Definition Things.h:53
void setPortWriter(yarp::os::PortWriter *writer)
Set the reference to a PortWriter object.
Definition Things.cpp:26
A single value (typically within a Bottle).
Definition Value.h:43
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:186
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition Value.cpp:204
virtual size_t asBlobLength() const
Get binary data length.
Definition Value.cpp:267
virtual const char * asBlob() const
Get binary data value.
Definition Value.cpp:261
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
An interface to the operating system, including Port based communication.