documentation of p3GroupDistrib, p3Channels, and CacheStrapper

give a hand at 
http://retroshare.sourceforge.net/wiki/index.php/API_Documentation


git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3279 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
chrisparker126 2010-07-10 16:02:04 +00:00
parent 1787e50b94
commit a25dbf7358
4 changed files with 599 additions and 310 deletions

View File

@ -65,31 +65,39 @@ typedef uint32_t RsPeerId;
typedef std::string RsPeerId; typedef std::string RsPeerId;
/******************************** CacheId ********************************/ /******************************** CacheId ********************************/
/*!
* Use this to identify the type of cache source, strapper,
*/
class CacheId class CacheId
{ {
public: public:
CacheId() :type(0), subid(0) { return; } CacheId() :type(0), subid(0) { return; }
CacheId(uint16_t a, uint16_t b) :type(a), subid(b) { return; } CacheId(uint16_t a, uint16_t b) :type(a), subid(b) { return; }
uint16_t type; uint16_t type; /// cache types, this should be set to services type
uint16_t subid; uint16_t subid; /// should be initialised when using multicache feature of cachestrapper
}; };
bool operator<(const CacheId &a, const CacheId &b); bool operator<(const CacheId &a, const CacheId &b);
/*!
* Use for identifying physical files that have been chosen as cache data
* note: this does not actual store the data but serves to locate on network (via hash attribute,
* and on file via path)
*/
class CacheData class CacheData
{ {
public: public:
RsPeerId pid; RsPeerId pid; /// peer id
std::string pname; /* peer name (can be used by cachestore) */ std::string pname; /// peer name (can be used by cachestore)
CacheId cid; CacheId cid; /// cache id
std::string path; std::string path; /// file system path where physical cache data is located
std::string name; std::string name;
std::string hash; std::string hash;
uint64_t size; uint64_t size;
time_t recvd; time_t recvd; /// received timestamp
}; };
@ -97,29 +105,37 @@ std::ostream &operator<<(std::ostream &out, const CacheData &d);
/***************************** CacheTransfer *****************************/ /***************************** CacheTransfer *****************************/
/**
* Interface for FileTransfer Class to support cache
*/
class CacheTransfer class CacheTransfer
{ {
public: public:
CacheTransfer(CacheStrapper *cs) :strapper(cs) { return; }
virtual ~CacheTransfer() {}
/* upload side of things .... searches through CacheStrapper. */ CacheTransfer(CacheStrapper *cs) :strapper(cs) { return; }
bool FindCacheFile(std::string hash, std::string &path, uint64_t &size); virtual ~CacheTransfer() {}
/*!
* upload side of things .... searches through CacheStrapper.
*/
bool FindCacheFile(std::string hash, std::string &path, uint64_t &size);
/* At the download side RequestCache() => overloaded RequestCacheFile() /*!
* the class should then call CompletedCache() or FailedCache() * At the download side RequestCache() => overloaded RequestCacheFile()
*/ * the class should then call CompletedCache() or FailedCache()
*/
bool RequestCache(CacheData &data, CacheStore *cbStore); /* request from CacheStore */ bool RequestCache(CacheData &data, CacheStore *cbStore); /* request from CacheStore */
protected: protected:
/* to be overloaded */
virtual bool RequestCacheFile(RsPeerId id, std::string path, std::string hash, uint64_t size);
virtual bool CancelCacheFile(RsPeerId id, std::string path, std::string hash, uint64_t size);
bool CompletedCache(std::string hash); /* internal completion -> does cb */ /*!
bool FailedCache(std::string hash); /* internal completion -> does cb */ * to be overloaded
*/
virtual bool RequestCacheFile(RsPeerId id, std::string path, std::string hash, uint64_t size);
virtual bool CancelCacheFile(RsPeerId id, std::string path, std::string hash, uint64_t size);
bool CompletedCache(std::string hash); /* internal completion -> does cb */
bool FailedCache(std::string hash); /* internal completion -> does cb */
private: private:
@ -135,117 +151,181 @@ bool FailedCache(std::string hash); /* internal completion
typedef std::map<uint16_t, CacheData> CacheSet; typedef std::map<uint16_t, CacheData> CacheSet;
/*!
* Implements features needed for a service to act as a cachesource and allow pushing a of cache data from service to strapper
* Service is able to use this class for refresh its cache (push cache data)
* and interface to load and check cache availablility among peers (source of cache data)
* Architecturally Cachestrapper maintains the cachesource (which is passed as a pointer handle) while the cachesource-inheriting
* service can update cachesource as to new cache sources (cache data) created. Equivalently it enquiries through cache source for
* new cache data from peers
* @see p3Distrib
*/
class CacheSource class CacheSource
{ {
public: public:
CacheSource(uint16_t t, bool m, CacheStrapper *cs, std::string cachedir);
virtual ~CacheSource() {}
/* called to determine available cache for peer - CacheSource(uint16_t t, bool m, CacheStrapper *cs, std::string cachedir);
* default acceptable (returns all) virtual ~CacheSource() {}
*/
virtual bool cachesAvailable(RsPeerId pid, std::map<CacheId, CacheData> &ids);
/*! /*!
* function called at startup to load from * called to determine available cache for peer -
* configuration file.... * default acceptable (returns all)
* to be overloaded by inherited class */
*/ virtual bool cachesAvailable(RsPeerId pid, std::map<CacheId, CacheData> &ids);
virtual bool loadLocalCache(const CacheData &data);
/* control Caches available */ /*!
bool refreshCache(const CacheData &data); * function called at startup to load from
bool clearCache(CacheId id); * configuration file....
* to be overloaded by inherited class
*/
virtual bool loadLocalCache(const CacheData &data);
/* get private data */ /* control Caches available */
std::string getCacheDir() { return cacheDir; } bool refreshCache(const CacheData &data);
bool isMultiCache() { return multiCache; } bool clearCache(CacheId id);
uint16_t getCacheType() { return cacheType; }
/* display */ /* get private data */
void listCaches(std::ostream &out); std::string getCacheDir() { return cacheDir; }
bool isMultiCache() { return multiCache; }
uint16_t getCacheType() { return cacheType; }
/* search */ /* display */
bool findCache(std::string hash, CacheData &data) const; void listCaches(std::ostream &out);
/* search */
bool findCache(std::string hash, CacheData &data) const;
protected: protected:
uint16_t cacheType; /* for checking */ uint16_t cacheType; /// for checking of cache type (usually of child class of source)
bool multiCache; /* do we care about subid's */ bool multiCache; /// whether multisource is in use or not.
CacheStrapper *mStrapper; CacheStrapper *mStrapper;
/*** MUTEX LOCKING */ /*** MUTEX LOCKING */
void lockData() const; void lockData() const;
void unlockData() const; void unlockData() const;
CacheSet caches; CacheSet caches; /// all cache data local and remote stored here
private: private:
std::string cacheDir;
mutable RsMutex cMutex;
std::string cacheDir;
mutable RsMutex cMutex;
}; };
/*!
* Base Class for data cache. eg. FileCache/Store.
* @see p3Distrib. pqiMonitor
*/
class CacheStore class CacheStore
{ {
public: public:
CacheStore(uint16_t t, bool m, CacheStrapper *cs, CacheTransfer *cft, std::string cachedir); /*!
virtual ~CacheStore() {} *
* @param t set to particular rs_service id. see rsserviceids.h
* @param m whether this is multicache service (true) or not(false)
* @param cs cache strapper instance responsible for maintaining the cache service
* @param cft cache transfer instance responsible for rquestiing and tranfering caches
* @param cachedir directory used to store cache related info for cachestore client
* @return
*/
CacheStore(uint16_t t, bool m, CacheStrapper *cs, CacheTransfer *cft, std::string cachedir);
virtual ~CacheStore() {}
/* current stored data */ /* current stored data */
bool getStoredCache(CacheData &data); /* use pid/cid in data */
bool getAllStoredCaches(std::list<CacheData> &data); /* use pid/cid in data */
/* input from CacheStrapper -> store can then download new data */ /*!
void availableCache(const CacheData &data); *
* @param data returns cache data for pid/cid set in data itself
* @return false is unsuccessful and vice versa
*/
bool getStoredCache(CacheData &data); /* use pid/cid in data */
/* called when the download is completed ... updates internal data */ /*!
void downloadedCache(const CacheData &data); *
* @param data all cache store by cachestore is store here
* @return false not returned, only true at the moment
*/
bool getAllStoredCaches(std::list<CacheData> &data); /* use pid/cid in data */
/* called if the download fails */ /*!
void failedCache(const CacheData &data); * input from CacheStrapper -> store can then download new data
*/
void availableCache(const CacheData &data);
/* virtual functions overloaded by cache implementor */ /*!
virtual bool fetchCache(const CacheData &data); /* a question? */ * should be called when the download is completed ... cache data is loaded
virtual int nameCache(CacheData &data); /* fill in the name/path */ */
virtual int loadCache(const CacheData &data); /* actual load, once data available */ void downloadedCache(const CacheData &data);
/* get private data */ /*!
std::string getCacheDir() { return cacheDir; } * called if the download fails, TODO: nothing done yet
bool isMultiCache() { return multiCache; } */
uint16_t getCacheType() { return cacheType; } void failedCache(const CacheData &data);
/* display */ /* virtual functions overloaded by cache implementor */
void listCaches(std::ostream &out);
/*!
* @param data cache data is stored here
* @return false is failed (cache does not exist), otherwise true
*/
virtual bool fetchCache(const CacheData &data); /* a question? */
virtual int nameCache(CacheData &data); /* fill in the name/path */
virtual int loadCache(const CacheData &data); /* actual load, once data available */
/* get private data */
std::string getCacheDir() { return cacheDir; }
bool isMultiCache() { return multiCache; }
uint16_t getCacheType() { return cacheType; }
/*!
* display, e.g. can pass std::out, cerr, ofstream, etc
*/
void listCaches(std::ostream &out);
protected: protected:
/*** MUTEX LOCKING */
void lockData() const;
void unlockData() const;
/* This function is called to store Cache Entry in the CacheStore Table. /*!
* it must be called from within a Mutex Lock.... * ** MUTEX LOCKING
* */
* It doesn't lock itself -> to avoid race conditions void lockData() const;
*/
void locked_storeCacheEntry(const CacheData &data);
bool locked_getStoredCache(CacheData &data);
private: /*!
* ** MUTEX LOCKING
*/
void unlockData() const;
uint16_t cacheType; /* for checking */ /*! This function is called to store Cache Entry in the CacheStore Table.
bool multiCache; /* do we care about subid's */ * it must be called from within a Mutex Lock....
*
* It doesn't lock itself -> to avoid race conditions
*/
void locked_storeCacheEntry(const CacheData &data);
CacheStrapper *mStrapper; /*! This function is called to store Cache Entry in the CacheStore Table.
CacheTransfer *cacheTransfer; * it must be called from within a Mutex Lock....
*
* It doesn't lock itself -> to avoid race conditions
*/
bool locked_getStoredCache(CacheData &data);
std::string cacheDir; private:
mutable RsMutex cMutex; uint16_t cacheType; /* for checking */
bool multiCache; /* do we care about subid's */
std::map<RsPeerId, CacheSet> caches; CacheStrapper *mStrapper;
CacheTransfer *cacheTransfer;
std::string cacheDir;
mutable RsMutex cMutex;
std::map<RsPeerId, CacheSet> caches;
}; };
@ -254,28 +334,52 @@ bool locked_getStoredCache(CacheData &data);
/***************************** CacheStrapper *****************************/ /***************************** CacheStrapper *****************************/
/* Make Sure you get the Ids right! */ /*!
* a convenient to pass cache handles to cachestrapper to maintain the cache service
* Make Sure you get the Ids right! see rsservicesids.h.
* When creating a cache service this data structure is
* source, usually the child class of store and source also serves as both handles
* @see CacheStrapper
*/
class CachePair class CachePair
{ {
public: public:
CachePair()
:source(NULL), store(NULL), id(0, 0) { return; }
CachePair(CacheSource *a, CacheStore *b, CacheId c) /*!
:source(a), store(b), id(c) { return; } * Default constructor, all variables set to NULL
*/
CachePair()
:source(NULL), store(NULL), id(0, 0) { return; }
CacheSource *source; /*!
CacheStore *store; *
CacheId id; * @param a cache source for service
* @param b cache store for service
* @param c the cache service id, c.type should be set to service id service-child class of store and source
*/
CachePair(CacheSource *a, CacheStore *b, CacheId c)
:source(a), store(b), id(c) { return; }
CacheSource *source;
CacheStore *store;
CacheId id; /// should be set id type to service types of store and source service-child class, and subid for multicache use
}; };
bool operator<(const CachePair &a, const CachePair &b); bool operator<(const CachePair &a, const CachePair &b);
/*!
* CacheStrapper: maintains a set of CacheSources, and CacheStores,
* queries and updates as new information arrives.
*/
class CacheStrapper: public pqiMonitor, public p3Config class CacheStrapper: public pqiMonitor, public p3Config
{ {
public: public:
/*!
* @param cm handle used by strapper for getting peer connection information (online peers, sslids...)
* @return
*/
CacheStrapper(p3ConnectMgr *cm); CacheStrapper(p3ConnectMgr *cm);
virtual ~CacheStrapper() { return; } virtual ~CacheStrapper() { return; }
@ -284,12 +388,30 @@ virtual void statusChange(const std::list<pqipeer> &plist);
/************* from pqiMonitor *******************/ /************* from pqiMonitor *******************/
/* Feedback from CacheSources */ /* Feedback from CacheSources */
/*!
* send data to peers online and selfe
* @param data
*
*/
void refreshCache(const CacheData &data); void refreshCache(const CacheData &data);
/*!
* forces config savelist
* @param data
* @see saveList()
*/
void refreshCacheStore(const CacheData &data); void refreshCacheStore(const CacheData &data);
/* list of Caches to send out */ /*!
* list of Caches to send out
*/
bool getCacheUpdates(std::list<std::pair<RsPeerId, CacheData> > &updates); bool getCacheUpdates(std::list<std::pair<RsPeerId, CacheData> > &updates);
/*!
* add to strapper's cachepair set so a related service's store and source can be maintained
* @param pair the source and store handle for a service
*/
void addCachePair(CachePair pair); void addCachePair(CachePair pair);
/*** I/O (2) ***/ /*** I/O (2) ***/
@ -297,11 +419,20 @@ void recvCacheResponse(CacheData &data, time_t ts);
void handleCacheQuery(RsPeerId id, std::map<CacheId, CacheData> &data); void handleCacheQuery(RsPeerId id, std::map<CacheId, CacheData> &data);
/* search through CacheSources. */ /*!
* search through CacheSources.
* @return false if cachedate mapping to hash not found
*/
bool findCache(std::string hash, CacheData &data) const; bool findCache(std::string hash, CacheData &data) const;
/* display */ /* display */
void listCaches(std::ostream &out); void listCaches(std::ostream &out);
/*!
* does not do anything
* @param out
* @deprecated
*/
void listPeerStatus(std::ostream &out); void listPeerStatus(std::ostream &out);
/** /**

View File

@ -37,7 +37,7 @@
//! Channels is a distributed 'feed' service //! Channels is a distributed 'feed' service
/*! /*!
* Implementations of rschannels interface * Implementations of rschannels interface
* @see rsChannels for definition of implemented interface * @see RsChannels for definition of implemented interface
*/ */
class p3Channels: public p3GroupDistrib, public RsChannels class p3Channels: public p3GroupDistrib, public RsChannels
{ {

View File

@ -1097,7 +1097,7 @@ bool p3GroupDistrib::getPublishGroupList(std::list<std::string> &grpids)
return true; return true;
} }
bool p3GroupDistrib::getPopularGroupList(uint32_t popMin, uint32_t popMax, std::list<std::string> &grpids) void p3GroupDistrib::getPopularGroupList(uint32_t popMin, uint32_t popMax, std::list<std::string> &grpids)
{ {
RsStackMutex stack(distribMtx); /************* STACK MUTEX ************/ RsStackMutex stack(distribMtx); /************* STACK MUTEX ************/
std::map<std::string, GroupInfo>::iterator git; std::map<std::string, GroupInfo>::iterator git;
@ -1109,7 +1109,7 @@ bool p3GroupDistrib::getPopularGroupList(uint32_t popMin, uint32_t popMax, std::
grpids.push_back(git->first); grpids.push_back(git->first);
} }
} }
return true; return;
} }

View File

@ -31,7 +31,7 @@
#include "pqi/p3cfgmgr.h" #include "pqi/p3cfgmgr.h"
#include "services/p3service.h" #include "services/p3service.h"
#include "dbase/cachestrapper.h" #include "dbase/cachestrapper.h"
#include "serialiser/rsforumitems.h" #include "serialiser/rsdistribitems.h"
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <openssl/evp.h> #include <openssl/evp.h>
@ -90,23 +90,29 @@ const uint32_t GROUP_KEY_DISTRIB_ADMIN = 0x0040;
#endif #endif
//! key to be sent member or members of a groups //! for storing group keys to members of a group
/*! /*!
* This key but be of many types, including private/public publish key, or admin prite key for group * This key but be of many types, including private/public publish key, or admin prite key for group
* @see p3GroupDistrib
*/ */
class GroupKey class GroupKey
{ {
public: public:
GroupKey() GroupKey()
:type(0), startTS(0), endTS(0), key(NULL) { return; } :type(0), startTS(0), endTS(0), key(NULL) { return; }
uint32_t type; uint32_t type; /// whether key is full or public
std::string keyId; std::string keyId;
time_t startTS, endTS; time_t startTS, endTS;
EVP_PKEY *key; /// public key EVP_PKEY *key; /// actual group key in evp format
}; };
//! used to store group picture
/*!
* ensures use of png image format
* @see p3GroupDistrib
*/
class GroupIcon{ class GroupIcon{
public: public:
GroupIcon(): pngImageData(NULL), imageSize(0) { GroupIcon(): pngImageData(NULL), imageSize(0) {
@ -121,64 +127,67 @@ public:
return; return;
} }
unsigned char* pngImageData; unsigned char* pngImageData; /// pointer to image data in png format
int imageSize; int imageSize;
}; };
//! aggregates various information on group activities (i.e. messages, posts, etc) //! used by p3groupDistrib to store mirror info found in rsDistribGroup (i.e. messages, posts, etc)
/*! /*!
* The aim is to use this to keep track of group changes, so client can respond (get messages, post etc) * used by p3Groudistrib to store group info, also used to communicate group information
* to p3groupdistrib inherited classes. contain
* @see rsDistribGroup
*/ */
class GroupInfo class GroupInfo
{ {
public: public:
GroupInfo()
:distribGroup(NULL), grpFlags(0), pop(0), lastPost(0), flags(0), grpChanged(false)
{
return;
}
std::string grpId; /// the group id GroupInfo()
RsDistribGrp *distribGroup; /// item which contains further information on group :distribGroup(NULL), grpFlags(0), pop(0), lastPost(0), flags(0), grpChanged(false)
{
return;
}
std::list<std::string> sources; std::string grpId; /// the group id
std::map<std::string, RsDistribMsg *> msgs; RsDistribGrp *distribGroup; /// item which contains further information on group
/***********************************/ std::list<std::string> sources;
std::map<std::string, RsDistribMsg *> msgs;
/* Copied from DistribGrp */ /***********************************/
std::wstring grpName;
std::wstring grpDesc; /* Copied from DistribGrp */
std::wstring grpCategory; std::wstring grpName;
uint32_t grpFlags; /* PRIVACY & AUTHEN */ std::wstring grpDesc; /// group description
std::wstring grpCategory;
uint32_t grpFlags; /// PRIVACY & AUTHENTICATION
uint32_t pop; /// sources.size() uint32_t pop; /// popularity sources.size()
time_t lastPost; /// modded as msgs added time_t lastPost; /// modded as msgs added
/***********************************/ /***********************************/
uint32_t flags; /// PUBLISH, SUBSCRIBE, ADMIN uint32_t flags; /// PUBLISH, SUBSCRIBE, ADMIN
std::string publishKeyId; /// current active Publish Key std::string publishKeyId; /// current active Publish Key
std::map<std::string, GroupKey> publishKeys; std::map<std::string, GroupKey> publishKeys;
GroupKey adminKey; GroupKey adminKey;
GroupIcon grpIcon; GroupIcon grpIcon;
/* NOT USED YET */ /* NOT USED YET */
std::map<std::string, RsDistribMsg* > decrypted_msg_cache; /// stores a cache of messages that have been decrypted std::map<std::string, RsDistribMsg* > decrypted_msg_cache; /// stores a cache of messages that have been decrypted
bool publisher, allowAnon, allowUnknown; bool publisher, allowAnon, allowUnknown;
bool subscribed, listener; bool subscribed, listener;
uint32_t type; uint32_t type;
/// FLAG for Client - set if changed /// FLAG for Client - set if changed
bool grpChanged; bool grpChanged;
}; };
@ -193,9 +202,9 @@ class GroupCache
{ {
public: public:
std::string filename; std::string filename;
time_t start, end; time_t start, end;
uint16_t cacheSubId; /// used to resolve complete cache id uint16_t cacheSubId; /// used to resolve complete cache id
}; };
/* Flags for locked_notifyGroupChanged() ***/ /* Flags for locked_notifyGroupChanged() ***/
@ -208,7 +217,7 @@ const uint32_t GRP_SUBSCRIBED = 0x0005;
const uint32_t GRP_UNSUBSCRIBED = 0x0006; const uint32_t GRP_UNSUBSCRIBED = 0x0006;
//! Class service to implement group messages //! Cache based service to implement group messaging
/*! /*!
* *
* Group Description: * Group Description:
@ -218,9 +227,8 @@ const uint32_t GRP_UNSUBSCRIBED = 0x0006;
* Filter Lists. * Filter Lists.
* Publish Keys. * Publish Keys.
* *
* Publish Keys. (multiple possible) * Publish Keys.
* Filters: blacklist or whitelist. * TimeStore Length determined by inheriting class
* TimeStore Length ??? (could make it a minimum of this and system one)
* *
* Everyone gets: * Everyone gets:
* Master Public Key. * Master Public Key.
@ -236,17 +244,16 @@ const uint32_t GRP_UNSUBSCRIBED = 0x0006;
* *
* Group id is the public admin keys id * Group id is the public admin keys id
* *
* Create a Signing structure for Messages in general.
*/ */
class p3GroupDistrib: public CacheSource, public CacheStore, public p3Config, public p3Service class p3GroupDistrib: public CacheSource, public CacheStore, public p3Config, public p3Service
{ {
public: public:
p3GroupDistrib(uint16_t subtype, p3GroupDistrib(uint16_t subtype,
CacheStrapper *cs, CacheTransfer *cft, CacheStrapper *cs, CacheTransfer *cft,
std::string sourcedir, std::string storedir, std::string keyBackUpDir, std::string sourcedir, std::string storedir, std::string keyBackUpDir,
uint32_t configId, uint32_t configId,
uint32_t storePeriod, uint32_t pubPeriod); uint32_t storePeriod, uint32_t pubPeriod);
/***************************************************************************************/ /***************************************************************************************/
@ -255,24 +262,45 @@ class p3GroupDistrib: public CacheSource, public CacheStore, public p3Config, pu
/* TO FINISH */ /* TO FINISH */
public: public:
virtual bool loadLocalCache(const CacheData &data); /// overloaded from Cache Source
virtual int loadCache(const CacheData &data); /// overloaded from Cache Store virtual bool loadLocalCache(const CacheData &data); /// overloaded from Cache Source
virtual int loadCache(const CacheData &data); /// overloaded from Cache Store
private: private:
/* top level load */
int loadAnyCache(const CacheData &data, bool local);
/* load cache files */ /* top level load */
void loadFileGroups(std::string filename, std::string src, bool local); int loadAnyCache(const CacheData &data, bool local);
void loadFileMsgs(std::string filename, uint16_t cacheSubId, std::string src, uint32_t ts, bool local);
bool backUpKeys(const std::list<RsDistribGrpKey* > &keysToBackUp, std::string grpId); /* load cache files */
void locked_sharePubKey(); void loadFileGroups(std::string filename, std::string src, bool local);
void loadFileMsgs(std::string filename, uint16_t cacheSubId, std::string src, uint32_t ts, bool local);
bool backUpKeys(const std::list<RsDistribGrpKey* > &keysToBackUp, std::string grpId);
void locked_sharePubKey();
protected: protected:
/* load cache msgs */
void loadMsg(RsDistribSignedMsg *msg, std::string src, bool local); /* load cache msgs */
void loadGroup(RsDistribGrp *newGrp);
void loadGroupKey(RsDistribGrpKey *newKey); /*!
* msg is loaded to its group and republished,
* msg decrypted if grp is private
* @param msg msg to loaded
* @param src src of msg (peer id)
* @param local is this a local cache msg (your msg)
*/
void loadMsg(RsDistribSignedMsg *msg, std::string src, bool local);
/*!
* adds newgrp to grp set, GroupInfo type created and stored
* @param newGrp grp to be added
*/
void loadGroup(RsDistribGrp *newGrp);
/*!
* Adds new keys dependent on whether it is an admin or publish key
* @param newKey key to be added
*/
void loadGroupKey(RsDistribGrpKey *newKey);
/***************************************************************************************/ /***************************************************************************************/
@ -284,53 +312,101 @@ void loadGroupKey(RsDistribGrpKey *newKey);
/* TO FINISH */ /* TO FINISH */
public: public:
std::string createGroup(std::wstring name, std::wstring desc, uint32_t flags, unsigned char*, uint32_t imageSize);
//std::string modGroupDescription(std::string grpId, std::string discription);
//std::string modGroupIcon(std::string grpId, PIXMAP *icon);
std::string publishMsg(RsDistribMsg *msg, bool personalSign); /*!
* This create a distributed grp which is sent via cache system to connected peers
* @param name name of the group created
* @param desc description of the group
* @param flags privacy flag
* @param pngImageData pointer to image data, data is copied
* @param imageSize size of the image passed
* @return id of the group
*/
std::string createGroup(std::wstring name, std::wstring desc, uint32_t flags, unsigned char *pngImageData, uint32_t imageSize);
bool subscribeToGroup(std::string grpId, bool subscribe); /*!
* msg is packed into a signed message (and encrypted msg grp is private) and then sent via cache system to connnected peers
* @param msg
* @param personalSign whether to personal to sign image (this is done using gpg cert)
* @return the msg id
*/
std::string publishMsg(RsDistribMsg *msg, bool personalSign);
/*!
*
* @param grpId id of group to subscribe to
* @param subscribe true to subscribe and vice versa
* @return
*/
bool subscribeToGroup(std::string grpId, bool subscribe);
/***************************************************************************************/ /***************************************************************************************/
/***************************************************************************************/ /***************************************************************************************/
/***************************************************************************************/
/****************************** Access Content ***************************************/
/***************************************************************************************/
/***************************************************************************************/
/****************************** Access Content ***************************************/
/***************************************************************************************/
public: public:
/* get Group Lists */ /*!
bool getAllGroupList(std::list<std::string> &grpids); * get Group Lists
bool getSubscribedGroupList(std::list<std::string> &grpids); */
bool getPublishGroupList(std::list<std::string> &grpids); bool getAllGroupList(std::list<std::string> &grpids);
bool getPopularGroupList(uint32_t popMin, uint32_t popMax, std::list<std::string> &grpids); bool getSubscribedGroupList(std::list<std::string> &grpids);
bool getPublishGroupList(std::list<std::string> &grpids);
/*!
*
* @param popMin lower limit for a grp's populairty in grpids
* @param popMax upper limit for a grp's popularity in grpids
* @param grpids grpids of grps which adhere to upper and lower limit of popularity
* @return nothing returned
*/
void getPopularGroupList(uint32_t popMin, uint32_t popMax, std::list<std::string> &grpids);
/* get Msg Lists */ /* get Msg Lists */
bool getAllMsgList(std::string grpId, std::list<std::string> &msgIds); bool getAllMsgList(std::string grpId, std::list<std::string> &msgIds);
bool getParentMsgList(std::string grpId, std::string pId, std::list<std::string> &msgIds); bool getParentMsgList(std::string grpId, std::string pId, std::list<std::string> &msgIds);
bool getTimePeriodMsgList(std::string grpId, uint32_t timeMin, bool getTimePeriodMsgList(std::string grpId, uint32_t timeMin,
uint32_t timeMax, std::list<std::string> &msgIds); uint32_t timeMax, std::list<std::string> &msgIds);
GroupInfo *locked_getGroupInfo(std::string grpId); GroupInfo *locked_getGroupInfo(std::string grpId);
RsDistribMsg *locked_getGroupMsg(std::string grpId, std::string msgId); RsDistribMsg *locked_getGroupMsg(std::string grpId, std::string msgId);
/* Filter Messages */ /* Filter Messages */
/***************************************************************************************/ /***************************************************************************************/
/***************************** Event Feedback ******************************************/ /***************************** Event Feedback ******************************************/
/***************************************************************************************/ /***************************************************************************************/
protected: protected:
/*! /*!
* root version (p3Distrib::) of this function must be called * root version (p3Distrib::) of this function must be called
**/ */
virtual void locked_notifyGroupChanged(GroupInfo &info, uint32_t flags); virtual void locked_notifyGroupChanged(GroupInfo &info, uint32_t flags);
virtual bool locked_eventDuplicateMsg(GroupInfo *, RsDistribMsg *, std::string id) = 0;
virtual bool locked_eventNewMsg(GroupInfo *, RsDistribMsg *, std::string id) = 0; /*!
* client (inheriting class) should use this to determing behaviour of
* their service when a duplicate msg is found
* @param group should be called when duplicate message loaded
* @param the duplicate message
* @param id
* @return successfully executed or not
*/
virtual bool locked_eventDuplicateMsg(GroupInfo *, RsDistribMsg *, std::string id) = 0;
/*!
* Inheriting class should implement this as a response to a new msg arriving
* @param
* @param
* @param id src of msg (peer id)
* @return
*/
virtual bool locked_eventNewMsg(GroupInfo *, RsDistribMsg *, std::string id) = 0;
/***************************************************************************************/ /***************************************************************************************/
/********************************* p3Config ********************************************/ /********************************* p3Config ********************************************/
@ -339,16 +415,17 @@ virtual bool locked_eventNewMsg(GroupInfo *, RsDistribMsg *, std::string id) = 0
protected: protected:
virtual RsSerialiser *setupSerialiser(); virtual RsSerialiser *setupSerialiser();
virtual std::list<RsItem *> saveList(bool &cleanup); virtual std::list<RsItem *> saveList(bool &cleanup);
virtual void saveDone(); virtual void saveDone();
virtual bool loadList(std::list<RsItem *> load); virtual bool loadList(std::list<RsItem *> load);
/***************************************************************************************/ /***************************************************************************************/
/***************************************************************************************/ /***************************************************************************************/
public: public:
virtual int tick(); /* overloaded form pqiService */
virtual int tick(); /* overloaded form pqiService */
/***************************************************************************************/ /***************************************************************************************/
/**************************** Publish Content ******************************************/ /**************************** Publish Content ******************************************/
@ -356,91 +433,171 @@ virtual int tick(); /* overloaded form pqiService */
/* TO FINISH */ /* TO FINISH */
protected: protected:
/* create/mod cache content */ /* create/mod cache content */
void locked_toPublishMsg(RsDistribSignedMsg *msg);
void publishPendingMsgs();
void publishDistribGroups();
void clear_local_caches(time_t now);
void locked_publishPendingMsgs(); /*!
uint16_t locked_determineCacheSubId(); * adds msg to pending msg map
* @param msg a signed message by peer
*/
void locked_toPublishMsg(RsDistribSignedMsg *msg);
/** /*!
* @param grpId the grpId id for which backup keys should be restored * adds pending msg
* @return false if failed and vice versa */
*/ void publishPendingMsgs();
virtual bool restoreGrpKeys(std::string grpId); /// restores a group keys from backup
/** /*!
* @param grpId the group for which to share public keys * sends created groups to cache, to be passed to cache listeners
* @param peers The peers to which public keys should be sent */
*/ void publishDistribGroups();
virtual bool sharePubKey(std::string grpId, std::list<std::string>& peers);
/** /*!
* attempts to receive publication keys * removes old caches based on store period (anything that has been in local cache longer
*/ * than the store period is deleted
virtual void locked_receivePubKeys(); * @param now the current time when method is called
*/
void clear_local_caches(time_t now);
/** /*!
* this load received pub keys, useful in the case that publish keys have been received * assumes RsDistribMtx is locked when call is made
* but group info hasn't */
*/ void locked_publishPendingMsgs();
virtual void locked_loadRecvdPubKeys();
/** /*!
* Allows group admin(s) to change group icon, description and name * @return cache sub id
*@param grpId group id */
*@param gi the changes to grp name, icon, and description should be reflected here uint16_t locked_determineCacheSubId();
*/
virtual bool locked_editGroup(std::string grpId, GroupInfo& gi);
/** /**
* encrypts data using envelope encryption (taken from open ssl's evp_sealinit ) * grp keys are backed up when a grp is created this allows user to retrieve lost keys in case config saving fails
* only full publish key holders for can encrypt data for given group * @param grpId the grpId id for which backup keys should be restored
*@param out * @return false if failed and vice versa
*@param outlen */
*@param in virtual bool restoreGrpKeys(std::string grpId); /// restores a group keys from backup
*@param inlen
*/ /**
virtual bool encrypt(void *&out, int &outlen, const void *in, int inlen, std::string grpId); * Allows user to send keys to a list of peers
* @param grpId the group for which to share public keys
* @param peers The peers to which public keys should be sent
*/
virtual bool sharePubKey(std::string grpId, std::list<std::string>& peers);
/**
* Attempt to receive publication keys
*/
virtual void locked_receivePubKeys();
/**
* This loads received pub keys
*
*/
virtual void locked_loadRecvdPubKeys();
/**
* Allows group admin(s) to change group icon, description and name
*@param grpId group id
*@param gi the changes to grp name, icon, and description should be reflected here
*/
virtual bool locked_editGroup(std::string grpId, GroupInfo& gi);
/**
* Encrypts data using envelope encryption (taken from open ssl's evp_sealinit )
* only full publish key holders can encrypt data for given group
*@param out
*@param outlen
*@param in
*@param inlen
*/
virtual bool encrypt(void *&out, int &outlen, const void *in, int inlen, std::string grpId);
/** /**
* decrypts data using evelope decryption (taken from open ssl's evp_sealinit ) * Decrypts data using evelope decryption (taken from open ssl's evp_sealinit )
* only full publish key holders can decrypt data for a group * only full publish key holders can decrypt data for a group
*@param out where decrypted data is written to *@param out where decrypted data is written to
*@param outlen *@param outlen
*@param in *@param in
*@param inlen *@param inlen
*/ */
virtual bool decrypt(void *&out, int &outlen, const void *in, int inlen, std::string grpId); virtual bool decrypt(void *&out, int &outlen, const void *in, int inlen, std::string grpId);
/***************************************************************************************/ /***************************************************************************************/
/***************************************************************************************/ /***************************************************************************************/
/***************************************************************************************/ /***************************************************************************************/
/*************************** Overloaded Functions **************************************/ /*************************** Overloaded Functions **************************************/
/***************************************************************************************/ /***************************************************************************************/
/* Overloaded by inherited classes to Pack/UnPack their messages */ /*!
virtual RsSerialType *createSerialiser() = 0; * Overloaded by inherited classes to Pack/UnPack their messages
* @return inherited class's serialiser
*/
virtual RsSerialType *createSerialiser() = 0;
/* Used to Create/Load Cache Files only */ /*! Used to Create/Load Cache Files only
virtual pqistore *createStore(BinInterface *bio, std::string src, uint32_t bioflags); * @param bio binary i/o
* @param src peer id from which write/read content originates
* @param bioflags read write permision for bio
* @return pointer to pqistore instance
*/
virtual pqistore *createStore(BinInterface *bio, std::string src, uint32_t bioflags);
virtual bool validateDistribGrp(RsDistribGrp *newGrp); /*!
virtual bool locked_checkGroupInfo(GroupInfo &info, RsDistribGrp *newGrp); * checks to see if admin signature is valid
virtual bool locked_updateGroupInfo(GroupInfo &info, RsDistribGrp *newGrp); * @param newGrp grp to validate
virtual bool locked_checkGroupKeys(GroupInfo &info); * @return true if group's signature is valid
virtual bool locked_updateGroupAdminKey(GroupInfo &info, RsDistribGrpKey *newKey); */
virtual bool locked_updateGroupPublishKey(GroupInfo &info, RsDistribGrpKey *newKey); virtual bool validateDistribGrp(RsDistribGrp *newGrp);
virtual bool locked_checkGroupInfo(GroupInfo &info, RsDistribGrp *newGrp);
virtual bool locked_updateGroupInfo(GroupInfo &info, RsDistribGrp *newGrp);
virtual bool locked_checkGroupKeys(GroupInfo &info);
/*!
* @param info group for which admin key will be added to
* @param newKey admin key
* @return true if key successfully added
*/
virtual bool locked_updateGroupAdminKey(GroupInfo &info, RsDistribGrpKey *newKey);
virtual bool locked_validateDistribSignedMsg(GroupInfo &info, RsDistribSignedMsg *msg); /*!
virtual RsDistribMsg* unpackDistribSignedMsg(RsDistribSignedMsg *newMsg); * @param info group for which publish key will be added to
virtual bool locked_checkDistribMsg(GroupInfo &info, RsDistribMsg *msg); * @param newKey publish key
virtual bool locked_choosePublishKey(GroupInfo &info); * @return true if publish key successfully added
*/
virtual bool locked_updateGroupPublishKey(GroupInfo &info, RsDistribGrpKey *newKey);
/*!
* uses groupinfo public key to verify signature of signed message
* @param info groupinfo for which msg is meant for
* @param msg
* @return false if verfication of signature is not passed
*/
virtual bool locked_validateDistribSignedMsg(GroupInfo &info, RsDistribSignedMsg *msg);
/*!
* Use this to retrieve packed message from a signed message
* @param newMsg signed message
* @return pointer to unpacked msg
*/
virtual RsDistribMsg* unpackDistribSignedMsg(RsDistribSignedMsg *newMsg);
/*!
* message is checked to see if it is in a valid time range
* @param info
* @param msg message to be checked
* @return false if msg is outside correct time range
*/
virtual bool locked_checkDistribMsg(GroupInfo &info, RsDistribMsg *msg);
/*!
* chooses the best publish key based on it being full and latest
* @param info group to choose publish key
* @return true if a publish key could be found
*/
virtual bool locked_choosePublishKey(GroupInfo &info);
//virtual RsDistribGrp *locked_createPublicDistribGrp(GroupInfo &info); //virtual RsDistribGrp *locked_createPublicDistribGrp(GroupInfo &info);
@ -451,9 +608,10 @@ virtual bool locked_choosePublishKey(GroupInfo &info);
/***************************** Utility Functions ***************************************/ /***************************** Utility Functions ***************************************/
/***************************************************************************************/ /***************************************************************************************/
/* TO FINISH */ /* TO FINISH */
/* utilities */
std::string HashRsItem(const RsItem *item); /* utilities */
bool locked_updateChildTS(GroupInfo &gi, RsDistribMsg *msg); std::string HashRsItem(const RsItem *item);
bool locked_updateChildTS(GroupInfo &gi, RsDistribMsg *msg);
/***************************************************************************************/ /***************************************************************************************/
/***************************************************************************************/ /***************************************************************************************/
@ -463,11 +621,12 @@ bool locked_updateChildTS(GroupInfo &gi, RsDistribMsg *msg);
/***************************************************************************************/ /***************************************************************************************/
public: public:
void printGroups(std::ostream &out); void printGroups(std::ostream &out);
/*!
* returns list of ids for group caches that have changed /*!
*/ * returns list of ids for group caches that have changed
bool groupsChanged(std::list<std::string> &groupIds); */
bool groupsChanged(std::list<std::string> &groupIds);
/***************************************************************************************/ /***************************************************************************************/
/***************************************************************************************/ /***************************************************************************************/
@ -478,32 +637,31 @@ bool groupsChanged(std::list<std::string> &groupIds);
/* storage */ /* storage */
protected: protected:
RsMutex distribMtx; /// Protects All Data Below RsMutex distribMtx; /// Protects all class atrributes
std::string mOwnId; std::string mOwnId; /// rs peer id
private: private:
std::list<GroupCache> mLocalCaches; std::list<GroupCache> mLocalCaches;
std::map<std::string, GroupInfo> mGroups; std::map<std::string, GroupInfo> mGroups;
uint32_t mStorePeriod, mPubPeriod; uint32_t mStorePeriod, mPubPeriod;
/* Message Publishing */ /* Message Publishing */
std::list<RsDistribSignedMsg *> mPendingPublish; std::list<RsDistribSignedMsg *> mPendingPublish;
time_t mLastPublishTime; time_t mLastPublishTime;
std::map<uint32_t, uint16_t> mLocalCacheTs; std::map<uint32_t, uint16_t> mLocalCacheTs;
uint16_t mMaxCacheSubId; uint16_t mMaxCacheSubId;
bool mGroupsChanged; bool mGroupsChanged;
bool mGroupsRepublish; bool mGroupsRepublish;
std::list<RsItem *> saveCleanupList; /* TEMPORARY LIST WHEN SAVING */ std::list<RsItem *> saveCleanupList; /* TEMPORARY LIST WHEN SAVING */
std::string mKeyBackUpDir; std::string mKeyBackUpDir;
const std::string BACKUP_KEY_FILE; const std::string BACKUP_KEY_FILE;
std::map<std::string, RsDistribGrpKey* > mRecvdPubKeys; /// full publishing keys received from users
std::map<std::string, std::list<std::string> > mPendingPubKeyRecipients; /// peers to receive publics key for a given grp
time_t mLastKeyPublishTime, mLastRecvdKeyTime;
std::map<std::string, RsDistribGrpKey* > mRecvdPubKeys; /// full publishing keys received from users
std::map<std::string, std::list<std::string> > mPendingPubKeyRecipients; /// peers to receive publics key for a given grp
time_t mLastKeyPublishTime, mLastRecvdKeyTime;
}; };