/******************************************************************************* * libretroshare/src/gxs: rsgxsutil.h * * * * libretroshare: retroshare core library * * * * Copyright (C) 2013 Christopher Evi-Parker * * Copyright (C) 2018-2021 Gioacchino Mazzurco * * Copyright (C) 2021 AsociaciĆ³n Civil Altermundi * * * * 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 . * * * *******************************************************************************/ #pragma once #include #include "rsitems/rsnxsitems.h" #include "rsgds.h" class RsGixs ; class RsGenExchange ; class RsGeneralDataService ; // temporary holds a map of pointers to class T, and destroys all pointers on delete. class non_copiable { public: non_copiable() {} private: non_copiable& operator=(const non_copiable&) { return *this ;} non_copiable(const non_copiable&) {} }; template class t_RsGxsGenericDataTemporaryMap: public std::map, public non_copiable { public: virtual ~t_RsGxsGenericDataTemporaryMap() { clear() ; } virtual void clear() { for(typename t_RsGxsGenericDataTemporaryMap::iterator it = this->begin();it!=this->end();++it) if(it->second != NULL) delete it->second ; std::map::clear() ; } }; template class t_RsGxsGenericDataTemporaryMapVector: public std::map >, public non_copiable { public: virtual ~t_RsGxsGenericDataTemporaryMapVector() { clear() ; } virtual void clear() { for(typename t_RsGxsGenericDataTemporaryMapVector::iterator it = this->begin();it!=this->end();++it) { for(uint32_t i=0;isecond.size();++i) delete it->second[i] ; it->second.clear(); } std::map >::clear() ; } }; template class t_RsGxsGenericDataTemporaryList: public std::list, public non_copiable { public: virtual ~t_RsGxsGenericDataTemporaryList() { clear() ; } virtual void clear() { for(typename t_RsGxsGenericDataTemporaryList::iterator it = this->begin();it!=this->end();++it) delete *it; std::list::clear() ; } }; typedef std::map > RsGxsGrpMetaTemporaryMap; // This map doesn't need to delete elements since it holds typedef std::map > > RsGxsMsgMetaTemporaryMap; // shared_ptr's. typedef t_RsGxsGenericDataTemporaryMap RsNxsGrpDataTemporaryMap; typedef t_RsGxsGenericDataTemporaryMapVector RsNxsMsgDataTemporaryMap ; typedef t_RsGxsGenericDataTemporaryList RsNxsGrpDataTemporaryList ; typedef t_RsGxsGenericDataTemporaryList RsNxsMsgDataTemporaryList ; inline RsGxsGrpMsgIdPair getMsgIdPair(RsNxsMsg& msg) { return RsGxsGrpMsgIdPair(std::make_pair(msg.grpId, msg.msgId)); } inline RsGxsGrpMsgIdPair getMsgIdPair(RsGxsMsgItem& msg) { return RsGxsGrpMsgIdPair(std::make_pair(msg.meta.mGroupId, msg.meta.mMsgId)); } /*! * Does message clean up based on individual group expirations first * if avialable. If not then deletion s */ class RsGxsCleanUp { public: /*! * * @param dataService * @param mGroupTS * @param chunkSize * @param sleepPeriod */ RsGxsCleanUp(RsGeneralDataService* const dataService, RsGenExchange *genex, uint32_t chunkSize); /*! * On construction this should be called to progress deletions * Deletion will process by chunk size * @return true if no more messages to delete, false otherwise */ bool clean(RsGxsGroupId& next_group_to_check,std::vector& grps_to_delete,GxsMsgReq& messages_to_delete); private: RsGeneralDataService* const mDs; RsGenExchange *mGenExchangeClient; uint32_t CHUNK_SIZE; }; /*! * Checks the integrity message and groups * in rsDataService using computed hash */ class RsGxsIntegrityCheck : public RsThread { enum CheckState { CheckStart, CheckChecking }; public: RsGxsIntegrityCheck( RsGeneralDataService* const dataService, RsGenExchange* genex, RsSerialType&, RsGixs* gixs ); static bool check(uint16_t service_type, RsGixs *mgixs, RsGeneralDataService *mds); bool isDone(); void run(); void getDeletedIds(std::vector &grpIds, GxsMsgReq &msgIds); private: RsGeneralDataService* const mDs; RsGenExchange *mGenExchangeClient; bool mDone; RsMutex mIntegrityMutex; std::vector mDeletedGrps; GxsMsgReq mDeletedMsgs; RsGixs* mGixs; }; /*! * Checks the integrity message and groups * in rsDataService using computed hash */ class RsGxsSinglePassIntegrityCheck { public: static bool check( uint16_t service_type, RsGixs* mgixs, RsGeneralDataService* mds, std::vector& grpsToDel, GxsMsgReq& msgsToDel ); }; class GroupUpdate { public: GroupUpdate() : oldGrpMeta(NULL), newGrp(NULL), validUpdate(false) {} const RsGxsGrpMetaData* oldGrpMeta; RsNxsGrp* newGrp; bool validUpdate; }; class GroupUpdatePublish { public: GroupUpdatePublish(RsGxsGrpItem* item, uint32_t token) : grpItem(item), mToken(token) {} RsGxsGrpItem* grpItem; uint32_t mToken; }; class GroupDeletePublish { public: GroupDeletePublish(const RsGxsGroupId& grpId, uint32_t token) : mGroupId(grpId), mToken(token) {} RsGxsGroupId mGroupId; uint32_t mToken; }; class MsgDeletePublish { public: MsgDeletePublish(const GxsMsgReq& msgs, uint32_t token) : mMsgs(msgs), mToken(token) {} GxsMsgReq mMsgs ; uint32_t mToken; };