converted RsMsgItems to new serialization. Added format and flag members to RsSerialiser and used the later to handle signature and config serialization techniques

This commit is contained in:
csoler 2017-04-23 22:40:26 +02:00
parent 63b4b3a1f7
commit 31c07f4dfd
10 changed files with 202 additions and 183 deletions

View File

@ -222,7 +222,7 @@ bool DistributedChatService::checkSignature(RsChatLobbyBouncingObject *obj,const
mGixs->requestKey(obj->signature.keyId,peer_list,RsIdentityUsage(RS_SERVICE_TYPE_CHAT,RsIdentityUsage::CHAT_LOBBY_MSG_VALIDATION,RsGxsGroupId(),RsGxsMessageId(),obj->lobby_id));
uint32_t size = obj->serial_size_for_signature() ;
uint32_t size = RsChatSerialiser(RsSerializer::SERIALIZATION_FLAG_SIGNATURE).size((RsItem*)obj) ;
RsTemporaryMemory memory(size) ;
#ifdef DEBUG_CHAT_LOBBIES
@ -230,7 +230,7 @@ bool DistributedChatService::checkSignature(RsChatLobbyBouncingObject *obj,const
std::cerr << " signature id: " << obj->signature.keyId << std::endl;
#endif
if(!obj->serialize_for_signature(memory,size))
if(!RsChatSerialiser(RsSerializer::SERIALIZATION_FLAG_SIGNATURE).serialise((RsItem*)obj,memory,&size))
{
std::cerr << " (EE) Cannot serialise message item. " << std::endl;
return false ;
@ -239,7 +239,7 @@ bool DistributedChatService::checkSignature(RsChatLobbyBouncingObject *obj,const
uint32_t error_status ;
RsIdentityUsage use_info(RS_SERVICE_TYPE_CHAT,RsIdentityUsage::CHAT_LOBBY_MSG_VALIDATION,RsGxsGroupId(),RsGxsMessageId(),obj->lobby_id) ;
if(!mGixs->validateData(memory,obj->serial_size_for_signature(),obj->signature,false,use_info,error_status))
if(!mGixs->validateData(memory,size,obj->signature,false,use_info,error_status))
{
bool res = false ;
@ -970,10 +970,10 @@ bool DistributedChatService::locked_initLobbyBouncableObject(const ChatLobbyId&
// now sign the object, if the lobby expects it
uint32_t size = item.serial_size_for_signature() ;
uint32_t size = RsChatSerialiser(RsSerializer::SERIALIZATION_FLAG_SIGNATURE).size((RsItem*)&item) ;
RsTemporaryMemory memory(size) ;
if(!item.serialize_for_signature(memory,size))
if(!RsChatSerialiser(RsSerializer::SERIALIZATION_FLAG_SIGNATURE).serialise((RsItem*)&item,memory,&size))
{
std::cerr << "(EE) Cannot sign message item. " << std::endl;
return false ;

View File

@ -80,13 +80,13 @@ RsChatAvatarItem::~RsChatAvatarItem()
}
}
void RsChatLobbyBouncingObject::serial_process(RsItem::SerializeJob j, SerializeContext& ctx, bool include_signature)
void RsChatLobbyBouncingObject::serial_process(RsItem::SerializeJob j, SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,lobby_id,"lobby_id") ;
RsTypeSerializer::serial_process(j,ctx,msg_id ,"msg_id") ;
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_NAME,nick,"nick") ;
if(include_signature)
if(!(ctx.mFlags & RsSerializer::SERIALIZATION_FLAG_SIGNATURE))
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,signature,"signature") ;
}
@ -94,13 +94,7 @@ void RsChatLobbyMsgItem::serial_process(RsItem::SerializeJob j,SerializeContext&
{
RsChatMsgItem::serial_process(j,ctx) ;
RsTypeSerializer::serial_process(j,ctx,parent_msg_id,"parent_msg_id") ;
RsChatLobbyBouncingObject::serial_process(j,ctx,true) ;
}
void RsChatLobbyMsgItem::serial_process_for_signature(RsItem::SerializeJob j,SerializeContext& ctx)
{
RsChatMsgItem::serial_process(j,ctx) ;
RsTypeSerializer::serial_process(j,ctx,parent_msg_id,"parent_msg_id") ;
RsChatLobbyBouncingObject::serial_process(j,ctx,false) ;
RsChatLobbyBouncingObject::serial_process(j,ctx) ;
}
void RsChatLobbyListRequestItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
@ -129,15 +123,7 @@ void RsChatLobbyEventItem::serial_process(RsItem::SerializeJob j,SerializeContex
RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_NAME,string1,"string1") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,sendTime ,"sendTime") ;
RsChatLobbyBouncingObject::serial_process(j,ctx,true) ;
}
void RsChatLobbyEventItem::serial_process_for_signature(RsItem::SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint8_t>(j,ctx,event_type,"event_type") ;
RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_NAME,string1,"string1") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,sendTime ,"sendTime") ;
RsChatLobbyBouncingObject::serial_process(j,ctx,false) ;
RsChatLobbyBouncingObject::serial_process(j,ctx) ;
}
void RsChatLobbyUnsubscribeItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{
@ -209,41 +195,4 @@ void RsPrivateChatMsgConfigItem::get(RsChatMsgItem *ci)
ci->recvTime = recvTime;
}
uint32_t RsChatLobbyBouncingObject::serial_size_for_signature() const
{
SerializeContext ctx(NULL,0);
ctx.mOffset = 8;
const_cast<RsChatLobbyBouncingObject*>(this)->serial_process_for_signature(RsItem::SIZE_ESTIMATE,ctx) ;
return ctx.mOffset ;
}
bool RsChatLobbyBouncingObject::serialize_for_signature(uint8_t *data,uint32_t size) const
{
SerializeContext ctx(data,0);
uint32_t tlvsize = serial_size_for_signature() ;
if(tlvsize > size)
throw std::runtime_error("Cannot serialise: not enough room.") ;
if(!setRsItemHeader(data, tlvsize, PacketId(), tlvsize))
{
std::cerr << "RsSerializer::serialise_item(): ERROR. Not enough size!" << std::endl;
return false ;
}
ctx.mOffset = 8;
ctx.mSize = tlvsize;
const_cast<RsChatLobbyBouncingObject*>(this)->serial_process_for_signature(RsItem::SERIALIZE,ctx) ;
if(ctx.mSize != ctx.mOffset)
{
std::cerr << "RsChatSerializer::serialise_item(): ERROR. offset does not match expected size!" << std::endl;
return false ;
}
return true ;
}

