1 #ifndef _iindexrequester_
\r
2 #define _iindexrequester_
\r
4 #include "../idatabase.h"
\r
5 #include "../ilogger.h"
\r
6 #include "../datetime.h"
\r
7 #include "../option.h"
\r
8 #include "../stringfunctions.h"
\r
9 #include "ifreenetregistrable.h"
\r
10 #include "ifcpconnected.h"
\r
11 #include "ifcpmessagehandler.h"
\r
12 #include "iperiodicprocessor.h"
\r
18 template <class IDTYPE>
\r
19 class IIndexRequester:public IFreenetRegistrable,public IFCPConnected,public IFCPMessageHandler,public IPeriodicProcessor,public IDatabase,public ILogger
\r
23 IIndexRequester(FCPv2 *fcp);
\r
24 virtual ~IIndexRequester() {}
\r
26 virtual void FCPConnected();
\r
27 virtual void FCPDisconnected();
\r
28 virtual const bool HandleMessage(FCPMessage &message);
\r
30 virtual void Process();
\r
32 virtual void RegisterWithThread(FreenetMasterThread *thread);
\r
35 void InitializeIIndexRequester();
\r
36 virtual void Initialize()=0; // initialize m_maxrequests and m_fcpuniquename
\r
37 virtual void PopulateIDList()=0;
\r
38 virtual void StartRequest(const IDTYPE &id)=0;
\r
39 virtual const bool HandleAllData(FCPMessage &message)=0;
\r
40 virtual const bool HandleGetFailed(FCPMessage &message)=0;
\r
41 virtual void RemoveFromRequestList(const IDTYPE id);
\r
43 DateTime m_tempdate;
\r
44 DateTime m_lastreceived;
\r
45 DateTime m_lastpopulated;
\r
46 std::string m_messagebase;
\r
47 std::map<IDTYPE,bool> m_ids; // map of all ids we know and whether we have requested file from them yet
\r
48 std::vector<IDTYPE> m_requesting; // list of ids we are currently requesting from
\r
50 // these MUST be populated by child class
\r
52 std::string m_fcpuniquename;
\r
56 template <class IDTYPE>
\r
57 IIndexRequester<IDTYPE>::IIndexRequester()
\r
59 InitializeIIndexRequester();
\r
62 template <class IDTYPE>
\r
63 IIndexRequester<IDTYPE>::IIndexRequester(FCPv2 *fcp):IFCPConnected(fcp)
\r
65 InitializeIIndexRequester();
\r
68 template <class IDTYPE>
\r
69 void IIndexRequester<IDTYPE>::FCPConnected()
\r
71 // make sure variables have been initialized by the derived class
\r
72 if(m_maxrequests==-1)
\r
74 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IIndexRequester<IDTYPE>::FCPConnected maxrequests not initialized correctly!");
\r
76 if(m_fcpuniquename=="")
\r
78 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IIndexRequester<IDTYPE>::FCPConnected fcpuniquename not initialized correctly!");
\r
80 if(m_fcpuniquename.find("|")!=std::string::npos)
\r
82 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IIndexRequester<IDTYPE>::FCPConnected fcpuniquename contains | character! This is not a valid character!");
\r
85 m_lastreceived.SetToGMTime();
\r
86 m_requesting.clear();
\r
88 m_lastpopulated.SetToGMTime();
\r
91 template <class IDTYPE>
\r
92 void IIndexRequester<IDTYPE>::FCPDisconnected()
\r
97 template <class IDTYPE>
\r
98 const bool IIndexRequester<IDTYPE>::HandleMessage(FCPMessage &message)
\r
101 if(message["Identifier"].find(m_fcpuniquename)==0)
\r
104 m_lastreceived.SetToGMTime();
\r
106 if(message.GetName()=="DataFound")
\r
111 if(message.GetName()=="AllData")
\r
113 return HandleAllData(message);
\r
116 if(message.GetName()=="GetFailed")
\r
118 return HandleGetFailed(message);
\r
121 if(message.GetName()=="IdentifierCollision")
\r
123 // remove one of the ids from the requesting list
\r
125 std::vector<std::string> idparts;
\r
126 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
127 StringFunctions::Convert(idparts[1],id);
\r
128 RemoveFromRequestList(id);
\r
136 template <class IDTYPE>
\r
137 void IIndexRequester<IDTYPE>::InitializeIIndexRequester()
\r
140 m_fcpuniquename="";
\r
142 Option::Instance()->Get("MessageBase",m_messagebase);
\r
143 m_tempdate.SetToGMTime();
\r
144 m_lastreceived.SetToGMTime();
\r
145 m_lastpopulated.SetToGMTime();
\r
146 m_lastpopulated.Add(0,-10);
\r
149 template <class IDTYPE>
\r
150 void IIndexRequester<IDTYPE>::Process()
\r
155 // max is the smaller of the config value or the total number of ids we will request from
\r
156 typename std::map<IDTYPE,bool>::size_type max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;
\r
158 // try to keep up to max requests going
\r
159 if(m_requesting.size()<max)
\r
161 typename std::map<IDTYPE,bool>::iterator i=m_ids.begin();
\r
163 while(i!=m_ids.end() && (*i).second==true)
\r
170 StartRequest((*i).first);
\r
174 // we requested from all ids in the list, repopulate the list (only every 10 minutes)
\r
175 if(m_lastpopulated<(now-1.0/144.0))
\r
178 m_lastpopulated.SetToGMTime();
\r
182 // special case - if there were 0 ids on the list when we started then we will never get a chance to repopulate the list
\r
183 // this will recheck for ids every minute
\r
184 if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))
\r
189 // if we haven't received any messages to this object in 10 minutes, clear the requests and repopulate id list
\r
190 if(m_ids.size()>0 && m_lastreceived<(now-(1.0/144.0)))
\r
192 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IIndexRequester<IDTYPE>::Process() Object has not received any messages in 10 minutes. Restarting requests.");
\r
198 template <class IDTYPE>
\r
199 void IIndexRequester<IDTYPE>::RegisterWithThread(FreenetMasterThread *thread)
\r
201 thread->RegisterFCPConnected(this);
\r
202 thread->RegisterFCPMessageHandler(this);
\r
203 thread->RegisterPeriodicProcessor(this);
\r
206 template <class IDTYPE>
\r
207 void IIndexRequester<IDTYPE>::RemoveFromRequestList(const IDTYPE id)
\r
209 typename std::vector<IDTYPE>::iterator i=m_requesting.begin();
\r
210 while(i!=m_requesting.end() && (*i)!=id)
\r
214 if(i!=m_requesting.end())
\r
216 m_requesting.erase(i);
\r
220 #endif // _iindexrequester_
\r