From f75d0add474ffff0b2ca148d32749e920882e099 Mon Sep 17 00:00:00 2001 From: csoler Date: Tue, 11 Feb 2020 16:37:53 +0100 Subject: [PATCH] added notification for circle cache updates to avoid the need for an additional UI timer --- libretroshare/src/retroshare/rsgxscircles.h | 4 ++ libretroshare/src/services/p3gxscircles.cc | 43 ++++++++++++++++---- libretroshare/src/services/p3gxscircles.h | 2 + retroshare-gui/src/gui/Identity/IdDialog.cpp | 1 + 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/libretroshare/src/retroshare/rsgxscircles.h b/libretroshare/src/retroshare/rsgxscircles.h index ab8448611..61e743323 100644 --- a/libretroshare/src/retroshare/rsgxscircles.h +++ b/libretroshare/src/retroshare/rsgxscircles.h @@ -187,6 +187,10 @@ enum class RsGxsCircleEventCode: uint8_t /** mCircleId contains the circle id */ NEW_CIRCLE = 0x06, + + /** no additional information. Simply means that the info previously from the cache has changed. */ + CACHE_DATA_UPDATED = 0x07, + }; struct RsGxsCircleEvent: RsEvent diff --git a/libretroshare/src/services/p3gxscircles.cc b/libretroshare/src/services/p3gxscircles.cc index 63270e235..8d5e878c1 100644 --- a/libretroshare/src/services/p3gxscircles.cc +++ b/libretroshare/src/services/p3gxscircles.cc @@ -103,6 +103,7 @@ #define MIN_CIRCLE_LOAD_GAP 5 #define GXS_CIRCLE_DELAY_TO_FORCE_MEMBERSHIP_UPDATE 60 // re-check every 1 mins. Normally this shouldn't be necessary since notifications inform abotu new messages. #define GXS_CIRCLE_DELAY_TO_CHECK_MEMBERSHIP_UPDATE 60 // re-check every 1 mins. Normally this shouldn't be necessary since notifications inform abotu new messages. +#define GXS_CIRCLE_DELAY_TO_SEND_CACHE_UPDATED_EVENT 10 // do not send cache update events more often than every 10 secs. /********************************************************************************/ /******************* Startup / Tick ******************************************/ @@ -117,11 +118,13 @@ p3GxsCircles::p3GxsCircles( RsGxsCircles(static_cast(*this)), GxsTokenQueue(this), RsTickEvent(), mIdentities(identities), mPgpUtils(pgpUtils), mCircleMtx("p3GxsCircles"), - mCircleCache(DEFAULT_MEM_CACHE_SIZE, "GxsCircleCache" ) + mCircleCache(DEFAULT_MEM_CACHE_SIZE, "GxsCircleCache" ), + mCacheUpdated(false) { // Kick off Cache Testing, + Others. //RsTickEvent::schedule_in(CIRCLE_EVENT_CACHETEST, CACHETEST_PERIOD); mLastCacheMembershipUpdateTS = 0 ; + mLastCacheUpdateEvent = 0; RsTickEvent::schedule_now(CIRCLE_EVENT_LOADIDS); @@ -490,7 +493,21 @@ void p3GxsCircles::service_tick() GxsTokenQueue::checkRequests(); // GxsTokenQueue handles all requests. rstime_t now = time(NULL); - if(now > mLastCacheMembershipUpdateTS + GXS_CIRCLE_DELAY_TO_CHECK_MEMBERSHIP_UPDATE) + + if(mCacheUpdated && now > mLastCacheUpdateEvent + GXS_CIRCLE_DELAY_TO_SEND_CACHE_UPDATED_EVENT) + { + if(rsEvents) + { + auto ev = std::make_shared(); + ev->mCircleEventType = RsGxsCircleEventCode::CACHE_DATA_UPDATED; + rsEvents->postEvent(ev); + } + + mLastCacheUpdateEvent = now; + mCacheUpdated = false; + } + + if(now > mLastCacheMembershipUpdateTS + GXS_CIRCLE_DELAY_TO_CHECK_MEMBERSHIP_UPDATE) { checkCircleCache(); mLastCacheMembershipUpdateTS = now ; @@ -552,6 +569,7 @@ void p3GxsCircles::notifyChanges(std::vector &changes) } mCircleCache.erase(circle_id); + mCacheUpdated = true; } } @@ -577,6 +595,7 @@ void p3GxsCircles::notifyChanges(std::vector &changes) { RsStackMutex stack(mCircleMtx); /********** STACK LOCKED MTX ******/ mCircleCache.erase(RsGxsCircleId(*git)); + mCacheUpdated = true; } } } @@ -646,6 +665,7 @@ void p3GxsCircles::notifyChanges(std::vector &changes) { RsStackMutex stack(mCircleMtx); /********** STACK LOCKED MTX ******/ mCircleCache.erase(RsGxsCircleId(*git)); + mCacheUpdated = true; } } @@ -700,7 +720,6 @@ bool p3GxsCircles::getCircleDetails( } } - return true; } } @@ -1462,6 +1481,7 @@ bool p3GxsCircles::cache_load_for_token(uint32_t token) RsTickEvent::schedule_in(CIRCLE_EVENT_RELOADIDS, GXSID_LOAD_CYCLE, id.toStdString()); } + mCacheUpdated = true; } return true; @@ -1498,6 +1518,8 @@ bool p3GxsCircles::locked_processLoadingCacheEntry(RsGxsCircleCache& cache) if(mIdentities->haveKey(pit->first)) { pit->second.subscription_flags |= GXS_EXTERNAL_CIRCLE_FLAGS_KEY_AVAILABLE; + + mCacheUpdated = true; #ifdef DEBUG_CIRCLES std::cerr << " Key is now available!"<< std::endl; #endif @@ -1573,8 +1595,8 @@ bool p3GxsCircles::locked_processLoadingCacheEntry(RsGxsCircleCache& cache) // We can check for self inclusion in the circle right away, since own ids are always loaded. // that allows to subscribe/unsubscribe uncomplete circles - - locked_checkCircleCacheForAutoSubscribe(cache); + + locked_checkCircleCacheForAutoSubscribe(cache); locked_checkCircleCacheForMembershipUpdate(cache); // always store in cache even if uncomplete. But do not remove the loading items so that they can be kept in loading state. @@ -1718,8 +1740,8 @@ bool p3GxsCircles::locked_checkCircleCacheForAutoSubscribe(RsGxsCircleCache &cac if(it2 != cache.mMembershipStatus.end()) { - in_admin_list = in_admin_list || bool(it2->second.subscription_flags & GXS_EXTERNAL_CIRCLE_FLAGS_IN_ADMIN_LIST) ; - member_request= member_request|| bool(it2->second.subscription_flags & GXS_EXTERNAL_CIRCLE_FLAGS_SUBSCRIBED) ; + in_admin_list = in_admin_list || bool(it2->second.subscription_flags & GXS_EXTERNAL_CIRCLE_FLAGS_IN_ADMIN_LIST) ; + member_request= member_request|| bool(it2->second.subscription_flags & GXS_EXTERNAL_CIRCLE_FLAGS_SUBSCRIBED) ; } } @@ -1748,6 +1770,8 @@ bool p3GxsCircles::locked_checkCircleCacheForAutoSubscribe(RsGxsCircleCache &cac RsGenExchange::setGroupStatusFlags(token2, RsGxsGroupId(cache.mCircleId), 0, GXS_SERV::GXS_GRP_STATUS_UNPROCESSED); cache.mGroupStatus &= ~GXS_SERV::GXS_GRP_STATUS_UNPROCESSED; + + mCacheUpdated = true; return true; } @@ -1771,6 +1795,7 @@ bool p3GxsCircles::locked_checkCircleCacheForAutoSubscribe(RsGxsCircleCache &cac cache.mGroupStatus &= ~GXS_SERV::GXS_GRP_STATUS_UNPROCESSED; + mCacheUpdated = true; return true ; } } @@ -2427,6 +2452,7 @@ bool p3GxsCircles::processMembershipRequests(uint32_t token) else std::cerr << " (EE) unknown subscription order type: " << item->subscription_type ; + mCacheUpdated = true; #ifdef DEBUG_CIRCLES std::cerr << " UPDATING" << std::endl; #endif @@ -2441,7 +2467,8 @@ bool p3GxsCircles::processMembershipRequests(uint32_t token) } data.mLastUpdatedMembershipTS = time(NULL) ; - } + mCacheUpdated = true; + } RsStackMutex stack(mCircleMtx); /********** STACK LOCKED MTX ******/ uint32_t token2; diff --git a/libretroshare/src/services/p3gxscircles.h b/libretroshare/src/services/p3gxscircles.h index 1f91964f0..26aca35a7 100644 --- a/libretroshare/src/services/p3gxscircles.h +++ b/libretroshare/src/services/p3gxscircles.h @@ -345,6 +345,8 @@ public: uint32_t mDummyIdToken; std::list mDummyPgpLinkedIds; std::list mDummyOwnIds; + bool mCacheUpdated ; + rstime_t mLastCacheUpdateEvent; RS_SET_CONTEXT_DEBUG_LEVEL(2) }; diff --git a/retroshare-gui/src/gui/Identity/IdDialog.cpp b/retroshare-gui/src/gui/Identity/IdDialog.cpp index ffdc2fe80..dfe81a07c 100644 --- a/retroshare-gui/src/gui/Identity/IdDialog.cpp +++ b/retroshare-gui/src/gui/Identity/IdDialog.cpp @@ -429,6 +429,7 @@ void IdDialog::handleEvent_main_thread(std::shared_ptr event) case RsGxsCircleEventCode::CIRCLE_MEMBERSHIP_LEAVE: case RsGxsCircleEventCode::CIRCLE_MEMBERSHIP_JOIN: case RsGxsCircleEventCode::CIRCLE_MEMBERSHIP_REVOQUED: + case RsGxsCircleEventCode::CACHE_DATA_UPDATED: updateCircles(); default: