Merge pull request #1385 from G10h4ck/safer_api

0.6.5 Safer rsGxsChannel API
This commit is contained in:
csoler 2018-11-20 21:44:40 +01:00 committed by GitHub
commit 8c8ce53e4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 966 additions and 539 deletions

View File

@ -36,20 +36,17 @@ bool GxsTokenQueue::queueRequest(uint32_t token, uint32_t req_type)
void GxsTokenQueue::checkRequests() void GxsTokenQueue::checkRequests()
{ {
{ {
RsStackMutex stack(mQueueMtx); /********** STACK LOCKED MTX ******/ RS_STACK_MUTEX(mQueueMtx);
if (mQueue.empty()) if (mQueue.empty()) return;
{
return;
}
} }
// Must check all, and move to a different list - for reentrant / good mutex behaviour. // Must check all, and move to a different list - for reentrant / good mutex behaviour.
std::list<GxsTokenQueueItem> toload; std::list<GxsTokenQueueItem> toload;
std::list<GxsTokenQueueItem>::iterator it; std::list<GxsTokenQueueItem>::iterator it;
bool stuffToLoad = false; bool stuffToLoad = false;
{ {
RsStackMutex stack(mQueueMtx); /********** STACK LOCKED MTX ******/ RS_STACK_MUTEX(mQueueMtx);
for(it = mQueue.begin(); it != mQueue.end();) for(it = mQueue.begin(); it != mQueue.end();)
{ {
uint32_t token = it->mToken; uint32_t token = it->mToken;
@ -62,29 +59,29 @@ void GxsTokenQueue::checkRequests()
stuffToLoad = true; stuffToLoad = true;
#ifdef GXS_DEBUG #ifdef GXS_DEBUG
std::cerr << "GxsTokenQueue::checkRequests() token: " << token << " Complete"; std::cerr << "GxsTokenQueue::checkRequests() token: " << token
std::cerr << std::endl; << " Complete" << std::endl;
#endif #endif
++it; ++it;
} }
else if (status == RsTokenService::FAILED) else if (status == RsTokenService::FAILED)
{ {
// maybe we should do alternative callback? // maybe we should do alternative callback?
std::cerr << "GxsTokenQueue::checkRequests() ERROR Request Failed: " << token; std::cerr << __PRETTY_FUNCTION__ << " ERROR Request Failed! "
std::cerr << std::endl; << " token: " << token << std::endl;
it = mQueue.erase(it); it = mQueue.erase(it);
} }
else else
{ {
#ifdef GXS_DEBUG #ifdef GXS_DEBUG
std::cerr << "GxsTokenQueue::checkRequests() token: " << token << " is unfinished, status: " << status; std::cerr << "GxsTokenQueue::checkRequests() token: " << token
std::cerr << std::endl; << " is unfinished, status: " << status << std::endl;
#endif #endif
++it; ++it;
} }
} }
} } // RS_STACK_MUTEX(mQueueMtx) END
if (stuffToLoad) if (stuffToLoad)
{ {
@ -95,11 +92,3 @@ void GxsTokenQueue::checkRequests()
} }
} }
// This must be overloaded to complete the functionality.
void GxsTokenQueue::handleResponse(uint32_t token, uint32_t req_type)
{
std::cerr << "GxsTokenQueue::handleResponse(" << token << "," << req_type << ") ERROR: NOT HANDLED";
std::cerr << std::endl;
}

View File

@ -54,7 +54,7 @@ public:
protected: protected:
/// This must be overloaded to complete the functionality. /// This must be overloaded to complete the functionality.
virtual void handleResponse(uint32_t token, uint32_t req_type); virtual void handleResponse(uint32_t token, uint32_t req_type) = 0;
private: private:
RsGenExchange *mGenExchange; RsGenExchange *mGenExchange;

View File

