converted GxsReputationItems to new serialization

This commit is contained in:
csoler 2017-04-22 21:10:55 +02:00
parent e2d9152b22
commit 311f257090
5 changed files with 156 additions and 78 deletions

View File

@ -28,40 +28,14 @@
#include "serialiser/rsbaseserial.h" #include "serialiser/rsbaseserial.h"
#include "serialiser/rsgxsreputationitems.h" #include "serialiser/rsgxsreputationitems.h"
#include "serialization/rstypeserializer.h"
/*** /***
#define RSSERIAL_DEBUG 1 #define RSSERIAL_DEBUG 1
***/ ***/
#include <iostream> #include <iostream>
// re-defined here, in order to avoid cross-includes
#define REPUTATION_IDENTITY_FLAG_NEEDS_UPDATE 0x0100
/*************************************************************************/
bool RsReputationItem::serialise_header(void *data,uint32_t& pktsize,uint32_t& tlvsize, uint32_t& offset) const
{
tlvsize = serial_size() ;
offset = 0;
if (pktsize < tlvsize)
return false; /* not enough space */
pktsize = tlvsize;
if(!setRsItemHeader(data, tlvsize, PacketId(), tlvsize))
{
std::cerr << "RsReputationItem::serialise_header(): ERROR. Not enough size!" << std::endl;
return false ;
}
#ifdef RSSERIAL_DEBUG
std::cerr << "RsReputationItem::serialise() Header: " << ok << std::endl;
#endif
offset += 8;
return true ;
}
/*************************************************************************/ /*************************************************************************/
void RsGxsReputationSetItem::clear() void RsGxsReputationSetItem::clear()
@ -79,6 +53,45 @@ void RsGxsReputationBannedNodeSetItem::clear()
mKnownIdentities.TlvClear(); mKnownIdentities.TlvClear();
} }
void RsGxsReputationConfigItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process (j,ctx,mPeerId,"mPeerId") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,mLatestUpdate,"mLatestUpdate") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,mLastQuery,"mLastQuery") ;
}
void RsGxsReputationBannedNodeSetItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process (j,ctx,mPgpId,"mPgpId") ;
RsTypeSerializer::serial_process<uint32_t> (j,ctx,mLastActivityTS,"mLastActivityTS") ;
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,mKnownIdentities,"mKnownIdentities") ;
}
void RsGxsReputationSetItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process (j,ctx,mGxsId,"mGxsId") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,mOwnOpinion,"mOwnOpinion") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,mOwnOpinionTS,"mOwnOpinionTS") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,mIdentityFlags,"mIdentityFlags") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,mLastUsedTS,"mLastUsedTS") ;
RsTypeSerializer::serial_process (j,ctx,mOwnerNodeId,"mOwnerNodeId") ;
RsTypeSerializer::serial_process (j,ctx,mOpinions,"mOpinions") ;
}
void RsGxsReputationUpdateItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint32_t>(j,ctx,mLatestUpdate,"mLatestUpdate") ;
RsTypeSerializer::serial_process (j,ctx,mOpinions,"mOpinions") ;
}
void RsGxsReputationRequestItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint32_t>(j,ctx,mLastUpdate,"mLastUpdate") ;
}
#ifdef TO_REMOVE
// re-defined here, in order to avoid cross-includes
#define REPUTATION_IDENTITY_FLAG_NEEDS_UPDATE 0x0100
/*************************************************************************/ /*************************************************************************/
/*************************************************************************/ /*************************************************************************/
/*************************************************************************/ /*************************************************************************/
@ -360,6 +373,7 @@ RsGxsReputationConfigItem *RsGxsReputationSerialiser::deserialiseReputationConfi
return item; return item;
} }
RsGxsReputationBannedNodeSetItem *RsGxsReputationSerialiser::deserialiseReputationBannedNodeSetItem(void *data,uint32_t size) RsGxsReputationBannedNodeSetItem *RsGxsReputationSerialiser::deserialiseReputationBannedNodeSetItem(void *data,uint32_t size)
{ {
uint32_t offset = 8; // skip the header uint32_t offset = 8; // skip the header
@ -382,6 +396,8 @@ RsGxsReputationBannedNodeSetItem *RsGxsReputationSerialiser::deserialiseReputati
return item; return item;
} }
RsGxsReputationSetItem *RsGxsReputationSerialiser::deserialiseReputationSetItem_deprecated(void *data,uint32_t tlvsize) RsGxsReputationSetItem *RsGxsReputationSerialiser::deserialiseReputationSetItem_deprecated(void *data,uint32_t tlvsize)
{ {
uint32_t offset = 8; // skip the header uint32_t offset = 8; // skip the header
@ -502,6 +518,7 @@ RsGxsReputationSetItem *RsGxsReputationSerialiser::deserialiseReputationSetItem(
return item; return item;
} }
RsGxsReputationUpdateItem *RsGxsReputationSerialiser::deserialiseReputationUpdateItem(void *data,uint32_t tlvsize) RsGxsReputationUpdateItem *RsGxsReputationSerialiser::deserialiseReputationUpdateItem(void *data,uint32_t tlvsize)
{ {
uint32_t offset = 8; // skip the header uint32_t offset = 8; // skip the header
@ -559,6 +576,7 @@ RsGxsReputationRequestItem *RsGxsReputationSerialiser::deserialiseReputationRequ
return item; return item;
} }
/*************************************************************************/ /*************************************************************************/
RsItem *RsGxsReputationSerialiser::deserialise(void *data, uint32_t *pktsize) RsItem *RsGxsReputationSerialiser::deserialise(void *data, uint32_t *pktsize)
@ -587,8 +605,25 @@ RsItem *RsGxsReputationSerialiser::deserialise(void *data, uint32_t *pktsize)
break; break;
} }
} }
#endif
/*************************************************************************/ /*************************************************************************/
RsItem *RsGxsReputationSerialiser::create_item(uint16_t service,uint8_t subtype) const
{
if(service != RS_SERVICE_GXS_TYPE_REPUTATION)
return NULL ;
switch(subtype)
{
case RS_PKT_SUBTYPE_GXS_REPUTATION_SET_ITEM : return new RsGxsReputationSetItem() ;
case RS_PKT_SUBTYPE_GXS_REPUTATION_BANNED_NODE_SET_ITEM : return new RsGxsReputationBannedNodeSetItem();
case RS_PKT_SUBTYPE_GXS_REPUTATION_UPDATE_ITEM : return new RsGxsReputationUpdateItem();
case RS_PKT_SUBTYPE_GXS_REPUTATION_REQUEST_ITEM : return new RsGxsReputationRequestItem() ;
case RS_PKT_SUBTYPE_GXS_REPUTATION_CONFIG_ITEM : return new RsGxsReputationConfigItem () ;
default:
std::cerr << "(EE) RsGxsReputationSerialiser::create_item(): unhandled item type " << subtype << std::endl;
return NULL ;
}
}