View File

@ -137,16 +137,12 @@ public:
virtual RsChatLobbyBouncingObject *duplicate() const = 0 ;
uint32_t serial_size_for_signature() const ;
bool serialize_for_signature(uint8_t *data,uint32_t size) const ;
protected:
// The functions below handle the serialisation of data that is specific to the bouncing object level.
// They are called by serial_size() and serialise() from children, but should not overload the serial_size() and
// serialise() methods, otherwise the wrong method will be called when serialising from this top level class.
virtual void serial_process(RsItem::SerializeJob j,SerializeContext& ctx,bool include_signature);
virtual void serial_process_for_signature(RsItem::SerializeJob j,SerializeContext& ctx)=0;
virtual void serial_process(RsItem::SerializeJob j, SerializeContext& ctx);
virtual uint32_t PacketId() const= 0;
};
@ -160,7 +156,6 @@ public:
virtual RsChatLobbyBouncingObject *duplicate() const { return new RsChatLobbyMsgItem(*this) ; }
void serial_process(RsItem::SerializeJob j,SerializeContext& ctx) ;
void serial_process_for_signature(RsItem::SerializeJob j,SerializeContext& ctx) ; // This one is new, and used to add/remove signature on demand
ChatLobbyMsgId parent_msg_id ; // Used for threaded chat.
@ -177,7 +172,6 @@ public:
virtual RsChatLobbyBouncingObject *duplicate() const { return new RsChatLobbyEventItem(*this) ; }
//
void serial_process(RsItem::SerializeJob j,SerializeContext& ctx);
void serial_process_for_signature(RsItem::SerializeJob j,SerializeContext& ctx) ; // This one is new, and used to add/remove signature on demand
// members.
//
@ -330,7 +324,8 @@ class RsChatAvatarItem: public RsChatItem
class RsChatSerialiser: public RsSerializer
{
public:
RsChatSerialiser() :RsSerializer(RS_SERVICE_TYPE_CHAT) {}
RsChatSerialiser(SerializationFlags flags = SERIALIZATION_FLAG_NONE)
:RsSerializer(RS_SERVICE_TYPE_CHAT,SerializeContext::FORMAT_BINARY,flags) {}
virtual RsItem *create_item(uint16_t service_id,uint8_t item_sub_id) const ;
};

