added method to serialise/deserialise groups to/from memory and export of GxsIdentities to memory chunk in radix format

This commit is contained in:
csoler 2017-04-08 19:07:33 +02:00
parent 4067c95761
commit 4f5da86aca
5 changed files with 175 additions and 7 deletions

View File

@ -1273,6 +1273,58 @@ bool RsGenExchange::getMsgRelatedMeta(const uint32_t &token, GxsMsgRelatedMetaMa
return ok;
}
bool RsGenExchange::getSerializedGroupData(const uint32_t &token, RsGxsGroupId& id,unsigned char *& data,uint32_t& size)
{
RS_STACK_MUTEX(mGenMtx) ;
std::list<RsNxsGrp*> nxsGrps;
if(!mDataAccess->getGroupData(token, nxsGrps))
return false ;
if(nxsGrps.size() != 1)
{
std::cerr << "(EE) getSerializedGroupData() got multiple groups in single request. This is unexpected." << std::endl;
for(std::list<RsNxsGrp*>::const_iterator it(nxsGrps.begin());it!=nxsGrps.end();++it)
delete *it ;
return false ;
}
RsNxsGrp *nxs_grp = *(nxsGrps.begin());
size = nxs_grp->serial_size() ;
id = nxs_grp->metaData->mGroupId ;
if(size > 1024*1024 || NULL==(data = (unsigned char *)rs_malloc(size)))
{
std::cerr << "(EE) getSerializedGroupData() cannot allocate mem chunk of size " << size << ". Too big, or no room." << std::endl;
delete nxs_grp ;
return false ;
}
return nxs_grp->serialise(data,size) ;
}
bool RsGenExchange::deserializeGroupData(unsigned char *data,uint32_t size)
{
RS_STACK_MUTEX(mGenMtx) ;
RsItem *item = mSerialiser->deserialise(data, &size);
RsNxsGrp *nxs_grp = dynamic_cast<RsNxsGrp*>(item) ;
if(item == NULL)
{
std::cerr << "(EE) RsGenExchange::deserializeGroupData(): cannot deserialise this data. Something's wrong." << std::endl;
delete item ;
return false ;
}
mReceivedGrps.push_back( GxsPendingItem<RsNxsGrp*, RsGxsGroupId>(nxs_grp, nxs_grp->metaData->mGroupId,time(NULL)) );
return true ;
}
bool RsGenExchange::getGroupData(const uint32_t &token, std::vector<RsGxsGrpItem *>& grpItem)
{

View File

@ -288,6 +288,20 @@ protected:
*/
bool getGroupData(const uint32_t &token, std::vector<RsGxsGrpItem*>& grpItem);
/*!
* \brief getSerializedGroupData
* Retrieves the complete group data serialized into a chunk of memory. This can be useful to
* transfer a full group from one machine to another.
*
* \param token token previously obtained from cache request
* \param data memory chunk allocated (using malloc)
* \param size size of the memory chunk.
* \return
*/
bool getSerializedGroupData(const uint32_t &token, RsGxsGroupId &id, unsigned char *& data, uint32_t& size);
bool deserializeGroupData(unsigned char *data, uint32_t size);
template<class GrpType>
bool getGroupDataT(const uint32_t &token, std::vector<GrpType*>& grpItem)
{

View File

@ -305,6 +305,9 @@ public:
virtual bool setAsRegularContact(const RsGxsId& id,bool is_a_contact) = 0 ;
virtual bool isARegularContact(const RsGxsId& id) = 0 ;
virtual bool serialiseIdentityToMemory(const RsGxsId& id,std::string& radix_string)=0;
virtual bool deserialiseIdentityFromMemory(const std::string& radix_string)=0;
/*!
* \brief overallReputationLevel
* Returns the overall reputation level of the supplied identity. See rsreputations.h

View File

@ -70,6 +70,8 @@ static const time_t MAX_KEEP_KEYS_SIGNED_KNOWN = 30 * 86400 ; // signed ident
static const uint32_t MAX_DELAY_BEFORE_CLEANING= 1800 ; // clean old keys every 30 mins
static const uint32_t MAX_SERIALISED_IDENTITY_AGE = 600 ; // after 10 mins, a serialised identity record must be renewed.
RsIdentity *rsIdentity = NULL;
/******
@ -97,13 +99,12 @@ RsIdentity *rsIdentity = NULL;
#define BG_REPUTATION 3
#define GXSIDREQ_CACHELOAD 0x0001
#define GXSIDREQ_CACHEOWNIDS 0x0002
#define GXSIDREQ_PGPHASH 0x0010
#define GXSIDREQ_RECOGN 0x0020
#define GXSIDREQ_OPINION 0x0030
#define GXSIDREQ_CACHELOAD 0x0001
#define GXSIDREQ_CACHEOWNIDS 0x0002
#define GXSIDREQ_PGPHASH 0x0010
#define GXSIDREQ_RECOGN 0x0020
#define GXSIDREQ_OPINION 0x0030
#define GXSIDREQ_SERIALIZE_TO_MEMORY 0x0040
#define GXSIDREQ_CACHETEST 0x1000
@ -697,6 +698,84 @@ bool p3IdService::getOwnIds(std::list<RsGxsId> &ownIds)
return true ;
}
bool p3IdService::serialiseIdentityToMemory(const RsGxsId& id,std::string& radix_string)
{
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
// look into cache. If available, return the data. If not, request it.
std::map<RsGxsId,SerialisedIdentityStruct>::const_iterator it = mSerialisedIdentities.find(id);
if(it != mSerialisedIdentities.end())
{
Radix64::encode(it->second.mMem,it->second.mSize,radix_string) ;
if(it->second.mLastUsageTS + MAX_SERIALISED_IDENTITY_AGE > time(NULL))
return true ;
std::cerr << "Identity " << id << " will be re-serialised, because the last record is too old." << std::endl;
}
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA;
uint32_t token = 0;
std::list<RsGxsGroupId> groupIds;
groupIds.push_back(RsGxsGroupId(id)) ;
RsGenExchange::getTokenService()->requestGroupInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, groupIds);
GxsTokenQueue::queueRequest(token, GXSIDREQ_SERIALIZE_TO_MEMORY);
return false;
}
void p3IdService::handle_get_serialized_grp(uint32_t token)
{
// store the serialized data in cache.
unsigned char *mem = NULL;
uint32_t size;
RsGxsGroupId id ;
if(!RsGenExchange::getSerializedGroupData(token,id, mem,size))
{
std::cerr << "(EE) call to RsGenExchage::getSerializedGroupData() failed." << std::endl;
return ;
}
std::cerr << "Received serialised group from RsGenExchange." << std::endl;
std::map<RsGxsId,SerialisedIdentityStruct>::const_iterator it = mSerialisedIdentities.find(RsGxsId(id));
if(it != mSerialisedIdentities.end())
free(it->second.mMem) ;
SerialisedIdentityStruct s ;
s.mMem = mem ;
s.mSize = size ;
s.mLastUsageTS = time(NULL) ;
mSerialisedIdentities[RsGxsId(id)] = s ;
}
bool p3IdService::deserialiseIdentityFromMemory(const std::string& radix_string)
{
std::vector<uint8_t> mem = Radix64::decode(radix_string) ;
if(mem.empty())
{
std::cerr << "Cannot decode radix string \"" << radix_string << "\"" << std::endl;
return false ;
}
if(!RsGenExchange::deserializeGroupData(mem.data(),mem.size()))
{
std::cerr << "Cannot load identity from radix string \"" << radix_string << "\"" << std::endl;
return false ;
}
return true ;
}
bool p3IdService::createIdentity(uint32_t& token, RsIdentityParameters &params)
{
@ -4117,6 +4196,9 @@ void p3IdService::handleResponse(uint32_t token, uint32_t req_type)
case GXSIDREQ_OPINION:
opinion_handlerequest(token);
break;
case GXSIDREQ_SERIALIZE_TO_MEMORY:
handle_get_serialized_grp(token) ;
default:
/* error */
std::cerr << "p3IdService::handleResponse() Unknown Request Type: " << req_type;

View File

@ -212,6 +212,13 @@ private:
void init(const RsGxsIdGroupItem *item, const RsTlvPublicRSAKey& in_pub_key, const RsTlvPrivateRSAKey& in_priv_key,const std::list<RsRecognTag> &tagList);
};
struct SerialisedIdentityStruct
{
unsigned char *mMem ;
uint32_t mSize ;
time_t mLastUsageTS;
};
// Not sure exactly what should be inherited here?
// Chris - please correct as necessary.
@ -302,6 +309,8 @@ public:
virtual bool requestKey(const RsGxsId &id, const std::list<RsPeerId> &peers, const RsIdentityUsage &use_info);
virtual bool requestPrivateKey(const RsGxsId &id);
virtual bool serialiseIdentityToMemory(const RsGxsId& id,std::string& radix_string);
virtual bool deserialiseIdentityFromMemory(const std::string& radix_string);
/**************** RsGixsReputation Implementation ****************/
@ -394,6 +403,12 @@ private:
bool mBgSchedule_Active;
uint32_t mBgSchedule_Mode;
/***********************************8
* Fonction to receive and handle group serialisation to memory
*/
virtual void handle_get_serialized_grp(uint32_t token);
/************************************************************************
* pgphash processing.
*
@ -524,6 +539,8 @@ private:
std::map<RsGxsId, std::list<RsPeerId> > mIdsNotPresent;
std::map<RsGxsId,keyTSInfo> mKeysTS ;
std::map<RsGxsId,SerialisedIdentityStruct> mSerialisedIdentities ;
// keep a list of regular contacts. This is useful to sort IDs, and allow some services to priviledged ids only.
std::set<RsGxsId> mContacts;
RsNetworkExchangeService* mNes;