View File

@ -34,6 +34,8 @@
#include "retroshare/rsgxsifacetypes.h" #include "retroshare/rsgxsifacetypes.h"
#include "retroshare/rsreputations.h" #include "retroshare/rsreputations.h"
#include "serialization/rsserializer.h"
#define RS_PKT_SUBTYPE_GXS_REPUTATION_CONFIG_ITEM 0x01 #define RS_PKT_SUBTYPE_GXS_REPUTATION_CONFIG_ITEM 0x01
#define RS_PKT_SUBTYPE_GXS_REPUTATION_SET_ITEM_deprecated2 0x02 #define RS_PKT_SUBTYPE_GXS_REPUTATION_SET_ITEM_deprecated2 0x02
#define RS_PKT_SUBTYPE_GXS_REPUTATION_UPDATE_ITEM 0x03 #define RS_PKT_SUBTYPE_GXS_REPUTATION_UPDATE_ITEM 0x03
@ -53,15 +55,7 @@ class RsReputationItem: public RsItem
} }
virtual ~RsReputationItem() {} virtual ~RsReputationItem() {}
virtual bool serialise(void *data,uint32_t& size) const = 0 ;
virtual uint32_t serial_size() const = 0 ;
virtual void clear() = 0 ; virtual void clear() = 0 ;
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0;
protected:
bool serialise_header(void *data, uint32_t& pktsize, uint32_t& tlvsize, uint32_t& offset) const;
}; };
class RsGxsReputationConfigItem: public RsReputationItem class RsGxsReputationConfigItem: public RsReputationItem
@ -71,16 +65,15 @@ public:
virtual ~RsGxsReputationConfigItem() {} virtual ~RsGxsReputationConfigItem() {}
virtual void clear() {} virtual void clear() {}
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) const ; virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */) ;
virtual uint32_t serial_size() const ;
RsPeerId mPeerId; RsPeerId mPeerId;
uint32_t mLatestUpdate; // timestamp they returned. uint32_t mLatestUpdate; // timestamp they returned.
uint32_t mLastQuery; // when we sent out. uint32_t mLastQuery; // when we sent out.
}; };
#ifdef TO_REMOVE
// This class should disappear. Deprecated since Jan 7, 2017. The class definition is actually not needed, // This class should disappear. Deprecated since Jan 7, 2017. The class definition is actually not needed,
// that is why it's commented out. Kept here in order to explains how the deserialisation works. // that is why it's commented out. Kept here in order to explains how the deserialisation works.
// //
@ -91,10 +84,8 @@ public:
virtual ~RsGxsReputationSetItem_deprecated3() {} virtual ~RsGxsReputationSetItem_deprecated3() {}
virtual void clear() {} virtual void clear() {}
std::ostream &print(std::ostream &out, uint16_t /*indent*/ = 0) { return out;}
virtual bool serialise(void */*data*/,uint32_t& /*size*/) const { return false ;} virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */) ;
virtual uint32_t serial_size() const { return 0;}
RsGxsId mGxsId; RsGxsId mGxsId;
uint32_t mOwnOpinion; uint32_t mOwnOpinion;
@ -103,6 +94,8 @@ public:
RsPgpId mOwnerNodeId; RsPgpId mOwnerNodeId;
std::map<RsPeerId, uint32_t> mOpinions; // RsPeerId -> Opinion. std::map<RsPeerId, uint32_t> mOpinions; // RsPeerId -> Opinion.
}; };
#endif
class RsGxsReputationSetItem: public RsReputationItem class RsGxsReputationSetItem: public RsReputationItem
{ {
public: public:
@ -116,10 +109,8 @@ public:
virtual ~RsGxsReputationSetItem() {} virtual ~RsGxsReputationSetItem() {}
virtual void clear(); virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) const ; virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */) ;
virtual uint32_t serial_size() const ;
RsGxsId mGxsId; RsGxsId mGxsId;
uint32_t mOwnOpinion; uint32_t mOwnOpinion;
@ -136,10 +127,8 @@ public:
virtual ~RsGxsReputationBannedNodeSetItem() {} virtual ~RsGxsReputationBannedNodeSetItem() {}
virtual void clear(); virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) const ; virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */) ;
virtual uint32_t serial_size() const ;
RsPgpId mPgpId ; RsPgpId mPgpId ;
uint32_t mLastActivityTS ; uint32_t mLastActivityTS ;
@ -153,12 +142,10 @@ public:
virtual ~RsGxsReputationUpdateItem() {} virtual ~RsGxsReputationUpdateItem() {}
virtual void clear(); virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) const ; virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */) ;
virtual uint32_t serial_size() const ;
uint32_t mLatestUpdate; uint32_t mLatestUpdate;
std::map<RsGxsId, uint32_t> mOpinions; // GxsId -> Opinion. std::map<RsGxsId, uint32_t> mOpinions; // GxsId -> Opinion.
}; };
@ -169,40 +156,20 @@ public:
virtual ~RsGxsReputationRequestItem() {} virtual ~RsGxsReputationRequestItem() {}
virtual void clear() {} virtual void clear() {}
std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) const ; virtual void serial_process(SerializeJob /* j */,SerializeContext& /* ctx */) ;
virtual uint32_t serial_size() const ;
uint32_t mLastUpdate; uint32_t mLastUpdate;
}; };
class RsGxsReputationSerialiser: public RsSerialType class RsGxsReputationSerialiser: public RsSerializer
{ {
public: public:
RsGxsReputationSerialiser() :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_GXS_TYPE_REPUTATION){} RsGxsReputationSerialiser() :RsSerializer(RS_SERVICE_GXS_TYPE_REPUTATION){}
virtual ~RsGxsReputationSerialiser(){} virtual ~RsGxsReputationSerialiser(){}
virtual uint32_t size(RsItem *item) virtual RsItem *create_item(uint16_t service,uint8_t item_type) const;
{
return dynamic_cast<RsReputationItem*>(item)->serial_size() ;
}
virtual bool serialise (RsItem *item, void *data, uint32_t *size)
{
return dynamic_cast<RsReputationItem*>(item)->serialise(data,*size) ;
}
virtual RsItem * deserialise(void *data, uint32_t *size);
private:
static RsGxsReputationConfigItem *deserialiseReputationConfigItem (void *data, uint32_t size);
static RsGxsReputationSetItem *deserialiseReputationSetItem (void *data, uint32_t size);
static RsGxsReputationSetItem *deserialiseReputationSetItem_deprecated (void *data, uint32_t size);
static RsGxsReputationSetItem_deprecated3 *deserialiseReputationSetItem_deprecated3 (void *data, uint32_t size);
static RsGxsReputationUpdateItem *deserialiseReputationUpdateItem (void *data, uint32_t size);
static RsGxsReputationRequestItem *deserialiseReputationRequestItem (void *data, uint32_t size);
static RsGxsReputationBannedNodeSetItem *deserialiseReputationBannedNodeSetItem (void *data, uint32_t size);
}; };
/**************************************************************************/ /**************************************************************************/

