X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=include%2Ffreenet%2Fiindexrequester.h;h=1966407c83b8811ee3e783903932d1bb86868cc0;hb=59a5414ec47a2932a7802fcd1d98c4d80166564f;hp=35c16c1ca95d801c22b72bdc430cbdca10e0cf60;hpb=df316253862dc50e8e5a790d9634ef90be37badb;p=fms.git diff --git a/include/freenet/iindexrequester.h b/include/freenet/iindexrequester.h index 35c16c1..1966407 100644 --- a/include/freenet/iindexrequester.h +++ b/include/freenet/iindexrequester.h @@ -3,7 +3,6 @@ #include "../idatabase.h" #include "../ilogger.h" -#include "../datetime.h" #include "../option.h" #include "../stringfunctions.h" #include "ifreenetregistrable.h" @@ -11,6 +10,12 @@ #include "ifcpmessagehandler.h" #include "iperiodicprocessor.h" +#include +#include +#include + +#include + #ifdef XMEM #include #endif @@ -19,13 +24,13 @@ template class IIndexRequester:public IFreenetRegistrable,public IFCPConnected,public IFCPMessageHandler,public IPeriodicProcessor,public IDatabase,public ILogger { public: - IIndexRequester(); - IIndexRequester(FCPv2 *fcp); + IIndexRequester(SQLite3DB::DB *db); + IIndexRequester(SQLite3DB::DB *db, FCPv2::Connection *fcp); virtual ~IIndexRequester() {} virtual void FCPConnected(); virtual void FCPDisconnected(); - virtual const bool HandleMessage(FCPMessage &message); + virtual const bool HandleMessage(FCPv2::Message &message); virtual void Process(); @@ -36,29 +41,31 @@ protected: virtual void Initialize()=0; // initialize m_maxrequests and m_fcpuniquename virtual void PopulateIDList()=0; virtual void StartRequest(const IDTYPE &id)=0; - virtual const bool HandleAllData(FCPMessage &message)=0; - virtual const bool HandleGetFailed(FCPMessage &message)=0; + virtual const bool HandleAllData(FCPv2::Message &message)=0; + virtual const bool HandleGetFailed(FCPv2::Message &message)=0; virtual void RemoveFromRequestList(const IDTYPE id); - DateTime m_tempdate; + Poco::DateTime m_tempdate; + Poco::DateTime m_lastreceived; + Poco::DateTime m_lastpopulated; std::string m_messagebase; std::map m_ids; // map of all ids we know and whether we have requested file from them yet std::vector m_requesting; // list of ids we are currently requesting from // these MUST be populated by child class - long m_maxrequests; + int m_maxrequests; std::string m_fcpuniquename; }; template -IIndexRequester::IIndexRequester() +IIndexRequester::IIndexRequester(SQLite3DB::DB *db):IDatabase(db) { InitializeIIndexRequester(); } template -IIndexRequester::IIndexRequester(FCPv2 *fcp):IFCPConnected(fcp) +IIndexRequester::IIndexRequester(SQLite3DB::DB *db, FCPv2::Connection *fcp):IDatabase(db),IFCPConnected(fcp) { InitializeIIndexRequester(); } @@ -69,19 +76,21 @@ void IIndexRequester::FCPConnected() // make sure variables have been initialized by the derived class if(m_maxrequests==-1) { - m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IIndexRequester::FCPConnected maxrequests not initialized correctly!"); + m_log->fatal("IIndexRequester::FCPConnected maxrequests not initialized correctly!"); } if(m_fcpuniquename=="") { - m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IIndexRequester::FCPConnected fcpuniquename not initialized correctly!"); + m_log->fatal("IIndexRequester::FCPConnected fcpuniquename not initialized correctly!"); } if(m_fcpuniquename.find("|")!=std::string::npos) { - m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IIndexRequester::FCPConnected fcpuniquename contains | character! This is not a valid character!"); + m_log->fatal("IIndexRequester::FCPConnected fcpuniquename "+m_fcpuniquename+" contains | character! This is not a valid character!"); } + m_lastreceived=Poco::Timestamp(); m_requesting.clear(); PopulateIDList(); + m_lastpopulated=Poco::Timestamp(); } template @@ -91,11 +100,14 @@ void IIndexRequester::FCPDisconnected() } template -const bool IIndexRequester::HandleMessage(FCPMessage &message) +const bool IIndexRequester::HandleMessage(FCPv2::Message &message) { if(message["Identifier"].find(m_fcpuniquename)==0) { + + m_lastreceived=Poco::Timestamp(); + if(message.GetName()=="DataFound") { return true; @@ -131,14 +143,20 @@ void IIndexRequester::InitializeIIndexRequester() { m_maxrequests=-1; m_fcpuniquename=""; + Option option(m_db); - Option::Instance()->Get("MessageBase",m_messagebase); - m_tempdate.SetToGMTime(); + option.Get("MessageBase",m_messagebase); + m_tempdate=Poco::Timestamp(); + m_lastreceived=Poco::Timestamp(); + m_lastpopulated=Poco::Timestamp(); + m_lastpopulated-=Poco::Timespan(0,0,10,0,0); } template void IIndexRequester::Process() { + Poco::DateTime now; + // max is the smaller of the config value or the total number of ids we will request from typename std::map::size_type max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests; @@ -158,19 +176,27 @@ void IIndexRequester::Process() } else { - // we requested from all ids in the list, repopulate the list - PopulateIDList(); + // we requested from all ids in the list, repopulate the list (only every 10 minutes) + if(m_lastpopulated<(now-Poco::Timespan(0,0,10,0,0))) + { + PopulateIDList(); + m_lastpopulated=Poco::Timestamp(); + } } } // special case - if there were 0 ids on the list when we started then we will never get a chance to repopulate the list // this will recheck for ids every minute - DateTime now; - now.SetToGMTime(); - if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0))) + if(m_ids.size()==0 && m_tempdate<(now-Poco::Timespan(0,0,1,0,0))) { PopulateIDList(); m_tempdate=now; } + // if we haven't received any messages to this object in 10 minutes, clear the requests and repopulate id list + if(m_ids.size()>0 && m_lastreceived<(now-Poco::Timespan(0,0,10,0,0))) + { + m_log->error("IIndexRequester::Process "+m_fcpuniquename+" Object has not received any messages in 10 minutes. Restarting requests."); + FCPConnected(); + } } @@ -185,11 +211,8 @@ void IIndexRequester::RegisterWithThread(FreenetMasterThread *thread) template void IIndexRequester::RemoveFromRequestList(const IDTYPE id) { - typename std::vector::iterator i=m_requesting.begin(); - while(i!=m_requesting.end() && (*i)!=id) - { - i++; - } + typename std::vector::iterator i=std::find(m_requesting.begin(),m_requesting.end(),id); + if(i!=m_requesting.end()) { m_requesting.erase(i);