added serialisation code, removed two nxsitem types as they are redundant. still to add deserialisation and size functionality, then test

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-new_cache_system@5139 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
chrisparker126 2012-05-03 21:49:55 +00:00
parent 79062f742e
commit db8d019b3f
2 changed files with 505 additions and 54 deletions

View File

@ -1,5 +1,485 @@
#include "rsnxsitems.h"
RsNxs::RsNxs()
{
RsNxsSerialiser::size(RsItem *item) {
RsGrpResp* grp;
RsGrpMsgResp* gmp;
RsSyncGrp* sg;
RsSyncGrpList* sgl;
RsSyncGrpMsg* sgm;
RsSyncGrpMsgList* sgml;
if((sg = dynamic_cast<RsSyncGrp*>(item)) != NULL)
{
sizeSyncGrp(sg);
}else if ((sgl = dynamic_cast<RsSyncGrpList*>(item)) != NULL)
{
sizeSyncGrpList(sgl);
}else if ((sgm = dynamic_cast<RsSyncGrpMsg*>(item)) != NULL)
{
sizeSyncGrpMsg(sgm);
}else if ((sgml = dynamic_cast<RsSyncGrpMsgList*>(item)) != NULL)
{
sizeSynGrpMsgList(sgml);
}else if((grp = dynamic_cast<RsGrpResp*>(item)) != NULL)
{
sizeGrpMsgResp(grp);
}else if((gmp = dynamic_cast<RsGrpMsgResp*>(item)) != NULL)
{
sizeGrpMsgResp(gmp);
}
}
RsItem* RsNxsSerialiser::deserialise(void *data, uint32_t *size) {
#ifdef RSSERIAL_DEBUG
std::cerr << "RsDistribSerialiser::deserialise()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(SERVICE_TYPE != getRsItemService(rstype)))
{
return NULL; /* wrong type */
}
switch(rstype)
{
case RS_PKT_SUBTYPE_SYNC_GRP:
return deserialSyncGrp(data, size);
case RS_PKT_SUBTYPE_SYNC_GRP_LIST:
return deserialGrpReq(data, size);
case RS_PKT_SUBTYPE_SYNC_MSG:
return deserialSynGrpMsg(data, size);
case RS_PKT_SUBTYPE_SYNC_MSG_LIST:
return deserialSynGrpMsgList(data, size);
case RS_PKT_SUBTYPE_GRPS_RESP:
return deserialGrpMsgResp(data, size);
case RS_PKT_SUBTYPE_MSG_RESP:
return deserialGrpMsgResp(data, size);
case RS_PKT_SUBTYPE_SEARCH_REQ:
return deserialNxsSearchReq(data, size);
case RS_PKT_SUBTYPE_SEARCH_RESP:
return deserialNxsSearchResp(data, size);
default:
{
#ifdef NXS_DEBUG
std::cerr << "RsNxsSerialiser::deserialise() : data has not type"
<< std::endl;
#endif
return NULL;
}
}
}
bool RsNxsSerialiser::serialise(RsItem *item, void *data, uint32_t *size){
RsGrpResp* grp;
RsGrpMsgResp* gmp;
RsSyncGrp* sg;
RsSyncGrpList* sgl;
RsSyncGrpMsg* sgm;
RsSyncGrpMsgList* sgml;
if((sg = dynamic_cast<RsSyncGrp*>(item)) != NULL)
{
return serialiseSyncGrp(sg, data, size);
}else if ((sgl = dynamic_cast<RsSyncGrpList*>(item)) != NULL)
{
return serialiseSyncGrpList(sgl, data, size);
}else if ((sgm = dynamic_cast<RsSyncGrpMsg*>(item)) != NULL)
{
return serialiseSyncGrpMsg(sgm, data, size);
}else if ((sgml = dynamic_cast<RsSyncGrpMsgList*>(item)) != NULL)
{
return serialiseSynGrpMsgList(sgml, data, size);
}else if((grp = dynamic_cast<RsGrpResp*>(item)) != NULL)
{
return serialiseGrpMsgResp(grp, data, size);
}else if((gmp = dynamic_cast<RsGrpMsgResp*>(item)) != NULL)
{
return serialiseGrpMsgResp(gmp, data, size);
}
#ifdef NXS_DEBUG
std::cerr << "RsNxsSerialiser::serialise() item does not caste to know type"
<< std::endl;
#endif
return NULL;
}
bool RsNxsSerialiser::serialiseSynGrpMsgList(RsSyncGrpMsgList *item, void *data, uint32_t *size){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSynGrpMsgList()" << std::endl;
#endif
uint32_t tlvsize = sizeSynGrpMsgList(item);
uint32_t offset = 0;
if(*size < tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSynGrpMsgList()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* RsSyncGrpMsgList */
ok &= SetTlvUInt8(data, size, offset, TLV_TYPE_UINT8_SERID, item->flag);
ok &= SetTlvString(data, size, offset, TLV_TYPE_STR_GROUPID, item->grpId);
std::map<std::string, std::list<uint32_t> >::iterator mit =
item->msgs.begin();
// number of msgs
uint32_t nMsgs = item->msgs.size();
ok &= SetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_SIZE, nMsgs);
for(; mit != item->msgs.end(); mit++){
// if not version contains then this
// entry is invalid
ok &= SetTlvString(data, size, offset, TLV_TYPE_STR_MSGID, mit->first);
uint32_t nVersions = mit->second.size();
ok &= SetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_SIZE, nVersions);
std::list<uint32_t>::iterator lit = mit->second.begin();
for(; lit != mit->second.end(); lit++)
{
ok &= SetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_AGE, *lit);
}
}
if(offset != tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSynGrpMsgList() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef RSSERIAL_DEBUG
if (!ok)
{
std::cerr << "RsNxsSerialiser::serialiseSynGrpMsgList() NOK" << std::endl;
}
#endif
return ok;
}
bool RsNxsSerialiser::serialiseGrpMsgResp(RsGrpMsgResp *item, void *data, uint32_t *size)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseGrpMsgResp()" << std::endl;
#endif
uint32_t tlvsize = sizeGrpMsgResp(item);
uint32_t offset = 0;
if(*size < tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseGrpMsgResp()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* RsSyncGrpMsgList */
// number of msgs
uint32_t nMsgs = item->msgs.size();
ok &= SetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_SIZE, nMsgs);
std::list<RsTlvBinaryData>::iterator lit = item->msgs.begin();
for(; lit != item->msgs.end(); lit++){
RsTlvBinaryData& b = *lit;
b.SetTlv(data, size, offset);
}
if(offset != tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseGrpMsgResp() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef RSSERIAL_DEBUG
if (!ok)
{
std::cerr << "RsNxsSerialiser::serialiseGrpMsgResp() NOK" << std::endl;
}
#endif
return ok;
}
bool RsNxsSerialiser::serialiseGrpResp(RsGrpResp *item, void *data, uint32_t *size){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseGrpReq()" << std::endl;
#endif
uint32_t tlvsize = sizeGrpResp(item);
uint32_t offset = 0;
if(*size < tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseGrpReq()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
// number of grps
uint32_t nGrps = item->grps.size();
ok &= SetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_SIZE, nGrps);
std::list<RsTlvBinaryData>::iterator lit = item->grps.begin();
for(; lit != item->grps.end(); lit++){
RsTlvBinaryData& b = *lit;
b.SetTlv(data, size, offset);
}
if(offset != tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseGrpResp() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef RSSERIAL_DEBUG
if (!ok)
{
std::cerr << "RsNxsSerialiser::serialiseGrpResp() NOK" << std::endl;
}
#endif
return ok;
}
bool RsNxsSerialiser::serialiseSyncGrp(RsSyncGrp *item, void *data, uint32_t *size)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSyncGrp()" << std::endl;
#endif
uint32_t tlvsize = sizeSyncGrp(item);
uint32_t offset = 0;
if(*size < tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSyncGrp()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
ok &= SetTlvUInt8(data, size, offset, TLV_TYPE_UINT8_SERID, item->flag);
ok &= SetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_AGE, item->syncAge);
ok &= SetTlvString(data, size, offset, TLV_TYPE_STR_HASH_SHA1, item->syncHash);
if(offset != tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSyncGrp() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef RSSERIAL_DEBUG
if (!ok)
{
std::cerr << "RsNxsSerialiser::serialiseSyncGrp() NOK" << std::endl;
}
#endif
return ok;
}
bool RsNxsSerialiser::serialiseSyncGrpList(RsSyncGrpList *item, void *data, uint32_t *size)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSyncGrpList()" << std::endl;
#endif
uint32_t tlvsize = sizeSyncGrpList(item);
uint32_t offset = 0;
if(*size < tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSyncGrpList()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* RsSyncGrpList */
std::map<std::string, std::list<uint32_t> >::iterator mit =
item->grps.begin();
// number of grps
uint32_t nGrps = item->msgs.size();
ok &= SetTlvUInt8(data, size, offset, TLV_TYPE_UINT8_SERID, item->flag);
ok &= SetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_SIZE, nGrps);
for(; mit != item->grps.end(); mit++){
// if not version contains then this
// entry is invalid
ok &= SetTlvString(data, size, offset, TLV_TYPE_STR_MSGID, mit->first);
uint32_t nVersions = mit->second.size();
ok &= SetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_SIZE, nVersions);
std::list<uint32_t>::iterator lit = mit->second.begin();
for(; lit != mit->second.end(); lit++)
{
ok &= SetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_AGE, *lit);
}
}
if(offset != tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSyncGrpList( FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef RSSERIAL_DEBUG
if (!ok)
{
std::cerr << "RsNxsSerialiser::serialiseSyncGrpList() NOK" << std::endl;
}
#endif
return ok;
}
bool RsNxsSerialiser::serialiseSyncGrpMsg(RsSyncGrpMsg *item, void *data, uint32_t *size){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSyncGrpMsg()" << std::endl;
#endif
uint32_t tlvsize = sizeSyncGrpMsg(item);
uint32_t offset = 0;
if(*size < tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSyncGrpMsg()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok = setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
ok &= SetTlvUInt8(data, size, offset, TLV_TYPE_UINT8_SERID, item->flag);
ok &= SetTlvUInt32(data, size, offset, TLV_TYPE_UINT32_AGE, item->syncAge);
ok &= SetTlvString(data, size, offset, TLV_TYPE_STR_HASH_SHA1, item->syncHash);
ok &= SetTlvString(data, size, offset, TLV_TYPE_STR_GROUPID, item->grpId);
if(offset != tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSyncGrpMsg() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef RSSERIAL_DEBUG
if (!ok)
{
std::cerr << "RsNxsSerialiser::serialiseSyncGrpMsg( NOK" << std::endl;
}
#endif
return ok;
}
// TODO: need to finalise search term members
bool RsNxsSerialiser::serialiseNxsSearchReq(RsNxsSearchReq *item, void *data, uint32_t *size){
return false;
}
bool RsNxsSerialiser::serialiseNxsSearchResp(RsNxsSearchResp *item, void *data, uint32_t *size){
return false;
}

View File

@ -38,11 +38,9 @@
const uint8_t RS_PKT_SUBTYPE_SYNC_GRP = 0x0001;
const uint8_t RS_PKT_SUBTYPE_SYNC_GRP_LIST = 0x0002;
const uint8_t RS_PKT_SUBTYPE_GRPS_REQ = 0x0004;
const uint8_t RS_PKT_SUBTYPE_GRPS_RESP = 0x0008;
const uint8_t RS_PKT_SUBTYPE_SYNC_MSG = 0x0010;
const uint8_t RS_PKT_SUBTYPE_SYNC_MSG_LIST = 0x0020;
const uint8_t RS_PKT_SUBTYPE_MSG_REQ = 0x0040;
const uint8_t RS_PKT_SUBTYPE_MSG_RESP = 0x0080;
const uint8_t RS_PKT_SUBTYPE_SEARCH_REQ = 0x0100;
const uint8_t RS_PKT_SUBTYPE_SEARCH_RESP = 0x0200;
@ -94,24 +92,16 @@ class RsSyncGrpList : public RsNxs
public:
RsSynchGrpList(uint16_t servtype) : RsNxs(servtype, RS_PKT_SUBTYPE_SYNC_GRP_LIST) { return ; }
static const uint8_t FLAG_REQUEST;
static const uint8_t FLAG_RESPONSE;
uint8_t flag; // request or response
/// groups held by sending peer
std::map<std::string, std::list<uint32_t> > grps; // grpId/ versions pair
std::map<std::string, std::list<uint32_t> > grps; // grpId/ n version pair
};
/*!
* Use to send to peer list grps
* wanted to by server peer
*/
class RsGrpReq : public RsNxs
{
public:
RsGrpReq(uint16_t servtype) : RsNxs(servtype, RS_PKT_SUBTYPE_GRPS_REQ) { return; }
std::map<std::string, std::list<uint32_t> > grps; // grp/versions pair
};
/*!
* Contains serialised group items
* Each item corresponds to a group which needs to be
@ -140,6 +130,7 @@ public:
std::string grpId;
uint32_t flag;
uint32_t syncAge;
uint32_t syncHash;
};
@ -151,26 +142,17 @@ class RsSyncGrpMsgList : public RsNxs
{
public:
static const uint8_t FLAG_RESPONSE;
static const uint8_t FLAG_REQUEST;
RsSyncGrpMsgList(uint16_t servtype) : RsNxs(servtype, RS_PKT_SUBTYPE_SYNC_MSG_LIST) { return; }
uint32_t flag; // response/req
std::string grpId;
std::map<std::string, std::list<uint32_t> > msgs; // msg/versions pairs
};
/*!
* Use to request msgs from a peer
* for a given group
*/
class RsGrpMsgReq : public RsNxs
{
public:
RsGrpMsgReq(uint16_t servtype) : RsNxs(servtype, RS_PKT_SUBTYPE_MSG_REQ) { return; }
std::string grpId;
std::map<std::string, std::list<uint32_t> > msgs;
};
/*!
* Used to respond to a RsGrpMsgsReq
@ -222,7 +204,7 @@ class RsNxsSerialiser : public RsSerialType
virtual ~RsNxsSerialiser() { return; }
virtual uint32_t size(RsItem *);
virtual uint32_t size(RsItem *item);
virtual bool serialise(RsItem *item, void *data, uint32_t *size);
virtual RsItem* deserialise(void *data, uint32_t *size);
@ -231,63 +213,52 @@ private:
/* for RS_PKT_SUBTYPE_SYNC_GRP */
virtual uint32_t sizeSyncGrp(RsItem* item);
virtual uint32_t sizeSyncGrp(RsSyncGrp* item);
virtual bool serialiseSyncGrp(RsSyncGrp *item, void *data, uint32_t *size);
virtual RsSyncGrp* deserialise(void *data, uint32_t *size);
virtual RsSyncGrp* deserialSyncGrp(void *data, uint32_t *size);
/* for RS_PKT_SUBTYPE_SYNC_GRP_LIST */
virtual uint32_t sizeSyncGrpList(RsItem* item);
virtual uint32_t sizeSyncGrpList(RsSyncGrpList* item);
virtual bool serialiseSyncGrpList(RsSyncGrpList *item, void *data, uint32_t *size);
virtual RsSyncGrpList* deserialise(void *data, uint32_t *size);
/* for RS_PKT_SUBTYPE_GRPS_REQ */
virtual uint32_t sizeGrpReq(RsGrpReq* item);
virtual bool serialiseGrpReq(RsGrpReq *item, void *data, uint32_t *size);
virtual RsGrpReq* deserialise(void *data, uint32_t *size);
virtual RsSyncGrpList* deserialSyncGrpList(void *data, uint32_t *size);
/* for RS_PKT_SUBTYPE_GRPS_RESP */
virtual uint32_t sizeGrpResp(RsGrpResp* item);
virtual bool serialiseGrpResp(RsGrpResp *item, void *data, uint32_t *size);
virtual RsGrpResp* deserialise(void *data, uint32_t *size);
virtual RsGrpResp* deserialGrpResp(void *data, uint32_t *size);
/* for RS_PKT_SUBTYPE_SYNC_MSG */
virtual uint32_t sizeSyncGrpMsg(RsSyncGrpMsg* item);
virtual bool serialiseSyncGrpMsg(RsItem *item, void *data, uint32_t *size);
virtual RsSyncGrpMsg* deserialise(void *data, uint32_t *size);
virtual bool serialiseSyncGrpMsg(RsSyncGrpMsg *item, void *data, uint32_t *size);
virtual RsSyncGrpMsg* deserialSyncGrpMsg(void *data, uint32_t *size);
/* RS_PKT_SUBTYPE_SYNC_MSG_LIST */
virtual uint32_t sizeSynGrpMsgList(RsSyncGrpMsgList* item);
virtual bool serialiseSynGrpMsgList(RsSyncGrpMsgList* item, void *data, uint32_t* size);
virtual RsSyncGrpMsgList* deserialise(void *data, uint32_t *size);
virtual RsSyncGrpMsgList* deserialSynGrpMsgList(void *data, uint32_t *size);
/* RS_PKT_SUBTYPE_MSG_REQ */
virtual uint32_t sizeGrpMsgReq(RsGrpMsgReq* item);
virtual bool serialiseGrpMsgReq(RsGrpMsgReq* item, void* data, uint32_t* size);
virtual RsGrpMsgReq deserialise(void *data, uint32_t *size);
/* RS_PKT_SUBTYPE_MSG_RESP */
virtual uint32_t sizeGrpMsgResp(RsGrpMsgResp* item);
virtual bool serialiseGrpMsgResp(RsGrpMsgResp* item, void* data, uint32_t* size);
virtual RsGrpMsgResp* deserialise(void *data, uint32_t *size);
virtual RsGrpMsgResp* deserialGrpMsgResp(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 RsNxsSearchResp* deserialise(void *data, uint32_t *size);
virtual RsNxsSearchResp* deserialNxsSearchReq(void *data, uint32_t *size);
/* RS_PKT_SUBTYPE_SEARCH_RESP */
virtual uint32_t sizeNxsSearchResp(RsNxsSearchResp *item);
virtual bool serialiseNxsSearchResp(RsNxsSearchResp *item, void *data, uint32_t *size);
virtual RsNxsSearchResp* deserialise(void *data, uint32_t *size);
virtual RsNxsSearchResp* deserialNxsSearchResp(void *data, uint32_t *size);
};