diff --git a/libretroshare/src/gxs/rsdataservice.cc b/libretroshare/src/gxs/rsdataservice.cc index e6178e6ec..46cb3848d 100644 --- a/libretroshare/src/gxs/rsdataservice.cc +++ b/libretroshare/src/gxs/rsdataservice.cc @@ -128,7 +128,8 @@ const std::string RsGeneralDataService::MSG_META_STATUS = KEY_MSG_STATUS; RsDataService::RsDataService(const std::string &serviceDir, const std::string &dbName, uint16_t serviceType, RsGxsSearchModule *mod) - : RsGeneralDataService(), mServiceDir(serviceDir), mDbName(mServiceDir + "/" + dbName), mServType(serviceType){ + : RsGeneralDataService(), mServiceDir(serviceDir), mDbName(mServiceDir + "/" + dbName), mServType(serviceType), + mDbMutex("RsDataService"){ initialise(); @@ -163,6 +164,8 @@ RsDataService::~RsDataService(){ void RsDataService::initialise(){ + RsStackMutex stack(mDbMutex); + // initialise database mDb = new RetroDb(mDbName, RetroDb::OPEN_READWRITE_CREATE); @@ -417,6 +420,9 @@ RsNxsMsg* RsDataService::getMessage(RetroCursor &c) int RsDataService::storeMessage(std::map &msg) { + + RsStackMutex stack(mDbMutex); + std::map::iterator mit = msg.begin(); // start a transaction @@ -496,6 +502,9 @@ int RsDataService::storeMessage(std::map &msg) int RsDataService::storeGroup(std::map &grp) { + + RsStackMutex stack(mDbMutex); + std::map::iterator sit = grp.begin(); // begin transaction @@ -576,70 +585,72 @@ int RsDataService::storeGroup(std::map &grp) int RsDataService::retrieveNxsGrps(std::map &grp, bool withMeta, bool cache){ - if(grp.empty()){ + if(grp.empty()){ - RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, grpColumns, "", ""); + RsStackMutex stack(mDbMutex); + RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, grpColumns, "", ""); - if(c) - { - std::vector grps; - - retrieveGroups(c, grps); - std::vector::iterator vit = grps.begin(); - - for(; vit != grps.end(); vit++) - { - grp[(*vit)->grpId] = *vit; - } - - delete c; - } - - }else{ - - std::map::iterator mit = grp.begin(); - - for(; mit != grp.end(); mit++) - { - const std::string& grpId = mit->first; - RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, grpColumns, "grpId='" + grpId + "'", ""); - - if(c) - { - std::vector grps; - retrieveGroups(c, grps); - - if(!grps.empty()) - { - RsNxsGrp* ng = grps.front(); - grp[ng->grpId] = ng; - }else{ - grp.erase(grpId); - } - - delete c; - } - } - } - - if(withMeta) + if(c) { - std::map metaMap; - std::map::iterator mit = grp.begin(); - for(; mit != grp.end(); mit++) - metaMap.insert(std::make_pair(mit->first, (RsGxsGrpMetaData*)(NULL))); + std::vector grps; - retrieveGxsGrpMetaData(metaMap); + retrieveGroups(c, grps); + std::vector::iterator vit = grps.begin(); - mit = grp.begin(); - for(; mit != grp.end(); mit++) - { - RsNxsGrp* grpPtr = grp[mit->first]; - grpPtr->metaData = metaMap[mit->first]; - } + for(; vit != grps.end(); vit++) + { + grp[(*vit)->grpId] = *vit; + } + + delete c; } - return 1; + }else{ + + RsStackMutex stack(mDbMutex); + std::map::iterator mit = grp.begin(); + + for(; mit != grp.end(); mit++) + { + const std::string& grpId = mit->first; + RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, grpColumns, "grpId='" + grpId + "'", ""); + + if(c) + { + std::vector grps; + retrieveGroups(c, grps); + + if(!grps.empty()) + { + RsNxsGrp* ng = grps.front(); + grp[ng->grpId] = ng; + }else{ + grp.erase(grpId); + } + + delete c; + } + } + } + + if(withMeta) + { + std::map metaMap; + std::map::iterator mit = grp.begin(); + for(; mit != grp.end(); mit++) + metaMap.insert(std::make_pair(mit->first, (RsGxsGrpMetaData*)(NULL))); + + retrieveGxsGrpMetaData(metaMap); + + mit = grp.begin(); + for(; mit != grp.end(); mit++) + { + RsNxsGrp* grpPtr = grp[mit->first]; + grpPtr->metaData = metaMap[mit->first]; + } + } + + return 1; } void RsDataService::retrieveGroups(RetroCursor* c, std::vector& grps){ @@ -677,6 +688,9 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b std::vector msgSet; if(msgIdV.empty()){ + + RsStackMutex stack(mDbMutex); + RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, msgColumns, KEY_GRP_ID+ "='" + grpId + "'", ""); if(c) @@ -690,6 +704,9 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b for(; sit!=msgIdV.end();sit++){ const std::string& msgId = *sit; + + RsStackMutex stack(mDbMutex); + RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, msgColumns, KEY_GRP_ID+ "='" + grpId + "' AND " + KEY_MSG_ID + "='" + msgId + "'", ""); @@ -721,7 +738,6 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b if(withMeta) { - GxsMsgMetaResult metaResult; // request with meta ids so there is no chance of @@ -791,6 +807,9 @@ void RsDataService::retrieveMessages(RetroCursor *c, std::vector &ms int RsDataService::retrieveGxsMsgMetaData(const GxsMsgReq& reqIds, GxsMsgMetaResult &msgMeta) { + + RsStackMutex stack(mDbMutex); + GxsMsgReq::const_iterator mit = reqIds.begin(); for(; mit != reqIds.end(); mit++) @@ -848,6 +867,7 @@ void RsDataService::retrieveMsgMeta(RetroCursor *c, std::vector& grp) { + RsStackMutex stack(mDbMutex); if(grp.empty()){ @@ -918,6 +938,7 @@ int RsDataService::resetDataStore() std::map::iterator mit = grps.begin(); + // remove all grp msgs files from service dir for(; mit != grps.end(); mit++){ std::string file = mServiceDir + "/" + mit->first; @@ -925,8 +946,11 @@ int RsDataService::resetDataStore() remove(file.c_str()); // remove group file remove(msgFile.c_str()); // and remove messages file } + { + RsStackMutex stack(mDbMutex); + mDb->closeDb(); + } - mDb->closeDb(); remove(mDbName.c_str()); // remove db file // recreate database diff --git a/libretroshare/src/gxs/rsdataservice.h b/libretroshare/src/gxs/rsdataservice.h index bf2496488..4daa24111 100644 --- a/libretroshare/src/gxs/rsdataservice.h +++ b/libretroshare/src/gxs/rsdataservice.h @@ -190,6 +190,8 @@ private: RetroDb* mDb; + RsMutex mDbMutex; + std::list msgColumns; std::list msgMetaColumns; diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 00b0eeb05..f84b2af42 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -688,6 +688,28 @@ void RsGenExchange::publishGrps() mGrpsToPublish.clear(); } + + +uint32_t RsGenExchange::generatePublicToken() +{ + return mDataAccess->generatePublicToken(); +} + +bool RsGenExchange::updatePublicRequestStatus(const uint32_t &token, const uint32_t &status) +{ + return mDataAccess->updatePublicRequestStatus(token, status); +} + +bool RsGenExchange::disposeOfPublicToken(const uint32_t &token) +{ + return mDataAccess->disposeOfPublicToken(token); +} + +RsGeneralDataService* RsGenExchange::getDataStore() +{ + return mDataStore; +} + void RsGenExchange::createDummyGroup(RsGxsGrpItem *grpItem) { diff --git a/libretroshare/src/gxs/rsgenexchange.h b/libretroshare/src/gxs/rsgenexchange.h index 7af1c73ad..dc66499d4 100644 --- a/libretroshare/src/gxs/rsgenexchange.h +++ b/libretroshare/src/gxs/rsgenexchange.h @@ -36,6 +36,7 @@ #include "rsnxsobserver.h" #include "retroshare/rsgxsservice.h" #include "serialiser/rsnxsitems.h" +//#include "rsgixs.h" typedef std::map > GxsMsgDataMap; typedef std::map GxsGroupDataMap; @@ -98,6 +99,12 @@ public: */ void tick(); + /*! + * Any backgroup processing needed by + */ + virtual void service_tick() = 0; + + /*! * * @return handle to token service handle for making @@ -160,6 +167,41 @@ protected: */ void createDummyGroup(RsGxsGrpItem* grpItem); +protected: + + /*! + * Assigns a token value to passed integer + * The status of the token can still be queried from request status feature + * @warning the token space is shared with RsGenExchange backend, so do not + * modify tokens except does you have created by calling generatePublicToken() + * @return token + */ + uint32_t generatePublicToken(); + + /*! + * Updates the status of associate token + * @warning the token space is shared with RsGenExchange backend, so do not + * modify tokens except does you have created by calling generatePublicToken() + * @param token + * @param status + * @return false if token could not be found, true if token disposed of + */ + bool updatePublicRequestStatus(const uint32_t &token, const uint32_t &status); + + /*! + * This gets rid of a publicly issued token + * @param token + * @return false if token could not found, true if token is disposed of + */ + bool disposeOfPublicToken(const uint32_t &token); + + /*! + * This gives access to the data store which hold msgs and groups + * for the service + * @return Data store for retrieving msgs and groups + */ + RsGeneralDataService* getDataStore(); + public: /*! diff --git a/libretroshare/src/gxs/rsgxsdataaccess.h b/libretroshare/src/gxs/rsgxsdataaccess.h index 6642ee6ce..7dca5ac60 100644 --- a/libretroshare/src/gxs/rsgxsdataaccess.h +++ b/libretroshare/src/gxs/rsgxsdataaccess.h @@ -254,7 +254,7 @@ public: uint32_t generatePublicToken(); /*! - * Update + * Updates the status of associate token * @param token * @param status * @return false if token could not be found, true if token disposed of diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 366ce9786..a0b0ebcb2 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -2311,11 +2311,7 @@ int RsServer::StartupRetroShare() // Testing New Cache Services. p3WireVEG *mWire = new p3WireVEG(RS_SERVICE_GXSV1_TYPE_WIRE); pqih -> addService(mWire); - - // Testing New Cache Services. - p3IdServiceVEG *mIdentity = new p3IdServiceVEG(RS_SERVICE_GXSV1_TYPE_IDENTITY); - pqih -> addService(mIdentity); - + // Testing New Cache Services. p3ForumsVEG *mForumsV2 = new p3ForumsVEG(RS_SERVICE_GXSV1_TYPE_FORUMS); pqih -> addService(mForumsV2); @@ -2584,8 +2580,8 @@ int RsServer::StartupRetroShare() rsPhotoV2 = mPhotoV2; #ifdef ENABLE_GXS_SERVICES - // Testing of new cache system interfaces. - rsIdentityVEG = mIdentity; + // Testing of new cache system interfaces. + rsWikiVEG = mWikis; rsWireVEG = mWire; rsForumsVEG = mForumsV2; diff --git a/libretroshare/src/services/p3photoserviceV2.cc b/libretroshare/src/services/p3photoserviceV2.cc index 92d7fa2a1..d208d2a2f 100644 --- a/libretroshare/src/services/p3photoserviceV2.cc +++ b/libretroshare/src/services/p3photoserviceV2.cc @@ -105,6 +105,11 @@ bool p3PhotoServiceV2::updated() return changed; } +void p3PhotoServiceV2::service_tick() +{ + +} + void p3PhotoServiceV2::groupsChanged(std::list& grpIds) diff --git a/libretroshare/src/services/p3photoserviceV2.h b/libretroshare/src/services/p3photoserviceV2.h index a4083a714..0401ad4d7 100644 --- a/libretroshare/src/services/p3photoserviceV2.h +++ b/libretroshare/src/services/p3photoserviceV2.h @@ -43,6 +43,11 @@ public: */ bool updated(); + /*! + * + */ + void service_tick(); + protected: void notifyChanges(std::vector& changes);