@ -1198,16 +1198,16 @@ void RsDataService::locked_retrieveGroups(RetroCursor* c, std::vector<RsNxsGrp*>
} }
} }
int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, bool /* cache */, bool withMeta) int RsDataService::retrieveNxsMsgs(
const GxsMsgReq &reqIds, GxsMsgResult &msg, bool /* cache */,
bool withMeta )
{ {
#ifdef RS_DATA_SERVICE_DEBUG_TIME #ifdef RS_DATA_SERVICE_DEBUG_TIME
rstime::RsScopeTimer timer(""); rstime::RsScopeTimer timer("");
int resultCount = 0; int resultCount = 0;
#endif #endif
GxsMsgReq::const_iterator mit = reqIds.begin(); for(auto mit = reqIds.begin(); mit != reqIds.end(); ++mit)
for(; mit != reqIds.end(); ++mit)
{ {
const RsGxsGroupId& grpId = mit->first; const RsGxsGroupId& grpId = mit->first;
@ -1216,9 +1216,9 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b
const std::set<RsGxsMessageId>& msgIdV = mit->second; const std::set<RsGxsMessageId>& msgIdV = mit->second;
std::vector<RsNxsMsg*> msgSet; std::vector<RsNxsMsg*> msgSet;
if(msgIdV.empty()){ if(msgIdV.empty())
{
RsStackMutex stack(mDbMutex); RS_STACK_MUTEX(mDbMutex);
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, withMeta ? mMsgColumnsWithMeta : mMsgColumns, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", ""); RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, withMeta ? mMsgColumnsWithMeta : mMsgColumns, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", "");
@ -1228,16 +1228,17 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b
} }
delete c; delete c;
}else{ }
else
{
RS_STACK_MUTEX(mDbMutex);
// request each grp // request each grp
std::set<RsGxsMessageId>::const_iterator sit = msgIdV.begin(); for( std::set<RsGxsMessageId>::const_iterator sit = msgIdV.begin();
sit!=msgIdV.end();++sit )
for(; sit!=msgIdV.end();++sit){ {
const RsGxsMessageId& msgId = *sit; const RsGxsMessageId& msgId = *sit;
RsStackMutex stack(mDbMutex);
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, withMeta ? mMsgColumnsWithMeta : mMsgColumns, KEY_GRP_ID+ "='" + grpId.toStdString() RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, withMeta ? mMsgColumnsWithMeta : mMsgColumns, KEY_GRP_ID+ "='" + grpId.toStdString()
+ "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", ""); + "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", "");

View File

@ -47,10 +47,14 @@ public:
* Retrieves all msgs * Retrieves all msgs
* @param reqIds requested msg ids (grpId,msgId), leave msg list empty to get all msgs for the grp * @param reqIds requested msg ids (grpId,msgId), leave msg list empty to get all msgs for the grp
* @param msg result of msg retrieval * @param msg result of msg retrieval
* @param cache whether to store results of this retrieval in memory for faster later retrieval * @param cache IGNORED whether to store results of this retrieval in memory
* for faster later retrieval
* @param strictFilter if true do not request any message if reqIds is empty
* @return error code * @return error code
*/ */
int retrieveNxsMsgs(const GxsMsgReq& reqIds, GxsMsgResult& msg, bool cache, bool withMeta = false); int retrieveNxsMsgs(
const GxsMsgReq& reqIds, GxsMsgResult& msg, bool cache,
bool withMeta = false );
/*! /*!
* Retrieves groups, if empty, retrieves all grps, if map is not empty * Retrieves groups, if empty, retrieves all grps, if map is not empty

View File

@ -137,16 +137,19 @@ public:
typedef std::map<RsNxsMsg*, RsGxsMsgMetaData*> MsgStoreMap; typedef std::map<RsNxsMsg*, RsGxsMsgMetaData*> MsgStoreMap;
RsGeneralDataService(){} RsGeneralDataService(){}
virtual ~RsGeneralDataService(){return;} virtual ~RsGeneralDataService(){}
/*! /*!
* Retrieves all msgs * Retrieves all msgs
* @param reqIds requested msg ids (grpId,msgId), leave msg list empty to get all msgs for the grp * @param reqIds requested msg ids (grpId,msgId), leave msg list empty to get all msgs for the grp
* @param msg result of msg retrieval * @param msg result of msg retrieval
* @param cache whether to store results of this retrieval in memory for faster later retrieval * @param cache whether to store results of this retrieval in memory for faster later retrieval
* @param strictFilter if true do not request any message if reqIds is empty
* @return error code * @return error code
*/ */
virtual int retrieveNxsMsgs(const GxsMsgReq& reqIds, GxsMsgResult& msg, bool cache, bool withMeta=false) = 0; virtual int retrieveNxsMsgs(
const GxsMsgReq& reqIds, GxsMsgResult& msg, bool cache,
bool withMeta = false ) = 0;
/*! /*!
* Retrieves all groups stored * Retrieves all groups stored

View File

@ -1925,7 +1925,7 @@ void RsGenExchange::setGroupStatusFlags(uint32_t& token, const RsGxsGroupId& grp
void RsGenExchange::setGroupServiceString(uint32_t& token, const RsGxsGroupId& grpId, const std::string& servString) void RsGenExchange::setGroupServiceString(uint32_t& token, const RsGxsGroupId& grpId, const std::string& servString)
{ {
RS_STACK_MUTEX(mGenMtx) ; RS_STACK_MUTEX(mGenMtx);
token = mDataAccess->generatePublicToken(); token = mDataAccess->generatePublicToken();
GrpLocMetaData g; GrpLocMetaData g;

View File

@ -39,14 +39,16 @@ RsGxsDataAccess::~RsGxsDataAccess()
for(std::map<uint32_t, GxsRequest*>::const_iterator it(mRequests.begin());it!=mRequests.end();++it) for(std::map<uint32_t, GxsRequest*>::const_iterator it(mRequests.begin());it!=mRequests.end();++it)
delete it->second ; delete it->second ;
} }
bool RsGxsDataAccess::requestGroupInfo(uint32_t &token, uint32_t ansType, const RsTokReqOptions &opts, bool RsGxsDataAccess::requestGroupInfo(
const std::list<RsGxsGroupId> &groupIds) uint32_t &token, uint32_t ansType, const RsTokReqOptions &opts,
const std::list<RsGxsGroupId> &groupIds )
{ {
if(groupIds.empty()) if(groupIds.empty())
{ {
std::cerr << "(WW) Group Id list is empty" << std::endl; std::cerr << __PRETTY_FUNCTION__ << " (WW) Group Id list is empty!"
return false; << std::endl;
} return false;
}
GxsRequest* req = NULL; GxsRequest* req = NULL;
uint32_t reqType = opts.mReqType; uint32_t reqType = opts.mReqType;
@ -76,19 +78,19 @@ bool RsGxsDataAccess::requestGroupInfo(uint32_t &token, uint32_t ansType, const
req = gir; req = gir;
} }
if(req == NULL) if(!req)
{ {
std::cerr << "RsGxsDataAccess::requestGroupInfo() request type not recognised, type " std::cerr << __PRETTY_FUNCTION__ << " request type not recognised, "
<< reqType << std::endl; << "reqType: " << reqType << std::endl;
return false; return false;
}else }
{
generateToken(token); generateToken(token);
#ifdef DATA_DEBUG #ifdef DATA_DEBUG
std::cerr << "RsGxsDataAccess::requestGroupInfo() gets Token: " << token << std::endl; std::cerr << "RsGxsDataAccess::requestGroupInfo() gets token: " << token
<< std::endl;
#endif #endif
}
setReq(req, token, ansType, opts); setReq(req, token, ansType, opts);
storeRequest(req); storeRequest(req);
@ -130,11 +132,8 @@ bool RsGxsDataAccess::requestGroupInfo(uint32_t &token, uint32_t ansType, const
void RsGxsDataAccess::generateToken(uint32_t &token) void RsGxsDataAccess::generateToken(uint32_t &token)
{ {
RsStackMutex stack(mDataMutex); /****** LOCKED *****/ RS_STACK_MUTEX(mDataMutex);
token = mNextToken++; token = mNextToken++;
return;
} }
@ -301,15 +300,12 @@ void RsGxsDataAccess::setReq(GxsRequest* req, uint32_t token, uint32_t ansType,
req->Options = opts; req->Options = opts;
return; return;
} }
void RsGxsDataAccess::storeRequest(GxsRequest* req) void RsGxsDataAccess::storeRequest(GxsRequest* req)
{ {
RsStackMutex stack(mDataMutex); /****** LOCKED *****/ RS_STACK_MUTEX(mDataMutex);
req->status = PENDING;
req->status = PENDING; req->reqTime = time(NULL);
req->reqTime = time(NULL);
mRequests[req->token] = req; mRequests[req->token] = req;
return;
} }
RsTokenService::GxsRequestStatus RsGxsDataAccess::requestStatus(uint32_t token) RsTokenService::GxsRequestStatus RsGxsDataAccess::requestStatus(uint32_t token)
@ -1040,29 +1036,42 @@ bool RsGxsDataAccess::getMsgData(MsgDataReq* req)
{ {
GxsMsgReq msgIdOut; GxsMsgReq msgIdOut;
const RsTokReqOptions& opts(req->Options);
// filter based on options // filter based on options
getMsgList(req->mMsgIds, req->Options, msgIdOut); getMsgList(req->mMsgIds, opts, msgIdOut);
// If the list is empty because of filtering do not retrieve from DB
if((opts.mMsgFlagMask || opts.mStatusMask) && msgIdOut.empty())
return true;
mDataStore->retrieveNxsMsgs(msgIdOut, req->mMsgData, true, true); mDataStore->retrieveNxsMsgs(msgIdOut, req->mMsgData, true, true);
return true; return true;
} }
bool RsGxsDataAccess::getMsgSummary(MsgMetaReq* req) bool RsGxsDataAccess::getMsgSummary(MsgMetaReq* req)
{ {
GxsMsgReq msgIdOut; GxsMsgReq msgIdOut;
// filter based on options const RsTokReqOptions& opts(req->Options);
getMsgList(req->mMsgIds, req->Options, msgIdOut);
mDataStore->retrieveGxsMsgMetaData(msgIdOut, req->mMsgMetaData); // filter based on options
getMsgList(req->mMsgIds, opts, msgIdOut);
// If the list is empty because of filtering do not retrieve from DB
if((opts.mMsgFlagMask || opts.mStatusMask) && msgIdOut.empty())
return true;
mDataStore->retrieveGxsMsgMetaData(msgIdOut, req->mMsgMetaData);
return true; return true;
} }
bool RsGxsDataAccess::getMsgList(const GxsMsgReq& msgIds, const RsTokReqOptions& opts, GxsMsgReq& msgIdsOut) bool RsGxsDataAccess::getMsgList(
const GxsMsgReq& msgIds, const RsTokReqOptions& opts,
GxsMsgReq& msgIdsOut )
{ {
GxsMsgMetaResult result; GxsMsgMetaResult result;
@ -1693,41 +1702,45 @@ void RsGxsDataAccess::cleanseMsgMetaMap(GxsMsgMetaResult& result)
return; return;
} }
void RsGxsDataAccess::filterMsgList(GxsMsgIdResult& msgIds, const RsTokReqOptions& opts, void RsGxsDataAccess::filterMsgList(
const MsgMetaFilter& msgMetas) const GxsMsgIdResult& resultsMap, const RsTokReqOptions& opts,
const MsgMetaFilter& msgMetas ) const
{ {
for( GxsMsgIdResult::iterator grpIt = resultsMap.begin();
GxsMsgIdResult::iterator mit = msgIds.begin(); grpIt != resultsMap.end(); ++grpIt )
for(;mit != msgIds.end(); ++mit)
{ {
const RsGxsGroupId& groupId(grpIt->first);
std::set<RsGxsMessageId>& msgsIdSet(grpIt->second);
MsgMetaFilter::const_iterator cit = msgMetas.find(mit->first); MsgMetaFilter::const_iterator cit = msgMetas.find(groupId);
if(cit == msgMetas.end()) continue;
if(cit == msgMetas.end()) std::cerr << __PRETTY_FUNCTION__ << " " << msgsIdSet.size()
continue; << " for group: " << groupId << " before filtering"
<< std::endl;
std::set<RsGxsMessageId>& msgs = mit->second; for( std::set<RsGxsMessageId>::iterator msgIdIt = msgsIdSet.begin();
std::set<RsGxsMessageId>::iterator vit = msgs.begin(); msgIdIt != msgsIdSet.end(); )
const std::map<RsGxsMessageId, RsGxsMsgMetaData*>& meta = cit->second;
std::map<RsGxsMessageId, RsGxsMsgMetaData*>::const_iterator cit2;
for(; vit != msgs.end();)
{ {
const RsGxsMessageId& msgId(*msgIdIt);
const std::map<RsGxsMessageId, RsGxsMsgMetaData*>& msgsMetaMap =
cit->second;
bool keep = false; bool keep = false;
if( (cit2 = meta.find(*vit)) != meta.end() ) std::map<RsGxsMessageId, RsGxsMsgMetaData*>::const_iterator msgsMetaMapIt;
if( (msgsMetaMapIt = msgsMetaMap.find(msgId)) != msgsMetaMap.end() )
{ {
keep = checkMsgFilter(opts, cit2->second); keep = checkMsgFilter(opts, msgsMetaMapIt->second);
} }
if(keep) if(keep) ++msgIdIt;
{ else msgIdIt = msgsIdSet.erase(msgIdIt);
++vit;
}else
{
vit = msgs.erase(vit);
}
} }
std::cerr << __PRETTY_FUNCTION__ << " " << msgsIdSet.size()
<< " for group: " << groupId << " after filtering"
<< std::endl;
} }
} }
@ -1905,62 +1918,87 @@ bool RsGxsDataAccess::checkGrpFilter(const RsTokReqOptions &opts, const RsGxsGrp
return subscribeMatch; return subscribeMatch;
} }
bool RsGxsDataAccess::checkMsgFilter(const RsTokReqOptions& opts, const RsGxsMsgMetaData* meta) const bool RsGxsDataAccess::checkMsgFilter(
const RsTokReqOptions& opts, const RsGxsMsgMetaData* meta ) const
{ {
bool statusMatch = false; if (opts.mStatusMask)
if (opts.mStatusMask)
{ {
// Exact Flags match required. // Exact Flags match required.
if ((opts.mStatusMask & opts.mStatusFilter) == (opts.mStatusMask & meta->mMsgStatus)) if ( (opts.mStatusMask & opts.mStatusFilter) ==
(opts.mStatusMask & meta->mMsgStatus) )
{ {
std::cerr << "checkMsgFilter() Accepting Msg as StatusMatches: "; #ifdef DATA_DEBUG
std::cerr << " Mask: " << opts.mStatusMask << " StatusFilter: " << opts.mStatusFilter; std::cerr << __PRETTY_FUNCTION__
std::cerr << " MsgStatus: " << meta->mMsgStatus << " MsgId: " << meta->mMsgId; << " Continue checking Msg as StatusMatches: "
std::cerr << std::endl; << " Mask: " << opts.mStatusMask
<< " StatusFilter: " << opts.mStatusFilter
statusMatch = true; << " MsgStatus: " << meta->mMsgStatus
<< " MsgId: " << meta->mMsgId << std::endl;
#endif
} }
else else
{ {
std::cerr << "checkMsgFilter() Dropping Msg due to !StatusMatch "; #ifdef DATA_DEBUG
std::cerr << " Mask: " << opts.mStatusMask << " StatusFilter: " << opts.mStatusFilter; std::cerr << __PRETTY_FUNCTION__
std::cerr << " MsgStatus: " << meta->mMsgStatus << " MsgId: " << meta->mMsgId; << " Dropping Msg due to !StatusMatch "
std::cerr << std::endl; << " Mask: " << opts.mStatusMask
<< " StatusFilter: " << opts.mStatusFilter
<< " MsgStatus: " << meta->mMsgStatus
<< " MsgId: " << meta->mMsgId << std::endl;
#endif
return false;
} }
} }
else else
{ {
// no status comparision, #ifdef DATA_DEBUG
statusMatch = true; std::cerr << __PRETTY_FUNCTION__
<< " Status check not requested"
<< " mStatusMask: " << opts.mStatusMask
<< " MsgId: " << meta->mMsgId << std::endl;
#endif
} }
bool flagMatch = false; if(opts.mMsgFlagMask)
{
// Exact Flags match required.
if ( (opts.mMsgFlagMask & opts.mMsgFlagFilter) ==
(opts.mMsgFlagMask & meta->mMsgFlags) )
{
#ifdef DATA_DEBUG
std::cerr << __PRETTY_FUNCTION__
<< " Accepting Msg as FlagMatches: "
<< " Mask: " << opts.mMsgFlagMask
<< " FlagFilter: " << opts.mMsgFlagFilter
<< " MsgFlag: " << meta->mMsgFlags
<< " MsgId: " << meta->mMsgId << std::endl;
#endif
}
else
{
#ifdef DATA_DEBUG
std::cerr << __PRETTY_FUNCTION__
<< " Dropping Msg due to !FlagMatch "
<< " Mask: " << opts.mMsgFlagMask
<< " FlagFilter: " << opts.mMsgFlagFilter
<< " MsgFlag: " << meta->mMsgFlags
<< " MsgId: " << meta->mMsgId << std::endl;
#endif
if(opts.mMsgFlagMask) return false;
{ }
// Exact Flags match required. }
if ((opts.mMsgFlagMask & opts.mMsgFlagFilter) == (opts.mMsgFlagMask & meta->mMsgFlags)) else
{ {
std::cerr << "checkMsgFilter() Accepting Msg as FlagMatches: "; #ifdef DATA_DEBUG
std::cerr << " Mask: " << opts.mMsgFlagMask << " FlagFilter: " << opts.mMsgFlagFilter; std::cerr << __PRETTY_FUNCTION__
std::cerr << " MsgFlag: " << meta->mMsgFlags << " MsgId: " << meta->mMsgId; << " Flags check not requested"
std::cerr << std::endl; << " mMsgFlagMask: " << opts.mMsgFlagMask
<< " MsgId: " << meta->mMsgId << std::endl;
#endif
}
flagMatch = true; return true;
}
else
{
std::cerr << "checkMsgFilter() Dropping Msg due to !FlagMatch ";
std::cerr << " Mask: " << opts.mMsgFlagMask << " FlagFilter: " << opts.mMsgFlagFilter;
std::cerr << " MsgFlag: " << meta->mMsgFlags << " MsgId: " << meta->mMsgId;
std::cerr << std::endl;
flagMatch = false;
}
}else{
flagMatch = true;
}
return statusMatch && flagMatch;
} }

View File

@ -5290,7 +5290,8 @@ void RsGxsNetService::receiveTurtleSearchResults(TurtleRequestId req,const unsig
#ifdef NXS_NET_DEBUG_8 #ifdef NXS_NET_DEBUG_8
GXSNETDEBUG___ << " passing the grp data to observer." << std::endl; GXSNETDEBUG___ << " passing the grp data to observer." << std::endl;
#endif #endif
mObserver->receiveNewGroups(new_grps); mObserver->receiveNewGroups(new_grps);
mObserver->receiveDistantSearchResults(req, grpId);
} }
bool RsGxsNetService::search( const std::string& substring, bool RsGxsNetService::search( const std::string& substring,

View File

@ -22,7 +22,7 @@
* * * *
*******************************************************************************/ *******************************************************************************/
#include <inttypes.h> #include <cstdint>
#include <string> #include <string>
#include <list> #include <list>
#include <functional> #include <functional>
@ -32,6 +32,7 @@
#include "retroshare/rsgxscommon.h" #include "retroshare/rsgxscommon.h"
#include "serialiser/rsserializable.h" #include "serialiser/rsserializable.h"
#include "retroshare/rsturtle.h" #include "retroshare/rsturtle.h"
#include "util/rsdeprecate.h"
class RsGxsChannels; class RsGxsChannels;
@ -100,10 +101,70 @@ std::ostream &operator<<(std::ostream& out, const RsGxsChannelPost& post);
class RsGxsChannels: public RsGxsIfaceHelper, public RsGxsCommentService class RsGxsChannels: public RsGxsIfaceHelper, public RsGxsCommentService
{ {
public: public:
explicit RsGxsChannels(RsGxsIface& gxs) : RsGxsIfaceHelper(gxs) {} explicit RsGxsChannels(RsGxsIface& gxs) : RsGxsIfaceHelper(gxs) {}
virtual ~RsGxsChannels() {} virtual ~RsGxsChannels() {}
/**
* @brief Create channel. Blocking API.
* @jsonapi{development}
* @param[inout] channel Channel data (name, description...)
* @return false on error, true otherwise
*/
virtual bool createChannel(RsGxsChannelGroup& channel) = 0;
/**
* @brief Create channel post. Blocking API.
* @jsonapi{development}
* @param[inout] post
* @return false on error, true otherwise
*/
virtual bool createPost(RsGxsChannelPost& post) = 0;
/**
* @brief Edit channel details.
* @jsonapi{development}
* @param[in] channel Channel data (name, description...) with modifications
* @return false on error, true otherwise
*/
virtual bool editChannel(RsGxsChannelGroup& channel) = 0;
/**
* @brief Share extra file
* Can be used to share extra file attached to a channel post
* @jsonapi{development}
* @param[in] path file path
* @return false on error, true otherwise
*/
virtual bool ExtraFileHash(const std::string& path) = 0;
/**
* @brief Remove extra file from shared files
* @jsonapi{development}
* @param[in] hash hash of the file to remove
* @return false on error, true otherwise
*/
virtual bool ExtraFileRemove(const RsFileHash& hash) = 0;
/**
* @brief Get auto-download option value for given channel
* @jsonapi{development}
* @param[in] channelId channel id
* @param[out] enabled storage for the auto-download option value
* @return false if something failed, true otherwhise
*/
virtual bool getChannelAutoDownload(
const RsGxsGroupId& channelId, bool& enabled ) = 0;
/**
* @brief Get download directory for the given channel
* @jsonapi{development}
* @param[in] channelId id of the channel
* @param[out] directory reference to string where to store the path
* @return false on error, true otherwise
*/
virtual bool getChannelDownloadDirectory( const RsGxsGroupId& channelId,
std::string& directory ) = 0;
/** /**
* @brief Get channels summaries list. Blocking API. * @brief Get channels summaries list. Blocking API.
* @jsonapi{development} * @jsonapi{development}
@ -138,63 +199,37 @@ public:
std::vector<RsGxsComment>& comments ) = 0; std::vector<RsGxsComment>& comments ) = 0;
/** /**
* @brief Create channel. Blocking API. * @brief Toggle post read status. Blocking API.
* @jsonapi{development} * @jsonapi{development}
* @param[inout] channel Channel data (name, description...) * @param[in] postId post identifier
* @param[in] read true to mark as read, false to mark as unread
* @return false on error, true otherwise * @return false on error, true otherwise
*/ */
virtual bool createChannel(RsGxsChannelGroup& channel) = 0; virtual bool markRead(const RsGxsGrpMsgIdPair& postId, bool read) = 0;
/** /**
* @brief Create channel post. Blocking API. * @brief Enable or disable auto-download for given channel. Blocking API
* @jsonapi{development} * @jsonapi{development}
* @param[inout] post * @param[in] channelId channel id
* @return false on error, true otherwise
*/
virtual bool createPost(RsGxsChannelPost& post) = 0;
/* Specific Service Data
* TODO: change the orrible const uint32_t &token to uint32_t token
* TODO: create a new typedef for token so code is easier to read
*/
virtual bool getGroupData(const uint32_t &token, std::vector<RsGxsChannelGroup> &groups) = 0;
virtual bool getPostData(const uint32_t &token, std::vector<RsGxsChannelPost> &posts, std::vector<RsGxsComment> &cmts) = 0;
virtual bool getPostData(const uint32_t &token, std::vector<RsGxsChannelPost> &posts) = 0;
/**
* @brief toggle message read status
* @jsonapi{development}
* @param[out] token GXS token queue token
* @param[in] msgId
* @param[in] read
*/
virtual void setMessageReadStatus(
uint32_t& token, const RsGxsGrpMsgIdPair& msgId, bool read) = 0;
/**
* @brief Enable or disable auto-download for given channel
* @jsonapi{development}
* @param[in] groupId channel id
* @param[in] enable true to enable, false to disable * @param[in] enable true to enable, false to disable
* @return false if something failed, true otherwhise * @return false if something failed, true otherwhise
*/ */
virtual bool setChannelAutoDownload( virtual bool setChannelAutoDownload(
const RsGxsGroupId &groupId, bool enable) = 0; const RsGxsGroupId& channelId, bool enable ) = 0;
/** /**
* @brief Get auto-download option value for given channel * @brief Share channel publishing key
* This can be used to authorize other peers to post on the channel
* @jsonapi{development} * @jsonapi{development}
* @param[in] groupId channel id * @param[in] channelId id of the channel
* @param[in] enabled storage for the auto-download option value * @param[in] peers peers to share the key with
* @return false if something failed, true otherwhise * @return false on error, true otherwise
*/ */
virtual bool getChannelAutoDownload( virtual bool shareChannelKeys(
const RsGxsGroupId &groupId, bool& enabled) = 0; const RsGxsGroupId& channelId, const std::set<RsPeerId>& peers ) = 0;
/** /**
* @brief Set download directory for the given channel * @brief Set download directory for the given channel. Blocking API.
* @jsonapi{development} * @jsonapi{development}
* @param[in] channelId id of the channel * @param[in] channelId id of the channel
* @param[in] directory path * @param[in] directory path
@ -204,90 +239,14 @@ public:
const RsGxsGroupId& channelId, const std::string& directory) = 0; const RsGxsGroupId& channelId, const std::string& directory) = 0;
/** /**
* @brief Get download directory for the given channel * @brief Subscrbe to a channel. Blocking API
* @jsonapi{development} * @jsonapi{development}
* @param[in] channelId id of the channel * @param[in] channelId Channel id
* @param[out] directory reference to string where to store the path * @param[in] subscribe true to subscribe, false to unsubscribe
* @return false on error, true otherwise * @return false on error, true otherwise
*/ */
virtual bool getChannelDownloadDirectory( const RsGxsGroupId& channelId, virtual bool subscribeToChannel( const RsGxsGroupId &channelId,
std::string& directory ) = 0; bool subscribe ) = 0;
/**
* @brief Share channel publishing key
* This can be used to authorize other peers to post on the channel
* @jsonapi{development}
* @param[in] groupId Channel id
* @param[in] peers peers to which share the key
* @return false on error, true otherwise
*/
virtual bool groupShareKeys(
const RsGxsGroupId& groupId, const std::set<RsPeerId>& peers ) = 0;
/**
* @brief Request subscription to a group.
* The action is performed asyncronously, so it could fail in a subsequent
* phase even after returning true.
* @jsonapi{development}
* @param[out] token Storage for RsTokenService token to track request
* status.
* @param[in] groupId Channel id
* @param[in] subscribe
* @return false on error, true otherwise
*/
virtual bool subscribeToGroup( uint32_t& token, const RsGxsGroupId &groupId,
bool subscribe ) = 0;
/**
* @brief Request channel creation.
* The action is performed asyncronously, so it could fail in a subsequent
* phase even after returning true.
* @param[out] token Storage for RsTokenService token to track request
* status.
* @param[in] group Channel data (name, description...)
* @return false on error, true otherwise
*/
virtual bool createGroup(uint32_t& token, RsGxsChannelGroup& group) = 0;
/**
* @brief Request post creation.
* The action is performed asyncronously, so it could fail in a subsequent
* phase even after returning true.
* @param[out] token Storage for RsTokenService token to track request
* status.
* @param[in] post
* @return false on error, true otherwise
*/
virtual bool createPost(uint32_t& token, RsGxsChannelPost& post) = 0;
/**
* @brief Request channel change.
* The action is performed asyncronously, so it could fail in a subsequent
* phase even after returning true.
* @jsonapi{development}
* @param[out] token Storage for RsTokenService token to track request
* status.
* @param[in] group Channel data (name, description...) with modifications
* @return false on error, true otherwise
*/
virtual bool updateGroup(uint32_t& token, RsGxsChannelGroup& group) = 0;
/**
* @brief Share extra file
* Can be used to share extra file attached to a channel post
* @jsonapi{development}
* @param[in] path file path
* @return false on error, true otherwise
*/
virtual bool ExtraFileHash(const std::string& path) = 0;
/**
* @brief Remove extra file from shared files
* @jsonapi{development}
* @param[in] hash hash of the file to remove
* @return false on error, true otherwise
*/
virtual bool ExtraFileRemove(const RsFileHash& hash) = 0;
/** /**
* @brief Request remote channels search * @brief Request remote channels search
@ -303,15 +262,123 @@ public:
const std::function<void (const RsGxsGroupSummary& result)>& multiCallback, const std::function<void (const RsGxsGroupSummary& result)>& multiCallback,
rstime_t maxWait = 300 ) = 0; rstime_t maxWait = 300 ) = 0;
/**
* @brief Request remote channel
* @jsonapi{development}
* @param[in] channelId id of the channel to request to distants peers
* @param multiCallback function that will be called each time a result is
* received
* @param[in] maxWait maximum wait time in seconds for search results
* @return false on error, true otherwise
*/
virtual bool turtleChannelRequest(
const RsGxsGroupId& channelId,
const std::function<void (const RsGxsChannelGroup& result)>& multiCallback,
rstime_t maxWait = 300 ) = 0;
/* Following functions are deprecated as they expose internal functioning
* semantic, instead of a safe to use API */
RS_DEPRECATED_FOR(getChannelsInfo)
virtual bool getGroupData(const uint32_t &token, std::vector<RsGxsChannelGroup> &groups) = 0;
RS_DEPRECATED_FOR(getChannelsContent)
virtual bool getPostData(const uint32_t &token, std::vector<RsGxsChannelPost> &posts, std::vector<RsGxsComment> &cmts) = 0;
RS_DEPRECATED_FOR(getChannelsContent)
virtual bool getPostData(const uint32_t &token, std::vector<RsGxsChannelPost> &posts) = 0;
/**
* @brief toggle message read status
* @deprecated
* @param[out] token GXS token queue token
* @param[in] msgId
* @param[in] read
*/
RS_DEPRECATED_FOR(markRead)
virtual void setMessageReadStatus(
uint32_t& token, const RsGxsGrpMsgIdPair& msgId, bool read) = 0;
/**
* @brief Share channel publishing key
* This can be used to authorize other peers to post on the channel
* @deprecated
* @param[in] groupId Channel id
* @param[in] peers peers to which share the key
* @return false on error, true otherwise
*/
RS_DEPRECATED_FOR(shareChannelKeys)
virtual bool groupShareKeys(
const RsGxsGroupId& groupId, const std::set<RsPeerId>& peers ) = 0;
/**
* @brief Request subscription to a group.
* The action is performed asyncronously, so it could fail in a subsequent
* phase even after returning true.
* @deprecated
* @param[out] token Storage for RsTokenService token to track request
* status.
* @param[in] groupId Channel id
* @param[in] subscribe
* @return false on error, true otherwise
*/
RS_DEPRECATED_FOR(subscribeToChannel)
virtual bool subscribeToGroup( uint32_t& token, const RsGxsGroupId &groupId,
bool subscribe ) = 0;
/**
* @brief Request channel creation.
* The action is performed asyncronously, so it could fail in a subsequent
* phase even after returning true.
* @deprecated
* @param[out] token Storage for RsTokenService token to track request
* status.
* @param[in] group Channel data (name, description...)
* @return false on error, true otherwise
*/
RS_DEPRECATED_FOR(createChannel)
virtual bool createGroup(uint32_t& token, RsGxsChannelGroup& group) = 0;
/**
* @brief Request post creation.
* The action is performed asyncronously, so it could fail in a subsequent
* phase even after returning true.
* @deprecated
* @param[out] token Storage for RsTokenService token to track request
* status.
* @param[in] post
* @return false on error, true otherwise
*/
RS_DEPRECATED
virtual bool createPost(uint32_t& token, RsGxsChannelPost& post) = 0;
/**
* @brief Request channel change.
* The action is performed asyncronously, so it could fail in a subsequent
* phase even after returning true.
* @deprecated
* @param[out] token Storage for RsTokenService token to track request
* status.
* @param[in] group Channel data (name, description...) with modifications
* @return false on error, true otherwise
*/
RS_DEPRECATED_FOR(editChannel)
virtual bool updateGroup(uint32_t& token, RsGxsChannelGroup& group) = 0;
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
/// Distant synchronisation methods /// /// Distant synchronisation methods ///
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
/// ///
RS_DEPRECATED_FOR(turtleChannelRequest)
virtual TurtleRequestId turtleGroupRequest(const RsGxsGroupId& group_id)=0; virtual TurtleRequestId turtleGroupRequest(const RsGxsGroupId& group_id)=0;
RS_DEPRECATED
virtual TurtleRequestId turtleSearchRequest(const std::string& match_string)=0; virtual TurtleRequestId turtleSearchRequest(const std::string& match_string)=0;
RS_DEPRECATED_FOR(turtleSearchRequest)
virtual bool retrieveDistantSearchResults(TurtleRequestId req, std::map<RsGxsGroupId, RsGxsGroupSummary> &results) =0; virtual bool retrieveDistantSearchResults(TurtleRequestId req, std::map<RsGxsGroupId, RsGxsGroupSummary> &results) =0;
RS_DEPRECATED
virtual bool clearDistantSearchResults(TurtleRequestId req)=0; virtual bool clearDistantSearchResults(TurtleRequestId req)=0;
RS_DEPRECATED_FOR(turtleChannelRequest)
virtual bool retrieveDistantGroup(const RsGxsGroupId& group_id,RsGxsChannelGroup& distant_group)=0; virtual bool retrieveDistantGroup(const RsGxsGroupId& group_id,RsGxsChannelGroup& distant_group)=0;
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
}; };

View File

@ -76,7 +76,10 @@ p3GxsChannels::p3GxsChannels(
RsGenExchange( gds, nes, new RsGxsChannelSerialiser(), RsGenExchange( gds, nes, new RsGxsChannelSerialiser(),
RS_SERVICE_GXS_TYPE_CHANNELS, gixs, channelsAuthenPolicy() ), RS_SERVICE_GXS_TYPE_CHANNELS, gixs, channelsAuthenPolicy() ),
RsGxsChannels(static_cast<RsGxsIface&>(*this)), GxsTokenQueue(this), RsGxsChannels(static_cast<RsGxsIface&>(*this)), GxsTokenQueue(this),
mSearchCallbacksMapMutex("GXS channels search") mSubscribedGroupsMutex("GXS channels subscribed groups cache"),
mKnownChannelsMutex("GXS channels known channels timestamp cache"),
mSearchCallbacksMapMutex("GXS channels search callbacks map"),
mDistantChannelsCallbacksMapMutex("GXS channels distant channels callbacks map")
{ {
// For Dummy Msgs. // For Dummy Msgs.
mGenActive = false; mGenActive = false;
@ -174,7 +177,10 @@ bool p3GxsChannels::saveList(bool &cleanup, std::list<RsItem *>&saveList)
RsGxsForumNotifyRecordsItem *item = new RsGxsForumNotifyRecordsItem ; RsGxsForumNotifyRecordsItem *item = new RsGxsForumNotifyRecordsItem ;
item->records = mKnownChannels ; {
RS_STACK_MUTEX(mKnownChannelsMutex);
item->records = mKnownChannels;
}
saveList.push_back(item) ; saveList.push_back(item) ;
return true; return true;
@ -191,8 +197,9 @@ bool p3GxsChannels::loadList(std::list<RsItem *>& loadList)
RsGxsForumNotifyRecordsItem *fnr = dynamic_cast<RsGxsForumNotifyRecordsItem*>(item) ; RsGxsForumNotifyRecordsItem *fnr = dynamic_cast<RsGxsForumNotifyRecordsItem*>(item) ;
if(fnr != NULL) if(fnr)
{ {
RS_STACK_MUTEX(mKnownChannelsMutex);
mKnownChannels.clear(); mKnownChannels.clear();
for(auto it(fnr->records.begin());it!=fnr->records.end();++it) for(auto it(fnr->records.begin());it!=fnr->records.end();++it)
@ -228,7 +235,7 @@ void p3GxsChannels::notifyChanges(std::vector<RsGxsNotify *> &changes)
std::cerr << "p3GxsChannels::notifyChanges() : " << changes.size() << "changes to notify" << std::endl; std::cerr << "p3GxsChannels::notifyChanges() : " << changes.size() << "changes to notify" << std::endl;
#endif #endif
p3Notify *notify = NULL; p3Notify* notify = nullptr;
if (!changes.empty()) if (!changes.empty())
{ {
notify = RsServer::notify(); notify = RsServer::notify();
@ -271,9 +278,8 @@ void p3GxsChannels::notifyChanges(std::vector<RsGxsNotify *> &changes)
std::cerr << "p3GxsChannels::notifyChanges() Msgs for Group: " << mit->first; std::cerr << "p3GxsChannels::notifyChanges() Msgs for Group: " << mit->first;
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
bool enabled = false ; bool enabled = false;
if (autoDownloadEnabled(mit->first, enabled) && enabled)
if (autoDownloadEnabled(mit->first, enabled) && enabled)
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::notifyChanges() AutoDownload for Group: " << mit->first; std::cerr << "p3GxsChannels::notifyChanges() AutoDownload for Group: " << mit->first;
@ -306,6 +312,7 @@ void p3GxsChannels::notifyChanges(std::vector<RsGxsNotify *> &changes)
/* group received */ /* group received */
std::list<RsGxsGroupId> &grpList = grpChange->mGrpIdList; std::list<RsGxsGroupId> &grpList = grpChange->mGrpIdList;
std::list<RsGxsGroupId>::iterator git; std::list<RsGxsGroupId>::iterator git;
RS_STACK_MUTEX(mKnownChannelsMutex);
for (git = grpList.begin(); git != grpList.end(); ++git) for (git = grpList.begin(); git != grpList.end(); ++git)
{ {
if(mKnownChannels.find(*git) == mKnownChannels.end()) if(mKnownChannels.find(*git) == mKnownChannels.end())
@ -338,15 +345,15 @@ void p3GxsChannels::notifyChanges(std::vector<RsGxsNotify *> &changes)
/* shouldn't need to worry about groups - as they need to be subscribed to */ /* shouldn't need to worry about groups - as they need to be subscribed to */
} }
request_SpecificSubscribedGroups(unprocessedGroups); if(!unprocessedGroups.empty())
request_SpecificSubscribedGroups(unprocessedGroups);
RsGxsIfaceHelper::receiveChanges(changes); RsGxsIfaceHelper::receiveChanges(changes);
} }
void p3GxsChannels::service_tick() void p3GxsChannels::service_tick()
{ {
static rstime_t last_dummy_tick = 0;
static rstime_t last_dummy_tick = 0;
if (time(NULL) > last_dummy_tick + 5) if (time(NULL) > last_dummy_tick + 5)
{ {
@ -414,75 +421,84 @@ bool p3GxsChannels::groupShareKeys(
* at the moment - fix it up later * at the moment - fix it up later
*/ */
bool p3GxsChannels::getPostData(const uint32_t &token, std::vector<RsGxsChannelPost> &msgs, std::vector<RsGxsComment> &cmts) bool p3GxsChannels::getPostData(
const uint32_t &token, std::vector<RsGxsChannelPost> &msgs,
std::vector<RsGxsComment> &cmts )
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::getPostData()"; std::cerr << __PRETTY_FUNCTION__ << std::cerr << std::endl;
std::cerr << std::endl;
#endif #endif
GxsMsgDataMap msgData; GxsMsgDataMap msgData;
bool ok = RsGenExchange::getMsgData(token, msgData); if(!RsGenExchange::getMsgData(token, msgData))
if(ok)
{ {
GxsMsgDataMap::iterator mit = msgData.begin(); std::cerr << __PRETTY_FUNCTION__ <<" ERROR in request" << std::endl;
return false;
}
for(; mit != msgData.end(); ++mit) GxsMsgDataMap::iterator mit = msgData.begin();
for(; mit != msgData.end(); ++mit)
{
std::vector<RsGxsMsgItem*>& msgItems = mit->second;
std::vector<RsGxsMsgItem*>::iterator vit = msgItems.begin();
for(; vit != msgItems.end(); ++vit)
{ {
std::vector<RsGxsMsgItem*>& msgItems = mit->second; RsGxsChannelPostItem* postItem =
std::vector<RsGxsMsgItem*>::iterator vit = msgItems.begin(); dynamic_cast<RsGxsChannelPostItem*>(*vit);
for(; vit != msgItems.end(); ++vit) if(postItem)
{ {
RsGxsChannelPostItem* postItem = dynamic_cast<RsGxsChannelPostItem*>(*vit); RsGxsChannelPost msg;
postItem->toChannelPost(msg, true);
if(postItem) msgs.push_back(msg);
delete postItem;
}
else
{
RsGxsCommentItem* cmtItem =
dynamic_cast<RsGxsCommentItem*>(*vit);
if(cmtItem)
{ {
RsGxsChannelPost msg; RsGxsComment cmt;
postItem->toChannelPost(msg, true); RsGxsMsgItem *mi = (*vit);
msgs.push_back(msg); cmt = cmtItem->mMsg;
delete postItem; cmt.mMeta = mi->meta;
#ifdef GXSCOMMENT_DEBUG
std::cerr << "p3GxsChannels::getPostData Found Comment:" << std::endl;
cmt.print(std::cerr," ", "cmt");
#endif
cmts.push_back(cmt);
delete cmtItem;
} }
else else
{ {
RsGxsCommentItem* cmtItem = dynamic_cast<RsGxsCommentItem*>(*vit); RsGxsMsgItem* msg = (*vit);
if(cmtItem) //const uint16_t RS_SERVICE_GXS_TYPE_CHANNELS = 0x0217;
{ //const uint8_t RS_PKT_SUBTYPE_GXSCHANNEL_POST_ITEM = 0x03;
RsGxsComment cmt; //const uint8_t RS_PKT_SUBTYPE_GXSCOMMENT_COMMENT_ITEM = 0xf1;
RsGxsMsgItem *mi = (*vit); std::cerr << __PRETTY_FUNCTION__
cmt = cmtItem->mMsg; << " Not a GxsChannelPostItem neither a "
cmt.mMeta = mi->meta; << "RsGxsCommentItem PacketService=" << std::hex
#ifdef GXSCOMMENT_DEBUG << (int)msg->PacketService() << std::dec
std::cerr << "p3GxsChannels::getPostData Found Comment:" << std::endl; << " PacketSubType=" << std::hex
cmt.print(std::cerr," ", "cmt"); << (int)msg->PacketSubType() << std::dec
#endif << " , deleting!" << std::endl;
cmts.push_back(cmt); delete *vit;
delete cmtItem;
}
else
{
RsGxsMsgItem* msg = (*vit);
//const uint16_t RS_SERVICE_GXS_TYPE_CHANNELS = 0x0217;
//const uint8_t RS_PKT_SUBTYPE_GXSCHANNEL_POST_ITEM = 0x03;
//const uint8_t RS_PKT_SUBTYPE_GXSCOMMENT_COMMENT_ITEM = 0xf1;
std::cerr << "Not a GxsChannelPostItem neither a RsGxsCommentItem"
<< " PacketService=" << std::hex << (int)msg->PacketService() << std::dec
<< " PacketSubType=" << std::hex << (int)msg->PacketSubType() << std::dec
<< " , deleting!" << std::endl;
delete *vit;
}
} }
} }
} }
} }
else
{
std::cerr << "p3GxsChannels::getPostData() ERROR in request";
std::cerr << std::endl;
}
return ok; return true;
}
bool p3GxsChannels::getPostData(
const uint32_t& token, std::vector<RsGxsChannelPost>& posts )
{
std::vector<RsGxsComment> cmts;
return getPostData(token, posts, cmts);
} }
//Not currently used //Not currently used
@ -548,21 +564,6 @@ bool p3GxsChannels::getPostData(const uint32_t &token, std::vector<RsGxsChannelP
/********************************************************************************************/ /********************************************************************************************/
/********************************************************************************************/ /********************************************************************************************/
#if 0
bool p3GxsChannels::setChannelAutoDownload(uint32_t &token, const RsGxsGroupId &groupId, bool autoDownload)
{
std::cerr << "p3GxsChannels::setChannelAutoDownload()";
std::cerr << std::endl;
// we don't actually use the token at this point....
//bool p3GxsChannels::setAutoDownload(const RsGxsGroupId &groupId, bool enabled)
return;
}
#endif
bool p3GxsChannels::setChannelAutoDownload(const RsGxsGroupId &groupId, bool enabled) bool p3GxsChannels::setChannelAutoDownload(const RsGxsGroupId &groupId, bool enabled)
{ {
return setAutoDownload(groupId, enabled); return setAutoDownload(groupId, enabled);
@ -574,36 +575,37 @@ bool p3GxsChannels::getChannelAutoDownload(const RsGxsGroupId &groupId, bool& en
return autoDownloadEnabled(groupId,enabled); return autoDownloadEnabled(groupId,enabled);
} }
bool p3GxsChannels::setChannelDownloadDirectory(const RsGxsGroupId &groupId, const std::string& directory) bool p3GxsChannels::setChannelDownloadDirectory(
const RsGxsGroupId &groupId, const std::string& directory )
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::setDownloadDirectory() id: " << groupId << " to: " << directory << std::endl; std::cerr << __PRETTY_FUNCTION__ << " id: " << groupId << " to: "
<< directory << std::endl;
#endif #endif
RS_STACK_MUTEX(mSubscribedGroupsMutex);
std::map<RsGxsGroupId, RsGroupMetaData>::iterator it; std::map<RsGxsGroupId, RsGroupMetaData>::iterator it;
it = mSubscribedGroups.find(groupId);
it = mSubscribedGroups.find(groupId); if (it == mSubscribedGroups.end())
if (it == mSubscribedGroups.end()) {
{ std::cerr << __PRETTY_FUNCTION__ << " Error! Unknown groupId: "
#ifdef GXSCHANNELS_DEBUG << groupId.toStdString() << std::endl;
std::cerr << "p3GxsChannels::setAutoDownload() Missing Group" << std::endl; return false;
#endif }
return false;
}
/* extract from ServiceString */ /* extract from ServiceString */
SSGxsChannelGroup ss; SSGxsChannelGroup ss;
ss.load(it->second.mServiceString); ss.load(it->second.mServiceString);
if (directory == ss.mDownloadDirectory) if (directory == ss.mDownloadDirectory)
{ {
#ifdef GXSCHANNELS_DEBUG std::cerr << __PRETTY_FUNCTION__ << " Warning! groupId: " << groupId
std::cerr << "p3GxsChannels::setDownloadDirectory() WARNING setting looks okay already" << std::endl; << " Was already configured to download into: " << directory
#endif << std::endl;
return false;
}
}
/* we are just going to set it anyway. */
ss.mDownloadDirectory = directory; ss.mDownloadDirectory = directory;
std::string serviceString = ss.save(); std::string serviceString = ss.save();
uint32_t token; uint32_t token;
@ -611,6 +613,13 @@ bool p3GxsChannels::setChannelDownloadDirectory(const RsGxsGroupId &groupId, con
it->second.mServiceString = serviceString; // update Local Cache. it->second.mServiceString = serviceString; // update Local Cache.
RsGenExchange::setGroupServiceString(token, groupId, serviceString); // update dbase. RsGenExchange::setGroupServiceString(token, groupId, serviceString); // update dbase.
if(waitToken(token) != RsTokenService::COMPLETE)
{
std::cerr << __PRETTY_FUNCTION__ << " Error! Feiled setting group "
<< " service string" << std::endl;
return false;
}
/* now reload it */ /* now reload it */
std::list<RsGxsGroupId> groups; std::list<RsGxsGroupId> groups;
groups.push_back(groupId); groups.push_back(groupId);
@ -626,25 +635,24 @@ bool p3GxsChannels::getChannelDownloadDirectory(const RsGxsGroupId & groupId,std
std::cerr << "p3GxsChannels::getChannelDownloadDirectory(" << id << ")" << std::endl; std::cerr << "p3GxsChannels::getChannelDownloadDirectory(" << id << ")" << std::endl;
#endif #endif
RS_STACK_MUTEX(mSubscribedGroupsMutex);
std::map<RsGxsGroupId, RsGroupMetaData>::iterator it; std::map<RsGxsGroupId, RsGroupMetaData>::iterator it;
it = mSubscribedGroups.find(groupId); it = mSubscribedGroups.find(groupId);
if (it == mSubscribedGroups.end())
if (it == mSubscribedGroups.end()) {
{ std::cerr << __PRETTY_FUNCTION__ << " Error! Unknown groupId: "
#ifdef GXSCHANNELS_DEBUG << groupId.toStdString() << std::endl;
std::cerr << "p3GxsChannels::getChannelDownloadDirectory() No Entry" << std::endl; return false;
#endif }
return false;
}
/* extract from ServiceString */ /* extract from ServiceString */
SSGxsChannelGroup ss; SSGxsChannelGroup ss;
ss.load(it->second.mServiceString); ss.load(it->second.mServiceString);
directory = ss.mDownloadDirectory; directory = ss.mDownloadDirectory;
return true ; return true;
} }
void p3GxsChannels::request_AllSubscribedGroups() void p3GxsChannels::request_AllSubscribedGroups()
@ -668,7 +676,8 @@ void p3GxsChannels::request_AllSubscribedGroups()
} }
void p3GxsChannels::request_SpecificSubscribedGroups(const std::list<RsGxsGroupId> &groups) void p3GxsChannels::request_SpecificSubscribedGroups(
const std::list<RsGxsGroupId> &groups )
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::request_SpecificSubscribedGroups()"; std::cerr << "p3GxsChannels::request_SpecificSubscribedGroups()";
@ -681,8 +690,19 @@ void p3GxsChannels::request_SpecificSubscribedGroups(const std::list<RsGxsGroupI
uint32_t token = 0; uint32_t token = 0;
RsGenExchange::getTokenService()->requestGroupInfo(token, ansType, opts, groups); if(!RsGenExchange::getTokenService()->
GxsTokenQueue::queueRequest(token, GXSCHANNELS_SUBSCRIBED_META); requestGroupInfo(token, ansType, opts, groups))
{
std::cerr << __PRETTY_FUNCTION__ << " Failed requesting groups info!"
<< std::endl;
return;
}
if(!GxsTokenQueue::queueRequest(token, GXSCHANNELS_SUBSCRIBED_META))
{
std::cerr << __PRETTY_FUNCTION__ << " Failed queuing request!"
<< std::endl;
}
} }
@ -746,6 +766,7 @@ void p3GxsChannels::updateSubscribedGroup(const RsGroupMetaData &group)
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
RS_STACK_MUTEX(mSubscribedGroupsMutex);
mSubscribedGroups[group.mGroupId] = group; mSubscribedGroups[group.mGroupId] = group;
} }
@ -757,9 +778,8 @@ void p3GxsChannels::clearUnsubscribedGroup(const RsGxsGroupId &id)
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
//std::map<RsGxsGroupId, RsGrpMetaData> mSubscribedGroups; RS_STACK_MUTEX(mSubscribedGroupsMutex);
std::map<RsGxsGroupId, RsGroupMetaData>::iterator it; std::map<RsGxsGroupId, RsGroupMetaData>::iterator it;
it = mSubscribedGroups.find(id); it = mSubscribedGroups.find(id);
if (it != mSubscribedGroups.end()) if (it != mSubscribedGroups.end())
{ {
@ -838,24 +858,20 @@ void p3GxsChannels::request_GroupUnprocessedPosts(const std::list<RsGxsGroupId>
} }
void p3GxsChannels::load_SpecificUnprocessedPosts(const uint32_t &token) void p3GxsChannels::load_unprocessedPosts(uint32_t token)
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::load_SpecificUnprocessedPosts"; std::cerr << "p3GxsChannels::load_SpecificUnprocessedPosts" << std::endl;
std::cerr << std::endl;
#endif #endif
std::vector<RsGxsChannelPost> posts; std::vector<RsGxsChannelPost> posts;
if (!getPostData(token, posts)) if (!getPostData(token, posts))
{ {
#ifdef GXSCHANNELS_DEBUG std::cerr << __PRETTY_FUNCTION__ << " ERROR getting post data!"
std::cerr << "p3GxsChannels::load_SpecificUnprocessedPosts ERROR"; << std::endl;
std::cerr << std::endl;
#endif
return; return;
} }
std::vector<RsGxsChannelPost>::iterator it; std::vector<RsGxsChannelPost>::iterator it;
for(it = posts.begin(); it != posts.end(); ++it) for(it = posts.begin(); it != posts.end(); ++it)
{ {
@ -864,58 +880,27 @@ void p3GxsChannels::load_SpecificUnprocessedPosts(const uint32_t &token)
} }
} }
void p3GxsChannels::load_GroupUnprocessedPosts(const uint32_t &token)
{
#ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::load_GroupUnprocessedPosts";
std::cerr << std::endl;
#endif
std::vector<RsGxsChannelPost> posts;
if (!getPostData(token, posts))
{
#ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::load_GroupUnprocessedPosts ERROR";
std::cerr << std::endl;
#endif
return;
}
std::vector<RsGxsChannelPost>::iterator it;
for(it = posts.begin(); it != posts.end(); ++it)
{
handleUnprocessedPost(*it);
}
}
void p3GxsChannels::handleUnprocessedPost(const RsGxsChannelPost &msg) void p3GxsChannels::handleUnprocessedPost(const RsGxsChannelPost &msg)
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::handleUnprocessedPost() GroupId: " << msg.mMeta.mGroupId << " MsgId: " << msg.mMeta.mMsgId; std::cerr << __PRETTY_FUNCTION__ << " GroupId: " << msg.mMeta.mGroupId
std::cerr << std::endl; << " MsgId: " << msg.mMeta.mMsgId << std::endl;
#endif #endif
if (!IS_MSG_UNPROCESSED(msg.mMeta.mMsgStatus)) if (!IS_MSG_UNPROCESSED(msg.mMeta.mMsgStatus))
{ {
std::cerr << "p3GxsChannels::handleUnprocessedPost() Msg already Processed"; std::cerr << __PRETTY_FUNCTION__ << " ERROR Msg already Processed! "
std::cerr << std::endl; << "mMsgId: " << msg.mMeta.mMsgId << std::endl;
std::cerr << "p3GxsChannels::handleUnprocessedPost() ERROR - this should not happen";
std::cerr << std::endl;
return; return;
} }
bool enabled = false ;
/* check that autodownload is set */ /* check that autodownload is set */
if (autoDownloadEnabled(msg.mMeta.mGroupId,enabled) && enabled ) bool enabled = false;
if (autoDownloadEnabled(msg.mMeta.mGroupId, enabled) && enabled)
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::handleUnprocessedPost() AutoDownload Enabled ... handling"; std::cerr << __PRETTY_FUNCTION__ << " AutoDownload Enabled... handling"
std::cerr << std::endl; << std::endl;
#endif #endif
/* check the date is not too old */ /* check the date is not too old */
@ -928,8 +913,7 @@ void p3GxsChannels::handleUnprocessedPost(const RsGxsChannelPost &msg)
// MORE THOUGHT HAS TO GO INTO THAT STUFF. // MORE THOUGHT HAS TO GO INTO THAT STUFF.
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::handleUnprocessedPost() START DOWNLOAD"; std::cerr << __PRETTY_FUNCTION__ << " START DOWNLOAD" << std::endl;
std::cerr << std::endl;
#endif #endif
std::list<RsGxsFile>::const_iterator fit; std::list<RsGxsFile>::const_iterator fit;
@ -951,8 +935,11 @@ void p3GxsChannels::handleUnprocessedPost(const RsGxsChannelPost &msg)
rsFiles->FileRequest(fname, hash, size, localpath, flags, srcIds); rsFiles->FileRequest(fname, hash, size, localpath, flags, srcIds);
} }
else else
std::cerr << "WARNING: Channel file is not auto-downloaded because its size exceeds the threshold of " << CHANNEL_MAX_AUTO_DL << " bytes." << std::endl; std::cerr << __PRETTY_FUNCTION__ << "Channel file is not auto-"
<< "downloaded because its size exceeds the threshold"
<< " of " << CHANNEL_MAX_AUTO_DL << " bytes."
<< std::endl;
} }
} }
@ -986,19 +973,18 @@ void p3GxsChannels::handleResponse(uint32_t token, uint32_t req_type)
load_SubscribedGroups(token); load_SubscribedGroups(token);
break; break;
case GXSCHANNELS_UNPROCESSED_SPECIFIC: case GXSCHANNELS_UNPROCESSED_SPECIFIC:
load_SpecificUnprocessedPosts(token); load_unprocessedPosts(token);
break; break;
case GXSCHANNELS_UNPROCESSED_GENERIC: case GXSCHANNELS_UNPROCESSED_GENERIC:
load_SpecificUnprocessedPosts(token); load_unprocessedPosts(token);
break; break;
default: default:
/* error */ std::cerr << __PRETTY_FUNCTION__ << "ERROR Unknown Request Type: "
std::cerr << "p3GxsService::handleResponse() Unknown Request Type: " << req_type; << req_type << std::endl;
std::cerr << std::endl; break;
break;
} }
} }
@ -1045,20 +1031,63 @@ bool p3GxsChannels::getChannelsContent(
bool p3GxsChannels::createChannel(RsGxsChannelGroup& channel) bool p3GxsChannels::createChannel(RsGxsChannelGroup& channel)
{ {
uint32_t token; uint32_t token;
if( !createGroup(token, channel) if(!createGroup(token, channel))
|| waitToken(token) != RsTokenService::COMPLETE )
return false;
if(RsGenExchange::getPublishedGroupMeta(token, channel.mMeta))
{ {
#ifdef RS_DEEP_SEARCH std::cerr << __PRETTY_FUNCTION__ << "Error! Failed updating group."
DeepSearch::indexChannelGroup(channel); << std::endl;
#endif // RS_DEEP_SEARCH return false;
return true;
} }
return false; if(waitToken(token) != RsTokenService::COMPLETE)
{
std::cerr << __PRETTY_FUNCTION__ << "Error! GXS operation failed."
<< std::endl;
return false;
}
if(!RsGenExchange::getPublishedGroupMeta(token, channel.mMeta))
{
std::cerr << __PRETTY_FUNCTION__ << "Error! Failure getting updated "
<< " group data." << std::endl;
return false;
}
#ifdef RS_DEEP_SEARCH
DeepSearch::indexChannelGroup(channel);
#endif // RS_DEEP_SEARCH
return true;
}
bool p3GxsChannels::editChannel(RsGxsChannelGroup& channel)
{
uint32_t token;
if(!updateGroup(token, channel))
{
std::cerr << __PRETTY_FUNCTION__ << "Error! Failed updating group."
<< std::endl;
return false;
}
if(waitToken(token) != RsTokenService::COMPLETE)
{
std::cerr << __PRETTY_FUNCTION__ << "Error! GXS operation failed."
<< std::endl;
return false;
}
if(!RsGenExchange::getPublishedGroupMeta(token, channel.mMeta))
{
std::cerr << __PRETTY_FUNCTION__ << "Error! Failure getting updated "
<< " group data." << std::endl;
return false;
}
#ifdef RS_DEEP_SEARCH
DeepSearch::indexChannelGroup(channel);
#endif // RS_DEEP_SEARCH
return true;
} }
bool p3GxsChannels::createPost(RsGxsChannelPost& post) bool p3GxsChannels::createPost(RsGxsChannelPost& post)
@ -1079,6 +1108,29 @@ bool p3GxsChannels::createPost(RsGxsChannelPost& post)
return false; return false;
} }
bool p3GxsChannels::subscribeToChannel(
const RsGxsGroupId& groupId, bool subscribe )
{
uint32_t token;
if( !subscribeToGroup(token, groupId, subscribe)
|| waitToken(token) != RsTokenService::COMPLETE ) return false;
return true;
}
bool p3GxsChannels::markRead(const RsGxsGrpMsgIdPair& msgId, bool read)
{
uint32_t token;
setMessageReadStatus(token, msgId, read);
if(waitToken(token) != RsTokenService::COMPLETE ) return false;
return true;
}
bool p3GxsChannels::shareChannelKeys(
const RsGxsGroupId& channelId, const std::set<RsPeerId>& peers)
{
return groupShareKeys(channelId, peers);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// Blocking API implementation end /// Blocking API implementation end
@ -1092,20 +1144,17 @@ bool p3GxsChannels::createPost(RsGxsChannelPost& post)
bool p3GxsChannels::autoDownloadEnabled(const RsGxsGroupId &groupId,bool& enabled) bool p3GxsChannels::autoDownloadEnabled(const RsGxsGroupId &groupId,bool& enabled)
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::autoDownloadEnabled(" << id << ")"; std::cerr << "p3GxsChannels::autoDownloadEnabled(" << groupId << ")";
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
RS_STACK_MUTEX(mSubscribedGroupsMutex);
std::map<RsGxsGroupId, RsGroupMetaData>::iterator it; std::map<RsGxsGroupId, RsGroupMetaData>::iterator it;
it = mSubscribedGroups.find(groupId); it = mSubscribedGroups.find(groupId);
if (it == mSubscribedGroups.end()) if (it == mSubscribedGroups.end())
{ {
#ifdef GXSCHANNELS_DEBUG std::cerr << __PRETTY_FUNCTION__ << " WARNING requested channel: "
std::cerr << "p3GxsChannels::autoDownloadEnabled() No Entry"; << groupId << " is not subscribed" << std::endl;
std::cerr << std::endl;
#endif
return false; return false;
} }
@ -1190,23 +1239,20 @@ std::string SSGxsChannelGroup::save() const
return output; return output;
} }
bool p3GxsChannels::setAutoDownload(const RsGxsGroupId &groupId, bool enabled) bool p3GxsChannels::setAutoDownload(const RsGxsGroupId& groupId, bool enabled)
{ {
#ifdef GXSCHANNELS_DEBUG #ifdef GXSCHANNELS_DEBUG
std::cerr << "p3GxsChannels::setAutoDownload() id: " << groupId << " enabled: " << enabled; std::cerr << __PRETTY_FUNCTION__ << " id: " << groupId
std::cerr << std::endl; << " enabled: " << enabled << std::endl;
#endif #endif
RS_STACK_MUTEX(mSubscribedGroupsMutex);
std::map<RsGxsGroupId, RsGroupMetaData>::iterator it; std::map<RsGxsGroupId, RsGroupMetaData>::iterator it;
it = mSubscribedGroups.find(groupId); it = mSubscribedGroups.find(groupId);
if (it == mSubscribedGroups.end()) if (it == mSubscribedGroups.end())
{ {
#ifdef GXSCHANNELS_DEBUG std::cerr << __PRETTY_FUNCTION__ << " ERROR requested channel: "
std::cerr << "p3GxsChannels::setAutoDownload() Missing Group"; << groupId.toStdString() << " is not subscribed!" << std::endl;
std::cerr << std::endl;
#endif
return false; return false;
} }
@ -1215,27 +1261,21 @@ bool p3GxsChannels::setAutoDownload(const RsGxsGroupId &groupId, bool enabled)
ss.load(it->second.mServiceString); ss.load(it->second.mServiceString);
if (enabled == ss.mAutoDownload) if (enabled == ss.mAutoDownload)
{ {
/* it should be okay! */ std::cerr << __PRETTY_FUNCTION__ << " WARNING mAutoDownload was already"
#ifdef GXSCHANNELS_DEBUG << " properly set to: " << enabled << " for channel:"
std::cerr << "p3GxsChannels::setAutoDownload() WARNING setting looks okay already"; << groupId.toStdString() << std::endl;
std::cerr << std::endl; return false;
#endif
} }
/* we are just going to set it anyway. */
ss.mAutoDownload = enabled; ss.mAutoDownload = enabled;
std::string serviceString = ss.save(); std::string serviceString = ss.save();
uint32_t token; uint32_t token;
RsGenExchange::setGroupServiceString(token, groupId, serviceString);
if(waitToken(token) != RsTokenService::COMPLETE) return false;
it->second.mServiceString = serviceString; // update Local Cache. it->second.mServiceString = serviceString; // update Local Cache.
RsGenExchange::setGroupServiceString(token, groupId, serviceString); // update dbase.
/* now reload it */
std::list<RsGxsGroupId> groups;
groups.push_back(groupId);
request_SpecificSubscribedGroups(groups);
return true; return true;
} }
@ -1604,7 +1644,7 @@ void p3GxsChannels::dummy_tick()
} }
cleanTimedOutSearches(); cleanTimedOutCallbacks();
} }
@ -1776,7 +1816,7 @@ TurtleRequestId p3GxsChannels::turtleGroupRequest(const RsGxsGroupId& group_id)
} }
TurtleRequestId p3GxsChannels::turtleSearchRequest(const std::string& match_string) TurtleRequestId p3GxsChannels::turtleSearchRequest(const std::string& match_string)
{ {
return netService()->turtleSearchRequest(match_string) ; return netService()->turtleSearchRequest(match_string);
} }
bool p3GxsChannels::clearDistantSearchResults(TurtleRequestId req) bool p3GxsChannels::clearDistantSearchResults(TurtleRequestId req)
@ -1834,13 +1874,43 @@ bool p3GxsChannels::turtleSearchRequest(
TurtleRequestId sId = turtleSearchRequest(matchString); TurtleRequestId sId = turtleSearchRequest(matchString);
{
RS_STACK_MUTEX(mSearchCallbacksMapMutex); RS_STACK_MUTEX(mSearchCallbacksMapMutex);
mSearchCallbacksMap.emplace( mSearchCallbacksMap.emplace(
sId, sId,
std::make_pair( std::make_pair(
multiCallback, multiCallback,
std::chrono::system_clock::now() + std::chrono::system_clock::now() +
std::chrono::seconds(maxWait) ) ); std::chrono::seconds(maxWait) ) );
}
return true;
}
/// @see RsGxsChannels::turtleChannelRequest
bool p3GxsChannels::turtleChannelRequest(
const RsGxsGroupId& channelId,
const std::function<void (const RsGxsChannelGroup& result)>& multiCallback,
rstime_t maxWait)
{
if(channelId.isNull())
{
std::cerr << __PRETTY_FUNCTION__ << "Error! channelId can't be null!"
<< std::endl;
return false;
}
TurtleRequestId sId = turtleGroupRequest(channelId);
{
RS_STACK_MUTEX(mDistantChannelsCallbacksMapMutex);
mDistantChannelsCallbacksMap.emplace(
sId,
std::make_pair(
multiCallback,
std::chrono::system_clock::now() +
std::chrono::seconds(maxWait) ) );
}
return true; return true;
} }
@ -1851,29 +1921,77 @@ void p3GxsChannels::receiveDistantSearchResults(
std::cerr << __PRETTY_FUNCTION__ << "(" << id << ", " << grpId << ")" std::cerr << __PRETTY_FUNCTION__ << "(" << id << ", " << grpId << ")"
<< std::endl; << std::endl;
RsGenExchange::receiveDistantSearchResults(id, grpId); {
RsGxsGroupSummary gs; RsGenExchange::receiveDistantSearchResults(id, grpId);
gs.mGroupId = grpId; RsGxsGroupSummary gs;
netService()->retrieveDistantGroupSummary(grpId, gs); gs.mGroupId = grpId;
netService()->retrieveDistantGroupSummary(grpId, gs);
{
RS_STACK_MUTEX(mSearchCallbacksMapMutex);
auto cbpt = mSearchCallbacksMap.find(id);
if(cbpt != mSearchCallbacksMap.end())
{
cbpt->second.first(gs);
return;
}
} // end RS_STACK_MUTEX(mSearchCallbacksMapMutex);
}
{
RS_STACK_MUTEX(mDistantChannelsCallbacksMapMutex);
auto cbpt = mDistantChannelsCallbacksMap.find(id);
if(cbpt != mDistantChannelsCallbacksMap.end())
{
std::function<void (const RsGxsChannelGroup&)> callback =
cbpt->second.first;
RsThread::async([this, callback, grpId]()
{
std::list<RsGxsGroupId> chanIds({grpId});
std::vector<RsGxsChannelGroup> channelsInfo;
if(!getChannelsInfo(chanIds, channelsInfo))
{
std::cerr << __PRETTY_FUNCTION__ << " Error! Received "
<< "distant channel result grpId: " << grpId
<< " but failed getting channel info"
<< std::endl;
return;
}
for(const RsGxsChannelGroup& chan : channelsInfo)
callback(chan);
} );
return;
}
} // RS_STACK_MUTEX(mDistantChannelsCallbacksMapMutex);
}
void p3GxsChannels::cleanTimedOutCallbacks()
{
auto now = std::chrono::system_clock::now();
{ {
RS_STACK_MUTEX(mSearchCallbacksMapMutex); RS_STACK_MUTEX(mSearchCallbacksMapMutex);
auto cbpt = mSearchCallbacksMap.find(id); for( auto cbpt = mSearchCallbacksMap.begin();
if(cbpt != mSearchCallbacksMap.end()) cbpt != mSearchCallbacksMap.end(); )
cbpt->second.first(gs); if(cbpt->second.second <= now)
} // end RS_STACK_MUTEX(mSearchCallbacksMapMutex); {
} clearDistantSearchResults(cbpt->first);
cbpt = mSearchCallbacksMap.erase(cbpt);
}
else ++cbpt;
} // RS_STACK_MUTEX(mSearchCallbacksMapMutex);
void p3GxsChannels::cleanTimedOutSearches() {
{ RS_STACK_MUTEX(mDistantChannelsCallbacksMapMutex);
RS_STACK_MUTEX(mSearchCallbacksMapMutex); for( auto cbpt = mDistantChannelsCallbacksMap.begin();
auto now = std::chrono::system_clock::now(); cbpt != mDistantChannelsCallbacksMap.end(); )
for( auto cbpt = mSearchCallbacksMap.begin(); if(cbpt->second.second <= now)
cbpt != mSearchCallbacksMap.end(); ) {
if(cbpt->second.second <= now) clearDistantSearchResults(cbpt->first);
{ cbpt = mDistantChannelsCallbacksMap.erase(cbpt);
clearDistantSearchResults(cbpt->first); }
cbpt = mSearchCallbacksMap.erase(cbpt); else ++cbpt;
} } // RS_STACK_MUTEX(mDistantChannelsCallbacksMapMutex)
else ++cbpt;
} }

