diff --git a/libretroshare/src/services/p3distrib.cc b/libretroshare/src/services/p3distrib.cc index cec13b62a..7297e5835 100644 --- a/libretroshare/src/services/p3distrib.cc +++ b/libretroshare/src/services/p3distrib.cc @@ -123,9 +123,12 @@ int p3GroupDistrib::tick() mGroupsRepublish = false; } + bool attemptRecv = false; + { RsStackMutex stack(distribMtx); - toPublish = (mPendingPubKeyRecipients.size() > 0) && (now > (time_t) (mPubPeriod + mLastKeyPublishTime)); + toPublish = (mPendingPubKeyRecipients.size() > 0) && (now > (time_t) (mPubPeriod + mLastKeyPublishTime)); + attemptRecv = (mRecvdPubKeys.size() > 0) && (now > (time_t) (mPubPeriod + mLastKeyPublishTime)); } if(toPublish){ @@ -133,6 +136,12 @@ int p3GroupDistrib::tick() locked_sharePubKey(); } + + if(attemptRecv) + { + attemptPublishKeysRecvd(); + } + bool toReceive = receivedItems(); if(toReceive){ @@ -485,7 +494,7 @@ void p3GroupDistrib::loadGroup(RsDistribGrp *newGrp) } -void p3GroupDistrib::loadGroupKey(RsDistribGrpKey *newKey) +bool p3GroupDistrib::loadGroupKey(RsDistribGrpKey *newKey) { /* load Key */ const std::string &gid = newKey -> grpId; @@ -521,13 +530,14 @@ void p3GroupDistrib::loadGroupKey(RsDistribGrpKey *newKey) // make sure key does not exist if(mRecvdPubKeys.find(gid) == mRecvdPubKeys.end()){ mRecvdPubKeys.insert(std::pair(gid, newKey)); - mPubKeyAvailableGrpId.push_back(gid); + return true; }else{ #ifdef DISTRIB_DEBUG std::cerr << "p3GroupDistrib::loadGroupKey() Key already received; discarding"; std::cerr << std::endl; #endif delete newKey; + } } @@ -536,11 +546,13 @@ void p3GroupDistrib::loadGroupKey(RsDistribGrpKey *newKey) std::cerr << "p3GroupDistrib::loadGroupKey() Key out of date: discarding"; std::cerr << std::endl; #endif + delete newKey; + } - return; + return false; } @@ -572,59 +584,28 @@ void p3GroupDistrib::loadGroupKey(RsDistribGrpKey *newKey) else { updateOk = true; + } } - std::map::iterator kit; - std::list::iterator sit; - bool canAdd = false; - - // check to see if client has received a private publish key - if(mRecvdPubKeys.end() != (kit = mRecvdPubKeys.find(gid))){ - - if(!locked_updateGroupPublishKey(it->second, kit->second)){ -#ifdef DISTRIB_DEBUG - std::cerr << "p3GroupDistrib::loadGroupKey() Failed Recvd Publish Key Update"; - std::cerr << std::endl; -#endif - }else{ -#ifdef DISTRIB_DEBUG - std::cerr << "p3GroupDistrib::loadGroupKey() Recvd Publish Key Update"; - std::cerr << std::endl; -#endif - - - sit = mPubKeyAvailableGrpId.begin(); - for(; sit != mPubKeyAvailableGrpId.end(); sit++){ - - if(gid == *sit) - canAdd = true; - - } - - if(canAdd) - mPubKeyAvailableGrpId.push_back(gid); - - mRecvdPubKeys.erase(gid); - updateOk = true; - } - - } - - if (updateOk) - { - locked_notifyGroupChanged(it->second, GRP_LOAD_KEY); - } - #ifdef DISTRIB_DEBUG std::cerr << "p3GroupDistrib::loadGroupKey() Done - Cleaning up."; std::cerr << std::endl; #endif - delete newKey; - return; + GroupInfo *gi = locked_getGroupInfo(gid); + + // special case, if user is not subscribed then save key + if(!(gi->flags & RS_DISTRIB_SUBSCRIBED) && updateOk){ + + mRecvdPubKeys.insert(std::pair(gid, newKey)); + return updateOk; + } + + delete newKey; + return updateOk; } @@ -1325,19 +1306,15 @@ bool p3GroupDistrib::subscribeToGroup(const std::string &grpId, bool subscrib } -bool p3GroupDistrib::attemptPublishKeysRecvd(GroupInfo& info) +bool p3GroupDistrib::attemptPublishKeysRecvd() { std::map::iterator mit; - mit = mRecvdPubKeys.find(info.grpId); + mit = mRecvdPubKeys.begin(); - if(mit == mRecvdPubKeys.end()) - return false; - - if(locked_updateGroupPublishKey(info, mit->second)) - mRecvdPubKeys.erase(mit); - else - return false; + for(; mit != mRecvdPubKeys.end(); mit++){ + loadGroupKey(mit->second); + } return true; } @@ -2121,6 +2098,8 @@ void p3GroupDistrib::receivePubKeys(){ RsItem* item; + GroupInfo *gi = NULL; + std::string gid; while(NULL != (item = recvItem())){ @@ -2137,18 +2116,32 @@ void p3GroupDistrib::receivePubKeys(){ #endif if(key_item->key.keyFlags & RSTLV_KEY_TYPE_FULL){ - loadGroupKey(key_item); + gid = key_item->grpId; + if(loadGroupKey(key_item)){ - } + if(NULL != (gi = locked_getGroupInfo(gid))){ + locked_notifyGroupChanged(*gi, GRP_LOAD_KEY); + gi = NULL; + } + + mPubKeyAvailableGrpId.insert(gid); + } + } else{ std::cerr << "p3GroupDistrib::locked_receiveKeys():" << "Not full public key" << "Deleting item"<< std::endl; delete key_item; - } - }else{ - delete item; - } - } + } + + } + else{ + + delete item; + + } + } + + RsStackMutex stack(distribMtx); @@ -2157,60 +2150,10 @@ void p3GroupDistrib::receivePubKeys(){ if(!mRecvdPubKeys.empty()) IndicateConfigChanged(); - - return; -} - -void p3GroupDistrib::locked_loadRecvdPubKeys(){ - - std::map::iterator mit; - std::list::iterator lit; - GroupInfo *gi; - bool cont = false; - -#ifdef DISTRIB_DEBUG - std::cerr << "p3GroupDistrib::locked_loadRecvdPubKeys() " << std::endl; -#endif - // look for keys to add to private publish key received notify list - for(mit = mRecvdPubKeys.begin(); mit != mRecvdPubKeys.end(); mit++ ){ - - gi = locked_getGroupInfo(mit->second->grpId); - - if(gi != NULL){ - - /* ensure grpId not added already */ - for(lit=mPubKeyAvailableGrpId.begin(); lit != mPubKeyAvailableGrpId.end(); lit++){ - - if(mit->second->grpId == *lit){ - cont = true; - break; - } - } - - if(cont) - { - cont = false; - continue; - } - - mPubKeyAvailableGrpId.push_back(mit->second->grpId); - locked_notifyGroupChanged(*gi, GRP_UPDATE); - - }else{ - - std::cerr << "p3GroupDistrib::locked_loadRecvdPubKeys(): group does not exist" << std::endl; - } - - } - - - mLastRecvdKeyTime = time(NULL); - - return; -} + } std::string p3GroupDistrib::publishMsg(RsDistribMsg *msg, bool personalSign) { @@ -3230,7 +3173,10 @@ RsDistribMsg *p3GroupDistrib::unpackDistribSignedMsg(RsDistribSignedMsg *newMsg) void p3GroupDistrib::getGrpListPubKeyAvailable(std::list& grpList) { RsStackMutex stack(distribMtx); - grpList = mPubKeyAvailableGrpId; + std::set::const_iterator cit = mPubKeyAvailableGrpId.begin(); + + for(; cit != mPubKeyAvailableGrpId.end(); cit++) + grpList.push_back(*cit); return; } diff --git a/libretroshare/src/services/p3distrib.h b/libretroshare/src/services/p3distrib.h index 8c9c02d72..13d8f511b 100644 --- a/libretroshare/src/services/p3distrib.h +++ b/libretroshare/src/services/p3distrib.h @@ -36,6 +36,8 @@ #include #include +#include + /* * Group Messages.... * @@ -281,7 +283,7 @@ class p3GroupDistrib: public CacheSource, public CacheStore, public p3Config, pu * Attempt to load public key from recvd list if it exists for grpId * @param grpId the id for the group for which private publish key is wanted */ - bool attemptPublishKeysRecvd(GroupInfo& info); + bool attemptPublishKeysRecvd(); protected: @@ -304,9 +306,11 @@ class p3GroupDistrib: public CacheSource, public CacheStore, public p3Config, pu /*! * Adds new keys dependent on whether it is an admin or publish key + * on return resource pointed to by newKey should be considered invalid * @param newKey key to be added + * @return if key is loaded to group or stored return true */ - void loadGroupKey(RsDistribGrpKey *newKey); + bool loadGroupKey(RsDistribGrpKey *newKey); /***************************************************************************************/ @@ -509,13 +513,6 @@ class p3GroupDistrib: public CacheSource, public CacheStore, public p3Config, pu */ virtual void receivePubKeys(); - /*! - * utility function to check whether grps exist - * for private publish keys received - * @deprecated - */ - virtual void locked_loadRecvdPubKeys(); - /** * Allows group admin(s) to change group icon, description and name *@param grpId group id @@ -684,7 +681,7 @@ class p3GroupDistrib: public CacheSource, public CacheStore, public p3Config, pu std::map mRecvdPubKeys; /// full publishing keys received from users std::map > mPendingPubKeyRecipients; /// peers to receive publics key for a given grp - std::list mPubKeyAvailableGrpId; // groups id for which public keys are available + std::set mPubKeyAvailableGrpId; // groups id for which public keys are available time_t mLastKeyPublishTime, mLastRecvdKeyTime; };