1 #include "../../include/freenet/introductionpuzzlerequester.h"
\r
2 #include "../../include/freenet/introductionpuzzlexml.h"
\r
3 #include "../../include/option.h"
\r
4 #include "../../include/stringfunctions.h"
\r
10 IntroductionPuzzleRequester::IntroductionPuzzleRequester()
\r
15 IntroductionPuzzleRequester::IntroductionPuzzleRequester(FCPv2 *fcp):IFCPConnected(fcp)
\r
20 void IntroductionPuzzleRequester::FCPConnected()
\r
22 m_requesting.clear();
\r
26 void IntroductionPuzzleRequester::FCPDisconnected()
\r
31 const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)
\r
34 SQLite3DB::Statement st;
\r
35 std::vector<std::string> idparts;
\r
37 std::vector<char> data;
\r
38 IntroductionPuzzleXML xml;
\r
43 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
44 StringFunctions::Convert(message["DataLength"],datalength);
\r
45 StringFunctions::Convert(idparts[1],identityid);
\r
46 StringFunctions::Convert(idparts[2],index);
\r
48 // wait for all data to be received from connection
\r
49 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
54 // if we got disconnected- return immediately
\r
55 if(m_fcp->Connected()==false)
\r
61 data.resize(datalength);
\r
62 m_fcp->ReceiveRaw(&data[0],datalength);
\r
64 // parse file into xml and update the database
\r
65 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
67 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found,UUID,Type,MimeType,PuzzleData) VALUES(?,?,?,?,?,?,?,?);");
\r
68 st.Bind(0,identityid);
\r
69 st.Bind(1,idparts[4]);
\r
72 st.Bind(4,xml.GetUUID());
\r
73 st.Bind(5,xml.GetType());
\r
74 st.Bind(6,xml.GetMimeType());
\r
75 st.Bind(7,xml.GetPuzzleData());
\r
79 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleRequester::HandleAllData parsed IntroductionPuzzle XML file : "+message["Identifier"]);
\r
83 // bad data - mark index
\r
84 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
85 st.Bind(0,identityid);
\r
86 st.Bind(1,idparts[4]);
\r
91 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData error parsing IntroductionPuzzle XML file : "+message["Identifier"]);
\r
94 // remove this identityid from request list
\r
95 RemoveFromRequestList(identityid);
\r
101 const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)
\r
104 SQLite3DB::Statement st;
\r
105 std::vector<std::string> idparts;
\r
110 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
111 StringFunctions::Convert(idparts[1],identityid);
\r
112 StringFunctions::Convert(idparts[2],index);
\r
114 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
115 if(message["Fatal"]=="true")
\r
117 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
118 st.Bind(0,identityid);
\r
119 st.Bind(1,idparts[4]);
\r
124 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
127 // remove this identityid from request list
\r
128 RemoveFromRequestList(identityid);
\r
134 const bool IntroductionPuzzleRequester::HandleMessage(FCPMessage &message)
\r
137 if(message["Identifier"].find("IntroductionPuzzleRequester")==0)
\r
139 if(message.GetName()=="DataFound")
\r
144 if(message.GetName()=="AllData")
\r
146 return HandleAllData(message);
\r
149 if(message.GetName()=="GetFailed")
\r
151 return HandleGetFailed(message);
\r
154 if(message.GetName()=="IdentifierCollision")
\r
156 // remove one of the ids from the requesting list
\r
158 std::vector<std::string> idparts;
\r
159 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
160 StringFunctions::Convert(idparts[1],identityid);
\r
161 RemoveFromRequestList(identityid);
\r
169 void IntroductionPuzzleRequester::Initialize()
\r
171 std::string tempval="";
\r
172 Option::Instance()->Get("MaxIntroductionPuzzleRequests",tempval);
\r
173 StringFunctions::Convert(tempval,m_maxrequests);
\r
174 if(m_maxrequests<1)
\r
177 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
179 if(m_maxrequests>100)
\r
181 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
183 Option::Instance()->Get("MessageBase",m_messagebase);
\r
184 m_tempdate.SetToGMTime();
\r
187 void IntroductionPuzzleRequester::PopulateIDList()
\r
194 // select identities that aren't single use and have been seen today
\r
195 SQLite3DB::Statement st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND SingleUse='false' AND LastSeen>='"+now.Format("%Y-%m-%d")+"';");
\r
200 while(st.RowReturned())
\r
202 st.ResultInt(0,id);
\r
208 void IntroductionPuzzleRequester::Process()
\r
210 // max is the smaller of the config value or the total number of identities we will request from
\r
211 long max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;
\r
213 // try to keep up to max requests going
\r
214 if(m_requesting.size()<max)
\r
216 std::map<long,bool>::iterator i=m_ids.begin();
\r
217 while(i!=m_ids.end() && (*i).second==true)
\r
224 StartRequest((*i).first);
\r
228 // we requested from all ids in the list, repopulate the list
\r
232 // special case - if there were 0 identities on the list when we started then we will never get a chance to repopulate the list
\r
233 // this will recheck for ids every minute
\r
236 if(m_tempdate<(now-(1.0/1440.0)))
\r
243 void IntroductionPuzzleRequester::RegisterWithThread(FreenetMasterThread *thread)
\r
245 thread->RegisterFCPConnected(this);
\r
246 thread->RegisterFCPMessageHandler(this);
\r
247 thread->RegisterPeriodicProcessor(this);
\r
250 void IntroductionPuzzleRequester::RemoveFromRequestList(const long identityid)
\r
252 std::vector<long>::iterator i=m_requesting.begin();
\r
253 while(i!=m_requesting.end() && (*i)!=identityid)
\r
257 if(i!=m_requesting.end())
\r
259 m_requesting.erase(i);
\r
263 void IntroductionPuzzleRequester::StartRequest(const long identityid)
\r
266 FCPMessage message;
\r
267 std::string publickey;
\r
269 std::string indexstr;
\r
270 std::string identityidstr;
\r
272 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
273 st.Bind(0,identityid);
\r
276 if(st.RowReturned())
\r
278 st.ResultText(0,publickey);
\r
282 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblIntroductionPuzzleRequests WHERE Day=? AND IdentityID=?;");
\r
283 st2.Bind(0,now.Format("%Y-%m-%d"));
\r
284 st2.Bind(1,identityid);
\r
288 if(st2.RowReturned())
\r
290 if(st2.ResultNull(0)==false)
\r
292 st2.ResultInt(0,index);
\r
298 StringFunctions::Convert(index,indexstr);
\r
299 StringFunctions::Convert(identityid,identityidstr);
\r
301 message.SetName("ClientGet");
\r
302 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";
\r
303 message["Identifier"]="IntroductionPuzzleRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
304 message["ReturnType"]="direct";
\r
305 message["MaxSize"]="1000000"; // 1 MB
\r
307 m_fcp->SendMessage(message);
\r
309 m_requesting.push_back(identityid);
\r
312 m_ids[identityid]=true;
\r