View File

@ -83,7 +83,7 @@ public:
virtual bool getGroupData(const uint32_t &token, std::vector<RsGxsChannelGroup> &groups); virtual bool getGroupData(const uint32_t &token, std::vector<RsGxsChannelGroup> &groups);
virtual bool getPostData(const uint32_t &token, std::vector<RsGxsChannelPost> &posts, std::vector<RsGxsComment> &cmts); virtual bool getPostData(const uint32_t &token, std::vector<RsGxsChannelPost> &posts, std::vector<RsGxsComment> &cmts);
virtual bool getPostData(const uint32_t &token, std::vector<RsGxsChannelPost> &posts) { std::vector<RsGxsComment> cmts; return getPostData( token, posts, cmts);} virtual bool getPostData(const uint32_t &token, std::vector<RsGxsChannelPost> &posts);
//Not currently used //Not currently used
//virtual bool getRelatedPosts(const uint32_t &token, std::vector<RsGxsChannelPost> &posts); //virtual bool getRelatedPosts(const uint32_t &token, std::vector<RsGxsChannelPost> &posts);
@ -112,6 +112,12 @@ virtual bool getChannelDownloadDirectory(const RsGxsGroupId &groupId, std::strin
const std::function<void (const RsGxsGroupSummary&)>& multiCallback, const std::function<void (const RsGxsGroupSummary&)>& multiCallback,
rstime_t maxWait = 300 ); rstime_t maxWait = 300 );
/// @see RsGxsChannels::turtleChannelRequest
virtual bool turtleChannelRequest(
const RsGxsGroupId& channelId,
const std::function<void (const RsGxsChannelGroup& result)>& multiCallback,
rstime_t maxWait = 300 );
/** /**
* Receive results from turtle search @see RsGenExchange @see RsNxsObserver * Receive results from turtle search @see RsGenExchange @see RsNxsObserver
* @see RsGxsNetService::receiveTurtleSearchResults * @see RsGxsNetService::receiveTurtleSearchResults
@ -183,9 +189,22 @@ virtual bool ExtraFileRemove(const RsFileHash &hash);
/// Implementation of @see RsGxsChannels::createChannel /// Implementation of @see RsGxsChannels::createChannel
virtual bool createChannel(RsGxsChannelGroup& channel); virtual bool createChannel(RsGxsChannelGroup& channel);
/// Implementation of @see RsGxsChannels::editChannel
virtual bool editChannel(RsGxsChannelGroup& channel);
/// Implementation of @see RsGxsChannels::createPost /// Implementation of @see RsGxsChannels::createPost
virtual bool createPost(RsGxsChannelPost& post); virtual bool createPost(RsGxsChannelPost& post);
/// Implementation of @see RsGxsChannels::subscribeToChannel
virtual bool subscribeToChannel( const RsGxsGroupId &groupId,
bool subscribe );
/// Implementation of @see RsGxsChannels::setPostRead
virtual bool markRead(const RsGxsGrpMsgIdPair& msgId, bool read);
virtual bool shareChannelKeys(
const RsGxsGroupId& channelId, const std::set<RsPeerId>& peers );
protected: protected:
// Overloaded from GxsTokenQueue for Request callbacks. // Overloaded from GxsTokenQueue for Request callbacks.
virtual void handleResponse(uint32_t token, uint32_t req_type); virtual void handleResponse(uint32_t token, uint32_t req_type);
@ -201,10 +220,8 @@ static uint32_t channelsAuthenPolicy();
void load_SubscribedGroups(const uint32_t &token); void load_SubscribedGroups(const uint32_t &token);
void request_SpecificUnprocessedPosts(std::list<std::pair<RsGxsGroupId, RsGxsMessageId> > &ids); void request_SpecificUnprocessedPosts(std::list<std::pair<RsGxsGroupId, RsGxsMessageId> > &ids);
void load_SpecificUnprocessedPosts(const uint32_t &token);
void request_GroupUnprocessedPosts(const std::list<RsGxsGroupId> &grouplist); void request_GroupUnprocessedPosts(const std::list<RsGxsGroupId> &grouplist);
void load_GroupUnprocessedPosts(const uint32_t &token); void load_unprocessedPosts(uint32_t token);
void handleUnprocessedPost(const RsGxsChannelPost &msg); void handleUnprocessedPost(const RsGxsChannelPost &msg);
@ -214,11 +231,6 @@ static uint32_t channelsAuthenPolicy();
bool setAutoDownload(const RsGxsGroupId &groupId, bool enabled); bool setAutoDownload(const RsGxsGroupId &groupId, bool enabled);
bool autoDownloadEnabled(const RsGxsGroupId &groupId, bool &enabled); bool autoDownloadEnabled(const RsGxsGroupId &groupId, bool &enabled);
std::map<RsGxsGroupId, RsGroupMetaData> mSubscribedGroups;
// DUMMY DATA, // DUMMY DATA,
virtual bool generateDummyData(); virtual bool generateDummyData();
@ -246,14 +258,21 @@ bool generateGroup(uint32_t &token, std::string groupName);
RsGxsMessageId mMsgId; RsGxsMessageId mMsgId;
}; };
std::map<RsGxsGroupId, RsGroupMetaData> mSubscribedGroups;
RsMutex mSubscribedGroupsMutex;
/** G10h4ck: Is this stuff really used? And for what? BEGIN */
uint32_t mGenToken; uint32_t mGenToken;
bool mGenActive; bool mGenActive;
int mGenCount; int mGenCount;
std::vector<ChannelDummyRef> mGenRefs; std::vector<ChannelDummyRef> mGenRefs;
RsGxsMessageId mGenThreadId; RsGxsMessageId mGenThreadId;
/** G10h4ck: Is this stuff really used? And for what? END */
p3GxsCommentService *mCommentService; p3GxsCommentService* mCommentService;
std::map<RsGxsGroupId,rstime_t> mKnownChannels;
std::map<RsGxsGroupId,rstime_t> mKnownChannels;
RsMutex mKnownChannelsMutex;
/** Store search callbacks with timeout*/ /** Store search callbacks with timeout*/
std::map< std::map<
@ -264,8 +283,17 @@ bool generateGroup(uint32_t &token, std::string groupName);
> mSearchCallbacksMap; > mSearchCallbacksMap;
RsMutex mSearchCallbacksMapMutex; RsMutex mSearchCallbacksMapMutex;
/// Cleanup mSearchCallbacksMap /** Store distant channels requests callbacks with timeout*/
void cleanTimedOutSearches(); std::map<
TurtleRequestId,
std::pair<
std::function<void (const RsGxsChannelGroup&)>,
std::chrono::system_clock::time_point >
> mDistantChannelsCallbacksMap;
RsMutex mDistantChannelsCallbacksMapMutex;
/// Cleanup mSearchCallbacksMap and mDistantChannelsCallbacksMap
void cleanTimedOutCallbacks();
}; };
#endif #endif

