added more extensive test for rsdataservice and subsequent bug fixes.

added test for RsNxsTransac item. 
applied rule of three to RsTlvBinaryData (destructor, assign op, copy constructor implemented)

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-new_cache_system@5196 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
chrisparker126 2012-06-05 14:46:18 +00:00
parent 36103b29a1
commit 822e395f93
17 changed files with 1107 additions and 73 deletions

View file

@ -21,28 +21,32 @@ uint32_t RsNxsSerialiser::size(RsItem *item) {
RsSyncGrpList* sgl;
RsSyncGrpMsg* sgm;
RsSyncGrpMsgList* sgml;
RsNxsTransac* ntx;
if((sg = dynamic_cast<RsSyncGrp*>(item)) != NULL)
{
sizeSyncGrp(sg);
return sizeSyncGrp(sg);
}else if ((sgl = dynamic_cast<RsSyncGrpList*>(item)) != NULL)
}else if(( ntx = dynamic_cast<RsNxsTransac*>(item)) != NULL){
return sizeNxsTrans(ntx);
}
else if ((sgl = dynamic_cast<RsSyncGrpList*>(item)) != NULL)
{
sizeSyncGrpList(sgl);
return sizeSyncGrpList(sgl);
}else if ((sgm = dynamic_cast<RsSyncGrpMsg*>(item)) != NULL)
{
sizeSyncGrpMsg(sgm);
return sizeSyncGrpMsg(sgm);
}else if ((sgml = dynamic_cast<RsSyncGrpMsgList*>(item)) != NULL)
{
sizeSyncGrpMsgList(sgml);
return sizeSyncGrpMsgList(sgml);
}else if((ngp = dynamic_cast<RsNxsGrp*>(item)) != NULL)
{
sizeNxsGrp(ngp);
return sizeNxsGrp(ngp);
}else if((nmg = dynamic_cast<RsNxsMsg*>(item)) != NULL)
{
sizeNxsMsg(nmg);
return sizeNxsMsg(nmg);
}
}
@ -77,6 +81,8 @@ RsItem* RsNxsSerialiser::deserialise(void *data, uint32_t *size) {
return deserialNxsGrp(data, size);
case RS_PKT_SUBTYPE_NXS_MSG:
return deserialNxsMsg(data, size);
case RS_PKT_SUBTYPE_NXS_TRANS:
return deserialNxsTrans(data, size);
case RS_PKT_SUBTYPE_NXS_EXTENDED:
return deserialNxsExtended(data, size);
default:
@ -102,11 +108,16 @@ bool RsNxsSerialiser::serialise(RsItem *item, void *data, uint32_t *size){
RsSyncGrpMsg* sgm;
RsSyncGrpMsgList* sgml;
RsNxsExtended* nxt;
RsNxsTransac* ntx;
if((sg = dynamic_cast<RsSyncGrp*>(item)) != NULL)
{
return serialiseSyncGrp(sg, data, size);
}else if ((ntx = dynamic_cast<RsNxsTransac*>(item)) != NULL)
{
return serialiseNxsTrans(ntx, data, size);
}else if ((sgl = dynamic_cast<RsSyncGrpList*>(item)) != NULL)
{
return serialiseSyncGrpList(sgl, data, size);
@ -157,7 +168,7 @@ bool RsNxsSerialiser::serialiseSynGrpMsgList(RsSyncGrpMsgList *item, void *data,
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
@ -208,7 +219,7 @@ bool RsNxsSerialiser::serialiseNxsMsg(RsNxsMsg *item, void *data, uint32_t *size
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
@ -262,7 +273,7 @@ bool RsNxsSerialiser::serialiseNxsGrp(RsNxsGrp *item, void *data, uint32_t *size
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
@ -316,7 +327,7 @@ bool RsNxsSerialiser::serialiseSyncGrp(RsSyncGrp *item, void *data, uint32_t *si
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
@ -343,6 +354,55 @@ bool RsNxsSerialiser::serialiseSyncGrp(RsSyncGrp *item, void *data, uint32_t *si
}
bool RsNxsSerialiser::serialiseNxsTrans(RsNxsTransac *item, void *data, uint32_t *size){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseNxsTrans()" << std::endl;
#endif
uint32_t tlvsize = sizeNxsTrans(item);
uint32_t offset = 0;
if(*size < tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseNxsTrans() size do not match" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
ok &= setRawUInt16(data, *size, &offset, item->transactFlag);
ok &= setRawUInt32(data, *size, &offset, item->nItems);
ok &= setRawUInt32(data, *size, &offset, item->timeout);
ok &= setRawUInt32(data, *size, &offset, item->transactionId);
if(offset != tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseNxsTrans() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef RSSERIAL_DEBUG
if (!ok)
{
std::cerr << "RsNxsSerialiser::serialiseNxsTrans() NOK" << std::endl;
}
#endif
return ok;
}
bool RsNxsSerialiser::serialiseSyncGrpList(RsSyncGrpList *item, void *data, uint32_t *size)
{
#ifdef RSSERIAL_DEBUG
@ -363,7 +423,7 @@ bool RsNxsSerialiser::serialiseSyncGrpList(RsSyncGrpList *item, void *data, uint
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
@ -411,7 +471,7 @@ bool RsNxsSerialiser::serialiseSyncGrpMsg(RsSyncGrpMsg *item, void *data, uint32
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
@ -438,11 +498,6 @@ bool RsNxsSerialiser::serialiseSyncGrpMsg(RsSyncGrpMsg *item, void *data, uint32
return ok;
}
// TODO: need to finalise search term members
bool RsNxsSerialiser::serialiseNxsSearchReq(RsNxsSearchReq *item, void *data, uint32_t *size){
return false;
}
bool RsNxsSerialiser::serialiseNxsExtended(RsNxsExtended *item, void *data, uint32_t *size){
@ -726,6 +781,74 @@ RsSyncGrpList* RsNxsSerialiser::deserialSyncGrpList(void *data, uint32_t *size){
return item;
}
RsNxsTransac* RsNxsSerialiser::deserialNxsTrans(void *data, uint32_t *size){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsTrans()" << 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)) ||
(SERVICE_TYPE != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_NXS_TRANS != getRsItemSubType(rstype)))
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsTrans() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialSyncGrpMsgList( FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
/* skip the header */
offset += 8;
bool ok = true;
RsNxsTransac* item = new RsNxsTransac(SERVICE_TYPE);
ok &= getRawUInt16(data, *size, &offset, &(item->transactFlag));
ok &= getRawUInt32(data, *size, &offset, &(item->nItems));
ok &= getRawUInt32(data, *size, &offset, &(item->timeout));
ok &= getRawUInt32(data, *size, &offset, &(item->transactionId));
if (offset != rssize)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsTrans() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsTrans() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
RsSyncGrpMsgList* RsNxsSerialiser::deserialSyncGrpMsgList(void *data, uint32_t *size){
@ -864,11 +987,6 @@ RsSyncGrpMsg* RsNxsSerialiser::deserialSyncGrpMsg(void *data, uint32_t *size)
}
RsNxsSearchReq* RsNxsSerialiser::deserialNxsSearchReq(void *data, uint32_t *size)
{
return NULL;
}
RsNxsExtended* RsNxsSerialiser::deserialNxsExtended(void *data, uint32_t *size){
return NULL;
}
@ -963,8 +1081,15 @@ uint32_t RsNxsSerialiser::sizeSyncGrpMsgList(RsSyncGrpMsgList *item)
return s;
}
uint32_t RsNxsSerialiser::sizeNxsSearchReq(RsNxsSearchReq *item){
return 0;
uint32_t RsNxsSerialiser::sizeNxsTrans(RsNxsTransac *item){
uint32_t s = 8; // header size
s += 2; // flag
s += 4; // nMsgs
s += 4; // timeout
s += 4; // transaction id
return s;
}
uint32_t RsNxsSerialiser::sizeNxsExtended(RsNxsExtended *item){
@ -1029,6 +1154,13 @@ void RsSyncGrpMsgList::clear()
grpId.clear();
}
void RsNxsTransac::clear(){
transactFlag = 0;
nItems = 0;
timeout = 0;
transactionId = 0;
}
std::ostream& RsSyncGrp::print(std::ostream &out, uint16_t indent)
{
@ -1164,3 +1296,22 @@ std::ostream& RsNxsMsg::print(std::ostream &out, uint16_t indent){
printRsItemEnd(out ,"RsNxsMsg", indent);
return out;
}
std::ostream& RsNxsTransac::print(std::ostream &out, uint16_t indent){
printRsItemBase(out, "RsNxsTransac", indent);
uint16_t int_Indent = indent + 2;
out << "transactFlag: " << transactFlag << std::endl;
printIndent(out , int_Indent);
out << "nItems: " << nItems << std::endl;
printIndent(out , int_Indent);
out << "timeout: " << timeout << std::endl;
printIndent(out , int_Indent);
out << "transactionId: " << transactionId << std::endl;
printIndent(out , int_Indent);
printRsItemEnd(out ,"RsNxsTransac", indent);
return out;
}

View file

@ -42,7 +42,8 @@ const uint8_t RS_PKT_SUBTYPE_NXS_GRP = 0x0004;
const uint8_t RS_PKT_SUBTYPE_SYNC_MSG = 0x0008;
const uint8_t RS_PKT_SUBTYPE_SYNC_MSG_LIST = 0x0010;
const uint8_t RS_PKT_SUBTYPE_NXS_MSG = 0x0020;
const uint8_t RS_PKT_SUBTYPE_SEARCH_REQ = 0x0040;
const uint8_t RS_PKT_SUBTYPE_NXS_TRANS = 0x0040;
// possibility create second service to deal with this functionality
@ -51,6 +52,7 @@ const uint8_t RS_PKT_SUBTYPE_EXT_SEARCH_GRP = 0x0001;
const uint8_t RS_PKT_SUBTYPE_EXT_SEARCH_MSG = 0x0002;
const uint8_t RS_PKT_SUBTYPE_EXT_DELETE_GRP = 0x0004;
const uint8_t RS_PKT_SUBTYPE_EXT_DELETE_MSG = 0x0008;
const uint8_t RS_PKT_SUBTYPE_EXT_SEARCH_REQ = 0x0010;
/*!
@ -105,7 +107,6 @@ public:
*/
class RsNxsTransac : public RsNxsItem {
public:
/** transaction **/
@ -114,15 +115,29 @@ public:
static const uint16_t FLAG_END_P1;
static const uint16_t FLAG_END_P2;
static const uint16_t FLAG_CANCEL;
static const uint16_t FLAG_FAIL_NUM;
static const uint16_t FLAG_FAIL_TIMEOUT;
static const uint16_t FLAG_FAIL_FULL;
/** transaction type **/
static const uint16_t FLAG_TYPE_GRP_LIST_RESP;
static const uint16_t FLAG_TYPE_MSG_LIST_RESP;
static const uint16_t FLAG_TYPE_GRP_LIST_REQ;
static const uint16_t FLAG_TYPE_
static const uint16_t FLAG_TYPE_MSG_LIST_REQ;
static const uint16_t FLAG_TYPE_GRPS;
static const uint16_t FLAG_TYPE_MSGS;
RsNxsTransac(uint16_t servtype) : RsNxsItem(servtype, RS_PKT_SUBTYPE_NXS_TRANS) { return; }
virtual ~RsNxsTransac() { return ; }
virtual void clear();
virtual std::ostream &print(std::ostream &out, uint16_t indent);
uint16_t transactFlag;
uint32_t nItems;
uint32_t timeout;
uint32_t transactionId;
};
/*!
@ -261,7 +276,7 @@ class RsNxsSearchReq : public RsNxsItem
{
public:
RsNxsSearchReq(uint16_t servtype): RsNxsItem(servtype, RS_PKT_SUBTYPE_SEARCH_REQ), serviceSearchItem(servtype) { return; }
RsNxsSearchReq(uint16_t servtype): RsNxsItem(servtype, RS_PKT_SUBTYPE_EXT_SEARCH_REQ), serviceSearchItem(servtype) { return; }
virtual ~RsNxsSearchReq() { return;}
virtual void clear() { return;}
@ -415,10 +430,10 @@ private:
virtual bool serialiseNxsMsg(RsNxsMsg* item, void* data, uint32_t* size);
virtual RsNxsMsg* deserialNxsMsg(void *data, uint32_t *size);
/* RS_PKT_SUBTYPE_SEARCH_REQ */
virtual uint32_t sizeNxsSearchReq(RsNxsSearchReq* item);
virtual bool serialiseNxsSearchReq(RsNxsSearchReq* item, void* data, uint32_t* size);
virtual RsNxsSearchReq* deserialNxsSearchReq(void* data, uint32_t *size);
/* RS_PKT_SUBTYPE_NXS_TRANS */
virtual uint32_t sizeNxsTrans(RsNxsTransac* item);
virtual bool serialiseNxsTrans(RsNxsTransac* item, void* data, uint32_t* size);
virtual RsNxsTransac* deserialNxsTrans(void* data, uint32_t *size);
/* RS_PKT_SUBTYPE_EXTENDED */
virtual RsNxsExtended* deserialNxsExtended(void* data, uint32_t *size);

View file

@ -76,6 +76,12 @@ RsTlvBinaryData::RsTlvBinaryData(uint16_t t)
return;
}
RsTlvBinaryData::RsTlvBinaryData(const RsTlvBinaryData &b)
: tlvtype(b.tlvtype), bin_data(NULL), bin_len(0) {
setBinData(b.bin_data, b.bin_len);
}
RsTlvBinaryData::~RsTlvBinaryData()
{
TlvClear();

View file

@ -65,8 +65,9 @@ class RsTlvBinaryData: public RsTlvItem
{
public:
RsTlvBinaryData(uint16_t t);
void operator=(const RsTlvBinaryData& b);
virtual ~RsTlvBinaryData();
RsTlvBinaryData(const RsTlvBinaryData& b); // as per rule of three
void operator=(const RsTlvBinaryData& b); // as per rule of three
virtual ~RsTlvBinaryData(); // as per rule of three
virtual uint32_t TlvSize();
virtual void TlvClear(); /*! Initialize fields to empty legal values ( "0", "", etc) */
virtual void TlvShallowClear(); /*! Don't delete the binary data */