Implementation of GxsId Opinion, and start of Reputation system.

- Removed old reputation system from p3idservice
 - Removed old Msg types as well (no longer needed)
 - Added real submitOpinion()
 - Modified serviceString to support Reputation System.
 - Added reputation information to both Cached Data, and Standard ID Data.
 - Added basics of new reputation system as alternative service. (not gxs).
 - Added Generic TLV types for maps.
 - Moved configuration files to their own directory .rs/config/
 - Finally, fixed up bits missed from the change to updateGroup!



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7131 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2014-02-19 11:08:37 +00:00
parent 9a4c696688
commit 1b2ed66814
33 changed files with 2046 additions and 916 deletions

View file

@ -107,3 +107,5 @@ const uint8_t QOS_PRIORITY_RS_DSDV_DATA = 2 ;
// GXS
//
const uint8_t QOS_PRIORITY_RS_GXS_NET = 3 ;
// GXS Reputation.
const uint8_t QOS_PRIORITY_RS_GXSREPUTATION_ITEM = 2;

View file

@ -35,13 +35,16 @@
uint32_t RsGxsIdSerialiser::size(RsItem *item)
{
RsGxsIdGroupItem* grp_item = NULL;
#if 0
RsGxsIdOpinionItem* op_item = NULL;
RsGxsIdCommentItem* com_item = NULL;
#endif
if((grp_item = dynamic_cast<RsGxsIdGroupItem*>(item)) != NULL)
{
return sizeGxsIdGroupItem(grp_item);
}
#if 0
else if((op_item = dynamic_cast<RsGxsIdOpinionItem*>(item)) != NULL)
{
return sizeGxsIdOpinionItem(op_item);
@ -50,6 +53,7 @@ uint32_t RsGxsIdSerialiser::size(RsItem *item)
{
return sizeGxsIdCommentItem(com_item);
}
#endif
std::cerr << "RsGxsIdSerialiser::size() ERROR invalid item" << std::endl;
return 0;
}
@ -57,13 +61,16 @@ uint32_t RsGxsIdSerialiser::size(RsItem *item)
bool RsGxsIdSerialiser::serialise(RsItem *item, void *data, uint32_t *size)
{
RsGxsIdGroupItem* grp_item = NULL;
#if 0
RsGxsIdOpinionItem* op_item = NULL;
RsGxsIdCommentItem* com_item = NULL;
#endif
if((grp_item = dynamic_cast<RsGxsIdGroupItem*>(item)) != NULL)
{
return serialiseGxsIdGroupItem(grp_item, data, size);
}
#if 0
else if((op_item = dynamic_cast<RsGxsIdOpinionItem*>(item)) != NULL)
{
return serialiseGxsIdOpinionItem(op_item, data, size);
@ -72,6 +79,7 @@ bool RsGxsIdSerialiser::serialise(RsItem *item, void *data, uint32_t *size)
{
return serialiseGxsIdCommentItem(com_item, data, size);
}
#endif
std::cerr << "RsGxsIdSerialiser::serialise() ERROR invalid item" << std::endl;
return false;
}
@ -94,12 +102,14 @@ RsItem* RsGxsIdSerialiser::deserialise(void* data, uint32_t* size)
case RS_PKT_SUBTYPE_GXSID_GROUP_ITEM:
return deserialiseGxsIdGroupItem(data, size);
break;
#if 0
case RS_PKT_SUBTYPE_GXSID_OPINION_ITEM:
return deserialiseGxsIdOpinionItem(data, size);
break;
case RS_PKT_SUBTYPE_GXSID_COMMENT_ITEM:
return deserialiseGxsIdCommentItem(data, size);
break;
#endif
default:
#ifdef GXSID_DEBUG
std::cerr << "RsGxsIdSerialiser::deserialise(): unknown subtype";
@ -287,6 +297,7 @@ RsGxsIdGroupItem* RsGxsIdSerialiser::deserialiseGxsIdGroupItem(void *data, uint3
/*****************************************************************************************/
/*****************************************************************************************/
#if 0
void RsGxsIdOpinionItem::clear()
{
@ -575,6 +586,7 @@ RsGxsIdCommentItem* RsGxsIdSerialiser::deserialiseGxsIdCommentItem(void *data, u
return item;
}
#endif
/*****************************************************************************************/
/*****************************************************************************************/

View file

@ -55,6 +55,7 @@ public:
RsGxsIdGroup group;
};
#if 0
class RsGxsIdOpinionItem : public RsGxsMsgItem
{
public:
@ -79,6 +80,7 @@ public:
RsGxsIdComment comment;
};
#endif
class RsGxsIdSerialiser : public RsSerialType
{
@ -99,6 +101,7 @@ public:
bool serialiseGxsIdGroupItem (RsGxsIdGroupItem *item, void *data, uint32_t *size);
RsGxsIdGroupItem * deserialiseGxsIdGroupItem(void *data, uint32_t *size);
#if 0
uint32_t sizeGxsIdOpinionItem(RsGxsIdOpinionItem *item);
bool serialiseGxsIdOpinionItem (RsGxsIdOpinionItem *item, void *data, uint32_t *size);
RsGxsIdOpinionItem * deserialiseGxsIdOpinionItem(void *data, uint32_t *size);
@ -106,6 +109,8 @@ public:
uint32_t sizeGxsIdCommentItem(RsGxsIdCommentItem *item);
bool serialiseGxsIdCommentItem (RsGxsIdCommentItem *item, void *data, uint32_t *size);
RsGxsIdCommentItem * deserialiseGxsIdCommentItem(void *data, uint32_t *size);
#endif
};

View file

@ -0,0 +1,203 @@
/*
* libretroshare/src/serialiser: rsbanlist.cc
*
* RetroShare Serialiser.
*
* Copyright 2011 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "serialiser/rsbaseserial.h"
#include "serialiser/rsbanlistitems.h"
#include "serialiser/rstlvbanlist.h"
/***
#define RSSERIAL_DEBUG 1
***/
#include <iostream>
/*************************************************************************/
RsBanListItem::~RsBanListItem()
{
return;
}
void RsBanListItem::clear()
{
peerList.TlvClear();
}
std::ostream &RsBanListItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsBanListItem", indent);
uint16_t int_Indent = indent + 2;
peerList.print(out, int_Indent);
printRsItemEnd(out, "RsBanListItem", indent);
return out;
}
uint32_t RsBanListSerialiser::sizeList(RsBanListItem *item)
{
uint32_t s = 8; /* header */
s += item->peerList.TlvSize();
return s;
}
/* serialise the data to the buffer */
bool RsBanListSerialiser::serialiseList(RsBanListItem *item, void *data, uint32_t *pktsize)
{
uint32_t tlvsize = sizeList(item);
uint32_t offset = 0;
if (*pktsize < tlvsize)
return false; /* not enough space */
*pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
#ifdef RSSERIAL_DEBUG
std::cerr << "RsDsdvSerialiser::serialiseRoute() Header: " << ok << std::endl;
std::cerr << "RsDsdvSerialiser::serialiseRoute() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= item->peerList.SetTlv(data, tlvsize, &offset);
if (offset != tlvsize)
{
ok = false;
#ifdef RSSERIAL_DEBUG
std::cerr << "RsDsdvSerialiser::serialiseRoute() Size Error! " << std::endl;
#endif
}
return ok;
}
RsBanListItem *RsBanListSerialiser::deserialiseList(void *data, uint32_t *pktsize)
{
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t tlvsize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_TYPE_BANLIST != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_BANLIST_ITEM != getRsItemSubType(rstype)))
{
return NULL; /* wrong type */
}
if (*pktsize < tlvsize) /* check size */
return NULL; /* not enough data */
/* set the packet length */
*pktsize = tlvsize;
bool ok = true;
/* ready to load */
RsBanListItem *item = new RsBanListItem();
item->clear();
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= item->peerList.GetTlv(data, tlvsize, &offset);
if (offset != tlvsize)
{
/* error */
delete item;
return NULL;
}
if (!ok)
{
delete item;
return NULL;
}
return item;
}
/*************************************************************************/
uint32_t RsBanListSerialiser::size(RsItem *i)
{
RsBanListItem *dri;
if (NULL != (dri = dynamic_cast<RsBanListItem *>(i)))
{
return sizeList(dri);
}
return 0;
}
bool RsBanListSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsize)
{
RsBanListItem *dri;
if (NULL != (dri = dynamic_cast<RsBanListItem *>(i)))
{
return serialiseList(dri, data, pktsize);
}
return false;
}
RsItem *RsBanListSerialiser::deserialise(void *data, uint32_t *pktsize)
{
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_TYPE_BANLIST != getRsItemService(rstype)))
{
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_BANLIST_ITEM:
return deserialiseList(data, pktsize);
break;
default:
return NULL;
break;
}
}
/*************************************************************************/