View File

@ -27,6 +27,8 @@
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
#include <semaphore.h> #include <semaphore.h>
#include <thread>
#include <functional>
#include <util/rsmemory.h> #include <util/rsmemory.h>
#include "util/rstime.h" #include "util/rstime.h"
@ -239,7 +241,7 @@ pthread_t createThread(RsThread &thread);
class RsThread class RsThread
{ {
public: public:
RsThread(); RsThread();
virtual ~RsThread() {} virtual ~RsThread() {}
@ -259,6 +261,17 @@ class RsThread
void ask_for_stop(); void ask_for_stop();
/**
* Execute given function on another thread without blocking the caller
* execution.
* This can be generalized with variadic template, ATM it is enough to wrap
* any kind of function call or job into a lambda which get no paramethers
* and return nothing but can capture
* This can be easly optimized later by using a thread pool
*/
static void async(const std::function<void()>& fn)
{ std::thread(fn).detach(); }
protected: protected:
virtual void runloop() =0; /* called once the thread is started. Should be overloaded by subclasses. */ virtual void runloop() =0; /* called once the thread is started. Should be overloaded by subclasses. */
void go() ; // this one calls runloop and also sets the flags correctly when the thread is finished running. void go() ; // this one calls runloop and also sets the flags correctly when the thread is finished running.

View File

@ -20,6 +20,7 @@
#include <QMenu> #include <QMenu>
#include <QFileDialog> #include <QFileDialog>
#include <QMetaObject>
#include <retroshare/rsfiles.h> #include <retroshare/rsfiles.h>
@ -33,6 +34,7 @@
#include "gui/settings/rsharesettings.h" #include "gui/settings/rsharesettings.h"
#include "gui/notifyqt.h" #include "gui/notifyqt.h"
#include "gui/common/GroupTreeWidget.h" #include "gui/common/GroupTreeWidget.h"
#include "util/qtthreadsutils.h"
class GxsChannelGroupInfoData : public RsUserdata class GxsChannelGroupInfoData : public RsUserdata
{ {
@ -275,17 +277,37 @@ QWidget *GxsChannelDialog::createCommentHeaderWidget(const RsGxsGroupId &grpId,
void GxsChannelDialog::toggleAutoDownload() void GxsChannelDialog::toggleAutoDownload()
{ {
RsGxsGroupId grpId = groupId(); RsGxsGroupId grpId = groupId();
if (grpId.isNull()) { if (grpId.isNull()) return;
bool autoDownload;
if(!rsGxsChannels->getChannelAutoDownload(grpId, autoDownload))
{
std::cerr << __PRETTY_FUNCTION__ << " failed to get autodownload value "
<< "for channel: " << grpId.toStdString() << std::endl;
return; return;
} }
bool autoDownload ; RsThread::async([this, grpId, autoDownload]()
if(!rsGxsChannels->getChannelAutoDownload(grpId,autoDownload) || !rsGxsChannels->setChannelAutoDownload(grpId, !autoDownload))
{ {
std::cerr << "GxsChannelDialog::toggleAutoDownload() Auto Download failed to set"; if(!rsGxsChannels->setChannelAutoDownload(grpId, !autoDownload))
std::cerr << std::endl; {
} std::cerr << __PRETTY_FUNCTION__ << " failed to set autodownload "
<< "for channel: " << grpId << std::endl;
return;
}
RsQThreadUtils::postToObject( [=]()
{
/* Here it goes any code you want to be executed on the Qt Gui
* thread, for example to update the data model with new information
* after a blocking call to RetroShare API complete, note that
* Qt::QueuedConnection is important!
*/
std::cerr << __PRETTY_FUNCTION__ << " Has been executed on GUI "
<< "thread but was scheduled by async thread" << std::endl;
}, this );
});
} }
void GxsChannelDialog::loadGroupSummaryToken(const uint32_t &token, std::list<RsGroupMetaData> &groupInfo, RsUserdata *&userdata) void GxsChannelDialog::loadGroupSummaryToken(const uint32_t &token, std::list<RsGroupMetaData> &groupInfo, RsUserdata *&userdata)

View File

@ -31,8 +31,10 @@
#include "gui/settings/rsharesettings.h" #include "gui/settings/rsharesettings.h"
#include "gui/feeds/SubFileItem.h" #include "gui/feeds/SubFileItem.h"
#include "gui/notifyqt.h" #include "gui/notifyqt.h"
#include <algorithm>
#include "util/DateTime.h" #include "util/DateTime.h"
#include "util/qtthreadsutils.h"
#include <algorithm>
#define CHAN_DEFAULT_IMAGE ":/images/channels.png" #define CHAN_DEFAULT_IMAGE ":/images/channels.png"
@ -621,13 +623,13 @@ bool GxsChannelPostsWidget::navigatePostItem(const RsGxsMessageId &msgId)
void GxsChannelPostsWidget::subscribeGroup(bool subscribe) void GxsChannelPostsWidget::subscribeGroup(bool subscribe)
{ {
if (groupId().isNull()) { RsGxsGroupId grpId(groupId());
return; if (grpId.isNull()) return;
}
uint32_t token; RsThread::async([=]()
rsGxsChannels->subscribeToGroup(token, groupId(), subscribe); {
// mChannelQueue->queueRequest(token, 0, RS_TOKREQ_ANSTYPE_ACK, TOKEN_TYPE_SUBSCRIBE_CHANGE); rsGxsChannels->subscribeToChannel(grpId, subscribe);
} );
} }
void GxsChannelPostsWidget::setAutoDownload(bool autoDl) void GxsChannelPostsWidget::setAutoDownload(bool autoDl)
@ -643,12 +645,35 @@ void GxsChannelPostsWidget::toggleAutoDownload()
return; return;
} }
bool autoDownload ; bool autoDownload;
if(!rsGxsChannels->getChannelAutoDownload(grpId,autoDownload) || !rsGxsChannels->setChannelAutoDownload(grpId, !autoDownload)) if(!rsGxsChannels->getChannelAutoDownload(grpId, autoDownload))
{ {
std::cerr << "GxsChannelDialog::toggleAutoDownload() Auto Download failed to set"; std::cerr << __PRETTY_FUNCTION__ << " failed to get autodownload value "
std::cerr << std::endl; << "for channel: " << grpId.toStdString() << std::endl;
return;
} }
RsThread::async([this, grpId, autoDownload]()
{
if(!rsGxsChannels->setChannelAutoDownload(grpId, !autoDownload))
{
std::cerr << __PRETTY_FUNCTION__ << " failed to set autodownload "
<< "for channel: " << grpId.toStdString() << std::endl;
return;
}
RsQThreadUtils::postToObject( [=]()
{
/* Here it goes any code you want to be executed on the Qt Gui
* thread, for example to update the data model with new information
* after a blocking call to RetroShare API complete, note that
* Qt::QueuedConnection is important!
*/
std::cerr << __PRETTY_FUNCTION__ << " Has been executed on GUI "
<< "thread but was scheduled by async thread" << std::endl;
}, this );
});
} }
bool GxsChannelPostsWidget::insertGroupData(const uint32_t &token, RsGroupMetaData &metaData) bool GxsChannelPostsWidget::insertGroupData(const uint32_t &token, RsGroupMetaData &metaData)

View File

@ -424,6 +424,7 @@ HEADERS += rshare.h \
util/ObjectPainter.h \ util/ObjectPainter.h \
util/QtVersion.h \ util/QtVersion.h \
util/RsFile.h \ util/RsFile.h \
util/qtthreadsutils.h \
gui/profile/ProfileWidget.h \ gui/profile/ProfileWidget.h \
gui/profile/ProfileManager.h \ gui/profile/ProfileManager.h \
gui/profile/StatusMessage.h \ gui/profile/StatusMessage.h \

View File

@ -0,0 +1,117 @@
/*
* RetroShare
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
/* Thanks to KubaO which realeased original C++14 versions of this functions
* under public domain license
* https://github.com/KubaO/stackoverflown/blob/master/questions/metacall-21646467/main.cpp
* https://github.com/KubaO/stackoverflown/blob/master/LICENSE
*/
#include <QtGlobal>
#include <QtCore>
#include <type_traits>
namespace RsQThreadUtils {
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
/**
* @brief execute given function in the QThread where given QObject belongs
*/
template <typename F>
void postToObject(F &&fun, QObject *obj = qApp)
{
if (qobject_cast<QThread*>(obj))
qWarning() << "posting a call to a thread object - consider using postToThread";
QObject src;
auto type = obj->metaObject();
QObject::connect( &src, &QObject::destroyed, obj,
[fun, type, obj]
{
// ensure that the object is not being destructed
if (obj->metaObject()->inherits(type)) fun();
}, Qt::QueuedConnection );
}
/**
* @brief execute given function in the given QThread
*/
template <typename F>
void postToThread(F &&fun, QThread *thread = qApp->thread())
{
QObject * obj = QAbstractEventDispatcher::instance(thread);
Q_ASSERT(obj);
QObject src;
auto type = obj->metaObject();
QObject::connect( &src, &QObject::destroyed, obj,
[fun, type, obj]
{
// ensure that the object is not being destructed
if (obj->metaObject()->inherits(type)) fun();
}, Qt::QueuedConnection );
}
#else // QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
template <typename F>
struct FEvent : QEvent
{
using Fun = typename std::decay<F>::type;
const QObject *const obj;
const QMetaObject *const type = obj->metaObject();
Fun fun;
template <typename Fun>
FEvent(const QObject *obj, Fun &&fun) :
QEvent(QEvent::None), obj(obj), fun(std::forward<Fun>(fun)) {}
~FEvent()
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
// ensure that the object is not being destructed
if (obj->metaObject()->inherits(type)) fun();
#else // QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
fun();
#endif // QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
}
};
/**
* @brief execute given function in the QThread where given QObject belongs
*/
template <typename F>
static void postToObject(F &&fun, QObject *obj = qApp)
{
if (qobject_cast<QThread*>(obj))
qWarning() << "posting a call to a thread object - consider using postToThread";
QCoreApplication::postEvent(obj, new FEvent<F>(obj, std::forward<F>(fun)));
}
/**
* @brief execute given function in the given QThread
*/
template <typename F>
static void postToThread(F &&fun, QThread *thread = qApp->thread())
{
QObject * obj = QAbstractEventDispatcher::instance(thread);
Q_ASSERT(obj);
QCoreApplication::postEvent(obj, new FEvent<F>(obj, std::forward<F>(fun)));
}
#endif // QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
}