RetroShare/libretroshare/src/services/p3idservice.h

438 lines
12 KiB
C
Raw Normal View History

/*
* libretroshare/src/services: p3idservice.h
*
* Identity interface for RetroShare.
*
* Copyright 2012-2012 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#ifndef P3_IDENTITY_SERVICE_HEADER
#define P3_IDENTITY_SERVICE_HEADER
#include "retroshare/rsidentity.h" // External Interfaces.
#include "gxs/rsgenexchange.h" // GXS service.
#include "gxs/rsgixs.h" // Internal Interfaces.
#include "gxs/gxstokenqueue.h"
#include <map>
#include <string>
#include "util/rsmemcache.h"
#include "util/rstickevent.h"
#include "util/rsrecogn.h"
#include "pqi/authgpg.h"
#include "serialiser/rsgxsrecognitems.h"
/*
* Identity Service
*
*/
// INTERNAL DATA TYPES.
// Describes data stored in GroupServiceString.
class SSBit
{
public:
virtual bool load(const std::string &input) = 0;
virtual std::string save() const = 0;
};
class SSGxsIdPgp: public SSBit
{
public:
SSGxsIdPgp()
:idKnown(false), lastCheckTs(0), checkAttempts(0) { return; }
virtual bool load(const std::string &input);
virtual std::string save() const;
bool idKnown;
time_t lastCheckTs;
uint32_t checkAttempts;
std::string pgpId;
};
class SSGxsIdRecognTags: public SSBit
{
public:
SSGxsIdRecognTags()
:tagFlags(0), publishTs(0), lastCheckTs(0) { return; }
virtual bool load(const std::string &input);
virtual std::string save() const;
void setTags(bool processed, bool pending, uint32_t flags);
bool tagsProcessed() const; // have we processed?
bool tagsPending() const; // should we reprocess?
bool tagValid(int i) const;
time_t publishTs;
time_t lastCheckTs;
uint32_t tagFlags;
};
class SSGxsIdScore: public SSBit
{
public:
SSGxsIdScore()
:score(0) { return; }
virtual bool load(const std::string &input);
virtual std::string save() const;
int score;
};
class SSGxsIdCumulator: public SSBit
{
public:
SSGxsIdCumulator()
:count(0), nullcount(0), sum(0), sumsq(0) { return; }
virtual bool load(const std::string &input);
virtual std::string save() const;
uint32_t count;
uint32_t nullcount;
double sum;
double sumsq;
// derived parameters:
};
class SSGxsIdGroup: public SSBit
{
public:
SSGxsIdGroup() { return; }
virtual bool load(const std::string &input);
virtual std::string save() const;
// pgphash status
SSGxsIdPgp pgp;
// recogTags.
SSGxsIdRecognTags recogntags;
// reputation score.
SSGxsIdScore score;
SSGxsIdCumulator opinion;
SSGxsIdCumulator reputation;
};
#define ID_LOCAL_STATUS_FULL_CALC_FLAG 0x00010000
#define ID_LOCAL_STATUS_INC_CALC_FLAG 0x00020000
#define MAX_CACHE_SIZE 100 // Small for testing..
//#define MAX_CACHE_SIZE 10000 // More useful size
class RsGxsIdGroupItem;
class RsGxsIdCache
{
public:
RsGxsIdCache();
RsGxsIdCache(const RsGxsIdGroupItem *item, const RsTlvSecurityKey &in_pkey,
const std::list<RsRecognTag> &tagList);
void updateServiceString(std::string serviceString);
time_t mPublishTs;
std::list<RsRecognTag> mRecognTags; // Only partially validated.
RsIdentityDetails details;
RsTlvSecurityKey pubkey;
};
#if 0
class LruData
{
public:
RsGxsId key;
};
#endif
// Not sure exactly what should be inherited here?
// Chris - please correct as necessary.
class p3IdService: public RsGxsIdExchange, public RsIdentity,
public GxsTokenQueue, public RsTickEvent
{
public:
p3IdService(RsGeneralDataService* gds, RsNetworkExchangeService* nes);
static uint32_t idAuthenPolicy();
virtual void service_tick(); // needed for background processing.
/* General Interface is provided by RsIdentity / RsGxsIfaceImpl. */
/* Data Specific Interface */
// These are exposed via RsIdentity.
virtual bool getGroupData(const uint32_t &token, std::vector<RsGxsIdGroup> &groups);
virtual bool getMsgData(const uint32_t &token, std::vector<RsGxsIdOpinion> &opinions);
// These are local - and not exposed via RsIdentity.
virtual bool createGroup(uint32_t& token, RsGxsIdGroup &group);
virtual bool updateGroup(uint32_t& token, RsGxsIdGroup &group);
virtual bool createMsg(uint32_t& token, RsGxsIdOpinion &opinion);
/**************** RsIdentity External Interface.
* Notes:
*
* All the data is cached together for the moment - We should probably
* seperate and sort this out.
*
* Also need to handle Cache updates / invalidation from internal changes.
*
*/
//virtual bool getNickname(const RsGxsId &id, std::string &nickname);
virtual bool getIdDetails(const RsGxsId &id, RsIdentityDetails &details);
virtual bool getOwnIds(std::list<RsGxsId> &ownIds);
//
virtual bool submitOpinion(uint32_t& token, RsIdOpinion &opinion);
virtual bool createIdentity(uint32_t& token, RsIdentityParameters &params);
virtual bool updateIdentity(uint32_t& token, RsGxsIdGroup &group);
virtual bool parseRecognTag(const RsGxsId &id, const std::string &nickname,
const std::string &tag, RsRecognTagDetails &details);
virtual bool getRecognTagRequest(const RsGxsId &id, const std::string &comment,
uint16_t tag_class, uint16_t tag_type, std::string &tag);
/**************** RsGixs Implementation
* Notes:
* Interface is only suggestion at the moment, will be changed as necessary.
* Results should be cached / preloaded for maximum speed.
*
*/
virtual bool haveKey(const RsGxsId &id);
virtual bool requestKey(const RsGxsId &id, const std::list<PeerId> &peers);
virtual int getKey(const RsGxsId &id, RsTlvSecurityKey &key);
virtual bool havePrivateKey(const RsGxsId &id);
virtual bool requestPrivateKey(const RsGxsId &id);
virtual int getPrivateKey(const RsGxsId &id, RsTlvSecurityKey &key);
/**************** RsGixsReputation Implementation
* Notes:
* Again should be cached if possible.
*/
// get Reputation.
virtual bool haveReputation(const RsGxsId &id);
virtual bool loadReputation(const RsGxsId &id);
virtual bool getReputation(const RsGxsId &id, GixsReputation &rep);
protected:
/** Notifications **/
virtual void notifyChanges(std::vector<RsGxsNotify*>& changes);
/** Overloaded to add PgpIdHash to Group Definition **/
virtual ServiceCreate_Return service_CreateGroup(RsGxsGrpItem* grpItem, RsTlvSecurityKeySet& keySet);
// Overloaded from GxsTokenQueue for Request callbacks.
virtual void handleResponse(uint32_t token, uint32_t req_type);
// Overloaded from RsTickEvent.
virtual void handle_event(uint32_t event_type, const std::string &elabel);
* Refinement of new Cache Interface: - Defined expected parameters for GxsGroups (see comments in rsidentity.h) - Added Various #defines for Groups / Msgs (again in rsidentity.h) - Converted new Group / new Msg into async "token" calls, returing MetaData. - Refined Grp/Msg Status Flags... similarly to how Forums used them... Expect UNREAD & UNPROCESSED flags to be set for a new msg, and UPDATED flag to be set for the corresponding group. There is flexibility for services to add their own flags too. - removed groupsChanged(). This can alternatively be implemented using. getGroupList(opts.Status == UPDATED)... - refined SubscribeFlags in a similar manner. - Added "ServiceString" to Group/Msg MetaData. This is freeform cache storage for service to use... currently p3Posted uses it to count Votes. - Added MsgStatus & SubscribeFlag filtering to Cache Requests. - Implemented these filters in GxsDataProxy (no efficient yet!) * Cleaned up all 6 new Cache Services to conform to new interface. * Removed old interface code that was #ifdef'd out. * Implemented Basic Ranking algorithms for p3posted: - Background process to process new votes/comments. - getRanking(token) interface call. - Intercept StatusRequests, etc to hide internal data requests. - While the basic code is complete, it needs much testing / tweaking. - Should shift work to a seperate thread. - Comment Ranking has still to be done. - Interfacing with GUI not yet attempted. git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-new_cache_system@5276 b45a01b8-16f6-495d-af2f-9b41ad6348cc
2012-07-06 19:14:18 -04:00
private:
/************************************************************************
* This is the Cache for minimising calls to the DataStore.
*
*/
int cache_tick();
bool cache_request_load(const RsGxsId &id, const std::list<std::string>& peers = std::list<std::string>());
bool cache_start_load();
bool cache_load_for_token(uint32_t token);
bool cache_store(const RsGxsIdGroupItem *item);
bool cache_update_if_cached(const RsGxsId &id, std::string serviceString);
bool isPendingNetworkRequest(const RsGxsId& gxsId) const;
void requestIdsFromNet();
// Mutex protected.
//std::list<RsGxsId> mCacheLoad_ToCache;
std::map<RsGxsId, std::list<std::string> > mCacheLoad_ToCache, mPendingCache;
// Switching to RsMemCache for Key Caching.
RsMemCache<RsGxsId, RsGxsIdCache> mPublicKeyCache;
RsMemCache<RsGxsId, RsGxsIdCache> mPrivateKeyCache;
/************************************************************************
* Refreshing own Ids.
*
*/
bool cache_request_ownids();
bool cache_load_ownids(uint32_t token);
std::list<RsGxsId> mOwnIds;
/************************************************************************
* Test fns for Caching.
*
*/
bool cachetest_tick();
bool cachetest_getlist();
bool cachetest_handlerequest(uint32_t token);
/************************************************************************
* for processing background tasks that use the serviceString.
* - must be mutually exclusive to avoid clashes.
*/
bool CacheArbitration(uint32_t mode);
void CacheArbitrationDone(uint32_t mode);
bool mBgSchedule_Active;
uint32_t mBgSchedule_Mode;
/************************************************************************
* pgphash processing.
*
*/
bool pgphash_start();
bool pgphash_handlerequest(uint32_t token);
bool pgphash_process();
bool checkId(const RsGxsIdGroup &grp, PGPIdType &pgp_id);
void getPgpIdList();
/* MUTEX PROTECTED DATA (mIdMtx - maybe should use a 2nd?) */
std::map<PGPIdType, PGPFingerprintType> mPgpFingerprintMap;
std::list<RsGxsIdGroup> mGroupsToProcess;
/************************************************************************
* recogn processing.
*
*/
bool recogn_start();
bool recogn_handlerequest(uint32_t token);
bool recogn_process();
// helper functions.
bool recogn_extract_taginfo(const RsGxsIdGroupItem *item, std::list<RsGxsRecognTagItem *> &tagItems);
bool cache_process_recogntaginfo(const RsGxsIdGroupItem *item, std::list<RsRecognTag> &tagList);
bool recogn_checktag(const RsGxsId &id, const std::string &nickname, RsGxsRecognTagItem *item, bool doSignCheck, bool &isPending);
void loadRecognKeys();
/************************************************************************
* for getting identities that are not present
*
*/
void checkPeerForIdentities();
/* MUTEX PROTECTED DATA (mIdMtx - maybe should use a 2nd?) */
bool checkRecognSignature_locked(std::string encoded, RSA &key, std::string signature);
bool getRecognKey_locked(std::string signer, RSA &key);
std::list<RsGxsGroupId> mRecognGroupIds;
std::list<RsGxsIdGroupItem *> mRecognGroupsToProcess;
std::map<std::string, RsGxsRecognSignerItem *> mRecognSignKeys;
std::map<std::string, uint32_t> mRecognOldSignKeys;
/************************************************************************
* Below is the background task for processing opinions => reputations
*
*/
virtual void generateDummyData();
void generateDummy_OwnIds();
void generateDummy_FriendPGP();
void generateDummy_UnknownPGP();
void generateDummy_UnknownPseudo();
std::string genRandomId(int len = 20);
bool reputation_start();
bool reputation_continue();
int background_tick();
bool background_checkTokenRequest();
bool background_requestGroups();
bool background_requestNewMessages();
bool background_processNewMessages();
bool background_FullCalcRequest();
bool background_processFullCalc();
bool background_cleanup();
RsMutex mIdMtx;
/***** below here is locked *****/
bool mLastBgCheck;
bool mBgProcessing;
uint32_t mBgToken;
uint32_t mBgPhase;
std::map<std::string, RsGroupMetaData> mBgGroupMap;
std::list<std::string> mBgFullCalcGroups;
/************************************************************************
* Other Data that is protected by the Mutex.
*/
std::vector<RsGxsGroupChange*> mGroupChange;
std::vector<RsGxsMsgChange*> mMsgChange;
private:
std::map<uint32_t, std::set<RsGxsGroupId> > mIdsPendingCache;
std::map<uint32_t, std::list<RsGxsGroupId> > mGroupNotPresent;
std::map<RsGxsId, std::list<std::string> > mIdsNotPresent;
RsNetworkExchangeService* mNes;
};
#endif // P3_IDENTITY_SERVICE_HEADER