View file

@ -0,0 +1,163 @@
#ifndef RS_GXSREPUTATION_ITEMS_H
#define RS_GXSREPUTATION_ITEMS_H
/*
* libretroshare/src/serialiser: rsgxsreputationitems.h
*
* RetroShare Serialiser.
*
* Copyright 2014 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include <map>
#include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h"
#include "serialiser/rstlvtypes.h"
#define RS_PKT_SUBTYPE_GXSREPUTATION_CONFIG_ITEM 0x01
#define RS_PKT_SUBTYPE_GXSREPUTATION_SET_ITEM 0x02
#define RS_PKT_SUBTYPE_GXSREPUTATION_UPDATE_ITEM 0x03
#define RS_PKT_SUBTYPE_GXSREPUTATION_REQUEST_ITEM 0x04
/**************************************************************************/
class RsGxsReputationConfigItem: public RsItem
{
public:
RsGxsReputationConfigItem()
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_GXSV2_TYPE_REPUTATION,
RS_PKT_SUBTYPE_GXSREPUTATION_CONFIG_ITEM)
{
setPriorityLevel(QOS_PRIORITY_RS_GXSREPUTATION_ITEM);
return;
}
virtual ~RsGxsReputationConfigItem();
virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
std::string mPeerId;
uint32_t mLatestUpdate; // timestamp they returned.
uint32_t mLastQuery; // when we sent out.
};
class RsGxsReputationSetItem: public RsItem
{
public:
RsGxsReputationSetItem()
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_GXSV2_TYPE_REPUTATION,
RS_PKT_SUBTYPE_GXSREPUTATION_SET_ITEM)
{
setPriorityLevel(QOS_PRIORITY_RS_GXSREPUTATION_ITEM);
return;
}
virtual ~RsGxsReputationSetItem();
virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
std::string mGxsId;
uint32_t mOwnOpinion;
uint32_t mOwnOpinionTs;
uint32_t mReputation;
std::map<std::string, uint32_t> mOpinions; // RsPeerId -> Opinion.
};
class RsGxsReputationUpdateItem: public RsItem
{
public:
RsGxsReputationUpdateItem()
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_GXSV2_TYPE_REPUTATION,
RS_PKT_SUBTYPE_GXSREPUTATION_UPDATE_ITEM)
{
setPriorityLevel(QOS_PRIORITY_RS_GXSREPUTATION_ITEM);
return;
}
virtual ~RsGxsReputationUpdateItem();
virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
std::map<std::string, uint32_t> mOpinions; // GxsId -> Opinion.
uint32_t mLatestUpdate;
};
class RsGxsReputationRequestItem: public RsItem
{
public:
RsGxsReputationRequestItem()
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_GXSV2_TYPE_REPUTATION,
RS_PKT_SUBTYPE_GXSREPUTATION_REQUEST_ITEM)
{
setPriorityLevel(QOS_PRIORITY_RS_GXSREPUTATION_ITEM);
return;
}
virtual ~RsGxsReputationRequestItem();
virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
uint32_t mLastUpdate;
};
class RsGxsReputationSerialiser: public RsSerialType
{
public:
RsGxsReputationSerialiser()
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_GXSV2_TYPE_REPUTATION)
{ return; }
virtual ~RsGxsReputationSerialiser()
{ return; }
virtual uint32_t size(RsItem *);
virtual bool serialise (RsItem *item, void *data, uint32_t *size);
virtual RsItem * deserialise(void *data, uint32_t *size);
private:
virtual uint32_t sizeConfig(RsGxsReputationConfigItem *);
virtual bool serialiseConfig(RsGxsReputationConfigItem *item, void *data, uint32_t *size);
virtual RsGxsReputationConfigItem *deserialiseConfig(void *data, uint32_t *size);
virtual uint32_t sizeSet(RsGxsReputationSetItem *);
virtual bool serialiseSet(RsGxsReputationSetItem *item, void *data, uint32_t *size);
virtual RsGxsReputationSetItem *deserialiseSet(void *data, uint32_t *size);
virtual uint32_t sizeUpdate(RsGxsReputationUpdateItem *);
virtual bool serialiseUpdate(RsGxsReputationUpdateItem *item, void *data, uint32_t *size);
virtual RsGxsReputationUpdateItem *deserialiseUpdate(void *data, uint32_t *size);
virtual uint32_t sizeRequest(RsGxsReputationRequestItem *);
virtual bool serialiseRequest(RsGxsReputationRequestItem *item, void *data, uint32_t *size);
virtual RsGxsReputationRequestItem *deserialiseRequest(void *data, uint32_t *size);
};
/**************************************************************************/
#endif /* RS_GXSREPUTATION_ITEMS_H */

