2018-11-10 08:45:05 -05:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/retroshare: rsgxsifacehelper.h *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright 2011 by Christopher Evi-Parker *
|
2019-04-04 19:43:23 -04:00
|
|
|
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
|
2018-11-10 08:45:05 -05:00
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2019-04-04 19:43:23 -04:00
|
|
|
#pragma once
|
2012-10-05 14:12:52 -04:00
|
|
|
|
2018-06-26 07:20:24 -04:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
|
2013-02-28 16:58:38 -05:00
|
|
|
#include "retroshare/rsgxsiface.h"
|
2016-12-26 09:59:53 -05:00
|
|
|
#include "retroshare/rsreputations.h"
|
2013-02-28 16:58:38 -05:00
|
|
|
#include "rsgxsflags.h"
|
2018-06-25 04:44:39 -04:00
|
|
|
#include "util/rsdeprecate.h"
|
2012-10-05 14:12:52 -04:00
|
|
|
|
|
|
|
/*!
|
2018-06-25 04:44:39 -04:00
|
|
|
* This class only make method of internal members visible tu upper level to
|
|
|
|
* offer a more friendly API.
|
|
|
|
* This is just a workaround to awkward GXS API design, do not take it as an
|
|
|
|
* example for your coding.
|
|
|
|
* To properly fix the API design many changes with the implied chain reactions
|
|
|
|
* are necessary, so at this point this workaround seems acceptable.
|
2012-10-05 14:12:52 -04:00
|
|
|
*/
|
2018-06-25 04:44:39 -04:00
|
|
|
struct RsGxsIfaceHelper
|
2012-10-05 14:12:52 -04:00
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
/*!
|
|
|
|
* @param gxs handle to RsGenExchange instance of service (Usually the
|
|
|
|
* service class itself)
|
|
|
|
*/
|
|
|
|
RsGxsIfaceHelper(RsGxsIface& gxs) :
|
|
|
|
mGxs(gxs), mTokenService(*gxs.getTokenService()) {}
|
2013-02-28 16:58:38 -05:00
|
|
|
|
|
|
|
~RsGxsIfaceHelper(){}
|
2012-10-05 14:12:52 -04:00
|
|
|
|
2012-10-15 16:31:56 -04:00
|
|
|
/*!
|
|
|
|
* Gxs services should call this for automatic handling of
|
|
|
|
* changes, send
|
|
|
|
* @param changes
|
|
|
|
*/
|
2013-02-28 16:58:38 -05:00
|
|
|
void receiveChanges(std::vector<RsGxsNotify *> &changes)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
mGxs.receiveChanges(changes);
|
2013-02-28 16:58:38 -05:00
|
|
|
}
|
2012-10-05 14:12:52 -04:00
|
|
|
|
2012-10-14 14:32:33 -04:00
|
|
|
/* Generic Lists */
|
|
|
|
|
|
|
|
/*!
|
2012-10-15 16:31:56 -04:00
|
|
|
* Retrieve list of group ids associated to a request token
|
2012-10-14 14:32:33 -04:00
|
|
|
* @param token token to be redeemed for this request
|
|
|
|
* @param groupIds the ids return for given request token
|
|
|
|
* @return false if request token is invalid, check token status for error report
|
|
|
|
*/
|
2012-10-05 14:12:52 -04:00
|
|
|
bool getGroupList(const uint32_t &token,
|
2013-02-28 16:58:38 -05:00
|
|
|
std::list<RsGxsGroupId> &groupIds)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getGroupList(token, groupIds);
|
2013-02-28 16:58:38 -05:00
|
|
|
}
|
2012-10-14 14:32:33 -04:00
|
|
|
|
|
|
|
/*!
|
2012-10-15 16:31:56 -04:00
|
|
|
* Retrieves list of msg ids associated to a request token
|
2012-10-14 14:32:33 -04:00
|
|
|
* @param token token to be redeemed for this request
|
|
|
|
* @param msgIds the ids return for given request token
|
|
|
|
* @return false if request token is invalid, check token status for error report
|
|
|
|
*/
|
2012-10-05 14:12:52 -04:00
|
|
|
bool getMsgList(const uint32_t &token,
|
2013-02-28 16:58:38 -05:00
|
|
|
GxsMsgIdResult& msgIds)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getMsgList(token, msgIds);
|
2013-02-28 16:58:38 -05:00
|
|
|
}
|
2012-10-05 14:12:52 -04:00
|
|
|
|
2012-11-13 17:36:06 -05:00
|
|
|
/*!
|
|
|
|
* Retrieves list of msg related ids associated to a request token
|
|
|
|
* @param token token to be redeemed for this request
|
|
|
|
* @param msgIds the ids return for given request token
|
|
|
|
* @return false if request token is invalid, check token status for error report
|
|
|
|
*/
|
2013-02-28 16:58:38 -05:00
|
|
|
bool getMsgRelatedList(const uint32_t &token, MsgRelatedIdResult &msgIds)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getMsgRelatedList(token, msgIds);
|
2013-02-28 16:58:38 -05:00
|
|
|
}
|
2012-11-13 17:36:06 -05:00
|
|
|
|
2012-10-14 14:32:33 -04:00
|
|
|
/*!
|
|
|
|
* @param token token to be redeemed for group summary request
|
|
|
|
* @param groupInfo the ids returned for given request token
|
|
|
|
* @return false if request token is invalid, check token status for error report
|
|
|
|
*/
|
2012-10-05 14:12:52 -04:00
|
|
|
bool getGroupSummary(const uint32_t &token,
|
2013-02-28 16:58:38 -05:00
|
|
|
std::list<RsGroupMetaData> &groupInfo)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getGroupMeta(token, groupInfo);
|
2013-02-28 16:58:38 -05:00
|
|
|
}
|
2012-10-05 14:12:52 -04:00
|
|
|
|
2012-10-14 14:32:33 -04:00
|
|
|
/*!
|
|
|
|
* @param token token to be redeemed for message summary request
|
|
|
|
* @param msgInfo the message metadata returned for given request token
|
|
|
|
* @return false if request token is invalid, check token status for error report
|
|
|
|
*/
|
2012-10-05 14:12:52 -04:00
|
|
|
bool getMsgSummary(const uint32_t &token,
|
2013-02-28 16:58:38 -05:00
|
|
|
GxsMsgMetaMap &msgInfo)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getMsgMeta(token, msgInfo);
|
2013-02-28 16:58:38 -05:00
|
|
|
}
|
2012-10-05 14:12:52 -04:00
|
|
|
|
2012-11-13 17:36:06 -05:00
|
|
|
/*!
|
|
|
|
* @param token token to be redeemed for message related summary request
|
|
|
|
* @param msgInfo the message metadata returned for given request token
|
|
|
|
* @return false if request token is invalid, check token status for error report
|
|
|
|
*/
|
2015-01-25 18:22:58 -05:00
|
|
|
bool getMsgRelatedSummary(const uint32_t &token, GxsMsgRelatedMetaMap &msgInfo)
|
2013-02-28 16:58:38 -05:00
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getMsgRelatedMeta(token, msgInfo);
|
2013-02-28 16:58:38 -05:00
|
|
|
}
|
2012-11-13 17:36:06 -05:00
|
|
|
|
2012-10-14 14:32:33 -04:00
|
|
|
/*!
|
|
|
|
* subscribes to group, and returns token which can be used
|
|
|
|
* to be acknowledged to get group Id
|
|
|
|
* @param token token to redeem for acknowledgement
|
|
|
|
* @param grpId the id of the group to subscribe to
|
|
|
|
*/
|
2013-02-28 16:58:38 -05:00
|
|
|
bool subscribeToGroup(uint32_t& token, const RsGxsGroupId& grpId, bool subscribe)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.subscribeToGroup(token, grpId, subscribe);
|
2013-02-28 16:58:38 -05:00
|
|
|
}
|
2012-10-14 14:32:33 -04:00
|
|
|
|
2012-10-05 14:12:52 -04:00
|
|
|
/*!
|
|
|
|
* This allows the client service to acknowledge that their msgs has
|
|
|
|
* been created/modified and retrieve the create/modified msg ids
|
|
|
|
* @param token the token related to modification/create request
|
|
|
|
* @param msgIds map of grpid->msgIds of message created/modified
|
|
|
|
* @return true if token exists false otherwise
|
|
|
|
*/
|
2013-02-28 16:58:38 -05:00
|
|
|
bool acknowledgeMsg(const uint32_t& token, std::pair<RsGxsGroupId, RsGxsMessageId>& msgId)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.acknowledgeTokenMsg(token, msgId);
|
2013-02-28 16:58:38 -05:00
|
|
|
}
|
2012-10-05 14:12:52 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* This allows the client service to acknowledge that their grps has
|
|
|
|
* been created/modified and retrieve the create/modified grp ids
|
|
|
|
* @param token the token related to modification/create request
|
|
|
|
* @param msgIds vector of ids of groups created/modified
|
|
|
|
* @return true if token exists false otherwise
|
|
|
|
*/
|
2013-02-28 16:58:38 -05:00
|
|
|
bool acknowledgeGrp(const uint32_t& token, RsGxsGroupId& grpId)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.acknowledgeTokenGrp(token, grpId);
|
2013-02-28 16:58:38 -05:00
|
|
|
}
|
2012-10-05 14:12:52 -04:00
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
/*!
|
|
|
|
* Gets service statistic for a given services
|
|
|
|
* @param token value to to retrieve requested stats
|
|
|
|
* @param stats the status
|
|
|
|
* @return true if token exists false otherwise
|
|
|
|
*/
|
|
|
|
bool getServiceStatistic(const uint32_t& token, GxsServiceStatistic& stats)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getServiceStatistic(token, stats);
|
2013-03-23 10:07:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* @param token to be redeemed
|
|
|
|
* @param stats the stats associated to token request
|
|
|
|
* @return true if token is false otherwise
|
|
|
|
*/
|
|
|
|
bool getGroupStatistic(const uint32_t& token, GxsGroupStatistic& stats)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getGroupStatistic(token, stats);
|
2013-03-23 10:07:52 -04:00
|
|
|
}
|
|
|
|
|
2014-02-22 19:08:11 -05:00
|
|
|
/*!
|
|
|
|
* This determines the reputation threshold messages need to surpass in order
|
|
|
|
* for it to be accepted by local user from remote source
|
|
|
|
* NOTE: threshold only enforced if service require author signature
|
|
|
|
* @param token value set to be redeemed with acknowledgement
|
|
|
|
* @param grpId group id for cutoff value to be set
|
|
|
|
* @param CutOff The cut off value to set
|
|
|
|
*/
|
|
|
|
void setGroupReputationCutOff(uint32_t& token, const RsGxsGroupId& grpId, int CutOff)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.setGroupReputationCutOff(token, grpId, CutOff);
|
2014-02-22 19:08:11 -05:00
|
|
|
}
|
|
|
|
|
2015-05-02 14:48:10 -04:00
|
|
|
/*!
|
2016-12-05 16:14:48 -05:00
|
|
|
* @return storage/sync time of messages in secs
|
2015-05-02 14:48:10 -04:00
|
|
|
*/
|
2016-12-05 16:14:48 -05:00
|
|
|
uint32_t getDefaultStoragePeriod()
|
2015-05-02 14:48:10 -04:00
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getDefaultStoragePeriod();
|
2016-12-05 16:14:48 -05:00
|
|
|
}
|
|
|
|
uint32_t getStoragePeriod(const RsGxsGroupId& grpId)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getStoragePeriod(grpId);
|
2016-12-05 16:14:48 -05:00
|
|
|
}
|
|
|
|
void setStoragePeriod(const RsGxsGroupId& grpId,uint32_t age_in_secs)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
mGxs.setStoragePeriod(grpId,age_in_secs);
|
2016-12-05 16:14:48 -05:00
|
|
|
}
|
|
|
|
uint32_t getDefaultSyncPeriod()
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getDefaultSyncPeriod();
|
2016-12-05 16:14:48 -05:00
|
|
|
}
|
|
|
|
uint32_t getSyncPeriod(const RsGxsGroupId& grpId)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.getSyncPeriod(grpId);
|
2016-12-05 16:14:48 -05:00
|
|
|
}
|
|
|
|
void setSyncPeriod(const RsGxsGroupId& grpId,uint32_t age_in_secs)
|
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
mGxs.setSyncPeriod(grpId,age_in_secs);
|
2015-05-02 14:48:10 -04:00
|
|
|
}
|
|
|
|
|
2019-02-15 13:29:36 -05:00
|
|
|
RsReputationLevel minReputationForForwardingMessages(
|
|
|
|
uint32_t group_sign_flags, uint32_t identity_flags )
|
2016-12-26 09:59:53 -05:00
|
|
|
{
|
2018-06-25 04:44:39 -04:00
|
|
|
return mGxs.minReputationForForwardingMessages(group_sign_flags,identity_flags);
|
2016-12-26 09:59:53 -05:00
|
|
|
}
|
2012-10-05 14:12:52 -04:00
|
|
|
|
2018-06-25 04:44:39 -04:00
|
|
|
/// @see RsTokenService::requestGroupInfo
|
|
|
|
bool requestGroupInfo( uint32_t& token, const RsTokReqOptions& opts,
|
|
|
|
const std::list<RsGxsGroupId> &groupIds )
|
|
|
|
{ return mTokenService.requestGroupInfo(token, 0, opts, groupIds); }
|
|
|
|
|
|
|
|
/// @see RsTokenService::requestGroupInfo
|
|
|
|
bool requestGroupInfo(uint32_t& token, const RsTokReqOptions& opts)
|
|
|
|
{ return mTokenService.requestGroupInfo(token, 0, opts); }
|
|
|
|
|
|
|
|
/// @see RsTokenService::requestMsgInfo
|
|
|
|
bool requestMsgInfo( uint32_t& token,
|
|
|
|
const RsTokReqOptions& opts, const GxsMsgReq& msgIds )
|
|
|
|
{ return mTokenService.requestMsgInfo(token, 0, opts, msgIds); }
|
|
|
|
|
|
|
|
/// @see RsTokenService::requestMsgInfo
|
|
|
|
bool requestMsgInfo(
|
|
|
|
uint32_t& token, const RsTokReqOptions& opts,
|
|
|
|
const std::list<RsGxsGroupId>& grpIds )
|
|
|
|
{ return mTokenService.requestMsgInfo(token, 0, opts, grpIds); }
|
|
|
|
|
|
|
|
/// @see RsTokenService::requestMsgRelatedInfo
|
|
|
|
bool requestMsgRelatedInfo(
|
|
|
|
uint32_t& token, const RsTokReqOptions& opts,
|
|
|
|
const std::vector<RsGxsGrpMsgIdPair>& msgIds )
|
|
|
|
{ return mTokenService.requestMsgRelatedInfo(token, 0, opts, msgIds); }
|
|
|
|
|
2018-06-26 07:20:24 -04:00
|
|
|
/**
|
|
|
|
* @jsonapi{development}
|
|
|
|
* @param[in] token
|
|
|
|
*/
|
2018-06-25 04:44:39 -04:00
|
|
|
RsTokenService::GxsRequestStatus requestStatus(uint32_t token)
|
|
|
|
{ return mTokenService.requestStatus(token); }
|
|
|
|
|
|
|
|
/// @see RsTokenService::requestServiceStatistic
|
|
|
|
void requestServiceStatistic(uint32_t& token)
|
|
|
|
{ mTokenService.requestServiceStatistic(token); }
|
|
|
|
|
|
|
|
/// @see RsTokenService::requestGroupStatistic
|
|
|
|
void requestGroupStatistic(uint32_t& token, const RsGxsGroupId& grpId)
|
|
|
|
{ mTokenService.requestGroupStatistic(token, grpId); }
|
|
|
|
|
|
|
|
/// @see RsTokenService::cancelRequest
|
|
|
|
bool cancelRequest(uint32_t token)
|
|
|
|
{ return mTokenService.cancelRequest(token); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* Token service methods are already exposed by this helper, so you should
|
|
|
|
* not need to get token service pointer directly anymore.
|
|
|
|
*/
|
|
|
|
RS_DEPRECATED RsTokenService* getTokenService() { return &mTokenService; }
|
|
|
|
|
2018-06-26 07:20:24 -04:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Block caller while request is being processed.
|
|
|
|
* Useful for blocking API implementation.
|
|
|
|
* @param[in] token token associated to the request caller is waiting for
|
|
|
|
* @param[in] maxWait maximum waiting time in milliseconds
|
2019-04-04 19:43:23 -04:00
|
|
|
* @param[in] checkEvery time in millisecond between status checks
|
2018-06-26 07:20:24 -04:00
|
|
|
*/
|
|
|
|
RsTokenService::GxsRequestStatus waitToken(
|
|
|
|
uint32_t token,
|
2019-04-04 19:43:23 -04:00
|
|
|
std::chrono::milliseconds maxWait = std::chrono::milliseconds(500),
|
|
|
|
std::chrono::milliseconds checkEvery = std::chrono::milliseconds(2))
|
2018-06-26 07:20:24 -04:00
|
|
|
{
|
2019-04-04 19:43:23 -04:00
|
|
|
#if defined(__ANDROID__) && (__ANDROID_API__ < 24)
|
|
|
|
auto wkStartime = std::chrono::steady_clock::now();
|
|
|
|
int maxWorkAroundCnt = 10;
|
|
|
|
LLwaitTokenBeginLabel:
|
|
|
|
#endif
|
2018-06-26 07:20:24 -04:00
|
|
|
auto timeout = std::chrono::steady_clock::now() + maxWait;
|
|
|
|
auto st = requestStatus(token);
|
|
|
|
while( !(st == RsTokenService::FAILED || st >= RsTokenService::COMPLETE)
|
|
|
|
&& std::chrono::steady_clock::now() < timeout )
|
|
|
|
{
|
2019-04-04 19:43:23 -04:00
|
|
|
std::this_thread::sleep_for(checkEvery);
|
2018-06-26 07:20:24 -04:00
|
|
|
st = requestStatus(token);
|
|
|
|
}
|
2019-04-04 19:43:23 -04:00
|
|
|
|
|
|
|
#if defined(__ANDROID__) && (__ANDROID_API__ < 24)
|
|
|
|
/* Work around for very slow/old android devices, we don't expect this
|
|
|
|
* to be necessary on newer devices. If it take unreasonably long
|
|
|
|
* something worser is already happening elsewere amd we return anyway.
|
|
|
|
*/
|
|
|
|
if( st > RsTokenService::FAILED && st < RsTokenService::COMPLETE
|
|
|
|
&& maxWorkAroundCnt-- > 0 )
|
|
|
|
{
|
|
|
|
maxWait *= 10;
|
|
|
|
checkEvery *= 3;
|
|
|
|
std::cerr << __PRETTY_FUNCTION__ << " Slow Android device "
|
|
|
|
<< " workaround st: " << st
|
|
|
|
<< " maxWorkAroundCnt: " << maxWorkAroundCnt
|
|
|
|
<< " maxWait: " << maxWait.count()
|
|
|
|
<< " checkEvery: " << checkEvery.count() << std::endl;
|
|
|
|
goto LLwaitTokenBeginLabel;
|
|
|
|
}
|
|
|
|
std::cerr << __PRETTY_FUNCTION__ << " lasted: "
|
|
|
|
<< std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now() - wkStartime ).count()
|
|
|
|
<< "ms" << std::endl;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-06-26 07:20:24 -04:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2018-06-25 04:44:39 -04:00
|
|
|
private:
|
|
|
|
RsGxsIface& mGxs;
|
|
|
|
RsTokenService& mTokenService;
|
2012-10-05 14:12:52 -04:00
|
|
|
};
|