added stats exchange system to gather number of posts in unsubscribed groups without the need to actually DL the messages (reduced bw a lot)

This commit is contained in:
csoler 2015-12-19 17:38:52 -05:00
parent e8b881b2f1
commit 6910ad3695
5 changed files with 386 additions and 56 deletions

View file

@ -330,6 +330,54 @@ bool RsNxsSerialiser::serialiseNxsGrp(RsNxsGrp *item, void *data, uint32_t *size
return ok;
}
bool RsNxsSerialiser::serialiseNxsSyncGrpStats(RsNxsSyncGrpStats *item, void *data, uint32_t *size)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseNxsSyncGrpStats()" << std::endl;
#endif
uint32_t tlvsize = sizeNxsSyncGrpStats(item);
uint32_t offset = 0;
if(*size < tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseNxsSyncGrpStats()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
ok &= setRawUInt32(data, *size, &offset, item->request_type);
ok &= item->grpId.serialise(data, *size, offset) ;
ok &= setRawUInt32(data, *size, &offset, item->number_of_posts);
ok &= setRawUInt32(data, *size, &offset, item->last_post_TS);
if(offset != tlvsize){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::serialiseSyncGrpStats() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef RSSERIAL_DEBUG
if (!ok)
{
std::cerr << "RsNxsSerialiser::serialiseSyncGrpStats() NOK" << std::endl;
}
#endif
return ok;
}
bool RsNxsSerialiser::serialiseNxsSyncGrp(RsNxsSyncGrp *item, void *data, uint32_t *size)
{
@ -707,6 +755,71 @@ RsNxsMsg* RsNxsSerialiser::deserialNxsMsg(void *data, uint32_t *size){
return item;
}
RsNxsSyncGrpStats* RsNxsSerialiser::deserialNxsSyncGrpStats(void *data, uint32_t *size){
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrpStats()" << 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_SYNC_GRP_STATS != getRsItemSubType(rstype)))
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrpStats() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrpStats() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsNxsSyncGrpStats* item = new RsNxsSyncGrpStats(getRsItemService(rstype));
/* skip the header */
offset += 8;
ok &= getRawUInt32(data, *size, &offset, &(item->request_type));
ok &= item->grpId.deserialise(data, *size, offset) ;
ok &= getRawUInt32(data, *size, &offset, &(item->number_of_posts));
ok &= getRawUInt32(data, *size, &offset, &(item->last_post_TS));
if (offset != rssize)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrpStats() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsNxsSerialiser::deserialNxsSyncGrpStats() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
RsNxsSyncGrp* RsNxsSerialiser::deserialNxsSyncGrp(void *data, uint32_t *size){
@ -1166,7 +1279,17 @@ uint32_t RsNxsSerialiser::sizeNxsSyncGrp(RsNxsSyncGrp *item)
return s;
}
uint32_t RsNxsSerialiser::sizeNxsSyncGrpStats(RsNxsSyncGrpStats *item)
{
uint32_t s = 8; // header size
s += 4; // request type
s += item->grpId.serial_size();
s += 4; // number_of_posts
s += 4; // last_post_TS
return s;
}
uint32_t RsNxsSerialiser::sizeNxsSyncGrpItem(RsNxsSyncGrpItem *item)
{
@ -1260,7 +1383,6 @@ void RsNxsSyncMsg::clear()
syncHash.clear();
updateTS = 0;
}
void RsNxsSyncGrpItem::clear()
{
flag = 0;
@ -1430,7 +1552,23 @@ std::ostream& RsNxsMsg::print(std::ostream &out, uint16_t indent){
return out;
}
std::ostream& RsNxsSyncGrpStats::print(std::ostream &out, uint16_t indent){
printRsItemBase(out, "RsNxsSyncGrpStats", indent);
uint16_t int_Indent = indent + 2;
out << "available posts: " << number_of_posts << std::endl;
printIndent(out , int_Indent);
out << "last update: " << last_post_TS << std::endl;
printIndent(out , int_Indent);
out << "group ID: " << grpId << std::endl;
printIndent(out , int_Indent);
out << "request type: " << request_type << std::endl;
printIndent(out , int_Indent);
printRsItemEnd(out ,"RsNxsSyncGrpStats", indent);
return out;
}
std::ostream& RsNxsTransac::print(std::ostream &out, uint16_t indent){
printRsItemBase(out, "RsNxsTransac", indent);

View file

@ -36,15 +36,17 @@
#include "serialiser/rstlvkeys.h"
#include "gxs/rsgxsdata.h"
// These items have "flag type" numbers, but this is not used.
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_GRP = 0x0001;
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_GRP_ITEM = 0x0002;
const uint8_t RS_PKT_SUBTYPE_NXS_GRP = 0x0004;
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_MSG_ITEM = 0x0008;
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_MSG = 0x0010;
const uint8_t RS_PKT_SUBTYPE_NXS_MSG = 0x0020;
const uint8_t RS_PKT_SUBTYPE_NXS_TRANS = 0x0040;
const uint8_t RS_PKT_SUBTYPE_NXS_GRP_PUBLISH_KEY = 0x0080;
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_GRP = 0x01;
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_GRP_ITEM = 0x02;
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_GRP_STATS = 0x03;
const uint8_t RS_PKT_SUBTYPE_NXS_GRP = 0x04;
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_MSG_ITEM = 0x08;
const uint8_t RS_PKT_SUBTYPE_NXS_SYNC_MSG = 0x10;
const uint8_t RS_PKT_SUBTYPE_NXS_MSG = 0x20;
const uint8_t RS_PKT_SUBTYPE_NXS_TRANS = 0x40;
const uint8_t RS_PKT_SUBTYPE_NXS_GRP_PUBLISH_KEY = 0x80;
// possibility create second service to deal with this functionality
@ -53,7 +55,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;
const uint8_t RS_PKT_SUBTYPE_EXT_SEARCH_REQ = 0x0010;
/*!
@ -107,6 +109,27 @@ public:
};
/*!
* Use to request statistics about a particular group
*/
class RsNxsSyncGrpStats : public RsNxsItem
{
public:
RsNxsSyncGrpStats(uint16_t servtype) : RsNxsItem(servtype, RS_PKT_SUBTYPE_NXS_SYNC_GRP_STATS) {}
virtual void clear() {}
virtual std::ostream &print(std::ostream &out, uint16_t indent);
static const uint8_t GROUP_INFO_TYPE_REQUEST = 0x01;
static const uint8_t GROUP_INFO_TYPE_RESPONSE = 0x02;
uint32_t request_type; // used to determine the type of request
RsGxsGroupId grpId; // id of the group
uint32_t number_of_posts; // number of posts in that group
uint32_t last_post_TS; // time_stamp of last post
};
/*!
* Use to request grp list from peer
* Server may advise client peer to use sync file
@ -445,6 +468,10 @@ private:
virtual bool serialiseNxsSyncGrp(RsNxsSyncGrp *item, void *data, uint32_t *size);
virtual RsNxsSyncGrp* deserialNxsSyncGrp(void *data, uint32_t *size);
virtual uint32_t sizeNxsSyncGrpStats(RsNxsSyncGrpStats* item);
virtual bool serialiseNxsSyncGrpStats(RsNxsSyncGrpStats *item, void *data, uint32_t *size);
virtual RsNxsSyncGrpStats* deserialNxsSyncGrpStats(void *data, uint32_t *size);
/* for RS_PKT_SUBTYPE_SYNC_GRP_ITEM */
virtual uint32_t sizeNxsSyncGrpItem(RsNxsSyncGrpItem* item);