View file

@ -144,6 +144,7 @@ const uint16_t RS_SERVICE_GXSV2_TYPE_WIRE = 0xf315;
const uint16_t RS_SERVICE_GXSV2_TYPE_FORUMS = 0xf316;
const uint16_t RS_SERVICE_GXSV2_TYPE_POSTED = 0xf317;
const uint16_t RS_SERVICE_GXSV2_TYPE_CHANNELS = 0xf318;
const uint16_t RS_SERVICE_GXSV2_TYPE_REPUTATION = 0xf319;
const uint16_t RS_SERVICE_GXSV3_TYPE_GXSID = 0xf321;
const uint16_t RS_SERVICE_GXSV3_TYPE_PHOTO = 0xf322;
@ -153,6 +154,7 @@ const uint16_t RS_SERVICE_GXSV3_TYPE_FORUMS = 0xf325;
const uint16_t RS_SERVICE_GXSV3_TYPE_POSTED = 0xf326;
const uint16_t RS_SERVICE_GXSV3_TYPE_CHANNELS = 0xf327;
const uint16_t RS_SERVICE_GXSV3_TYPE_GXSCIRCLE = 0xf328;
const uint16_t RS_SERVICE_GXSV3_TYPE_REPUTATION = 0xf329;
const uint16_t RS_SERVICE_TYPE_GXS_RECOGN = 0xf331;

