switched rshistoryitems to new serialization

This commit is contained in:
csoler 2017-04-24 17:33:30 +02:00
parent 8ae6541ceb
commit af4fcf7679
4 changed files with 85 additions and 27 deletions

View File

@ -34,7 +34,31 @@
#include <iostream> #include <iostream>
/*************************************************************************/ void RsHistoryMsgItem::serial_process(SerializeJob j,SerializeContext& ctx)
{
uint16_t version=0;
RsTypeSerializer::serial_process<uint16_t>(j,ctx,version,"version") ;
RsTypeSerializer::serial_process (j,ctx,chatPeerId,"chatPeerId") ;
RsTypeSerializer::serial_process<bool> (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<uint32_t>(j,ctx,sendTime,"sendTime") ;
RsTypeSerializer::serial_process<uint32_t>(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) 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; saveToDisc = true;
} }
#ifdef TO_REMOVE
/*************************************************************************/
RsHistoryMsgItem::~RsHistoryMsgItem() RsHistoryMsgItem::~RsHistoryMsgItem()
{ {
} }
@ -92,9 +119,6 @@ std::ostream& RsHistoryMsgItem::print(std::ostream &out, uint16_t indent)
return out; return out;
} }
RsHistorySerialiser::RsHistorySerialiser() : RsSerialType(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_HISTORY_CONFIG)
{
}
RsHistorySerialiser::~RsHistorySerialiser() RsHistorySerialiser::~RsHistorySerialiser()
{ {
@ -114,7 +138,6 @@ uint32_t RsHistorySerialiser::sizeHistoryMsgItem(RsHistoryMsgItem* item)
return s; return s;
} }
/* serialise the data to the buffer */ /* serialise the data to the buffer */
bool RsHistorySerialiser::serialiseHistoryMsgItem(RsHistoryMsgItem* item, void* data, uint32_t* pktsize) bool RsHistorySerialiser::serialiseHistoryMsgItem(RsHistoryMsgItem* item, void* data, uint32_t* pktsize)
{ {
@ -266,3 +289,5 @@ RsItem* RsHistorySerialiser::deserialise(void *data, uint32_t *pktsize)
} }
/*************************************************************************/ /*************************************************************************/
#endif

View File

@ -28,18 +28,21 @@
#include "rsitems/rsserviceids.h" #include "rsitems/rsserviceids.h"
#include "serialiser/rsserial.h" #include "serialiser/rsserial.h"
#include "serialiser/rsconfigitems.h"
#include "retroshare/rstypes.h" #include "retroshare/rstypes.h"
#include "serialization/rsserializer.h"
/**************************************************************************/ /**************************************************************************/
class RsHistoryMsgItem: public RsItem class RsHistoryMsgItem: public RsItem
{ {
public: public:
RsHistoryMsgItem(); RsHistoryMsgItem();
virtual ~RsHistoryMsgItem(); virtual ~RsHistoryMsgItem() {}
virtual void clear() {}
virtual void clear(); virtual void serial_process(SerializeJob j,SerializeContext& ctx);
std::ostream& print(std::ostream &out, uint16_t indent = 0);
RsPeerId chatPeerId; // empty for global chat RsPeerId chatPeerId; // empty for global chat
bool incoming; bool incoming;
@ -54,20 +57,13 @@ public:
bool saveToDisc; bool saveToDisc;
}; };
class RsHistorySerialiser: public RsSerialType class RsHistorySerialiser: public RsConfigSerializer
{ {
public: public:
RsHistorySerialiser(); RsHistorySerialiser() : RsConfigSerializer(RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_HISTORY_CONFIG) {}
virtual ~RsHistorySerialiser(); virtual ~RsHistorySerialiser() {}
virtual uint32_t size(RsItem*); virtual RsItem *create_item(uint8_t service,uint8_t type) const ;
virtual bool serialise(RsItem* item, void* data, uint32_t* size);
virtual RsItem* deserialise(void* data, uint32_t* size);
private:
virtual uint32_t sizeHistoryMsgItem(RsHistoryMsgItem*);
virtual bool serialiseHistoryMsgItem (RsHistoryMsgItem* item, void* data, uint32_t* size);
virtual RsHistoryMsgItem* deserialiseHistoryMsgItem(void* data, uint32_t* size);
}; };
/**************************************************************************/ /**************************************************************************/

View File

@ -6,7 +6,7 @@ const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_NONE ( 0x0
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_CONFIG ( 0x0001 ); const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_CONFIG ( 0x0001 );
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE ( 0x0002 ); 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<void*>((const void*)data)) ; uint32_t rstype = getRsItemId(const_cast<void*>((const void*)data)) ;
@ -36,7 +36,36 @@ RsItem *RsGenericSerializer::deserialise(void *data, uint32_t *size)
delete item ; delete item ;
return NULL ; return NULL ;
} }
RsItem *RsConfigSerializer::deserialise(void *data, uint32_t *size)
{
uint32_t rstype = getRsItemId(const_cast<void*>((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<uint8_t*>(data),std::min(50u,*size)) << ((*size>50)?"...":"") << std::endl;
return NULL ;
}
SerializeContext ctx(const_cast<uint8_t*>(static_cast<uint8_t*>(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) bool RsGenericSerializer::serialise(RsItem *item,void *data,uint32_t *size)
{ {
SerializeContext ctx(static_cast<uint8_t*>(data),0,mFormat,mFlags); SerializeContext ctx(static_cast<uint8_t*>(data),0,mFormat,mFlags);

View File

@ -25,14 +25,9 @@ public:
static const SerializationFlags SERIALIZATION_FLAG_CONFIG ; // 0x0001 static const SerializationFlags SERIALIZATION_FLAG_CONFIG ; // 0x0001
static const SerializationFlags SERIALIZATION_FLAG_SIGNATURE ; // 0x0002 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. // 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) ; bool serialise(RsItem *item,void *data,uint32_t *size) ;
uint32_t size(RsItem *item) ; uint32_t size(RsItem *item) ;
void print(RsItem *item) ; void print(RsItem *item) ;
@ -51,7 +46,6 @@ protected:
: RsSerialType(RS_PKT_VERSION_SERVICE,service), mFormat(format),mFlags(flags) : RsSerialType(RS_PKT_VERSION_SERVICE,service), mFormat(format),mFlags(flags)
{} {}
private:
SerializeContext::SerializationFormat mFormat ; SerializeContext::SerializationFormat mFormat ;
SerializationFlags mFlags ; SerializationFlags mFlags ;
@ -65,6 +59,13 @@ public:
SerializationFlags flags = SERIALIZATION_FLAG_NONE) SerializationFlags flags = SERIALIZATION_FLAG_NONE)
: RsGenericSerializer(service_id,format,flags) {} : 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 class RsConfigSerializer: public RsGenericSerializer
@ -76,6 +77,13 @@ public:
SerializationFlags flags = RsGenericSerializer::SERIALIZATION_FLAG_NONE) SerializationFlags flags = RsGenericSerializer::SERIALIZATION_FLAG_NONE)
: RsGenericSerializer(config_class,config_type,format,flags) {} : 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) ;
}; };