View File

@ -85,6 +85,77 @@ class RsTypeSerializer
throw std::runtime_error("Unknown serial job") ; throw std::runtime_error("Unknown serial job") ;
} }
} }
//=================================================================================================//
// std::map<T,U> //
//=================================================================================================//
template<typename T,typename U>
static void serial_process(RsItem::SerializeJob j,SerializeContext& ctx,std::map<T,U>& v,const std::string& member_name)
{
switch(j)
{
case RsItem::SIZE_ESTIMATE:
{
ctx.mOffset += 4 ;
for(typename std::map<T,U>::iterator it(v.begin());it!=v.end();++it)
{
ctx.mOffset += serial_size(it->first) ;
ctx.mOffset += serial_size(it->second) ;
}
}
break ;
case RsItem::DESERIALIZE:
{
uint32_t n=0 ;
ctx.mOk = ctx.mOk && deserialize<uint32_t>(ctx.mData,ctx.mSize,ctx.mOffset,n) ;
for(uint32_t i=0;i<n;++i)
{
T t ;
U u ;
ctx.mOk = ctx.mOk && deserialize(ctx.mData,ctx.mSize,ctx.mOffset,t) ;
ctx.mOk = ctx.mOk && deserialize(ctx.mData,ctx.mSize,ctx.mOffset,u) ;
v[t] = u ;
}
}
break ;
case RsItem::SERIALIZE:
{
uint32_t n=v.size();
ctx.mOk = ctx.mOk && serialize<uint32_t>(ctx.mData,ctx.mSize,ctx.mOffset,n) ;
for(typename std::map<T,U>::iterator it(v.begin());it!=v.end();++it)
{
ctx.mOk = ctx.mOk && serialize(ctx.mData,ctx.mSize,ctx.mOffset,it->first) ;
ctx.mOk = ctx.mOk && serialize(ctx.mData,ctx.mSize,ctx.mOffset,it->second) ;
}
}
break ;
case RsItem::PRINT:
{
if(v.empty())
std::cerr << " Empty map"<< std::endl;
else
std::cerr << " std::map of " << v.size() << " elements:" << std::endl;
for(typename std::map<T,U>::iterator it(v.begin());it!=v.end();++it)
{
std::cerr << " " ;
print_data("first",it->first) ;
print_data("second",it->second) ;
}
}
break;
default:
break;
}
}
//=================================================================================================// //=================================================================================================//
// std::vector<T> // // std::vector<T> //

