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

@ -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;