diff --git a/libretroshare/src/serialiser/rshistoryitems.cc b/libretroshare/src/serialiser/rshistoryitems.cc index 2ecbf591b..6c05e9afb 100644 --- a/libretroshare/src/serialiser/rshistoryitems.cc +++ b/libretroshare/src/serialiser/rshistoryitems.cc @@ -34,7 +34,31 @@ #include -/*************************************************************************/ +void RsHistoryMsgItem::serial_process(SerializeJob j,SerializeContext& ctx) +{ + uint16_t version=0; + + RsTypeSerializer::serial_process(j,ctx,version,"version") ; + RsTypeSerializer::serial_process (j,ctx,chatPeerId,"chatPeerId") ; + RsTypeSerializer::serial_process (j,ctx,incoming,"incoming") ; + RsTypeSerializer::serial_process (j,ctx,peerId,"peerId") ; + RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_NAME,peerName,"peerName") ; + RsTypeSerializer::serial_process(j,ctx,sendTime,"sendTime") ; + RsTypeSerializer::serial_process(j,ctx,recvTime,"recvTime") ; + RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_MSG,message,"message") ; +} + +RsItem *RsHistorySerialiser::create_item(uint8_t serial_class,uint8_t serial_type) const +{ + if(serial_class != RS_PKT_CLASS_CONFIG) + return NULL ; + + if(serial_type == RS_PKT_SUBTYPE_DEFAULT) + return new RsHistoryMsgItem(); + + return NULL ; +} + RsHistoryMsgItem::RsHistoryMsgItem() : RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_HISTORY_CONFIG, RS_PKT_SUBTYPE_DEFAULT) { @@ -45,6 +69,9 @@ RsHistoryMsgItem::RsHistoryMsgItem() : RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_CONF saveToDisc = true; } +#ifdef TO_REMOVE + +/*************************************************************************/ RsHistoryMsgItem::~RsHistoryMsgItem() { } @@ -92,9 +119,6 @@ std::ostream& RsHistoryMsgItem::print(std::ostream &out, uint16_t indent) return out; } -RsHistorySerialiser::RsHistorySerialiser() : RsSerialType(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_HISTORY_CONFIG) -{ -} RsHistorySerialiser::~RsHistorySerialiser() { @@ -114,7 +138,6 @@ uint32_t RsHistorySerialiser::sizeHistoryMsgItem(RsHistoryMsgItem* item) return s; } - /* serialise the data to the buffer */ bool RsHistorySerialiser::serialiseHistoryMsgItem(RsHistoryMsgItem* item, void* data, uint32_t* pktsize) { @@ -266,3 +289,5 @@ RsItem* RsHistorySerialiser::deserialise(void *data, uint32_t *pktsize) } /*************************************************************************/ + +#endif diff --git a/libretroshare/src/serialiser/rshistoryitems.h b/libretroshare/src/serialiser/rshistoryitems.h index 72f186561..ffc5b571c 100644 --- a/libretroshare/src/serialiser/rshistoryitems.h +++ b/libretroshare/src/serialiser/rshistoryitems.h @@ -28,18 +28,21 @@ #include "rsitems/rsserviceids.h" #include "serialiser/rsserial.h" +#include "serialiser/rsconfigitems.h" #include "retroshare/rstypes.h" +#include "serialization/rsserializer.h" + /**************************************************************************/ class RsHistoryMsgItem: public RsItem { public: RsHistoryMsgItem(); - virtual ~RsHistoryMsgItem(); + virtual ~RsHistoryMsgItem() {} + virtual void clear() {} - virtual void clear(); - std::ostream& print(std::ostream &out, uint16_t indent = 0); + virtual void serial_process(SerializeJob j,SerializeContext& ctx); RsPeerId chatPeerId; // empty for global chat bool incoming; @@ -54,20 +57,13 @@ public: bool saveToDisc; }; -class RsHistorySerialiser: public RsSerialType +class RsHistorySerialiser: public RsConfigSerializer { public: - RsHistorySerialiser(); - virtual ~RsHistorySerialiser(); - - virtual uint32_t size(RsItem*); - virtual bool serialise(RsItem* item, void* data, uint32_t* size); - virtual RsItem* deserialise(void* data, uint32_t* size); + RsHistorySerialiser() : RsConfigSerializer(RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_HISTORY_CONFIG) {} + virtual ~RsHistorySerialiser() {} -private: - virtual uint32_t sizeHistoryMsgItem(RsHistoryMsgItem*); - virtual bool serialiseHistoryMsgItem (RsHistoryMsgItem* item, void* data, uint32_t* size); - virtual RsHistoryMsgItem* deserialiseHistoryMsgItem(void* data, uint32_t* size); + virtual RsItem *create_item(uint8_t service,uint8_t type) const ; }; /**************************************************************************/ diff --git a/libretroshare/src/serialization/rsserializer.cc b/libretroshare/src/serialization/rsserializer.cc index 9f14ee91e..ab47e9429 100644 --- a/libretroshare/src/serialization/rsserializer.cc +++ b/libretroshare/src/serialization/rsserializer.cc @@ -6,7 +6,7 @@ const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_NONE ( 0x0 const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_CONFIG ( 0x0001 ); const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE ( 0x0002 ); -RsItem *RsGenericSerializer::deserialise(void *data, uint32_t *size) +RsItem *RsServiceSerializer::deserialise(void *data, uint32_t *size) { uint32_t rstype = getRsItemId(const_cast((const void*)data)) ; @@ -36,7 +36,36 @@ RsItem *RsGenericSerializer::deserialise(void *data, uint32_t *size) delete item ; return NULL ; } +RsItem *RsConfigSerializer::deserialise(void *data, uint32_t *size) +{ + uint32_t rstype = getRsItemId(const_cast((const void*)data)) ; + RsItem *item = create_item(getRsItemClass(rstype),getRsItemSubType(rstype)) ; + + if(!item) + { + std::cerr << "(EE) cannot deserialise: unknown item type " << std::hex << rstype << std::dec << std::endl; + std::cerr << "(EE) Data is: " << RsUtil::BinToHex(static_cast(data),std::min(50u,*size)) << ((*size>50)?"...":"") << std::endl; + return NULL ; + } + + SerializeContext ctx(const_cast(static_cast(data)),*size,mFormat,mFlags); + ctx.mOffset = 8 ; + + item->serial_process(RsItem::DESERIALIZE, ctx) ; + + if(ctx.mSize != ctx.mOffset) + { + std::cerr << "RsSerializer::deserialise(): ERROR. offset does not match expected size!" << std::endl; + delete item ; + return NULL ; + } + if(ctx.mOk) + return item ; + + delete item ; + return NULL ; +} bool RsGenericSerializer::serialise(RsItem *item,void *data,uint32_t *size) { SerializeContext ctx(static_cast(data),0,mFormat,mFlags); diff --git a/libretroshare/src/serialization/rsserializer.h b/libretroshare/src/serialization/rsserializer.h index 33e643215..3285becf9 100644 --- a/libretroshare/src/serialization/rsserializer.h +++ b/libretroshare/src/serialization/rsserializer.h @@ -25,14 +25,9 @@ public: static const SerializationFlags SERIALIZATION_FLAG_CONFIG ; // 0x0001 static const SerializationFlags SERIALIZATION_FLAG_SIGNATURE ; // 0x0002 - /*! create_item - * should be overloaded to create the correct type of item depending on the data - */ - virtual RsItem *create_item(uint16_t /* service */, uint8_t /* item_sub_id */) const=0; - // The following functions overload RsSerialType. They *should not* need to be further overloaded. - RsItem *deserialise(void *data,uint32_t *size) ; + RsItem *deserialise(void *data,uint32_t *size) =0; bool serialise(RsItem *item,void *data,uint32_t *size) ; uint32_t size(RsItem *item) ; void print(RsItem *item) ; @@ -51,7 +46,6 @@ protected: : RsSerialType(RS_PKT_VERSION_SERVICE,service), mFormat(format),mFlags(flags) {} -private: SerializeContext::SerializationFormat mFormat ; SerializationFlags mFlags ; @@ -65,6 +59,13 @@ public: SerializationFlags flags = SERIALIZATION_FLAG_NONE) : RsGenericSerializer(service_id,format,flags) {} + + /*! create_item + * should be overloaded to create the correct type of item depending on the data + */ + virtual RsItem *create_item(uint16_t /* service */, uint8_t /* item_sub_id */) const=0; + + RsItem *deserialise(void *data,uint32_t *size) ; }; class RsConfigSerializer: public RsGenericSerializer @@ -76,6 +77,13 @@ public: SerializationFlags flags = RsGenericSerializer::SERIALIZATION_FLAG_NONE) : RsGenericSerializer(config_class,config_type,format,flags) {} + + /*! create_item + * should be overloaded to create the correct type of item depending on the data + */ + virtual RsItem *create_item(uint8_t /* class */, uint8_t /* item_type */) const=0; + + RsItem *deserialise(void *data,uint32_t *size) ; };