[ros-users] roscpp with transport plugins

Cedric Pradalier cedric.pradalier at gmail.com
Wed May 30 10:00:05 UTC 2012


Thanks for the link.

If the filter/plugin system ever get a chance to be moved upstream,
then creating a snappy module would be pretty trivial.
As an example, here is the bz2 filter in all its complexity.

Enjoy.

#include <bzlib.h>
#include "rosbz2_filter/bz2_transport_filter_plugin.h"
#include <pluginlib/class_list_macros.h>


using namespace ros;

PLUGINLIB_DECLARE_CLASS(rosbz2_filter, BZ2TransportFilterPlugin,
rosbz2_filter::BZ2TransportFilterPlugin, ros::TransportFilterPlugin);

namespace rosbz2_filter {

    BZ2TransportFilterPlugin::~BZ2TransportFilterPlugin() {
    }

    bool BZ2TransportFilterPlugin::apply(const TransportHints &
transport_hints, bool forward,
                    const SerializedMessage & src, SerializedMessage & dest) {
        if (forward) {
            return compress(src,dest);
        } else {
            return decompress(src,dest);
        }
    }

    bool BZ2TransportFilterPlugin::compress( const SerializedMessage &
src, SerializedMessage & dest) {
        int ret = 0;
        // Size following recommendation in bzip2 manual > 101% + 600 bytes
        uint32_t destLen = (102*src.num_bytes)/100 + 600;
        boost::shared_array<uint8_t> buf(new uint8_t[destLen+8]);
        // Copy the first 4 bytes (original length) to the beginning of the
        // buffer + 4. This will be useful for decompressing
        memcpy(buf.get()+4,src.buf.get(),4);
        // Removed the first 4 bytes corresponding the data length
        ret = BZ2_bzBuffToBuffCompress((char*)buf.get()+8,&destLen,
(char*)src.buf.get()+4, src.num_bytes-4, 5, 0, 30);
        if (ret != BZ_OK) {
            ROS_ERROR("BZ2_bzBuffToBuffCompress return %d",ret);
            return false;
        }
        // Finally set the 4 first byte to the data length
        *((uint32_t*)buf.get()) = destLen+4;
        dest = SerializedMessage(buf,destLen + 8);
        return true;
    }

    bool BZ2TransportFilterPlugin::decompress( const SerializedMessage
& src, SerializedMessage & dest) {
        int ret = 0;
        assert(src.num_bytes >= 4);
        uint32_t destLen =  *((uint32_t*)src.buf.get());
        boost::shared_array<uint8_t> buf(new uint8_t[destLen]);
        ret = BZ2_bzBuffToBuffDecompress((char*)buf.get(),&destLen,
(char*)src.buf.get()+4, src.num_bytes-4, 0, 0);
        if (ret != BZ_OK) {
            ROS_ERROR("BZ2_bzBuffToBuffDecompress return %d",ret);
            return false;
        }
        // Finally set the 4 first byte to the data length
        dest = SerializedMessage(buf,destLen);
        return true;
    }

};




On Wed, May 30, 2012 at 7:59 AM, Stéphane Magnenat
<stephane.magnenat at mavt.ethz.ch> wrote:
...
> For real-time compression of large data, snappy is probably a very good
> solution [1]:
>
> "Snappy is a compression/decompression library. It does not aim for maximum
> compression, or compatibility with any other compression library; instead,
> it aims for very high speeds and reasonable compression. For instance,
> compared to the fastest mode of zlib, Snappy is an order of magnitude faster
> for most inputs, but the resulting compressed files are anywhere from 20% to
> 100% bigger. On a single core of a Core i7 processor in 64-bit mode, Snappy
> compresses at about 250 MB/sec or more and decompresses at about 500 MB/sec
> or more.
>
> Snappy is widely used inside Google, in everything from BigTable and
> MapReduce to our internal RPC systems. (Snappy has previously been referred
> to as “Zippy” in some presentations and the likes.) "
>
> Kind regards,
>
> Stéphane
>
> [1] http://code.google.com/p/snappy/
>
> --
> Dr Stéphane Magnenat
> http://stephane.magnenat.net
>
> _______________________________________________
> ros-users mailing list
> ros-users at code.ros.org
> https://code.ros.org/mailman/listinfo/ros-users



-- 
Cedric Pradalier



More information about the ros-users mailing list