View File

@ -59,11 +59,12 @@ template<int n> class t_RsFlags32
uint32_t _bits ;
};
#define FLAGS_TAG_TRANSFER_REQS 0x4228af
#define FLAGS_TAG_TRANSFER_REQS 0x4228af
#define FLAGS_TAG_FILE_STORAGE 0x184738
#define FLAGS_TAG_FILE_SEARCH 0xf29ba5
#define FLAGS_TAG_FILE_SEARCH 0xf29ba5
#define FLAGS_TAG_SERVICE_PERM 0x380912
#define FLAGS_TAG_SERVICE_CHAT 0x839042
#define FLAGS_TAG_SERIALIZER 0xa0338d
// Flags for requesting transfers, ask for turtle, cache, speed, etc.
//
@ -85,3 +86,7 @@ typedef t_RsFlags32<FLAGS_TAG_SERVICE_PERM > ServicePermissionFlags ;
//
typedef t_RsFlags32<FLAGS_TAG_SERVICE_CHAT > ChatLobbyFlags ;
// Flags for serializer
//
typedef t_RsFlags32<FLAGS_TAG_SERIALIZER > SerializationFlags ;

View File

@ -30,12 +30,35 @@
#include "serialiser/rsmsgitems.h"
#include "serialiser/rstlvbase.h"
#include "serialization/rstypeserializer.h"
/***
#define RSSERIAL_DEBUG 1
***/
#include <iostream>
RsItem *RsMsgSerialiser::create_item(uint16_t service,uint8_t type) const
{
if(service != RS_SERVICE_TYPE_MSG)
return NULL ;
switch(type)
{
case RS_PKT_SUBTYPE_DEFAULT : return new RsMsgItem() ; //= 0x01;
case RS_PKT_SUBTYPE_MSG_TAG_TYPE : return new RsMsgTagType() ; //= 0x03;
case RS_PKT_SUBTYPE_MSG_TAGS : return new RsMsgTags() ; //= 0x04;
case RS_PKT_SUBTYPE_MSG_SRC_TAG : return new RsMsgSrcId(); //= 0x05;
case RS_PKT_SUBTYPE_MSG_PARENT_TAG : return new RsMsgParentId() ; //= 0x06;
case RS_PKT_SUBTYPE_MSG_INVITE : return new RsPublicMsgInviteConfigItem(); //= 0x07;
case RS_PKT_SUBTYPE_MSG_GROUTER_MAP : return new RsMsgGRouterMap(); //= 0x08;
case RS_PKT_SUBTYPE_MSG_DISTANT_MSG_MAP : return new RsMsgDistantMessagesHashMap();//= 0x09;
default:
return NULL ;
}
}
void RsMsgItem::clear()
{
msgId = 0;
@ -56,6 +79,107 @@ void RsMsgItem::clear()
attachment.TlvClear();
}
void RsPublicMsgInviteConfigItem::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_HASH_SHA1,hash,"hash") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,(uint32_t&)time_stamp,"time_stamp") ;
}
void RsMsgTagType::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_NAME,text,"text") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,rgb_color,"rgb_color") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,tagId,"tagId") ;
}
void RsMsgTags::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint32_t>(j,ctx,msgId,"msgId") ;
#warning this is not the correct way to serialise here. We should directly call serial_process<std::vector<uint32_t> >() but for backward compatibility, we cannot
if(j == RsItem::DESERIALIZE)
while(ctx.mOffset < ctx.mSize)
{
uint32_t n ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,n,"tagIds element") ;
tagIds.push_back(n) ;
}
else
for(std::list<uint32_t>::iterator it(tagIds.begin());it!=tagIds.end();++it)
RsTypeSerializer::serial_process<uint32_t>(j,ctx,*it,"tagIds element") ;
}
void RsMsgSrcId::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint32_t>(j,ctx,msgId,"msgId") ;
RsTypeSerializer::serial_process (j,ctx,srcId,"srcId") ;
}
void RsMsgGRouterMap::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,ongoing_msgs,"ongoing_msgs") ;
}
void RsMsgGRouterMap::clear()
{
ongoing_msgs.clear() ;
return;
}
void RsMsgDistantMessagesHashMap::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,hash_map,"hash_map") ;
}
void RsMsgParentId::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint32_t>(j,ctx,msgId,"msgId") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,msgParentId,"msgParentId") ;
}
void RsMsgItem::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint32_t>(j,ctx,msgFlags,"msgFlags");
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_SUBJECT,subject,"subject");
RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_MSG,message,"message");
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,rspeerid_msgto,"rspeerid_msgto");
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,rspeerid_msgcc,"rspeerid_msgcc");
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,rspeerid_msgbcc,"rspeerid_msgbcc");
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,rsgxsid_msgto,"rsgxsid_msgto");
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,rsgxsid_msgcc,"rsgxsid_msgcc");
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,rsgxsid_msgbcc,"rsgxsid_msgbcc");
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,attachment,"attachment");
if(ctx.mFlags & RsSerializer::SERIALIZATION_FLAG_CONFIG)
RsTypeSerializer::serial_process<uint32_t>(j,ctx,msgId,"msgId");
}
void RsMsgTagType::clear()
{
text.clear();
tagId = 0;
rgb_color = 0;
}
void RsPublicMsgInviteConfigItem::clear()
{
hash.clear() ;
time_stamp = 0 ;
}
void RsMsgTags::clear()
{
msgId = 0;
tagIds.clear();
}
#ifdef TO_REMOVE
std::ostream &RsMsgItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsMsgItem", indent);
@ -100,19 +224,6 @@ std::ostream &RsMsgItem::print(std::ostream &out, uint16_t indent)
printRsItemEnd(out, "RsMsgItem", indent);
return out;
}
void RsMsgTagType::clear()
{
text.clear();
tagId = 0;
rgb_color = 0;
}
void RsPublicMsgInviteConfigItem::clear()
{
hash.clear() ;
time_stamp = 0 ;
}
std::ostream& RsPublicMsgInviteConfigItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsPublicMsgInviteConfigItem", indent);
@ -128,12 +239,6 @@ std::ostream& RsPublicMsgInviteConfigItem::print(std::ostream &out, uint16_t ind
return out;
}
void RsMsgTags::clear()
{
msgId = 0;
tagIds.clear();
}
std::ostream& RsMsgTagType::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsMsgTagType", indent);
@ -346,7 +451,6 @@ uint32_t RsMsgTagType::serial_size(bool)
return s;
}
bool RsPublicMsgInviteConfigItem::serialise(void *data, uint32_t& pktsize,bool config)
{
uint32_t tlvsize = serial_size(config) ;
@ -382,8 +486,6 @@ bool RsPublicMsgInviteConfigItem::serialise(void *data, uint32_t& pktsize,bool c
return ok;
}
bool RsMsgTagType::serialise(void *data, uint32_t& pktsize,bool config)
{
uint32_t tlvsize = serial_size( config) ;
@ -536,7 +638,6 @@ uint32_t RsMsgTags::serial_size(bool)
return s;
}
bool RsMsgTags::serialise(void *data, uint32_t& pktsize,bool config)
{
uint32_t tlvsize = serial_size( config) ;
@ -672,8 +773,6 @@ uint32_t RsMsgSrcId::serial_size(bool)
return s;
}
bool RsMsgSrcId::serialise(void *data, uint32_t& pktsize,bool config)
{
uint32_t tlvsize = serial_size(config) ;
@ -777,14 +876,6 @@ std::ostream& RsMsgGRouterMap::print(std::ostream& out, uint16_t indent)
return out;
}
void RsMsgGRouterMap::clear()
{
ongoing_msgs.clear() ;
return;
}
uint32_t RsMsgGRouterMap::serial_size(bool)
{
uint32_t s = 8; /* header */
@ -933,7 +1024,6 @@ uint32_t RsMsgDistantMessagesHashMap::serial_size(bool)
return s;
}
bool RsMsgDistantMessagesHashMap::serialise(void *data, uint32_t& pktsize,bool config)
{
uint32_t tlvsize = serial_size(config) ;
@ -1073,7 +1163,6 @@ uint32_t RsMsgParentId::serial_size(bool)
return s;
}
bool RsMsgParentId::serialise(void *data, uint32_t& pktsize,bool config)
{
uint32_t tlvsize = serial_size( config) ;
@ -1198,3 +1287,6 @@ RsItem* RsMsgSerialiser::deserialise(void *data, uint32_t *pktsize)
/*************************************************************************/
#endif

View File

@ -90,10 +90,6 @@ class RsMessageItem: public RsItem
virtual ~RsMessageItem() {}
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0 ;
virtual bool serialise(void *data,uint32_t& size,bool config) = 0 ;
virtual uint32_t serial_size(bool config) = 0 ;
};
@ -105,10 +101,7 @@ class RsMsgItem: public RsMessageItem
virtual ~RsMsgItem() {}
virtual void clear();
virtual bool serialise(void *data,uint32_t& size,bool config) ;
virtual uint32_t serial_size(bool config) ;
virtual std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */);
// ----------- Specific fields ------------- //
@ -137,10 +130,7 @@ class RsMsgTagType : public RsMessageItem
public:
RsMsgTagType() :RsMessageItem(RS_PKT_SUBTYPE_MSG_TAG_TYPE) {}
virtual std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size,bool config) ;
virtual uint32_t serial_size(bool config) ;
virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */);
virtual ~RsMsgTagType() {}
virtual void clear();
@ -158,10 +148,7 @@ public:
RsMsgTags()
:RsMessageItem(RS_PKT_SUBTYPE_MSG_TAGS) {}
virtual bool serialise(void *data,uint32_t& size,bool config) ;
virtual uint32_t serial_size(bool config) ;
virtual std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */);
virtual ~RsMsgTags() {}
virtual void clear();
@ -177,13 +164,10 @@ class RsMsgSrcId : public RsMessageItem
public:
RsMsgSrcId() : RsMessageItem(RS_PKT_SUBTYPE_MSG_SRC_TAG) {}
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size,bool config) ;
virtual uint32_t serial_size(bool config) ;
virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */);
virtual ~RsMsgSrcId() {}
virtual void clear();
virtual void clear(){}
// ----------- Specific fields ------------- //
//
@ -196,10 +180,7 @@ class RsPublicMsgInviteConfigItem : public RsMessageItem
public:
RsPublicMsgInviteConfigItem() : RsMessageItem(RS_PKT_SUBTYPE_MSG_INVITE) {}
virtual bool serialise(void *data,uint32_t& size,bool config) ;
virtual uint32_t serial_size(bool config) ;
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */);
virtual ~RsPublicMsgInviteConfigItem() {}
virtual void clear();
@ -215,10 +196,7 @@ class RsMsgGRouterMap : public RsMessageItem
public:
RsMsgGRouterMap() : RsMessageItem(RS_PKT_SUBTYPE_MSG_GROUTER_MAP) {}
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size,bool config) ;
virtual uint32_t serial_size(bool config) ;
virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */);
virtual ~RsMsgGRouterMap() {}
virtual void clear();
@ -232,13 +210,10 @@ class RsMsgDistantMessagesHashMap : public RsMessageItem
public:
RsMsgDistantMessagesHashMap() : RsMessageItem(RS_PKT_SUBTYPE_MSG_DISTANT_MSG_MAP) {}
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size,bool config) ;
virtual uint32_t serial_size(bool config) ;
virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */);
virtual ~RsMsgDistantMessagesHashMap() {}
virtual void clear();
virtual void clear() { hash_map.clear() ;}
// ----------- Specific fields ------------- //
//
@ -249,13 +224,10 @@ class RsMsgParentId : public RsMessageItem
public:
RsMsgParentId() : RsMessageItem(RS_PKT_SUBTYPE_MSG_PARENT_TAG) {}
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size,bool config) ;
virtual uint32_t serial_size(bool config) ;
virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */);
virtual ~RsMsgParentId() {}
virtual void clear();
virtual void clear(){}
// ----------- Specific fields ------------- //
//
@ -263,39 +235,15 @@ class RsMsgParentId : public RsMessageItem
uint32_t msgParentId;
};
class RsMsgSerialiser: public RsSerialType
class RsMsgSerialiser: public RsSerializer
{
public:
RsMsgSerialiser(bool bConfiguration = false)
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_MSG), m_bConfiguration (bConfiguration) {}
RsMsgSerialiser(uint16_t type)
:RsSerialType(RS_PKT_VERSION_SERVICE, type), m_bConfiguration (false) {}
RsMsgSerialiser(SerializationFlags flags)
:RsSerializer(RS_SERVICE_TYPE_MSG,SerializeContext::FORMAT_BINARY,flags){}
virtual ~RsMsgSerialiser() {}
virtual uint32_t size(RsItem *item)
{
return dynamic_cast<RsMessageItem*>(item)->serial_size(m_bConfiguration) ;
}
virtual bool serialise (RsItem *item, void *data, uint32_t *size)
{
return dynamic_cast<RsMessageItem*>(item)->serialise(data,*size,m_bConfiguration) ;
}
virtual RsItem * deserialise(void *data, uint32_t *size);
private:
virtual RsMsgItem *deserialiseMsgItem(void *data, uint32_t *size);
virtual RsMsgTagType *deserialiseTagItem(void *data, uint32_t *size);
virtual RsMsgTags *deserialiseMsgTagItem(void *data, uint32_t *size);
virtual RsMsgSrcId *deserialiseMsgSrcIdItem(void *data, uint32_t *size);
virtual RsMsgParentId *deserialiseMsgParentIdItem(void *data, uint32_t *size);
virtual RsPublicMsgInviteConfigItem *deserialisePublicMsgInviteConfigItem(void *data, uint32_t *size);
virtual RsMsgGRouterMap *deserialiseMsgGRouterMap(void *data, uint32_t *size);
virtual RsMsgDistantMessagesHashMap *deserialiseMsgDistantMessageHashMap(void *data, uint32_t *size);
bool m_bConfiguration; // is set to true for saving configuration (enables serialising msgId)
virtual RsItem *create_item(uint16_t service,uint8_t type) const ;
};
/**************************************************************************/