View File

@ -1210,6 +1210,7 @@ bool p3GxsReputation::loadList(std::list<RsItem *>& loadList)
if (set) if (set)
loadReputationSet(set, peerSet); loadReputationSet(set, peerSet);
#ifdef TO_REMOVE
RsGxsReputationSetItem_deprecated3 *set2 = dynamic_cast<RsGxsReputationSetItem_deprecated3 *>(*it); RsGxsReputationSetItem_deprecated3 *set2 = dynamic_cast<RsGxsReputationSetItem_deprecated3 *>(*it);
if(set2) if(set2)
@ -1217,6 +1218,7 @@ bool p3GxsReputation::loadList(std::list<RsItem *>& loadList)
std::cerr << "(II) reading and converting old format ReputationSetItem." << std::endl; std::cerr << "(II) reading and converting old format ReputationSetItem." << std::endl;
loadReputationSet_deprecated3(set2, peerSet); loadReputationSet_deprecated3(set2, peerSet);
} }
#endif
RsGxsReputationBannedNodeSetItem *itm2 = dynamic_cast<RsGxsReputationBannedNodeSetItem*>(*it) ; RsGxsReputationBannedNodeSetItem *itm2 = dynamic_cast<RsGxsReputationBannedNodeSetItem*>(*it) ;
@ -1274,6 +1276,7 @@ bool p3GxsReputation::loadList(std::list<RsItem *>& loadList)
loadList.clear() ; loadList.clear() ;
return true; return true;
} }
#ifdef TO_REMOVE
bool p3GxsReputation::loadReputationSet_deprecated3(RsGxsReputationSetItem_deprecated3 *item, const std::set<RsPeerId> &peerSet) bool p3GxsReputation::loadReputationSet_deprecated3(RsGxsReputationSetItem_deprecated3 *item, const std::set<RsPeerId> &peerSet)
{ {
{ {
@ -1324,7 +1327,7 @@ bool p3GxsReputation::loadReputationSet_deprecated3(RsGxsReputationSetItem_depre
#endif #endif
return true; return true;
} }
#endif
bool p3GxsReputation::loadReputationSet(RsGxsReputationSetItem *item, const std::set<RsPeerId> &peerSet) bool p3GxsReputation::loadReputationSet(RsGxsReputationSetItem *item, const std::set<RsPeerId> &peerSet)
{ {

View File

@ -156,7 +156,9 @@ private:
// internal update of data. Takes care of cleaning empty boxes. // internal update of data. Takes care of cleaning empty boxes.
void locked_updateOpinion(const RsPeerId &from, const RsGxsId &about, RsReputations::Opinion op); void locked_updateOpinion(const RsPeerId &from, const RsGxsId &about, RsReputations::Opinion op);
bool loadReputationSet(RsGxsReputationSetItem *item, const std::set<RsPeerId> &peerSet); bool loadReputationSet(RsGxsReputationSetItem *item, const std::set<RsPeerId> &peerSet);
#ifdef TO_REMOVE
bool loadReputationSet_deprecated3(RsGxsReputationSetItem_deprecated3 *item, const std::set<RsPeerId> &peerSet); bool loadReputationSet_deprecated3(RsGxsReputationSetItem_deprecated3 *item, const std::set<RsPeerId> &peerSet);
#endif
int sendPackets(); int sendPackets();
void cleanup(); void cleanup();
void sendReputationRequests(); void sendReputationRequests();