View file

@ -116,6 +116,7 @@ const uint16_t TLV_TYPE_UINT32_AGE = 0x0032;
const uint16_t TLV_TYPE_UINT32_OFFSET = 0x0033;
const uint16_t TLV_TYPE_UINT32_SERID = 0x0034;
const uint16_t TLV_TYPE_UINT32_BW = 0x0035;
const uint16_t TLV_TYPE_UINT32_PARAM = 0x0030;
const uint16_t TLV_TYPE_UINT64_SIZE = 0x0040;
const uint16_t TLV_TYPE_UINT64_OFFSET = 0x0041;
@ -138,6 +139,7 @@ const uint16_t TLV_TYPE_STR_CERT_GPG = 0x005d;
const uint16_t TLV_TYPE_STR_PGPCERT = 0x005d; /* same as CERT_GPG */
const uint16_t TLV_TYPE_STR_CERT_SSL = 0x005e;
const uint16_t TLV_TYPE_STR_VERSION = 0x005f;
const uint16_t TLV_TYPE_STR_PARAM = 0x0054; /* same as VALUE ---- TO FIX */

View file

@ -0,0 +1,375 @@
/*
* libretroshare/src/serialiser: rstlvgenericmap.cc
*
* RetroShare Serialiser.
*
* Copyright 2014 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "rstlvbase.h"
#include "rstlvtypes.h"
#include "rstlvgenericmap.h"
#include "rsbaseserial.h"
#include "util/rsprint.h"
#include <ostream>
#include <sstream>
#include <iomanip>
#include <iostream>
#define TLV_DEBUG 1
template<>
uint32_t RsTlvParamRef<uint32_t>::TlvSize()
{
return GetTlvUInt32Size();
}
template<>
void RsTlvParamRef<uint32_t>::TlvClear()
{
mParam = 0;
}
template<>
bool RsTlvParamRef<uint32_t>::SetTlv(void *data, uint32_t size, uint32_t *offset)
{
return SetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_PARAM, mParam);
}
template<>
bool RsTlvParamRef<uint32_t>::GetTlv(void *data, uint32_t size, uint32_t *offset)
{
return GetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_PARAM, &mParam);
}
template<>
std::ostream & RsTlvParamRef<uint32_t>::print(std::ostream &out, uint16_t indent)
{
printIndent(out, indent);
out << mParam;
return out;
}
/***** std::string ****/
template<>
uint32_t RsTlvParamRef<std::string>::TlvSize()
{
return GetTlvStringSize(mParam);
}
template<>
void RsTlvParamRef<std::string>::TlvClear()
{
mParam.clear();
}
template<>
bool RsTlvParamRef<std::string>::SetTlv(void *data, uint32_t size, uint32_t *offset)
{
return SetTlvString(data, size, offset, TLV_TYPE_STR_PARAM, mParam);
}
template<>
bool RsTlvParamRef<std::string>::GetTlv(void *data, uint32_t size, uint32_t *offset)
{
return GetTlvString(data, size, offset, TLV_TYPE_STR_PARAM, mParam);
}
template<>
std::ostream & RsTlvParamRef<std::string>::print(std::ostream &out, uint16_t indent)
{
printIndent(out, indent);
out << mParam;
return out;
}
// declare likely combinations.
template class RsTlvParamRef<uint32_t>;
template class RsTlvParamRef<std::string>;
/*********************************** RsTlvGenericPairRef ***********************************/
template<class K, class V>
void RsTlvGenericPairRef<K, V>::TlvClear()
{
RsTlvParamRef<K> key(mKey);
RsTlvParamRef<V> value(mValue);
key.TlvClear();
value.TlvClear();
}
template<class K, class V>
uint32_t RsTlvGenericPairRef<K, V>::TlvSize()
{
uint32_t s = TLV_HEADER_SIZE; /* header */
RsTlvParamRef<K> key(mKey);
RsTlvParamRef<V> value(mValue);
s += key.TlvSize();
s += value.TlvSize();
return s;
}
template<class K, class V>
bool RsTlvGenericPairRef<K, V>::SetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */
{
/* must check sizes */
uint32_t tlvsize = TlvSize();
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend)
return false; /* not enough space */
bool ok = true;
/* start at data[offset] */
ok &= SetTlvBase(data, tlvend, offset, mPairType, tlvsize);
RsTlvParamRef<K> key(mKey);
RsTlvParamRef<V> value(mValue);
ok &= key.SetTlv(data, tlvend, offset);
ok &= value.SetTlv(data, tlvend, offset);
return ok;
}
template<class K, class V>
bool RsTlvGenericPairRef<K, V>::GetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */
{
if (size < *offset + TLV_HEADER_SIZE)
return false;
uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
uint32_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) );
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend) /* check size */
return false; /* not enough space */
if (tlvtype != mPairType) /* check type */
return false;
bool ok = true;
/* ready to load */
TlvClear();
RsTlvParamRef<K> key(mKey);
RsTlvParamRef<V> value(mValue);
ok &= key.GetTlv(data, tlvend, offset);
ok &= value.GetTlv(data, tlvend, offset);
/***************************************************************************
* NB: extra components could be added (for future expansion of the type).
* or be present (if this code is reading an extended version).
*
* We must chew up the extra characters to conform with TLV specifications
***************************************************************************/
if (*offset != tlvend)
{
#ifdef TLV_DEBUG
std::cerr << "RsTlvGenericPairRef::GetTlv() Warning extra bytes at end of item";
std::cerr << std::endl;
#endif
*offset = tlvend;
}
return ok;
}
template<class K, class V>
std::ostream &RsTlvGenericPairRef<K, V>::print(std::ostream &out, uint16_t indent)
{
printBase(out, "RsTlvGenericPairRef", indent);
uint16_t int_Indent = indent + 2;
RsTlvParamRef<K> key(mKey);
RsTlvParamRef<V> value(mValue);
printIndent(out, int_Indent);
out << "Key:";
key.print(out, 0);
out << std::endl;
printIndent(out, int_Indent);
out << "Value:";
value.print(out, 0);
out << std::endl;
printEnd(out, "RsTlvGenericPairRef", indent);
return out;
}
// declare likely combinations.
//template<> class RsTlvGenericPairRef<uint32_t, uint32_t>;
//template<> class RsTlvGenericPairRef<uint32_t, std::string>;
//template<> class RsTlvGenericPairRef<std::string, uint32_t>;
//template<> class RsTlvGenericPairRef<std::string, std::string>;
/************************************ RsTlvGenericMapRef ***********************************/
template<class K, class V>
void RsTlvGenericMapRef<K, V>::TlvClear()
{
mRefMap.clear(); //empty list
}
template<class K, class V>
uint32_t RsTlvGenericMapRef<K, V>::TlvSize()
{
uint32_t s = TLV_HEADER_SIZE; /* header */
typename std::map<K, V>::iterator it;
for(it = mRefMap.begin(); it != mRefMap.end(); ++it)
{
RsTlvGenericPairRef<const K, V> pair(mPairType, it->first, it->second);
s += pair.TlvSize();
}
return s;
}
template<class K, class V>
bool RsTlvGenericMapRef<K, V>::SetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */
{
/* must check sizes */
uint32_t tlvsize = TlvSize();
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend)
return false; /* not enough space */
bool ok = true;
/* start at data[offset] */
ok &= SetTlvBase(data, tlvend, offset, mMapType, tlvsize);
typename std::map<K, V>::iterator it;
for(it = mRefMap.begin(); it != mRefMap.end(); ++it)
{
RsTlvGenericPairRef<const K, V> pair(mPairType, it->first, it->second);
ok &= pair.SetTlv(data, size, offset);
}
return ok;
}
template<class K, class V>
bool RsTlvGenericMapRef<K, V>::GetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */
{
if (size < *offset + TLV_HEADER_SIZE)
return false;
uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
uint32_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) );
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend) /* check size */
return false; /* not enough space */
if (tlvtype != mMapType) /* check type */
return false;
bool ok = true;
/* ready to load */
TlvClear();
/* skip the header */
(*offset) += TLV_HEADER_SIZE;
/* while there is TLV */
while((*offset) + 2 < tlvend)
{
/* get the next type */
uint16_t tlvsubtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
if (tlvsubtype == mPairType)
{
K k;
V v;
RsTlvGenericPairRef<const K, V> pair(mPairType, k, v);
ok &= pair.GetTlv(data, size, offset);
if (ok)
{
mRefMap[k] = v;
}
}
else
{
ok &= SkipUnknownTlv(data, tlvend, offset);
ok = false;
}
if (!ok)
break;
}
/***************************************************************************
* NB: extra components could be added (for future expansion of the type).
* or be present (if this code is reading an extended version).
*
* We must chew up the extra characters to conform with TLV specifications
***************************************************************************/
if (*offset != tlvend)
{
#ifdef TLV_DEBUG
std::cerr << "RsTlvGenericMapRef::GetTlv() Warning extra bytes at end of item";
std::cerr << std::endl;
#endif
*offset = tlvend;
}
return ok;
}
template<class K, class V>
std::ostream &RsTlvGenericMapRef<K, V>::print(std::ostream &out, uint16_t indent)
{
printBase(out, "RsTlvGenericMapRef", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "MapType:" << mMapType << std::endl;
typename std::map<K, V>::iterator it;
for(it = mRefMap.begin(); it != mRefMap.end() ; ++it)
{
RsTlvGenericPairRef<const K, V> pair(mPairType, it->first, it->second);
pair.print(out, int_Indent);
}
printEnd(out, "RsTlvGenericMapRef", indent);
return out;
}
// declare likely combinations.
template class RsTlvGenericMapRef<uint32_t, uint32_t>;
template class RsTlvGenericMapRef<uint32_t, std::string>;
template class RsTlvGenericMapRef<std::string, uint32_t>;
template class RsTlvGenericMapRef<std::string, std::string>;

View file

@ -0,0 +1,100 @@
#ifndef RS_TLV_GENERIC_MAP_H
#define RS_TLV_GENERIC_MAP_H
/*
* libretroshare/src/serialiser: rstlvgenericmap.h
*
* RetroShare Serialiser.
*
* Copyright 2014 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "serialiser/rstlvtypes.h"
#include <map>
#include <stdlib.h>
#include <stdint.h>
/**** TLV *****
* Generic Parameters / Maps.
*/
template<class T>
class RsTlvParamRef: public RsTlvItem
{
public:
RsTlvParamRef(T &p): mParam(p) {}
virtual ~RsTlvParamRef() { return; }
virtual uint32_t TlvSize();
virtual void TlvClear();
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */
virtual std::ostream &print(std::ostream &out, uint16_t indent);
T &mParam;
};
/*********************************** RsTlvGenericPairRef ***********************************/
template<class K, class V>
class RsTlvGenericPairRef: public RsTlvItem
{
public:
RsTlvGenericPairRef(uint16_t pair_type, K &k, V &v)
:mPairType(pair_type), mKey(k), mValue(v) { return; }
virtual ~RsTlvGenericPairRef() { return; }
virtual uint32_t TlvSize();
virtual void TlvClear();
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */
virtual std::ostream &print(std::ostream &out, uint16_t indent);
uint16_t mPairType;
K &mKey;
V &mValue;
};
/************************************ RsTlvGenericMapRef ***********************************/
template<class K, class V>
class RsTlvGenericMapRef: public RsTlvItem
{
public:
RsTlvGenericMapRef(uint16_t map_type, uint16_t pair_type, std::map<K, V> &refmap)
:mMapType(map_type), mPairType(pair_type), mRefMap(refmap) { return; }
virtual ~RsTlvGenericMapRef() { return; }
virtual uint32_t TlvSize();
virtual void TlvClear();
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */
virtual std::ostream &print(std::ostream &out, uint16_t indent);
uint16_t mMapType;
uint16_t mPairType;
std::map<K, V> &mRefMap;
};
#endif

View file

@ -0,0 +1,41 @@
#ifndef RS_TLV_MAPS_H
#define RS_TLV_MAPS_H
/*
* libretroshare/src/serialiser: rstlvmaps.h
*
* RetroShare Serialiser.
*
* Copyright 2014 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "serialiser/rstlvgenericmaps.h"
class RsTlvOpinionMapRef: public RsTlvGenericMapRef<std::string, uint32_t>
{
public:
RsTlvOpinionMapRef(std::map<std::string, uint32_t> &refmap)
:RsTlvGenericMapRef(OPINION, STRING_INT_PAIR, refmap) { return; }
};
#endif

View file

@ -264,29 +264,6 @@ virtual std::ostream &print(std::ostream &out, uint16_t indent);
};
class RsTlvIntStringMap: public RsTlvItem
{
public:
RsTlvIntStringMap() { return; }
virtual ~RsTlvIntStringMap() { return; }
virtual uint32_t TlvSize();
virtual void TlvClear();
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */
virtual std::ostream &print(std::ostream &out, uint16_t indent);
uint32_t mapType;
std::map<uint32_t, std::string> map;
};
class RsTlvServiceIdMap: public RsTlvIntStringMap
{
public:
RsTlvServiceIdMap();
};
class RsTlvImage: public RsTlvItem
{
public: