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} //anonymous namespace
31
33{
34 std::string options_string=options.toString();
35 m_shouldCompress = (options.find("sender_side").asBool());
36
37 //parse the user parameters
38 m_debug_compression_size = options.check("debug_compression_info");
39
40 return true;
41}
42
46
48{
49 return false;
50}
51
53{
54 return false;
55}
56
58{
59 /*
60 //FIXME by DrDanz using c++17
61 if(m_shouldCompress)
62 {
63 //sender side / compressor
64 auto* b = thing.cast_as<yarp::os::Portable>();
65 if(b == nullptr)
66 {
67 yCError(BOTTLE_ZLIB_MONITOR, "Expected type Bottle in sender side, but got wrong data type!");
68 return false;
69 }
70 }
71 else
72 {
73 //receiver side / decompressor
74 auto* b = thing.cast_as<yarp::os::Portable>();
75 if(b == nullptr)
76 {
77 yCError(BOTTLE_ZLIB_MONITOR, "Expected type Bottle in receiver side, but got wrong data type!");
78 return false;
79 }
80 }
81 */
82 return true;
83}
84
86{
87 if(m_shouldCompress)
88 {
89 //sender side / compressor
90 //auto zzz = thing.cast_as<yarp::os::Portable>(); //FIXME by DrDanz using c++17
91 yarp::os::Portable* pwrite = dynamic_cast<yarp::os::Portable*>(thing.getPortWriter());//.cast_as<yarp::os::Portable>();
94
95 size_t sizeUncompressed = 0;
96 const unsigned char* uncompressedData = (const unsigned char*)(b.toBinary(&sizeUncompressed));
97
98 size_t sizeCompressed = (sizeUncompressed * 1.1) + 12;
99 unsigned char* compressedData = (unsigned char*) malloc (sizeCompressed);
100
103 double end_time = yarp::os::Time::now();
104 if(!ret)
105 {
106 yCError(BOTTLE_ZLIB_MONITOR, "Failed to compress, exiting...");
108 return thing;
109 }
110
111 m_data.clear();
114 m_data.add(v);
115 m_th.setPortWriter(&m_data);
116
117 if (m_debug_compression_size)
118 {
119 yCDebug(BOTTLE_ZLIB_MONITOR) << "uncompressed size:" << sizeUncompressed
120 << "compressed size" << sizeCompressed
121 << "ratio:" << (double)sizeUncompressed/(double)sizeCompressed <<":1"
122 << "time:" << end_time - start_time;
123 }
124
126 }
127 else
128 {
129 //receiver side / decompressor
131
132 size_t sizeUncompressed = b->get(0).asInt32();
133 size_t sizeCompressed=b->get(1).asBlobLength();
134 const unsigned char* CompressedData = (const unsigned char*) b->get(1).asBlob();
135
136 unsigned char* uncompressedData = (unsigned char*)malloc(sizeUncompressed);
137
140 double end_time = yarp::os::Time::now();
141 if(!ret)
142 {
143 yCError(BOTTLE_ZLIB_MONITOR, "Failed to decompress, exiting...");
145 return thing;
146 }
147
148 m_data.fromBinary((const char*)(uncompressedData),sizeUncompressed);
149 m_th.setPortWriter(&m_data);
150
151 if (m_debug_compression_size)
152 {
153 yCDebug(BOTTLE_ZLIB_MONITOR) << "uncompressed size:" << sizeUncompressed
154 << "compressed size" << sizeCompressed
155 << "ratio:" << (double)sizeUncompressed / (double)sizeCompressed << ":1"
156 << "time:" << end_time - start_time;
157 }
158
160 }
161
162 return m_th;
163}
164
165int BottleZlibMonitorObject::compressData(const unsigned char* in, const size_t& in_size, unsigned char* out, size_t& out_size)
166{
167 int z_result = compress((Bytef*)out, (uLongf*)&out_size, (Bytef*)in, in_size);
168 switch (z_result)
169 {
170 case Z_OK:
171 break;
172
173 case Z_MEM_ERROR:
174 yCError(BOTTLE_ZLIB_MONITOR, "zlib compression: out of memory");
175 return false;
176 break;
177
178 case Z_BUF_ERROR:
179 yCError(BOTTLE_ZLIB_MONITOR, "zlib compression: output buffer wasn't large enough");
180 return false;
181 break;
182 }
183
184 return true;
185}
186
187int BottleZlibMonitorObject::decompressData(const unsigned char* in, const size_t& in_size, unsigned char* out, size_t& out_size)
188{
189 int z_result = uncompress((Bytef*)out, (uLongf*)&out_size, (const Bytef*)in, in_size);
190 switch (z_result)
191 {
192 case Z_OK:
193 break;
194
195 case Z_MEM_ERROR:
196 yCError(BOTTLE_ZLIB_MONITOR, "zlib compression: out of memory");
197 return false;
198 break;
199
200 case Z_BUF_ERROR:
201 yCError(BOTTLE_ZLIB_MONITOR, "zlib compression: output buffer wasn't large enough");
202 return false;
203 break;
204
205 case Z_DATA_ERROR:
206 yCError(BOTTLE_ZLIB_MONITOR, "zlib compression: file contains corrupted data");
207 return false;
208 break;
209 }
210
211 return true;
212}
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:65
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition Bottle.cpp:315
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:252
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:228
void fromBinary(const char *buf, size_t len)
Initializes bottle from a binary representation.
Definition Bottle.cpp:222
A mini-server for performing network communication in the background.
static LogCallback printCallback()
Get current print callback.
Definition Log.cpp:892
static LogType minimumPrintLevel()
Get current minimum print level.
Definition Log.cpp:852
@ 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.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
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:44
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:192
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition Value.cpp:210
virtual size_t asBlobLength() const
Get binary data length.
Definition Value.cpp:279
virtual const char * asBlob() const
Get binary data value.
Definition Value.cpp:273
#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.
Definition OSTestMsg.cpp:13