switched rsposteditems to new serialization

This commit is contained in:
csoler 2017-04-27 14:41:31 +02:00
parent 22f6d150df
commit 35ecc19677
3 changed files with 27 additions and 369 deletions

View File

@ -25,365 +25,39 @@
*/
#include "serialiser/rsposteditems.h"
#include "rsbaseserial.h"
#include "rstlvbase.h"
#include "serialization/rstypeserializer.h"
uint32_t RsGxsPostedSerialiser::size(RsItem *item)
void RsGxsPostedPostItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
{
#ifdef POSTED_DEBUG
std::cerr << "RsGxsPostedSerialiser::size()" << std::endl;
#endif
RsGxsPostedGroupItem* pgItem = NULL;
RsGxsPostedPostItem* ppItem = NULL;
if ((pgItem = dynamic_cast<RsGxsPostedGroupItem*>(item)) != NULL)
{
return sizeGxsPostedGroupItem(pgItem);
}
else if ((ppItem = dynamic_cast<RsGxsPostedPostItem*>(item)) != NULL)
{
return sizeGxsPostedPostItem(ppItem);
}
else
{
return RsGxsCommentSerialiser::size(item);
}
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_LINK,mPost.mLink,"mPost.mLink") ;
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_MSG ,mPost.mNotes,"mPost.mNotes") ;
}
bool RsGxsPostedSerialiser::serialise(RsItem *item, void *data, uint32_t *size)
void RsGxsPostedGroupItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
{
#ifdef POSTED_DEBUG
std::cerr << "RsGxsPostedSerialiser::serialise()" << std::endl;
#endif
RsGxsPostedPostItem* ppItem = NULL;
RsGxsPostedGroupItem* pgItem = NULL;
if ((pgItem = dynamic_cast<RsGxsPostedGroupItem*>(item)) != NULL)
{
return serialiseGxsPostedGroupItem(pgItem, data, size);
}
else if ((ppItem = dynamic_cast<RsGxsPostedPostItem*>(item)) != NULL)
{
return serialiseGxsPostedPostItem(ppItem, data, size);
}
else
{
return RsGxsCommentSerialiser::serialise(item, data, size);
}
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_DESCR ,mGroup.mDescription,"mGroup.mDescription") ;
}
RsItem* RsGxsPostedSerialiser::deserialise(void *data, uint32_t *size)
RsItem *RsGxsPostedSerialiser::create_item(uint16_t service_id,uint8_t item_subtype) const
{
#ifdef POSTED_DEBUG
std::cerr << "RsGxsPostedSerialiser::deserialise()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if(service_id != RS_SERVICE_GXS_TYPE_POSTED)
return NULL ;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_GXS_TYPE_POSTED != getRsItemService(rstype)))
{
std::cerr << "RsGxsPostedSerialiser::deserialise() ERROR Wrong Type";
std::cerr << std::endl;
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_POSTED_GRP_ITEM:
return deserialiseGxsPostedGroupItem(data, size);
break;
case RS_PKT_SUBTYPE_POSTED_POST_ITEM:
return deserialiseGxsPostedPostItem(data, size);
break;
default:
return RsGxsCommentSerialiser::deserialise(data, size);
break;
}
switch(item_subtype)
{
case RS_PKT_SUBTYPE_POSTED_GRP_ITEM: return new RsGxsPostedGroupItem() ;
case RS_PKT_SUBTYPE_POSTED_POST_ITEM: return new RsGxsPostedPostItem() ;
default:
return RsGxsCommentSerialiser::create_item(service_id,item_subtype) ;
}
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
void RsGxsPostedPostItem::clear()
{
mPost.mLink.clear();
mPost.mNotes.clear();
}
std::ostream & RsGxsPostedPostItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsGxsPostedPostItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Link: " << mPost.mLink << std::endl;
printIndent(out, int_Indent);
out << "Notes: " << mPost.mNotes << std::endl;
printRsItemEnd(out ,"RsGxsPostedPostItem", indent);
return out;
}
uint32_t RsGxsPostedSerialiser::sizeGxsPostedPostItem(RsGxsPostedPostItem* item)
{
RsPostedPost& p = item->mPost;
uint32_t s = 8;
s += GetTlvStringSize(p.mLink);
s += GetTlvStringSize(p.mNotes);
return s;
}
bool RsGxsPostedSerialiser::serialiseGxsPostedPostItem(RsGxsPostedPostItem* item, void* data, uint32_t *size)
{
#ifdef POSTED_DEBUG
std::cerr << "RsGxsPostedSerialiser::serialiseGxsPostedPostItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsPostedPostItem(item);
uint32_t offset = 0;
if(*size < tlvsize){
std::cerr << "RsGxsPostedSerialiser::serialiseGxsPostedPostItem() Size too small" << std::endl;
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* RsPostedPost */
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_LINK, item->mPost.mLink);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_MSG, item->mPost.mNotes);
if(offset != tlvsize)
{
std::cerr << "RsGxsPostedSerialiser::serialiseGxsPostedPostItem() FAIL Size Error! " << std::endl;
ok = false;
}
#ifdef POSTED_DEBUG
if (!ok)
{
std::cerr << "RsGxsPostedSerialiser::serialiseGxsPostedPostItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsPostedPostItem* RsGxsPostedSerialiser::deserialiseGxsPostedPostItem(void *data, uint32_t *size)
{
#ifdef POSTED_DEBUG
std::cerr << "RsGxsPostedSerialiser::deserialiseGxsPostedPostItem()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_GXS_TYPE_POSTED != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_POSTED_POST_ITEM != getRsItemSubType(rstype)))
{
std::cerr << "RsGxsPostedSerialiser::deserialiseGxsPostedPostItem() FAIL wrong type" << std::endl;
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
std::cerr << "RsGxsPostedSerialiser::deserialiseGxsPostedPostItem() FAIL wrong size" << std::endl;
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsPostedPostItem* item = new RsGxsPostedPostItem();
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_LINK, item->mPost.mLink);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_MSG, item->mPost.mNotes);
if (offset != rssize)
{
std::cerr << "RsGxsPostedSerialiser::deserialiseGxsPostedPostItem() FAIL size mismatch" << std::endl;
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef POSTED_DEBUG
std::cerr << "RsGxsPostedSerialiser::deserialiseGxsPostedPostItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
void RsGxsPostedGroupItem::clear()
{
mGroup.mDescription.clear();
return;
}
std::ostream & RsGxsPostedGroupItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsGxsPostedGroupItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Description: " << mGroup.mDescription << std::endl;
printRsItemEnd(out ,"RsGxsPostedGroupItem", indent);
return out;
}
uint32_t RsGxsPostedSerialiser::sizeGxsPostedGroupItem(RsGxsPostedGroupItem* item)
{
RsPostedGroup& g = item->mGroup;
uint32_t s = 8; // header
s += GetTlvStringSize(g.mDescription);
return s;
}
bool RsGxsPostedSerialiser::serialiseGxsPostedGroupItem(RsGxsPostedGroupItem* item, void* data, uint32_t *size)
{
#ifdef POSTED_DEBUG
std::cerr << "RsGxsPostedSerialiser::serialiseGxsPostedGroupItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsPostedGroupItem(item);
uint32_t offset = 0;
if(*size < tlvsize){
#ifdef POSTED_DEBUG
std::cerr << "RsGxsPostedSerialiser::serialiseGxsPostedGroupItem()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* PostedGroupItem */
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_DESCR, item->mGroup.mDescription);
if(offset != tlvsize)
{
#ifdef POSTED_DEBUG
std::cerr << "RsGxsPostedSerialiser::serialiseGxsPostedGroupItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef POSTED_DEBUG
if (!ok)
{
std::cerr << "RsGxsPostedSerialiser::serialiseGxsPostedGroupItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsPostedGroupItem* RsGxsPostedSerialiser::deserialiseGxsPostedGroupItem(void *data, uint32_t *size)
{
#ifdef POSTED_DEBUG
std::cerr << "RsGxsPostedSerialiser::deserialiseGxsPostedGroupItem()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_GXS_TYPE_POSTED != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_POSTED_GRP_ITEM != getRsItemSubType(rstype)))
{
std::cerr << "RsGxsPostedSerialiser::deserialiseGxsPostedGroupItem() FAIL wrong type" << std::endl;
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
std::cerr << "RsGxsPostedSerialiser::deserialiseGxsPostedGroupItem() FAIL wrong size" << std::endl;
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsPostedGroupItem* item = new RsGxsPostedGroupItem();
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_DESCR, item->mGroup.mDescription);
if (offset != rssize)
{
std::cerr << "RsGxsPostedSerialiser::deserialiseGxsPostedGroupItem() FAIL size mismatch" << std::endl;
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef POSTED_DEBUG
std::cerr << "RsGxsPostedSerialiser::deserialiseGxsPostedGroupItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}

View File

@ -7,18 +7,17 @@
#include "retroshare/rsposted.h"
const uint8_t RS_PKT_SUBTYPE_POSTED_GRP_ITEM = 0x02;
const uint8_t RS_PKT_SUBTYPE_POSTED_GRP_ITEM = 0x02;
const uint8_t RS_PKT_SUBTYPE_POSTED_POST_ITEM = 0x03;
class RsGxsPostedGroupItem : public RsGxsGrpItem
{
public:
RsGxsPostedGroupItem() : RsGxsGrpItem(RS_SERVICE_GXS_TYPE_POSTED,
RS_PKT_SUBTYPE_POSTED_GRP_ITEM) { return; }
virtual ~RsGxsPostedGroupItem() { return; }
RsGxsPostedGroupItem() : RsGxsGrpItem(RS_SERVICE_GXS_TYPE_POSTED, RS_PKT_SUBTYPE_POSTED_GRP_ITEM) {}
virtual ~RsGxsPostedGroupItem() {}
void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
RsPostedGroup mGroup;
};
@ -26,40 +25,24 @@ public:
class RsGxsPostedPostItem : public RsGxsMsgItem
{
public:
RsGxsPostedPostItem() : RsGxsMsgItem(RS_SERVICE_GXS_TYPE_POSTED,
RS_PKT_SUBTYPE_POSTED_POST_ITEM) {return ; }
virtual ~RsGxsPostedPostItem() { return; }
RsGxsPostedPostItem() : RsGxsMsgItem(RS_SERVICE_GXS_TYPE_POSTED, RS_PKT_SUBTYPE_POSTED_POST_ITEM) {}
virtual ~RsGxsPostedPostItem() {}
void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
RsPostedPost mPost;
};
class RsGxsPostedSerialiser : public RsGxsCommentSerialiser
{
public:
RsGxsPostedSerialiser()
:RsGxsCommentSerialiser(RS_SERVICE_GXS_TYPE_POSTED)
{ return; }
RsGxsPostedSerialiser() :RsGxsCommentSerialiser(RS_SERVICE_GXS_TYPE_POSTED) {}
virtual ~RsGxsPostedSerialiser() { return; }
virtual ~RsGxsPostedSerialiser() {}
uint32_t size(RsItem *item);
bool serialise(RsItem *item, void *data, uint32_t *size);
RsItem* deserialise(void *data, uint32_t *size);
private:
uint32_t sizeGxsPostedGroupItem(RsGxsPostedGroupItem* item);
bool serialiseGxsPostedGroupItem(RsGxsPostedGroupItem* item, void* data, uint32_t *size);
RsGxsPostedGroupItem* deserialiseGxsPostedGroupItem(void *data, uint32_t *size);
uint32_t sizeGxsPostedPostItem(RsGxsPostedPostItem* item);
bool serialiseGxsPostedPostItem(RsGxsPostedPostItem* item, void* data, uint32_t *size);
RsGxsPostedPostItem* deserialiseGxsPostedPostItem(void *data, uint32_t *size);
virtual RsItem *create_item(uint16_t service_id,uint8_t item_subtype) const ;
};

View File

@ -156,6 +156,7 @@ bool p3Posted::getPostData(const uint32_t &token, std::vector<RsPostedPost> &msg
std::cerr << "Not a PostedPostItem neither a RsGxsCommentItem"
<< " PacketService=" << std::hex << (int)msg->PacketService() << std::dec
<< " PacketSubType=" << std::hex << (int)msg->PacketSubType() << std::dec
<< " type name =" << typeid(*msg).name()
<< " , deleting!" << std::endl;
delete *vit;
}