headers.cpp
Go to the documentation of this file.
00001 
00006 /*****************************************************************************
00007 ** Includes
00008 *****************************************************************************/
00009 
00010 #include "../../include/mm_messages/headers.hpp"
00011 #include <ecl/containers/stencil.hpp>
00012 #include <ecl/converters.hpp>
00013 //#include "../../../include/mm_messages/registry.hpp"
00014 
00015 /*****************************************************************************
00016 ** Namespaces
00017 *****************************************************************************/
00018 
00019 namespace mm_messages {
00020 
00021 /*****************************************************************************
00022 ** Definitions
00023 *****************************************************************************/
00024 
00025 const unsigned int PacketHeader::size = sizeof(unsigned int);
00026 const unsigned int SubPacketHeader::size = 2*sizeof(unsigned int);
00027 
00028 SubPacketHeader::SubPacketHeader(const int& id, const unsigned int& length)
00029   : id(id)
00030   , length(length)
00031 {}
00032 
00033 /*****************************************************************************
00034 ** Serialisations
00035 *****************************************************************************/
00036 
00037 void Message<PacketHeader>::encode(const PacketHeader& header, ByteArray& buffer) {
00038   // This is the header...the buffer should be clear anyway.
00039   buffer.resize(PacketHeader::size);
00040   ecl::Converter<ByteArray, unsigned int> to_byte_array;
00041   to_byte_array(buffer, header.signature);
00042 }
00043 
00044 PacketHeader Message<PacketHeader>::decode(const unsigned char* buffer, const unsigned int& size) {
00045   // TODO : check PacketHeader::size == size, throw exception
00046   unsigned int signature;
00047   ecl::Converter<unsigned int, ConstRawByteStencil> from_byte_array;
00048   unsigned int window_size = sizeof(unsigned int);
00049   ConstRawByteStencil stencil(buffer, size, buffer, buffer+window_size);
00050   signature = from_byte_array(stencil);
00051   return PacketHeader(signature);
00052 }
00053 
00054 void Message<SubPacketHeader>::encode(const SubPacketHeader& header, ByteArray& buffer) {
00055   ByteArray sub_packet_header_buffer(SubPacketHeader::size);
00056   unsigned int window_size = sizeof(unsigned int);
00057   ecl::Converter<ByteStencil, unsigned int> to_byte_array;
00058   ByteStencil stencil(sub_packet_header_buffer, sub_packet_header_buffer.begin(), buffer.end());
00059   stencil.resettle(0, window_size);           to_byte_array(stencil, header.id);
00060   stencil.resettle(window_size, window_size); to_byte_array(stencil, header.length);
00061   buffer.insert(buffer.end(), sub_packet_header_buffer.begin(), sub_packet_header_buffer.end());
00062 }
00063 
00064 SubPacketHeader Message<SubPacketHeader>::decode(const unsigned char* buffer, const unsigned int& size) {
00065   // TODO : check PacketHeader::size == size, throw exception
00066   unsigned int signature;
00067   ecl::Converter<unsigned int, ConstRawByteStencil> from_byte_array;
00068   unsigned int window_size = sizeof(unsigned int);
00069   unsigned int id, length;
00070   ConstRawByteStencil stencil(buffer, size, buffer, buffer+size);
00071   stencil.resettle(0,           window_size); id = from_byte_array(stencil);
00072   stencil.resettle(window_size, window_size); length = from_byte_array(stencil);
00073   return SubPacketHeader(id, length);
00074 }
00075 
00076 } // mm_messages


mm_messages
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:13:16