From 2f4b9b3e20ef32d013545ca85bcf08aef9e3cd22 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sun, 24 Jun 2018 23:24:52 +0200 Subject: [PATCH] Convert to RsTokenService::GxsRequestStatus Indicate GxsRequest status with an enum instead of uint_* that make the code less readable and more prone to errors --- libretroshare/src/gxs/rsgenexchange.cc | 15 ++-- libretroshare/src/gxs/rsgenexchange.h | 3 +- libretroshare/src/gxs/rsgxsdataaccess.cc | 74 +++++++------------ libretroshare/src/gxs/rsgxsdataaccess.h | 15 ++-- libretroshare/src/gxs/rsgxsrequesttypes.h | 6 +- libretroshare/src/retroshare/rstokenservice.h | 2 +- libretroshare/src/services/p3gxscommon.cc | 22 +++--- 7 files changed, 63 insertions(+), 74 deletions(-) diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index dc25d383b..6f58f2239 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -2414,7 +2414,7 @@ void RsGenExchange::processRoutingClues() void RsGenExchange::processGroupDelete() { - RS_STACK_MUTEX(mGenMtx) ; + RS_STACK_MUTEX(mGenMtx); // get keys for group delete publish typedef std::pair GrpNote; @@ -2435,8 +2435,9 @@ void RsGenExchange::processGroupDelete() for(; mit != toNotify.end(); ++mit) { GrpNote& note = mit->second; - uint8_t status = note.first ? RsTokenService::COMPLETE - : RsTokenService::FAILED; + RsTokenService::GxsRequestStatus status = + note.first ? RsTokenService::COMPLETE + : RsTokenService::FAILED; mGrpNotify.insert(std::make_pair(mit->first, note.second)); mDataAccess->updatePublicRequestStatus(mit->first, status); @@ -2744,8 +2745,9 @@ void RsGenExchange::publishGrps() for(; mit != toNotify.end(); ++mit) { GrpNote& note = mit->second; - uint8_t status = note.first ? RsTokenService::COMPLETE - : RsTokenService::FAILED; + RsTokenService::GxsRequestStatus status = + note.first ? RsTokenService::COMPLETE + : RsTokenService::FAILED; mGrpNotify.insert(std::make_pair(mit->first, note.second)); mDataAccess->updatePublicRequestStatus(mit->first, status); @@ -2781,7 +2783,8 @@ uint32_t RsGenExchange::generatePublicToken() return mDataAccess->generatePublicToken(); } -bool RsGenExchange::updatePublicRequestStatus(const uint32_t &token, const uint32_t &status) +bool RsGenExchange::updatePublicRequestStatus( + uint32_t token, RsTokenService::GxsRequestStatus status ) { return mDataAccess->updatePublicRequestStatus(token, status); } diff --git a/libretroshare/src/gxs/rsgenexchange.h b/libretroshare/src/gxs/rsgenexchange.h index 491ff017b..e60b70912 100644 --- a/libretroshare/src/gxs/rsgenexchange.h +++ b/libretroshare/src/gxs/rsgenexchange.h @@ -468,7 +468,8 @@ public: * @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); + bool updatePublicRequestStatus( + uint32_t token, RsTokenService::GxsRequestStatus status); /*! * This gets rid of a publicly issued token diff --git a/libretroshare/src/gxs/rsgxsdataaccess.cc b/libretroshare/src/gxs/rsgxsdataaccess.cc index b66a2811a..7c092fd68 100644 --- a/libretroshare/src/gxs/rsgxsdataaccess.cc +++ b/libretroshare/src/gxs/rsgxsdataaccess.cc @@ -315,15 +315,15 @@ void RsGxsDataAccess::storeRequest(GxsRequest* req) return; } -uint32_t RsGxsDataAccess::requestStatus(uint32_t token) +RsTokenService::GxsRequestStatus RsGxsDataAccess::requestStatus(uint32_t token) { - uint32_t status; + RsTokenService::GxsRequestStatus status; uint32_t reqtype; uint32_t anstype; time_t ts; { - RsStackMutex stack(mDataMutex); + RS_STACK_MUTEX(mDataMutex); // first check public tokens if(mPublicToken.find(token) != mPublicToken.end()) @@ -1761,11 +1761,11 @@ void RsGxsDataAccess::filterGrpList(std::list &grpIds, const RsTok } -bool RsGxsDataAccess::checkRequestStatus(const uint32_t& token, - uint32_t& status, uint32_t& reqtype, uint32_t& anstype, time_t& ts) +bool RsGxsDataAccess::checkRequestStatus( + uint32_t token, GxsRequestStatus& status, uint32_t& reqtype, + uint32_t& anstype, time_t& ts ) { - - RsStackMutex stack(mDataMutex); + RS_STACK_MUTEX(mDataMutex); GxsRequest* req = locked_retrieveRequest(token); @@ -1822,70 +1822,52 @@ void RsGxsDataAccess::tokenList(std::list& tokens) } } -bool RsGxsDataAccess::locked_updateRequestStatus(const uint32_t& token, - const uint32_t& status) +bool RsGxsDataAccess::locked_updateRequestStatus( + uint32_t token, RsTokenService::GxsRequestStatus status ) { - GxsRequest* req = locked_retrieveRequest(token); - if(req) - req->status = status; - else - return false; + if(req) req->status = status; + else return false; return true; } uint32_t RsGxsDataAccess::generatePublicToken() { - uint32_t token; generateToken(token); - { - RsStackMutex stack(mDataMutex); - mPublicToken[token] = RsTokenService::PENDING; - } + { + RS_STACK_MUTEX(mDataMutex); + mPublicToken[token] = RsTokenService::PENDING; + } return token; } -bool RsGxsDataAccess::updatePublicRequestStatus(const uint32_t& token, - const uint32_t& status) +bool RsGxsDataAccess::updatePublicRequestStatus( + uint32_t token, RsTokenService::GxsRequestStatus status ) { - RsStackMutex stack(mDataMutex); - std::map::iterator mit = mPublicToken.find(token); - - if(mit != mPublicToken.end()) - { - mit->second = status; - } - else - { - return false; - } - + RS_STACK_MUTEX(mDataMutex); + std::map::iterator mit = + mPublicToken.find(token); + if(mit != mPublicToken.end()) mit->second = status; + else return false; return true; } -bool RsGxsDataAccess::disposeOfPublicToken(const uint32_t& token) +bool RsGxsDataAccess::disposeOfPublicToken(uint32_t token) { - RsStackMutex stack(mDataMutex); - std::map::iterator mit = mPublicToken.find(token); - - if(mit != mPublicToken.end()) - { - mPublicToken.erase(mit); - } - else - { - return false; - } - + RS_STACK_MUTEX(mDataMutex); + std::map::iterator mit = + mPublicToken.find(token); + if(mit != mPublicToken.end()) mPublicToken.erase(mit); + else return false; return true; } diff --git a/libretroshare/src/gxs/rsgxsdataaccess.h b/libretroshare/src/gxs/rsgxsdataaccess.h index 77f94137e..2cbdabf9f 100644 --- a/libretroshare/src/gxs/rsgxsdataaccess.h +++ b/libretroshare/src/gxs/rsgxsdataaccess.h @@ -48,6 +48,8 @@ public: * deprecated and should be removed as soon as possible as it is cause of * many confusions, instead use const RsTokReqOptions::mReqType &opts to * specify the kind of data you are interested in. + * Most of the methods use const uint32_t &token as param type change it to + * uint32_t */ /*! @@ -121,7 +123,7 @@ public: /* Poll */ - uint32_t requestStatus(const uint32_t token); + GxsRequestStatus requestStatus(const uint32_t token); /* Cancel Request */ bool cancelRequest(const uint32_t &token); @@ -296,7 +298,7 @@ private: * @param status the status to set * @return */ - bool locked_updateRequestStatus(const uint32_t &token, const uint32_t &status); + bool locked_updateRequestStatus(uint32_t token, GxsRequestStatus status); /*! * Use to query the status and other values of a given token @@ -307,7 +309,8 @@ private: * @param ts time stamp * @return false if token does not exist, true otherwise */ - bool checkRequestStatus(const uint32_t &token, uint32_t &status, uint32_t &reqtype, uint32_t &anstype, time_t &ts); + bool checkRequestStatus( uint32_t token, GxsRequestStatus &status, + uint32_t &reqtype, uint32_t &anstype, time_t &ts); // special ones for testing (not in final design) /*! @@ -337,14 +340,14 @@ public: * @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); + bool updatePublicRequestStatus(uint32_t token, GxsRequestStatus status); /*! * This gets rid of a publicly issued token * @param token * @return false if token could not found, true if token disposed of */ - bool disposeOfPublicToken(const uint32_t &token); + bool disposeOfPublicToken(uint32_t token); private: @@ -484,7 +487,7 @@ private: RsMutex mDataMutex; /* protecting below */ uint32_t mNextToken; - std::map mPublicToken; + std::map mPublicToken; std::map mRequests; diff --git a/libretroshare/src/gxs/rsgxsrequesttypes.h b/libretroshare/src/gxs/rsgxsrequesttypes.h index 5c70c9d52..57e70acea 100644 --- a/libretroshare/src/gxs/rsgxsrequesttypes.h +++ b/libretroshare/src/gxs/rsgxsrequesttypes.h @@ -32,7 +32,9 @@ struct GxsRequest { - GxsRequest() : token(0), reqTime(0), ansType(0), reqType(0), status(0) {} + GxsRequest() : + token(0), reqTime(0), ansType(0), reqType(0), + status(RsTokenService::FAILED) {} virtual ~GxsRequest() {} uint32_t token; @@ -42,7 +44,7 @@ struct GxsRequest uint32_t reqType; RsTokReqOptions Options; - uint32_t status; + RsTokenService::GxsRequestStatus status; }; class GroupMetaReq : public GxsRequest diff --git a/libretroshare/src/retroshare/rstokenservice.h b/libretroshare/src/retroshare/rstokenservice.h index 5a9db6aef..ceefe28ad 100644 --- a/libretroshare/src/retroshare/rstokenservice.h +++ b/libretroshare/src/retroshare/rstokenservice.h @@ -199,7 +199,7 @@ public: * @param token value of token to check status for * @return the current status of request */ - virtual uint32_t requestStatus(const uint32_t token) = 0; + virtual GxsRequestStatus requestStatus(const uint32_t token) = 0; /*! * This request statistics on amount of data held diff --git a/libretroshare/src/services/p3gxscommon.cc b/libretroshare/src/services/p3gxscommon.cc index c30b39952..f5e8668f1 100644 --- a/libretroshare/src/services/p3gxscommon.cc +++ b/libretroshare/src/services/p3gxscommon.cc @@ -556,24 +556,22 @@ void p3GxsCommentService::load_PendingVoteParent(const uint32_t &token) pit = mPendingVotes.find(parentId); if (pit == mPendingVotes.end()) { - std::cerr << "p3GxsCommentService::load_PendingVoteParent() ERROR Finding Pending Vote"; - std::cerr << std::endl; + std::cerr << __PRETTY_FUNCTION__ + << " ERROR Finding Pending Vote" << std::endl; continue; } RsGxsVote vote = pit->second.mVote; if (meta.mMsgStatus & GXS_SERV::GXS_MSG_STATUS_VOTE_MASK) { - std::cerr << "p3GxsCommentService::load_PendingVoteParent() ERROR Already Voted"; - std::cerr << std::endl; - std::cerr << "mGroupId: " << meta.mGroupId; - std::cerr << std::endl; - std::cerr << "mMsgId: " << meta.mMsgId; - std::cerr << std::endl; + std::cerr << __PRETTY_FUNCTION__ << " ERROR Already Voted" + << std::endl + << "mGroupId: " << meta.mGroupId << std::endl + << "mMsgId: " << meta.mMsgId << std::endl; pit->second.mStatus = VoteHolder::VOTE_ERROR; - uint32_t status = RsTokenService::FAILED; - mExchange->updatePublicRequestStatus(pit->second.mReqToken, status); + mExchange->updatePublicRequestStatus( + pit->second.mReqToken, RsTokenService::FAILED ); continue; } @@ -617,8 +615,8 @@ void p3GxsCommentService::completeInternalVote(uint32_t &token) { if (it->second.mVoteToken == token) { - - uint32_t status = mExchange->getTokenService()->requestStatus(token); + RsTokenService::GxsRequestStatus status = + mExchange->getTokenService()->requestStatus(token); mExchange->updatePublicRequestStatus(it->second.mReqToken, status); #ifdef DEBUG_GXSCOMMON