From b52071d0c9e63477e2a60a701a6a73c235d74bf4 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 1 Apr 2017 18:05:53 +0200 Subject: [PATCH] removed RsSerializable and merged it into RsItem --- libretroshare/src/serialiser/rsserial.h | 19 ++++++++++++- .../src/serialization/rsserializer.h | 28 +++++++++++-------- .../src/serialization/rstypeserializer.h | 8 +++--- .../src/serialization/serial_test.cc | 21 +++++++------- 4 files changed, 49 insertions(+), 27 deletions(-) diff --git a/libretroshare/src/serialiser/rsserial.h b/libretroshare/src/serialiser/rsserial.h index bb8b84d8c..ad735abe5 100644 --- a/libretroshare/src/serialiser/rsserial.h +++ b/libretroshare/src/serialiser/rsserial.h @@ -70,6 +70,7 @@ const uint8_t RS_PKT_CLASS_CONFIG = 0x02; const uint8_t RS_PKT_SUBTYPE_DEFAULT = 0x01; /* if only one subtype */ +class SerializeContext ; class RsItem: public RsMemoryManagement::SmallObject { @@ -105,7 +106,23 @@ class RsItem: public RsMemoryManagement::SmallObject void setPacketService(uint16_t service); inline uint8_t priority_level() const { return _priority_level ;} - inline void setPriorityLevel(uint8_t l) { _priority_level = l ;} + inline void setPriorityLevel(uint8_t l) { _priority_level = l ;} + + /** + * @brief serialize this object to the given buffer + * @param Job to do: serialise or deserialize. + * @param data Chunk of memory were to dump the serialized data + * @param size Size of memory chunk + * @param offset Readed to determine at witch offset start writing data, + * written to inform caller were written data ends, the updated value + * is usually passed by the caller to serialize of another + * RsSerializable so it can write on the same chunk of memory just + * after where this RsSerializable has been serialized. + * @return true if serialization successed, false otherwise + */ + typedef enum { SIZE_ESTIMATE = 0x01, SERIALIZE = 0x02, DESERIALIZE = 0x03} SerializeJob ; + + virtual void serial_process(SerializeJob j,SerializeContext& ctx) = 0; private: uint32_t type; diff --git a/libretroshare/src/serialization/rsserializer.h b/libretroshare/src/serialization/rsserializer.h index 706ee3be4..e7725b955 100644 --- a/libretroshare/src/serialization/rsserializer.h +++ b/libretroshare/src/serialization/rsserializer.h @@ -5,7 +5,9 @@ #include #include -#include "rsserializable.h" +#include "serialiser/rsserial.h" + +#define SERIALIZE_ERROR() std::cerr << __PRETTY_FUNCTION__ << " : " class SerializeContext { @@ -20,22 +22,24 @@ class SerializeContext bool mOk ; }; -class RsSerializer +class RsSerializer: public RsSerialType { public: + RsSerializer(uint16_t service_id) : RsSerialType(service_id) {} + /*! create_item * should be overloaded to create the correct type of item depending on the data */ - virtual RsSerializable *create_item(uint16_t service, uint8_t item_sub_id) + virtual RsItem *create_item(uint16_t service, uint8_t item_sub_id) { return NULL ; } - RsSerializable *deserialize_item(const uint8_t *data,uint32_t size) + RsItem *deserialise(const uint8_t *data,uint32_t size) { uint32_t rstype = getRsItemId(const_cast((const void*)data)) ; - RsSerializable *item = create_item(getRsItemService(rstype),getRsItemSubType(rstype)) ; + RsItem *item = create_item(getRsItemService(rstype),getRsItemSubType(rstype)) ; if(!item) { @@ -46,7 +50,7 @@ class RsSerializer SerializeContext ctx(const_cast(data),size); ctx.mOffset = 8 ; - item->serial_process(RsSerializable::DESERIALIZE, ctx) ; + item->serial_process(RsItem::DESERIALIZE, ctx) ; if(ctx.mOk) return item ; @@ -55,16 +59,16 @@ class RsSerializer return NULL ; } - bool serialize_item(const RsSerializable *item,uint8_t *const data,uint32_t size) + bool serialise(RsItem *item,uint8_t *const data,uint32_t size) { SerializeContext ctx(data,0); - uint32_t tlvsize = size_item(item) ; + uint32_t tlvsize = this->size(item) ; if(tlvsize > size) throw std::runtime_error("Cannot serialise: not enough room.") ; - if(!setRsItemHeader(data, tlvsize, const_cast(item)->PacketId(), tlvsize)) + if(!setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize)) { std::cerr << "RsSerializer::serialise_item(): ERROR. Not enough size!" << std::endl; return false ; @@ -72,7 +76,7 @@ class RsSerializer ctx.mOffset = 8; ctx.mSize = tlvsize; - const_cast(item)->serial_process(RsSerializable::SERIALIZE,ctx) ; + item->serial_process(RsItem::SERIALIZE,ctx) ; if(ctx.mSize != ctx.mOffset) { @@ -82,12 +86,12 @@ class RsSerializer return true ; } - uint32_t size_item(const RsSerializable *item) + uint32_t size(RsItem *item) { SerializeContext ctx(NULL,0); ctx.mSize = 8 ; // header size - const_cast(item)->serial_process(RsSerializable::SIZE_ESTIMATE, ctx) ; + item->serial_process(RsItem::SIZE_ESTIMATE, ctx) ; return ctx.mSize ; } diff --git a/libretroshare/src/serialization/rstypeserializer.h b/libretroshare/src/serialization/rstypeserializer.h index a713832b3..4661ecc61 100644 --- a/libretroshare/src/serialization/rstypeserializer.h +++ b/libretroshare/src/serialization/rstypeserializer.h @@ -23,17 +23,17 @@ class RsTypeSerializer { public: template - static void serial_process(RsSerializable::SerializeJob j,SerializeContext& ctx,T& member) + static void serial_process(RsItem::SerializeJob j,SerializeContext& ctx,T& member) { switch(j) { - case RsSerializable::SIZE_ESTIMATE: ctx.mSize += serial_size(member) ; + case RsItem::SIZE_ESTIMATE: ctx.mSize += serial_size(member) ; break ; - case RsSerializable::DESERIALIZE: ctx.mOk = ctx.mOk && deserialize(ctx.mData,ctx.mSize,ctx.mOffset,member) ; + case RsItem::DESERIALIZE: ctx.mOk = ctx.mOk && deserialize(ctx.mData,ctx.mSize,ctx.mOffset,member) ; break ; - case RsSerializable::SERIALIZE: ctx.mOk = ctx.mOk && serialize(ctx.mData,ctx.mSize,ctx.mOffset,member) ; + case RsItem::SERIALIZE: ctx.mOk = ctx.mOk && serialize(ctx.mData,ctx.mSize,ctx.mOffset,member) ; break ; default: diff --git a/libretroshare/src/serialization/serial_test.cc b/libretroshare/src/serialization/serial_test.cc index 9b5745b4b..f1698ad0c 100644 --- a/libretroshare/src/serialization/serial_test.cc +++ b/libretroshare/src/serialization/serial_test.cc @@ -8,7 +8,7 @@ #include "util/rsprint.h" #include "serialiser/rsserial.h" -#include "serializer.h" +#include "rsserializer.h" #include "rstypeserializer.h" static const uint16_t RS_SERVICE_TYPE_TEST = 0xffff; @@ -75,10 +75,10 @@ template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t siz // - a serial_process method that tells which members to serialize // - overload the clear() and print() methods of RsItem // -class RsTestItem: public RsSerializable +class RsTestItem: public RsItem { public: - RsTestItem() : RsSerializable(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_TEST,RS_ITEM_SUBTYPE_TEST1) + RsTestItem() : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_TEST,RS_ITEM_SUBTYPE_TEST1) { str = "test string"; ts = time(NULL) ; @@ -88,9 +88,9 @@ class RsTestItem: public RsSerializable int_set.insert(lrand48()) ; } - // Derived from RsSerializable + // Derived from RsItem // - virtual void serial_process(RsSerializable::SerializeJob j, SerializeContext& ctx) + virtual void serial_process(RsItem::SerializeJob j, SerializeContext& ctx) { TlvString tt(str,TLV_TYPE_STR_DESCR) ; @@ -113,14 +113,15 @@ class RsTestItem: public RsSerializable }; // New user-defined serializer class. -// The only required member is the create_item() method, which creates the correct RsSerializable +// The only required member is the create_item() method, which creates the correct RsItem // that corresponds to a specific (service,subtype) couple. // class RsTestSerializer: public RsSerializer { public: + RsTestSerializer() : RsSerializer(RS_SERVICE_TYPE_TEST) {} - virtual RsSerializable *create_item(uint16_t service,uint8_t subtype) + virtual RsItem *create_item(uint8_t subtype) { switch(subtype) { @@ -144,7 +145,7 @@ int main(int argc,char *argv[]) { RsTestItem t1 ; - uint32_t size = RsTestSerializer().size_item(&t1); + uint32_t size = RsTestSerializer().size(&t1); std::cerr << "t1.serial_size() = " << size << std::endl; @@ -152,13 +153,13 @@ int main(int argc,char *argv[]) // RsTemporaryMemory mem1(size); - RsTestSerializer().serialize_item(&t1,mem1,mem1.size()) ; + RsTestSerializer().serialise(&t1,mem1,mem1.size()) ; std::cerr << "Serialized t1: " << RsUtil::BinToHex(mem1,mem1.size()) << std::endl; // Now deserialise into a new item // - RsSerializable *t2 = RsTestSerializer().deserialize_item(mem1,mem1.size()) ; + RsItem *t2 = RsTestSerializer().deserialise(mem1,mem1.size()) ; // make sure t1 is equal to t2 //