View File

@ -2,6 +2,10 @@
#include "serialization/rsserializer.h"
#include "serialization/rstypeserializer.h"
const SerializationFlags RsSerializer::SERIALIZATION_FLAG_NONE ( 0x0000 );
const SerializationFlags RsSerializer::SERIALIZATION_FLAG_CONFIG ( 0x0001 );
const SerializationFlags RsSerializer::SERIALIZATION_FLAG_SIGNATURE ( 0x0002 );
RsItem *RsSerializer::deserialise(void *data, uint32_t *size)
{
uint32_t rstype = getRsItemId(const_cast<void*>((const void*)data)) ;
@ -15,7 +19,7 @@ RsItem *RsSerializer::deserialise(void *data, uint32_t *size)
return NULL ;
}
SerializeContext ctx(const_cast<uint8_t*>(static_cast<uint8_t*>(data)),*size);
SerializeContext ctx(const_cast<uint8_t*>(static_cast<uint8_t*>(data)),*size,mFormat,mFlags);
ctx.mOffset = 8 ;
item->serial_process(RsItem::DESERIALIZE, ctx) ;
@ -35,7 +39,7 @@ RsItem *RsSerializer::deserialise(void *data, uint32_t *size)
bool RsSerializer::serialise(RsItem *item,void *data,uint32_t *size)
{
SerializeContext ctx(static_cast<uint8_t*>(data),0);
SerializeContext ctx(static_cast<uint8_t*>(data),0,mFormat,mFlags);
uint32_t tlvsize = this->size(item) ;
@ -62,7 +66,7 @@ bool RsSerializer::serialise(RsItem *item,void *data,uint32_t *size)
uint32_t RsSerializer::size(RsItem *item)
{
SerializeContext ctx(NULL,0);
SerializeContext ctx(NULL,0,mFormat,mFlags);
ctx.mOffset = 8 ; // header size
item->serial_process(RsItem::SIZE_ESTIMATE, ctx) ;
@ -72,7 +76,7 @@ uint32_t RsSerializer::size(RsItem *item)
void RsSerializer::print(RsItem *item)
{
SerializeContext ctx(NULL,0);
SerializeContext ctx(NULL,0,mFormat,mFlags);
std::cerr << "***** RsItem class: \"" << typeid(*item).name() << "\" *****" << std::endl;
item->serial_process(RsItem::PRINT, ctx) ;

View File

@ -6,13 +6,28 @@
#include <string>
#include "serialiser/rsserial.h"
#include "serialization/rstypeserializer.h"
#define SERIALIZE_ERROR() std::cerr << __PRETTY_FUNCTION__ << " : "
class RsSerializer: public RsSerialType
{
public:
RsSerializer(uint16_t service_id) : RsSerialType(RS_PKT_VERSION_SERVICE,service_id) {}
// These are convenience flags to be used by the items when processing the data. The names of the flags
// are not very important. What matters is that the serial_process() method of each item correctly
// deals with the data when it sees the flags, if the serialiser sets them. By default the flags are not
// set and shouldn't be handled.
// When deriving a new serializer, the user can set his own flags, using compatible values
static const SerializationFlags SERIALIZATION_FLAG_NONE ; // 0x0000
static const SerializationFlags SERIALIZATION_FLAG_CONFIG ; // 0x0001
static const SerializationFlags SERIALIZATION_FLAG_SIGNATURE ; // 0x0002
RsSerializer(uint16_t service_id,
SerializeContext::SerializationFormat format = SerializeContext::FORMAT_BINARY,
SerializationFlags flags = SERIALIZATION_FLAG_NONE)
: RsSerialType(RS_PKT_VERSION_SERVICE,service_id),mFormat(format),mFlags(flags) {}
/*! create_item
* should be overloaded to create the correct type of item depending on the data
@ -28,6 +43,10 @@ class RsSerializer: public RsSerialType
bool serialise(RsItem *item,void *data,uint32_t *size) ;
uint32_t size(RsItem *item) ;
void print(RsItem *item) ;
private:
SerializeContext::SerializationFormat mFormat ;
SerializationFlags mFlags ;
};

View File

@ -11,13 +11,20 @@ class SerializeContext
{
public:
SerializeContext(uint8_t *data,uint32_t size)
: mData(data),mSize(size),mOffset(0),mOk(true) {}
enum SerializationFormat {
FORMAT_BINARY = 0x01 ,
FORMAT_JSON = 0x02
};
SerializeContext(uint8_t *data,uint32_t size,SerializationFormat format,SerializationFlags flags)
: mData(data),mSize(size),mOffset(0),mOk(true),mFormat(format),mFlags(flags) {}
unsigned char *mData ;
uint32_t mSize ;
uint32_t mOffset ;
bool mOk ;
SerializationFormat mFormat ;
SerializationFlags mFlags ;
};

View File

@ -86,7 +86,7 @@ static const uint32_t RS_MSG_DISTANT_MESSAGE_HASH_KEEP_TIME = 2*30*86400 ; // ke
p3MsgService::p3MsgService(p3ServiceControl *sc, p3IdService *id_serv)
:p3Service(), p3Config(), mIdService(id_serv), mServiceCtrl(sc), mMsgMtx("p3MsgService"), mMsgUniqueId(0)
{
_serialiser = new RsMsgSerialiser(); // this serialiser is used for services. It's not the same than the one returned by setupSerialiser(). We need both!!
_serialiser = new RsMsgSerialiser(RsSerializer::SERIALIZATION_FLAG_NONE); // this serialiser is used for services. It's not the same than the one returned by setupSerialiser(). We need both!!
addSerialType(_serialiser);
mMsgUniqueId = 1 ; // MsgIds are not transmitted, but only used locally as a storage index. As such, thay do not need to be different
@ -519,7 +519,7 @@ RsSerialiser* p3MsgService::setupSerialiser() // this serialiser is used for con
{
RsSerialiser *rss = new RsSerialiser ;
rss->addSerialType(new RsMsgSerialiser(true));
rss->addSerialType(new RsMsgSerialiser(RsSerializer::SERIALIZATION_FLAG_CONFIG));
rss->addSerialType(new RsGeneralConfigSerialiser());
return rss;
@ -2024,10 +2024,10 @@ void p3MsgService::sendDistantMsgItem(RsMsgItem *msgitem)
// The item is serialized and turned into a generic turtle item. Use use the explicit serialiser to make sure that the msgId is not included
uint32_t msg_serialized_rssize = msgitem->serial_size(false) ;
uint32_t msg_serialized_rssize = RsMsgSerialiser(RsSerializer::SERIALIZATION_FLAG_NONE).size(msgitem) ;
RsTemporaryMemory msg_serialized_data(msg_serialized_rssize) ;
if(!msgitem->serialise(msg_serialized_data,msg_serialized_rssize,false))
if(!RsMsgSerialiser(RsSerializer::SERIALIZATION_FLAG_NONE).serialise(msgitem,msg_serialized_data,&msg_serialized_rssize))
{
std::cerr << "(EE) p3MsgService::sendTurtleData(): Serialization error." << std::endl;
return ;