From aee6cb85b42dacc9075679566c79bbadbe6fa715 Mon Sep 17 00:00:00 2001 From: drbob Date: Sun, 9 Nov 2008 22:17:20 +0000 Subject: [PATCH] * More bugfixes for file transfer. * Added File Transfer / ExtraList to channels. * fixed mutex deadlock. * added slow transfer for background tf. * added checks to FileRequest to accumulate peers. * added ExtraList callback. * etc, etc. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@797 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/ft/ftcontroller.cc | 183 ++++++++++++++++--- libretroshare/src/ft/ftcontroller.h | 7 +- libretroshare/src/ft/ftserver.cc | 7 +- libretroshare/src/ft/fttransfermodule.cc | 8 + libretroshare/src/rsiface/rsfiles.h | 2 + libretroshare/src/rsserver/p3face-startup.cc | 5 +- libretroshare/src/rsserver/p3face.h | 2 +- libretroshare/src/rsserver/p3peers.cc | 9 + libretroshare/src/services/p3channels.cc | 158 +++++++++++++++- libretroshare/src/services/p3channels.h | 7 +- libretroshare/src/services/p3distrib.cc | 35 ++-- libretroshare/src/services/p3distrib.h | 3 +- libretroshare/src/services/p3forums.cc | 6 +- libretroshare/src/services/p3forums.h | 4 +- 14 files changed, 385 insertions(+), 51 deletions(-) diff --git a/libretroshare/src/ft/ftcontroller.cc b/libretroshare/src/ft/ftcontroller.cc index 2020ce1e8..c2128a19a 100644 --- a/libretroshare/src/ft/ftcontroller.cc +++ b/libretroshare/src/ft/ftcontroller.cc @@ -41,6 +41,7 @@ #include "ft/fttransfermodule.h" #include "ft/ftsearch.h" #include "ft/ftdatamultiplex.h" +#include "ft/ftextralist.h" #include "util/rsdir.h" @@ -79,9 +80,10 @@ ftController::ftController(CacheStrapper *cs, ftDataMultiplex *dm, std::string c /* TODO */ } -void ftController::setFtSearch(ftSearch *search) +void ftController::setFtSearchNExtra(ftSearch *search, ftExtraList *list) { mSearch = search; + mExtraList = list; } void ftController::run() @@ -151,7 +153,18 @@ bool ftController::FlagFileComplete(std::string hash) bool ftController::completeFile(std::string hash) { - RsStackMutex stack(ctrlMutex); /******* LOCKED ********/ + /* variables... so we can drop mutex later */ + std::string path; + uint64_t size = 0; + uint32_t state = 0; + uint32_t period = 0; + uint32_t flags = 0; + + bool doCallback = false; + uint32_t callbackCode = 0; + + + { RsStackMutex stack(ctrlMutex); /******* LOCKED ********/ std::cerr << "ftController:completeFile(" << hash << ")"; std::cerr << std::endl; @@ -206,35 +219,68 @@ bool ftController::completeFile(std::string hash) /* Move to Correct Location */ if (0 == rename(fc->mCurrentPath.c_str(), fc->mDestination.c_str())) { +#ifdef CONTROL_DEBUG + std::cerr << "ftController::completeFile() renaming to: "; + std::cerr << fc->mDestination; + std::cerr << std::endl; +#endif + /* correct the file_name */ fc->mCurrentPath = fc->mDestination; } else { +#ifdef CONTROL_DEBUG + std::cerr << "ftController::completeFile() FAILED mv to: "; + std::cerr << fc->mDestination; + std::cerr << std::endl; +#endif + fc->mState = ftFileControl::ERROR_COMPLETION; } + /* switch map */ + mCompleted[fc->mHash] = *fc; + mDownloads.erase(it); + + + /* for extralist additions */ + path = fc->mDestination; + //hash = fc->mHash; + size = fc->mSize; + state = fc->mState; + period = 30 * 24 * 3600; /* 30 days */ + flags = 0; + + doCallback = fc->mDoCallback; + callbackCode = fc->mCallbackCode; + + } /******* UNLOCKED ********/ + + + /******************** NO Mutex from Now ******************** + * cos Callback can end up back in this class. + ***********************************************************/ /* If it has a callback - do it now */ - if (fc->mDoCallback) + if (doCallback) { #ifdef CONTROL_DEBUG std::cerr << "ftController::completeFile() doing Callback"; std::cerr << std::endl; #endif - - switch (fc->mCallbackCode) + switch (callbackCode) { case CB_CODE_CACHE: /* callback */ - if (fc->mState == ftFileControl::COMPLETED) + if (state == ftFileControl::COMPLETED) { #ifdef CONTROL_DEBUG std::cerr << "ftController::completeFile() doing Callback : Success"; std::cerr << std::endl; #endif - CompletedCache(fc->mHash); + CompletedCache(hash); } else { @@ -242,8 +288,18 @@ bool ftController::completeFile(std::string hash) std::cerr << "ftController::completeFile() Cache Callback : Failed"; std::cerr << std::endl; #endif - FailedCache(fc->mHash); + FailedCache(hash); } + break; + case CB_CODE_EXTRA: +#ifdef CONTROL_DEBUG + std::cerr << "ftController::completeFile() adding to ExtraList"; + std::cerr << std::endl; +#endif + + mExtraList->addExtraFile(path, hash, size, period, flags); + + break; case CB_CODE_MEDIA: #ifdef CONTROL_DEBUG @@ -263,11 +319,6 @@ bool ftController::completeFile(std::string hash) } - - /* switch map */ - mCompleted[fc->mHash] = *fc; - mDownloads.erase(it); - return true; } @@ -277,6 +328,7 @@ bool ftController::completeFile(std::string hash) /***************************************************************/ const uint32_t FT_CNTRL_STANDARD_RATE = 100 * 1024; +const uint32_t FT_CNTRL_SLOW_RATE = 10 * 1024; bool ftController::FileRequest(std::string fname, std::string hash, uint64_t size, std::string dest, uint32_t flags, @@ -300,6 +352,60 @@ bool ftController::FileRequest(std::string fname, std::string hash, std::cerr << std::endl; #endif + uint32_t rate = 0; + if (flags & RS_FILE_HINTS_BACKGROUND) + { + rate = FT_CNTRL_SLOW_RATE; + } + else + { + rate = FT_CNTRL_STANDARD_RATE; + } + + /* First check if the file is already being downloaded.... + * This is important as some guis request duplicate files regularly. + */ + + std::map::iterator dit; + dit = mDownloads.find(hash); + if (dit != mDownloads.end()) + { + /* we already have it! */ + +#ifdef CONTROL_DEBUG + std::cerr << "ftController::FileRequest() Already Downloading File"; + std::cerr << std::endl; + std::cerr << "\tNo need to download"; + std::cerr << std::endl; +#endif + /* but we should add this peer - if they don't exist! + * (needed for channels). + */ + + for(it = srcIds.begin(); it != srcIds.end(); it++) + { + uint32_t i, j; + if ((dit->second).mTransfer->getPeerState(*it, i, j)) + { +#ifdef CONTROL_DEBUG + std::cerr << "ftController::FileRequest() Peer Existing"; + std::cerr << std::endl; +#endif + continue; /* already added peer */ + } + +#ifdef CONTROL_DEBUG + std::cerr << "ftController::FileRequest() Adding Peer: " << *it; + std::cerr << std::endl; +#endif + /* add peer */ + (dit->second).mTransfer->setPeerState(*it, + PQIPEER_IDLE, rate); + + return true; + } + } + bool doCallback = false; uint32_t callbackCode = 0; if (flags & RS_FILE_HINTS_NO_SEARCH) @@ -314,6 +420,11 @@ bool ftController::FileRequest(std::string fname, std::string hash, doCallback = true; callbackCode = CB_CODE_CACHE; } + else if (flags & RS_FILE_HINTS_EXTRA) + { + doCallback = true; + callbackCode = CB_CODE_EXTRA; + } } else { @@ -361,7 +472,12 @@ bool ftController::FileRequest(std::string fname, std::string hash, } } - if (flags & RS_FILE_HINTS_MEDIA) + if (flags & RS_FILE_HINTS_EXTRA) + { + doCallback = true; + callbackCode = CB_CODE_EXTRA; + } + else if (flags & RS_FILE_HINTS_MEDIA) { doCallback = true; callbackCode = CB_CODE_MEDIA; @@ -408,7 +524,7 @@ bool ftController::FileRequest(std::string fname, std::string hash, //tm->setPeerState(*it, RS_FILE_RATE_FAST | // RS_FILE_PEER_ONLINE, 100000); //tm->setPeerState(*it, PQIPEER_IDLE, 10000); - tm->setPeerState(*it, PQIPEER_IDLE, FT_CNTRL_STANDARD_RATE); + tm->setPeerState(*it, PQIPEER_IDLE, rate); } else if (mConnMgr->isOnline(*it)) { @@ -420,7 +536,7 @@ bool ftController::FileRequest(std::string fname, std::string hash, //tm->setPeerState(*it, RS_FILE_RATE_TRICKLE | // RS_FILE_PEER_ONLINE, 10000); //tm->setPeerState(*it, PQIPEER_IDLE, 10000); - tm->setPeerState(*it, PQIPEER_IDLE, FT_CNTRL_STANDARD_RATE); + tm->setPeerState(*it, PQIPEER_IDLE, rate); } else { @@ -430,7 +546,7 @@ bool ftController::FileRequest(std::string fname, std::string hash, std::cerr << std::endl; #endif //tm->setPeerState(*it, RS_FILE_PEER_OFFLINE, 10000); - tm->setPeerState(*it, PQIPEER_NOT_ONLINE, FT_CNTRL_STANDARD_RATE); + tm->setPeerState(*it, PQIPEER_IDLE, rate); } } @@ -514,6 +630,10 @@ bool ftController::FileDownloads(std::list &hashs) { hashs.push_back(it->second.mHash); } + for(it = mCompleted.begin(); it != mCompleted.end(); it++) + { + hashs.push_back(it->second.mHash); + } return true; } @@ -590,11 +710,21 @@ bool ftController::FileDetails(std::string hash, FileInfo &info) { RsStackMutex stack(ctrlMutex); /******* LOCKED ********/ + bool completed = false; std::map::iterator it; it = mDownloads.find(hash); if (it == mDownloads.end()) { - return false; + /* search completed files too */ + it = mCompleted.find(hash); + if (it == mCompleted.end()) + { + /* Note: mTransfer & mCreator + * are both NULL + */ + return false; + } + completed = true; } /* extract details */ @@ -605,7 +735,10 @@ bool ftController::FileDetails(std::string hash, FileInfo &info) std::list peerIds; std::list::iterator pit; - it->second.mTransfer->getFileSources(peerIds); + if (!completed) + { + it->second.mTransfer->getFileSources(peerIds); + } double totalRate; uint32_t tfRate; @@ -648,7 +781,7 @@ bool ftController::FileDetails(std::string hash, FileInfo &info) } } - if ((it->second).mCreator->finished()) + if ((completed) || ((it->second).mCreator->finished())) { info.downloadStatus = FT_STATE_COMPLETE; } @@ -666,7 +799,15 @@ bool ftController::FileDetails(std::string hash, FileInfo &info) } info.tfRate = totalRate; info.size = (it->second).mSize; - info.transfered = (it->second).mCreator->getRecvd(); + + if (completed) + { + info.transfered = info.size; + } + else + { + info.transfered = (it->second).mCreator->getRecvd(); + } return true; diff --git a/libretroshare/src/ft/ftcontroller.h b/libretroshare/src/ft/ftcontroller.h index 277855318..eb2a85b81 100644 --- a/libretroshare/src/ft/ftcontroller.h +++ b/libretroshare/src/ft/ftcontroller.h @@ -42,6 +42,7 @@ class ftFileCreator; class ftTransferModule; class ftFileProvider; class ftSearch; +class ftExtraList; class ftDataMultiplex; #include "dbase/cachestrapper.h" @@ -55,7 +56,8 @@ class ftDataMultiplex; const uint32_t CB_CODE_CACHE = 0x0001; -const uint32_t CB_CODE_MEDIA = 0x0002; +const uint32_t CB_CODE_EXTRA = 0x0002; +const uint32_t CB_CODE_MEDIA = 0x0004; const uint32_t FC_TRANSFER_COMPLETE = 0x0001; @@ -91,7 +93,7 @@ class ftController: public CacheTransfer, public RsThread, public pqiMonitor, pu /* Setup */ ftController(CacheStrapper *cs, ftDataMultiplex *dm, std::string configDir); -void setFtSearch(ftSearch *); +void setFtSearchNExtra(ftSearch *, ftExtraList *); virtual void run(); @@ -152,6 +154,7 @@ bool completeFile(std::string hash); ftSearch *mSearch; ftDataMultiplex *mDataplex; + ftExtraList *mExtraList; RsMutex ctrlMutex; diff --git a/libretroshare/src/ft/ftserver.cc b/libretroshare/src/ft/ftserver.cc index 5866e61f4..ddfaf91db 100644 --- a/libretroshare/src/ft/ftserver.cc +++ b/libretroshare/src/ft/ftserver.cc @@ -113,7 +113,7 @@ void ftServer::SetupFtServer(NotifyBase *cb) /* make Controller */ mFtController = new ftController(mCacheStrapper, mFtDataplex, mConfigPath); - mFtController -> setFtSearch(mFtSearch); + mFtController -> setFtSearchNExtra(mFtSearch, mFtExtra); std::string tmppath = "."; mFtController->setPartialsDirectory(tmppath); mFtController->setDownloadDirectory(tmppath); @@ -261,7 +261,10 @@ bool ftServer::FileDetails(std::string hash, uint32_t hintflags, FileInfo &info) bool found = false; if (hintflags | RS_FILE_HINTS_DOWNLOAD) { - found = mFtDataplex->FileDetails(hash, hintflags, info); + //found = mFtDataplex->FileDetails(hash, hintflags, info); + // + // Use Controller for download searches. + found = mFtController->FileDetails(hash, info); } else if (hintflags | RS_FILE_HINTS_UPLOAD) { diff --git a/libretroshare/src/ft/fttransfermodule.cc b/libretroshare/src/ft/fttransfermodule.cc index 7b8c54d1c..0cf5fdc64 100644 --- a/libretroshare/src/ft/fttransfermodule.cc +++ b/libretroshare/src/ft/fttransfermodule.cc @@ -469,6 +469,14 @@ bool ftTransferModule::locked_tickPeerTransfer(peerInfo &info) int ageRecv = ts - info.recvTS; int ageReq = ts - info.lastTS; + /* if offline - ignore */ + if ((info.state == PQIPEER_SUSPEND) || + (info.state == PQIPEER_NOT_ONLINE)) + { + + return false; + } + if (ageReq > FT_TM_RESTART_DOWNLOAD) { info.state = PQIPEER_DOWNLOADING; diff --git a/libretroshare/src/rsiface/rsfiles.h b/libretroshare/src/rsiface/rsfiles.h index 384c2c228..511db19fd 100644 --- a/libretroshare/src/rsiface/rsfiles.h +++ b/libretroshare/src/rsiface/rsfiles.h @@ -77,6 +77,8 @@ const uint32_t RS_FILE_HINTS_NO_SEARCH = 0x02000000; //const uint32_t RS_FILE_HINTS_CACHE = 0x00000001; // ALREADY EXISTS const uint32_t RS_FILE_HINTS_MEDIA = 0x00001000; +const uint32_t RS_FILE_HINTS_BACKGROUND = 0x00002000; // To download slowly. + const uint32_t RS_FILE_EXTRA_DELETE = 0x0010; diff --git a/libretroshare/src/rsserver/p3face-startup.cc b/libretroshare/src/rsserver/p3face-startup.cc index 191fb861c..18fb70ea4 100644 --- a/libretroshare/src/rsserver/p3face-startup.cc +++ b/libretroshare/src/rsserver/p3face-startup.cc @@ -645,6 +645,7 @@ int RsServer::StartupRetroShare(RsInit *config) std::string config_dir = config->basedir; std::string localcachedir = config_dir + "/cache/local"; std::string remotecachedir = config_dir + "/cache/remote"; + std::string channelsdir = config_dir + "/channels"; mRanking = new p3Ranking(mConnMgr, RS_SERVICE_TYPE_RANK, /* declaration of cache enable service rank */ mCacheStrapper, mCacheTransfer, @@ -681,8 +682,8 @@ int RsServer::StartupRetroShare(RsInit *config) pqih -> addService(mForums); /* This must be also ticked as a service */ p3Channels *mChannels = new p3Channels(RS_SERVICE_TYPE_CHANNEL, - mCacheStrapper, mCacheTransfer, - localcachedir, remotecachedir, localcachedir); + mCacheStrapper, mCacheTransfer, rsFiles, + localcachedir, remotecachedir, channelsdir); CachePair cp5(mChannels, mChannels, CacheId(RS_SERVICE_TYPE_CHANNEL, 0)); mCacheStrapper -> addCachePair(cp5); diff --git a/libretroshare/src/rsserver/p3face.h b/libretroshare/src/rsserver/p3face.h index dd1ddf2dd..3cb5e1038 100644 --- a/libretroshare/src/rsserver/p3face.h +++ b/libretroshare/src/rsserver/p3face.h @@ -27,7 +27,7 @@ */ //#include "server/filedexserver.h" -#include "ft/ftserver.h"; +#include "ft/ftserver.h" //#include "pqi/pqissl.h" #include "pqi/p3cfgmgr.h" diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index 079208ac3..10ab11aa0 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -31,6 +31,15 @@ #include #include +/**************** PQI_USE_XPGP ******************/ +#if defined(PQI_USE_XPGP) + #include "pqi/authxpgp.h" +#else /* X509 Certificates */ +/**************** PQI_USE_XPGP ******************/ + #include "pqi/authssl.h" +#endif /* X509 Certificates */ +/**************** PQI_USE_XPGP ******************/ + RsPeers *rsPeers = NULL; diff --git a/libretroshare/src/services/p3channels.cc b/libretroshare/src/services/p3channels.cc index 50c03b0d9..0eddb0b05 100644 --- a/libretroshare/src/services/p3channels.cc +++ b/libretroshare/src/services/p3channels.cc @@ -68,13 +68,24 @@ RsChannels *rsChannels = NULL; #define CHANNEL_STOREPERIOD 10000 #define CHANNEL_PUBPERIOD 600 -p3Channels::p3Channels(uint16_t type, CacheStrapper *cs, CacheTransfer *cft, - std::string srcdir, std::string storedir, std::string chanDir) +p3Channels::p3Channels(uint16_t type, CacheStrapper *cs, + CacheTransfer *cft, RsFiles *files, + std::string srcdir, std::string storedir, std::string chanDir) :p3GroupDistrib(type, cs, cft, srcdir, storedir, CONFIG_TYPE_CHANNELS, CHANNEL_STOREPERIOD, CHANNEL_PUBPERIOD), + mRsFiles(files), mChannelsDir(chanDir) { //loadDummyData(); + + /* create chanDir */ + if (!RsDirUtil::checkCreateDirectory(mChannelsDir)) + { + std::cerr << "p3Channels() Failed to create Channels Directory: "; + std::cerr << mChannelsDir; + std::cerr << std::endl; + } + return; } @@ -269,6 +280,24 @@ RsDistribGrp *p3Channels::locked_createPrivateDistribGrp(GroupInfo &info) bool p3Channels::channelSubscribe(std::string cId, bool subscribe) { + std::cerr << "p3Channels::channelSubscribe() "; + std::cerr << cId; + std::cerr << std::endl; + + if (subscribe) + { + std::string channeldir = mChannelsDir + "/" + cId; + + /* create chanDir */ + if (!RsDirUtil::checkCreateDirectory(channeldir)) + { + std::cerr << "p3Channels::channelSubscribe()"; + std::cerr << " Failed to create Channels Directory: "; + std::cerr << channeldir; + std::cerr << std::endl; + } + } + return subscribeToGroup(cId, subscribe); } @@ -285,6 +314,10 @@ bool p3Channels::locked_eventUpdateGroup(GroupInfo *info, bool isNew) std::string msgId; std::string nullId; + std::cerr << "p3Channels::locked_eventUpdateGroup() "; + std::cerr << grpId; + std::cerr << std::endl; + if (isNew) { getPqiNotify()->AddFeedItem(RS_FEED_ITEM_CHAN_NEW, grpId, msgId, nullId); @@ -294,20 +327,137 @@ bool p3Channels::locked_eventUpdateGroup(GroupInfo *info, bool isNew) getPqiNotify()->AddFeedItem(RS_FEED_ITEM_CHAN_UPDATE, grpId, msgId, nullId); } + if (info->flags & RS_DISTRIB_SUBSCRIBED) + { + std::string channeldir = mChannelsDir + "/" + grpId; + + std::cerr << "p3Channels::locked_eventUpdateGroup() "; + std::cerr << " creating directory: " << channeldir; + std::cerr << std::endl; + + /* create chanDir */ + if (!RsDirUtil::checkCreateDirectory(channeldir)) + { + std::cerr << "p3Channels::locked_eventUpdateGroup() "; + std::cerr << "Failed to create Channels Directory: "; + std::cerr << channeldir; + std::cerr << std::endl; + } + } + + return true; } -bool p3Channels::locked_eventNewMsg(RsDistribMsg *msg) +/* only download in the first week of channel + * older stuff can be manually downloaded. + */ + +const uint32_t DOWNLOAD_PERIOD = 7 * 24 * 3600; + +bool p3Channels::locked_eventDuplicateMsg(GroupInfo *grp, RsDistribMsg *msg, std::string id) { std::string grpId = msg->grpId; std::string msgId = msg->msgId; std::string nullId; - getPqiNotify()->AddFeedItem(RS_FEED_ITEM_CHAN_MSG, grpId, msgId, nullId); + + std::cerr << "p3Channels::locked_eventDuplicateMsg() "; + std::cerr << " grpId: " << grpId << " msgId: " << msgId; + std::cerr << " peerId: " << id; + std::cerr << std::endl; + + + RsChannelMsg *chanMsg = dynamic_cast(msg); + if (!chanMsg) + { + return true; + } + + /* request the files + * NB: This will result in duplicates. + * it is upto ftserver/ftcontroller/ftextralist + * + * download, then add to + * + * */ + + //bool download = (grp->flags & (RS_DISTRIB_ADMIN | + // RS_DISTRIB_PUBLISH | RS_DISTRIB_SUBSCRIBED)) + bool download = (grp->flags & RS_DISTRIB_SUBSCRIBED); + + /* check subscribed */ + if (!download) + { + return true; + } + + /* check age */ + time_t age = time(NULL) - msg->timestamp; + + if (age > DOWNLOAD_PERIOD) + { + return true; + } + + /* Iterate through files */ + std::list::iterator fit; + for(fit = chanMsg->attachment.items.begin(); + fit != chanMsg->attachment.items.end(); fit++) + { + std::string fname = fit->name; + std::string hash = fit->hash; + uint64_t size = fit->filesize; + std::string channelname = grpId; + std::string localpath = mChannelsDir + "/" + channelname; + uint32_t flags = RS_FILE_HINTS_EXTRA; + std::list srcIds; + + srcIds.push_back(id); + + /* download it ... and flag for ExtraList + * don't do pre-search check as FileRequest does it better + */ + + std::cerr << "p3Channels::locked_eventDuplicateMsg() "; + std::cerr << " Downloading: " << fname; + std::cerr << " to: " << localpath; + std::cerr << " from: " << id; + std::cerr << std::endl; + + mRsFiles->FileRequest(fname, hash, size, + localpath, flags, srcIds); + } + + return true; } +bool p3Channels::locked_eventNewMsg(GroupInfo *grp, RsDistribMsg *msg, std::string id) +{ + std::string grpId = msg->grpId; + std::string msgId = msg->msgId; + std::string nullId; + + std::cerr << "p3Channels::locked_eventNewMsg() "; + std::cerr << " grpId: " << grpId; + std::cerr << " msgId: " << msgId; + std::cerr << " peerId: " << id; + std::cerr << std::endl; + + getPqiNotify()->AddFeedItem(RS_FEED_ITEM_CHAN_MSG, grpId, msgId, nullId); + + /* request the files + * NB: This could result in duplicates. + * which must be handled by ft side. + * + * this is exactly what DuplicateMsg does. + * */ + return locked_eventDuplicateMsg(grp, msg, id); +} + + /****************************************/ diff --git a/libretroshare/src/services/p3channels.h b/libretroshare/src/services/p3channels.h index c05426892..dc4631365 100644 --- a/libretroshare/src/services/p3channels.h +++ b/libretroshare/src/services/p3channels.h @@ -27,6 +27,7 @@ */ #include "rsiface/rschannels.h" +#include "rsiface/rsfiles.h" #include "services/p3distrib.h" #include "serialiser/rstlvtypes.h" @@ -37,7 +38,7 @@ class p3Channels: public p3GroupDistrib, public RsChannels { public: - p3Channels(uint16_t type, CacheStrapper *cs, CacheTransfer *cft, + p3Channels(uint16_t type, CacheStrapper *cs, CacheTransfer *cft, RsFiles *files, std::string srcdir, std::string storedir, std::string channelsdir); virtual ~p3Channels(); @@ -62,7 +63,8 @@ virtual bool channelSubscribe(std::string cId, bool subscribe); /***************************************************************************************/ virtual bool locked_eventUpdateGroup(GroupInfo *, bool isNew); -virtual bool locked_eventNewMsg(RsDistribMsg *); +virtual bool locked_eventNewMsg(GroupInfo *, RsDistribMsg *, std::string); +virtual bool locked_eventDuplicateMsg(GroupInfo *, RsDistribMsg *, std::string); /****************************************/ /********* Overloaded Functions *********/ @@ -78,6 +80,7 @@ virtual RsDistribGrp *locked_createPrivateDistribGrp(GroupInfo &info); private: + RsFiles *mRsFiles; std::string mChannelsDir; }; diff --git a/libretroshare/src/services/p3distrib.cc b/libretroshare/src/services/p3distrib.cc index 0c8961c78..a45895155 100644 --- a/libretroshare/src/services/p3distrib.cc +++ b/libretroshare/src/services/p3distrib.cc @@ -561,19 +561,6 @@ void p3GroupDistrib::loadMsg(RsDistribSignedMsg *newMsg, std::string src, bool l return; } - /* check for duplicate message */ - std::map::iterator mit; - if ((git->second).msgs.end() != (git->second).msgs.find(newMsg->msgId)) - { -#ifdef DISTRIB_DEBUG - std::cerr << "p3GroupDistrib::loadMsg() Msg already exists" << std::endl; - std::cerr << std::endl; -#endif - /* if already there -> remove */ - delete newMsg; - return; - } - /****************** check the msg ******************/ if (!locked_validateDistribSignedMsg(git->second, newMsg)) { @@ -585,6 +572,26 @@ void p3GroupDistrib::loadMsg(RsDistribSignedMsg *newMsg, std::string src, bool l return; } + /* check for duplicate message + * + * do this after validate - because we are calling + * duplicateMsg... only want to do if is good. + */ + std::map::iterator mit; + mit = (git->second).msgs.find(newMsg->msgId); + if (mit != (git->second).msgs.end()) + { +#ifdef DISTRIB_DEBUG + std::cerr << "p3GroupDistrib::loadMsg() Msg already exists" << std::endl; + std::cerr << std::endl; +#endif + /* if already there -> remove */ + locked_eventDuplicateMsg(&(git->second), mit->second, src); + delete newMsg; + return; + } + + /* convert Msg */ RsDistribMsg *msg = unpackDistribSignedMsg(newMsg); if (!msg) @@ -617,7 +624,7 @@ void p3GroupDistrib::loadMsg(RsDistribSignedMsg *newMsg, std::string src, bool l #endif /* Callback for any derived classes to play with */ - locked_eventNewMsg(msg); + locked_eventNewMsg(&(git->second), msg, src); /* else if group = subscribed | listener -> publish */ /* if it has come from us... then it has been published already */ diff --git a/libretroshare/src/services/p3distrib.h b/libretroshare/src/services/p3distrib.h index cccdd5328..b9d97ce4e 100644 --- a/libretroshare/src/services/p3distrib.h +++ b/libretroshare/src/services/p3distrib.h @@ -280,7 +280,8 @@ RsDistribMsg *locked_getGroupMsg(std::string grpId, std::string msgId); /***************************************************************************************/ virtual bool locked_eventUpdateGroup(GroupInfo *, bool isNew) = 0; -virtual bool locked_eventNewMsg(RsDistribMsg *) = 0; +virtual bool locked_eventDuplicateMsg(GroupInfo *, RsDistribMsg *, std::string id) = 0; +virtual bool locked_eventNewMsg(GroupInfo *, RsDistribMsg *, std::string id) = 0; /***************************************************************************************/ /********************************* p3Config ********************************************/ diff --git a/libretroshare/src/services/p3forums.cc b/libretroshare/src/services/p3forums.cc index 376a8de70..4bf7f2854 100644 --- a/libretroshare/src/services/p3forums.cc +++ b/libretroshare/src/services/p3forums.cc @@ -376,8 +376,12 @@ bool p3Forums::locked_eventUpdateGroup(GroupInfo *info, bool isNew) return true; } +bool p3Forums::locked_eventDuplicateMsg(GroupInfo *grp, RsDistribMsg *msg, std::string id) +{ + return true; +} -bool p3Forums::locked_eventNewMsg(RsDistribMsg *msg) +bool p3Forums::locked_eventNewMsg(GroupInfo *grp, RsDistribMsg *msg, std::string id) { std::string grpId = msg->grpId; std::string msgId = msg->msgId; diff --git a/libretroshare/src/services/p3forums.h b/libretroshare/src/services/p3forums.h index 0903284d8..551d304cb 100644 --- a/libretroshare/src/services/p3forums.h +++ b/libretroshare/src/services/p3forums.h @@ -100,7 +100,9 @@ virtual bool forumSubscribe(std::string fId, bool subscribe); /***************************************************************************************/ virtual bool locked_eventUpdateGroup(GroupInfo *, bool isNew); -virtual bool locked_eventNewMsg(RsDistribMsg *); +virtual bool locked_eventDuplicateMsg(GroupInfo *, RsDistribMsg *, std::string); +virtual bool locked_eventNewMsg(GroupInfo *, RsDistribMsg *, std::string); + /****************************************/