mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
added revised qblog core
* added new tlv type rstlvwide key value pair for internationalisation * profile interface method not fully implemented yet * blog methods implemented * added extra constructor to facilitate extensibility (rename packet type) git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@625 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
993a5e901d
commit
93d785b8ff
@ -12,6 +12,7 @@ RSOBJ += rstlvfileitem.o # TLV Objs
|
||||
RSOBJ += rstlvkeys.o # TLV Objs
|
||||
RSOBJ += rstlvimage.o # TLV Objs
|
||||
RSOBJ += rstlvutil.o # TLV Objs
|
||||
RSOBJ += rstlvkvwide.o # TLV Objs
|
||||
RSOBJ += rsbaseitems.o rsconfigitems.o rsdiscitems.o # RsItems
|
||||
RSOBJ += rsmsgitems.o rsrankitems.o # RsItems
|
||||
RSOBJ += rsphotoitems.o # RsItems
|
||||
@ -24,13 +25,13 @@ RSOBJ += rsstatusitems.o # RsItems
|
||||
|
||||
TESTOBJ = tlvbase_test.o tlvbase_test2.o tlvfileitem_test.o
|
||||
TESTOBJ += tlvitems_test.o tlvstack_test.o tlvconfig_test.o
|
||||
TESTOBJ += rsserial_test.o
|
||||
TESTOBJ += rsserial_test.o rstlvwidetest.o
|
||||
|
||||
#rsbaseitem_test.o
|
||||
|
||||
TESTS = tlvbase_test tlvbase_test2 tlvfileitem_test
|
||||
TESTS += tlvitems_test tlvstack_test tlvconfig_test
|
||||
TESTS +=
|
||||
TESTS += rstlvwidetest
|
||||
#rsserial_test
|
||||
|
||||
#rsbaseitem_test
|
||||
@ -61,6 +62,9 @@ rsserial_test : rsserial_test.o
|
||||
rsbaseitem_test : rsbaseitem_test.o
|
||||
$(CC) $(CFLAGS) -o rsbaseitem_test rsbaseitem_test.o $(OBJ) $(LIBS)
|
||||
|
||||
rstlvwidetest : rstlvwidetest.o
|
||||
$(CC) $(CFLAGS) -o rstlvwidetest rstlvwidetest.o $(OBJ) $(LIBS)
|
||||
|
||||
|
||||
###############################################################
|
||||
include $(RS_TOP_DIR)/scripts/rules.mk
|
||||
|
@ -94,6 +94,12 @@ class RsMsgItem: public RsItem
|
||||
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_MSG,
|
||||
RS_PKT_SUBTYPE_DEFAULT)
|
||||
{ return; }
|
||||
|
||||
RsMsgItem(uint16_t type)
|
||||
:RsItem(RS_PKT_VERSION_SERVICE, type,
|
||||
RS_PKT_SUBTYPE_DEFAULT)
|
||||
{ return; }
|
||||
|
||||
virtual ~RsMsgItem();
|
||||
virtual void clear();
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
@ -120,6 +126,11 @@ class RsMsgSerialiser: public RsSerialType
|
||||
RsMsgSerialiser()
|
||||
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_MSG)
|
||||
{ return; }
|
||||
|
||||
RsMsgSerialiser(uint16_t type)
|
||||
:RsSerialType(RS_PKT_VERSION_SERVICE, type)
|
||||
{ return; }
|
||||
|
||||
virtual ~RsMsgSerialiser() { return; }
|
||||
|
||||
virtual uint32_t size(RsItem *);
|
||||
|
@ -38,12 +38,6 @@ RsQblogMsg::~RsQblogMsg(void)
|
||||
return;
|
||||
}
|
||||
|
||||
void RsQblogMsg::clear()
|
||||
{
|
||||
timeStamp = 0;
|
||||
blogMsg.clear();
|
||||
}
|
||||
|
||||
|
||||
std::ostream &RsQblogMsg::print(std::ostream &out, uint16_t indent)
|
||||
{
|
||||
@ -52,135 +46,19 @@ std::ostream &RsQblogMsg::print(std::ostream &out, uint16_t indent)
|
||||
|
||||
/* print out the content of the item */
|
||||
printIndent(out, int_Indent);
|
||||
out << "blogMsg(time): " << timeStamp << std::endl;
|
||||
out << "blogMsg(send time): " << sendTime << std::endl;
|
||||
printIndent(out, int_Indent);
|
||||
std::string cnv_blog(blogMsg.begin(), blogMsg.end());
|
||||
out << "blogMsg(recvd time): " << recvTime << std::endl;
|
||||
printIndent(out, int_Indent);
|
||||
std::string cnv_blog(message.begin(), message.end());
|
||||
out << "blogMsg(message): " << cnv_blog << std::endl;
|
||||
printIndent(out, int_Indent);
|
||||
attachment.print(out, int_Indent);
|
||||
printIndent(out, int_Indent);
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint32_t RsQblogMsgSerialiser::sizeItem(RsQblogMsg *item)
|
||||
{
|
||||
uint32_t s = 8; // for header size
|
||||
s += 4; // blog creation time
|
||||
s += GetTlvWideStringSize(item->blogMsg); // size of actual blog
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
/*******************************************************************************/
|
||||
|
||||
bool RsQblogMsgSerialiser::serialiseItem(RsQblogMsg* item, void* data, uint32_t *size)
|
||||
{
|
||||
uint32_t tlvsize = sizeItem(item);
|
||||
uint32_t offset = 0;
|
||||
|
||||
if (*size < tlvsize)
|
||||
return false; /* not enough space */
|
||||
|
||||
*size = tlvsize;
|
||||
|
||||
bool ok = true;
|
||||
|
||||
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
|
||||
|
||||
#ifdef RSSERIAL_DEBUG
|
||||
std::cerr << "RsQblogSerialiser::serialiseItem() Header: " << ok << std::endl;
|
||||
std::cerr << "RsQblogSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
|
||||
#endif
|
||||
/* skip the header */
|
||||
offset += 8;
|
||||
|
||||
/* add mandatory parts first */
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, item->timeStamp);
|
||||
ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_STR_MSG, item->blogMsg);
|
||||
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
ok = false;
|
||||
#ifdef RSSERIAL_DEBUG
|
||||
std::cerr << "RsQblogSerialiser::serialiseItem() Size Error! " << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
RsQblogMsg* RsQblogMsgSerialiser::deserialiseItem(void * data, uint32_t *size)
|
||||
{
|
||||
|
||||
/* 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_TYPE_QBLOG != getRsItemService(rstype)) ||
|
||||
(RS_PKT_SUBTYPE_DEFAULT != getRsItemSubType(rstype)))
|
||||
{
|
||||
return NULL; /* wrong type */
|
||||
}
|
||||
|
||||
if (*size < rssize) /* check size */
|
||||
return NULL; /* not enough data */
|
||||
|
||||
/* set the packet length */
|
||||
*size = rssize;
|
||||
|
||||
bool ok = true;
|
||||
|
||||
/* ready to load */
|
||||
RsQblogMsg *item = new RsQblogMsg();
|
||||
item->clear();
|
||||
|
||||
/* skip the header */
|
||||
offset += 8;
|
||||
|
||||
/* get mandatory parts first */
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->timeStamp));
|
||||
ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_STR_MSG, item->blogMsg);
|
||||
|
||||
if (offset != rssize)
|
||||
{
|
||||
/* error */
|
||||
delete item;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
delete item;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/*********************************************************************/
|
||||
|
||||
bool RsQblogMsgSerialiser::serialise(RsItem *item, void* data, uint32_t* size)
|
||||
{
|
||||
return serialiseItem((RsQblogMsg *) item, data, size);
|
||||
}
|
||||
|
||||
RsItem* RsQblogMsgSerialiser::deserialise(void* data, uint32_t* size)
|
||||
{
|
||||
return deserialiseItem(data, size);
|
||||
}
|
||||
|
||||
uint32_t RsQblogMsgSerialiser::size(RsItem *item)
|
||||
{
|
||||
return sizeItem((RsQblogMsg *) item);
|
||||
}
|
||||
|
||||
/********************************************* RsQblogProfile section ***********************************/
|
||||
/********************************************* RsQblogProfile section ***********************************/
|
||||
/********************************************* RsQblogProfile section ***********************************/
|
||||
@ -194,7 +72,6 @@ void RsQblogProfile::clear()
|
||||
{
|
||||
timeStamp = 0;
|
||||
openProfile.TlvClear();
|
||||
favoriteFiles.TlvClear();
|
||||
}
|
||||
|
||||
|
||||
@ -205,8 +82,6 @@ std::ostream &RsQblogProfile::print(std::ostream &out, uint16_t indent)
|
||||
out << "RsQblogProfile::print() : timeStamp" << timeStamp;
|
||||
out << std::endl;
|
||||
openProfile.print(out, int_Indent);
|
||||
favoriteFiles.print(out, int_Indent);
|
||||
favoriteFiles.print(out, int_Indent);
|
||||
printRsItemEnd(out, "RsQblogProfile", indent);
|
||||
return out;
|
||||
}
|
||||
@ -218,7 +93,6 @@ uint32_t RsQblogProfileSerialiser::sizeItem(RsQblogProfile *item)
|
||||
uint32_t s = 8; // for header size
|
||||
s += 4; // time stamp
|
||||
s += item->openProfile.TlvSize();
|
||||
s += item->favoriteFiles.TlvSize();
|
||||
|
||||
return s;
|
||||
}
|
||||
@ -249,7 +123,6 @@ bool RsQblogProfileSerialiser::serialiseItem(RsQblogProfile* item, void* data, u
|
||||
/* add mandatory part */
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, item->timeStamp);
|
||||
ok &= item->openProfile.SetTlv(data, *size, &offset);
|
||||
ok &= item->favoriteFiles.SetTlv(data, *size, &offset);
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
@ -297,17 +170,14 @@ RsQblogProfile* RsQblogProfileSerialiser::deserialiseItem(void * data, uint32_t
|
||||
offset += 8;
|
||||
|
||||
/* get mandatory parts first */
|
||||
RsTlvKeyValueSet* kvSetOpen;
|
||||
RsTlvFileSet* fSet;
|
||||
RsTlvKeyValueWideSet* kvSetOpen;
|
||||
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->timeStamp));
|
||||
ok &= kvSetOpen->GetTlv(data, *size, &offset);
|
||||
ok &= fSet->GetTlv(data, *size, &offset);
|
||||
|
||||
/* copy over deserialised files */
|
||||
|
||||
item->openProfile = *kvSetOpen;
|
||||
item->favoriteFiles = *fSet;
|
||||
|
||||
if (offset != rssize)
|
||||
{
|
||||
|
@ -32,7 +32,8 @@
|
||||
|
||||
#include "serialiser/rsserviceids.h"
|
||||
#include "serialiser/rsserial.h"
|
||||
#include "serialiser/rstlvtypes.h"
|
||||
#include "serialiser/rsmsgitems.h"
|
||||
#include "serialiser/rstlvkvwide.h"
|
||||
|
||||
|
||||
const uint8_t RS_PKT_SUBTYPE_QBLOG_PROFILE = 0x01;
|
||||
@ -41,25 +42,18 @@ const uint8_t RS_PKT_SUBTYPE_QBLOG_PROFILE = 0x01;
|
||||
/*!
|
||||
* retroshare qblog msg item for storing received and sent blog message
|
||||
*/
|
||||
class RsQblogMsg: public RsItem
|
||||
class RsQblogMsg: public RsMsgItem
|
||||
{
|
||||
public:
|
||||
RsQblogMsg()
|
||||
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_QBLOG, // add msg id type
|
||||
RS_PKT_SUBTYPE_DEFAULT)
|
||||
:RsMsgItem(RS_SERVICE_TYPE_QBLOG)
|
||||
|
||||
{ return; }
|
||||
virtual ~RsQblogMsg();
|
||||
virtual void clear();
|
||||
|
||||
/// inherited method from RsItem
|
||||
std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
|
||||
/// use for time stamp
|
||||
uint32_t timeStamp;
|
||||
|
||||
/// contain blog mesgs and their blog time stamp (client time)
|
||||
std::wstring blogMsg;
|
||||
|
||||
};
|
||||
|
||||
/*!
|
||||
@ -82,53 +76,22 @@ std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
uint32_t timeStamp;
|
||||
|
||||
/// contains various profile information set by user, this and below use an open ended format
|
||||
RsTlvKeyValueSet openProfile;
|
||||
|
||||
/// favoirte files of the user.....
|
||||
RsTlvFileSet favoriteFiles;
|
||||
RsTlvKeyValueWideSet openProfile;
|
||||
|
||||
};
|
||||
|
||||
/*!
|
||||
* to serialise rsQblogItems: method names are self explanatory
|
||||
*/
|
||||
class RsQblogMsgSerialiser : public RsSerialType
|
||||
class RsQblogMsgSerialiser : public RsMsgSerialiser
|
||||
{
|
||||
|
||||
public:
|
||||
RsQblogMsgSerialiser()
|
||||
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_QBLOG)
|
||||
:RsMsgSerialiser(RS_SERVICE_TYPE_QBLOG)
|
||||
{ return; }
|
||||
virtual ~RsQblogMsgSerialiser()
|
||||
{ return; }
|
||||
/**
|
||||
* check size of RsItem to be serialised
|
||||
* @param RsItem RsItem which is going to be serilised
|
||||
* @return size of the RsItem in bytes
|
||||
*/
|
||||
virtual uint32_t size(RsItem *);
|
||||
|
||||
/**
|
||||
* serialise contents of item to data
|
||||
* @param item RsItem which is going to be serilised
|
||||
* @param data where contents will be serialised into
|
||||
* @return size of the RsItem in bytes
|
||||
*/
|
||||
virtual bool serialise (RsItem *item, void *data, uint32_t *size);
|
||||
|
||||
/**
|
||||
* serialise contents of item to data
|
||||
* @param data where contents will be deserialisedout of
|
||||
* @return size of the RsItem in bytes
|
||||
*/
|
||||
virtual RsItem * deserialise(void *data, uint32_t *size);
|
||||
|
||||
private:
|
||||
|
||||
|
||||
virtual uint32_t sizeItem(RsQblogMsg *);
|
||||
virtual bool serialiseItem (RsQblogMsg *item, void *data, uint32_t *size);
|
||||
virtual RsQblogMsg *deserialiseItem(void *data, uint32_t *size);
|
||||
|
||||
};
|
||||
|
||||
|
@ -190,7 +190,9 @@ const uint16_t TLV_TYPE_FILESET = 0x1001;
|
||||
const uint16_t TLV_TYPE_FILEDATA = 0x1002;
|
||||
|
||||
const uint16_t TLV_TYPE_KEYVALUE = 0x1010;
|
||||
const uint16_t TLV_TYPE_WKEYVALUE = 0x1012;
|
||||
const uint16_t TLV_TYPE_KEYVALUESET = 0x1011;
|
||||
const uint16_t TLV_TYPE_WKEYVALUESET = 0x1013;
|
||||
|
||||
const uint16_t TLV_TYPE_PEERSET = 0x1020;
|
||||
const uint16_t TLV_TYPE_SERVICESET = 0x1030;
|
||||
|
248
libretroshare/src/serialiser/rstlvkvwide.cc
Normal file
248
libretroshare/src/serialiser/rstlvkvwide.cc
Normal file
@ -0,0 +1,248 @@
|
||||
/*
|
||||
* libretroshare/src/serialiser: rstlvkvwide.cc
|
||||
*
|
||||
* RetroShare Serialiser.
|
||||
*
|
||||
* Copyright 2007-2008 by Chris Parker
|
||||
*
|
||||
* 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 "rstlvbase.h"
|
||||
#include "rstlvkvwide.h"
|
||||
#include "rsbaseserial.h"
|
||||
#include "util/rsprint.h"
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
void RsTlvKeyValueWide::TlvClear()
|
||||
{
|
||||
wKey.clear();
|
||||
wValue.clear();
|
||||
}
|
||||
|
||||
uint16_t RsTlvKeyValueWide::TlvSize()
|
||||
{
|
||||
uint32_t s = 4; /* header size */
|
||||
s += GetTlvWideStringSize(wKey);
|
||||
s += GetTlvWideStringSize(wValue);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
std::ostream &RsTlvKeyValueWide::print(std::ostream &out, uint16_t indent)
|
||||
{
|
||||
printBase(out, "RsTlvKeyValueWide", indent);
|
||||
uint16_t int_Indent = indent + 2;
|
||||
std::string cnv_str(wKey.begin(), wKey.end()); // to convert to string
|
||||
|
||||
printIndent(out, int_Indent);
|
||||
out << "wKey:" << cnv_str;
|
||||
cnv_str.clear();
|
||||
cnv_str.assign(wValue.begin(), wValue.end());
|
||||
printIndent(out, int_Indent);
|
||||
out << "wValue:" << cnv_str;
|
||||
out << std::endl;
|
||||
|
||||
printEnd(out, "RsTlvKeyValuewide", indent);
|
||||
return out;
|
||||
}
|
||||
|
||||
bool RsTlvKeyValueWide::SetTlv(void *data, uint32_t size, uint32_t *offset)
|
||||
{
|
||||
|
||||
/* must check sizes */
|
||||
uint16_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, TLV_TYPE_WKEYVALUE, tlvsize);
|
||||
|
||||
|
||||
|
||||
/* now optional ones */
|
||||
if (wKey.length() > 0)
|
||||
ok &= SetTlvWideString(data, tlvend, offset, TLV_TYPE_WSTR_KEY, wKey); // no base tlv type for title?
|
||||
if (wValue.length() > 0)
|
||||
ok &= SetTlvWideString(data, tlvend, offset, TLV_TYPE_WSTR_VALUE, wValue); // no base tlv type for comment?
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool RsTlvKeyValueWide::GetTlv(void *data, uint32_t size, uint32_t *offset)
|
||||
{
|
||||
if (size < *offset + 4)
|
||||
return false;
|
||||
|
||||
uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
|
||||
uint16_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) );
|
||||
uint32_t tlvend = *offset + tlvsize;
|
||||
|
||||
if (size < tlvend) /* check size */
|
||||
return false; /* not enough space */
|
||||
|
||||
if (tlvtype != TLV_TYPE_WKEYVALUE ) /* check type */
|
||||
return false;
|
||||
|
||||
bool ok = true;
|
||||
|
||||
/* ready to load */
|
||||
TlvClear();
|
||||
|
||||
/* skip the header */
|
||||
(*offset) += 4;
|
||||
|
||||
/* while there is TLV */
|
||||
while((*offset) + 2 < tlvend)
|
||||
{
|
||||
/* get the next type */
|
||||
uint16_t tlvsubtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
|
||||
|
||||
switch(tlvsubtype)
|
||||
{
|
||||
case TLV_TYPE_WSTR_KEY:
|
||||
ok &= GetTlvWideString(data, tlvend, offset, TLV_TYPE_WSTR_KEY, wKey);
|
||||
break;
|
||||
case TLV_TYPE_WSTR_VALUE:
|
||||
ok &= GetTlvWideString(data, tlvend, offset, TLV_TYPE_WSTR_VALUE, wValue);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
/******************************************* Wide Key Value set *************************************/
|
||||
|
||||
|
||||
void RsTlvKeyValueWideSet::TlvClear()
|
||||
{
|
||||
wPairs.clear();
|
||||
}
|
||||
|
||||
uint16_t RsTlvKeyValueWideSet::TlvSize()
|
||||
{
|
||||
uint32_t s = 4; /* header size */
|
||||
|
||||
std::list<RsTlvKeyValueWide>::iterator it;
|
||||
|
||||
for(it = wPairs.begin(); it != wPairs.end(); it++)
|
||||
{
|
||||
s += it->TlvSize();
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
std::ostream &RsTlvKeyValueWideSet::print(std::ostream &out, uint16_t indent)
|
||||
{
|
||||
printBase(out, "RsTlvKeyValueWide", indent);
|
||||
uint16_t int_Indent = indent + 2;
|
||||
|
||||
std::list<RsTlvKeyValueWide>::iterator it;
|
||||
|
||||
for(it = wPairs.begin(); it != wPairs.end(); it++)
|
||||
{
|
||||
it->print(out, int_Indent);
|
||||
}
|
||||
|
||||
printEnd(out, "RsTlvKeyValuewide", indent);
|
||||
return out;
|
||||
}
|
||||
|
||||
bool RsTlvKeyValueWideSet::SetTlv(void *data, uint32_t size, uint32_t* offset)
|
||||
{
|
||||
|
||||
/* must check sizes */
|
||||
uint16_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, TLV_TYPE_WKEYVALUESET, tlvsize);
|
||||
std::list<RsTlvKeyValueWide>::iterator it;
|
||||
|
||||
for(it = wPairs.begin(); it != wPairs.end(); it++)
|
||||
{
|
||||
ok &= it->SetTlv(data, size, offset);
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool RsTlvKeyValueWideSet::GetTlv(void *data, uint32_t size, uint32_t* offset)
|
||||
{
|
||||
if (size < *offset + 4)
|
||||
return false;
|
||||
|
||||
uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
|
||||
uint16_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) );
|
||||
uint32_t tlvend = *offset + tlvsize;
|
||||
|
||||
if (size < tlvend) /* check size */
|
||||
return false; /* not enough space */
|
||||
|
||||
if (tlvtype != TLV_TYPE_WKEYVALUESET) /* check type */
|
||||
return false;
|
||||
|
||||
bool ok = true;
|
||||
|
||||
/* ready to load */
|
||||
TlvClear();
|
||||
|
||||
/* skip the header */
|
||||
(*offset) += 4;
|
||||
|
||||
/* while there is TLV */
|
||||
while((*offset) + 2 < tlvend)
|
||||
{
|
||||
|
||||
RsTlvKeyValueWide wPair;
|
||||
|
||||
ok &= wPair.GetTlv(data, size, offset);
|
||||
|
||||
wPairs.push_back(wPair);
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
66
libretroshare/src/serialiser/rstlvkvwide.h
Normal file
66
libretroshare/src/serialiser/rstlvkvwide.h
Normal file
@ -0,0 +1,66 @@
|
||||
#ifndef RSTLVKVWIDE_H_
|
||||
#define RSTLVKVWIDE_H_
|
||||
|
||||
/*
|
||||
* libretroshare/src/serialiser: rstlvkvwide.h
|
||||
*
|
||||
* RetroShare Serialiser.
|
||||
*
|
||||
* Copyright 2007-2008 by Chris Parker
|
||||
*
|
||||
* 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 "rstlvtypes.h"
|
||||
|
||||
class RsTlvKeyValueWide: public RsTlvItem
|
||||
{
|
||||
public:
|
||||
|
||||
RsTlvKeyValueWide() { return;}
|
||||
virtual ~RsTlvKeyValueWide() { return;}
|
||||
|
||||
virtual uint16_t TlvSize();
|
||||
virtual void TlvClear();
|
||||
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset);
|
||||
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */
|
||||
virtual std::ostream &print(std::ostream &out, uint16_t indent);
|
||||
|
||||
std::wstring wKey;
|
||||
std::wstring wValue;
|
||||
};
|
||||
|
||||
|
||||
class RsTlvKeyValueWideSet : public RsTlvItem
|
||||
{
|
||||
public:
|
||||
|
||||
RsTlvKeyValueWideSet() { return;}
|
||||
virtual ~RsTlvKeyValueWideSet() { return; }
|
||||
|
||||
virtual uint16_t TlvSize();
|
||||
virtual void TlvClear();
|
||||
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset);
|
||||
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */
|
||||
virtual std::ostream &print(std::ostream &out, uint16_t indent);
|
||||
|
||||
std::list<RsTlvKeyValueWide> wPairs;
|
||||
};
|
||||
|
||||
|
||||
#endif /*RSTLVKVWIDE_H_*/
|
@ -224,6 +224,5 @@ virtual std::ostream &print(std::ostream &out, uint16_t indent);
|
||||
RsTlvBinaryData binData; // Mandatory: serialised file info
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
88
libretroshare/src/serialiser/rstlvwidetest.cc
Normal file
88
libretroshare/src/serialiser/rstlvwidetest.cc
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* libretroshare/src/serialiser: rstlvkvwidetest.cc
|
||||
*
|
||||
* RetroShare Serialiser.
|
||||
*
|
||||
* Copyright 2007-2008 by Chris Parker
|
||||
*
|
||||
* 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 <stdlib.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "serialiser/rstlvkvwide.h"
|
||||
#include "serialiser/rstlvutil.h"
|
||||
#include "util/utest.h"
|
||||
|
||||
|
||||
INITTEST();
|
||||
|
||||
/* define utility functions to test out with */
|
||||
|
||||
//static int testRstlvWide();
|
||||
static int testRsTlvWideSet();
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cerr << "RsTlvWideTest[Item/Data/...] Tests" << std::endl;
|
||||
|
||||
testRsTlvWideSet();
|
||||
|
||||
FINALREPORT("RsTlvWideTest[Item/Data/...] Tests");
|
||||
|
||||
return TESTRESULT();
|
||||
}
|
||||
|
||||
int testRsTlvWideSet()
|
||||
{
|
||||
RsTlvKeyValueWideSet i1, i2; // one to set and other to get
|
||||
RsTlvKeyValueWide i_pair; // input pair
|
||||
RsTlvFileItem hello;
|
||||
|
||||
std::string randString("it should work now.");
|
||||
int j, k;
|
||||
|
||||
std::cerr << "entering loop now" << std::endl;
|
||||
/* store a 15 random pairs */
|
||||
|
||||
for(int i = 0; i < 15 ; i++)
|
||||
{
|
||||
j = rand() % 4;
|
||||
k = rand() % 4;
|
||||
std::cerr << "j: " << j << " k: " << k << std::endl;
|
||||
i_pair.wKey.assign(randString.begin(), randString.end());
|
||||
std::cerr << "loop count:" << i << std::endl;
|
||||
i_pair.wValue.assign(randString.begin(), randString.end());
|
||||
std::cerr << "loop count:" << i << std::endl;
|
||||
i1.wPairs.push_back(i_pair);
|
||||
|
||||
i_pair.TlvClear();
|
||||
}
|
||||
|
||||
std::cerr << "out of loop now" << std::endl;
|
||||
|
||||
CHECK(test_SerialiseTlvItem(std::cerr, &i1, &i2));
|
||||
|
||||
/*check that the data is the same*/
|
||||
|
||||
REPORT("Serialize/Deserialize RsTlvKeyValueWideSet");
|
||||
|
||||
return 1;
|
||||
}
|
@ -35,6 +35,7 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "serialiser/rstlvtypes.h"
|
||||
#include "serialiser/rstlvkvwide.h"
|
||||
#include "serialiser/rstlvutil.h"
|
||||
#include "util/utest.h"
|
||||
|
||||
@ -44,6 +45,7 @@ static int testRsTlvPeerIdSet();
|
||||
static int testRsTlvServiceIdSet();
|
||||
static int testRsTlvKeyValue();
|
||||
static int testRsTlvKeyValueSet();
|
||||
static int testRsTlvWideSet();
|
||||
|
||||
|
||||
int main()
|
||||
@ -54,6 +56,7 @@ int main()
|
||||
testRsTlvServiceIdSet();//tbd
|
||||
testRsTlvKeyValue();//tbd
|
||||
testRsTlvKeyValueSet();//tbd
|
||||
testRsTlvWideSet();
|
||||
|
||||
|
||||
FINALREPORT("RsTlvConfig[Item/Data/...] Tests");
|
||||
@ -69,11 +72,11 @@ int testRsTlvPeerIdSet()
|
||||
std::string testString;
|
||||
|
||||
std::string randString[5];
|
||||
randString[0] = "e$424!£!£";
|
||||
randString[0] = "e$424!<EFBFBD>!<21>";
|
||||
randString[1] = "e~:@L{L{KHKG";
|
||||
randString[2] = "e{@O**/*/*";
|
||||
randString[3] = "e?<<BNMB>HG£!£%$";
|
||||
randString[4] = "e><?<NVBCEE£$$%*^";
|
||||
randString[3] = "e?<<BNMB>HG<EFBFBD>!<21>%$";
|
||||
randString[4] = "e><?<NVBCEE<EFBFBD>$$%*^";
|
||||
|
||||
/* store a number of random ids */
|
||||
|
||||
@ -133,11 +136,11 @@ int testRsTlvKeyValueSet()
|
||||
/* instantiate the objects values */
|
||||
|
||||
std::string randString[5];
|
||||
randString[0] = "e$424!£!£";
|
||||
randString[0] = "e$424!<EFBFBD>!<21>";
|
||||
randString[1] = "e~:@L{L{KHKG";
|
||||
randString[2] = "e{@O**/*/*";
|
||||
randString[3] = "e?<<BNMB>HG£!£%$";
|
||||
randString[4] = "e><?<NVBCEE£$$%*^";
|
||||
randString[3] = "e?<<BNMB>HG<EFBFBD>!<21>%$";
|
||||
randString[4] = "e><?<NVBCEE<EFBFBD>$$%*^";
|
||||
|
||||
for(int i = 0; i < 15; i++)
|
||||
{
|
||||
@ -156,4 +159,34 @@ int testRsTlvKeyValueSet()
|
||||
REPORT("Serialize/Deserialize RsTlvKeyValueSet");
|
||||
}
|
||||
|
||||
int testRsTlvWideSet()
|
||||
{
|
||||
RsTlvKeyValueWideSet i1, i2; // one to set and other to get
|
||||
RsTlvKeyValueWide i_pair; // input pair
|
||||
RsTlvFileItem hello;
|
||||
|
||||
std::string randString[5];
|
||||
randString[0] = "e$424!<21>!<21>";
|
||||
randString[1] = "e~:@L{L{KHKG";
|
||||
randString[2] = "e{@O**/*/*";
|
||||
randString[3] = "e?<<BNMB>HG<48>!<21>%$";
|
||||
randString[4] = "e><?<NVBCEE<45>$$%*^";
|
||||
|
||||
/* store a 15 random pairs */
|
||||
|
||||
for(int i = 0; i < 15 ; i++)
|
||||
{
|
||||
i_pair.wKey.assign(randString[(rand() % 4)].begin(), randString[(rand() % 4)].end());
|
||||
i_pair.wValue.assign(randString[(rand() % 4)].begin(), randString[(rand() % 4)].end());
|
||||
i1.wPairs.push_back(i_pair);
|
||||
}
|
||||
|
||||
CHECK(test_SerialiseTlvItem(std::cerr, &i1, &i2));
|
||||
|
||||
/*check that the data is the same*/
|
||||
|
||||
REPORT("Serialize/Deserialize RsTlvKeyValueWideSet");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user