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
68 // TODO - check if last part of UUID matches public key of identity who inserted it
\r
70 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found,UUID,Type,MimeType,PuzzleData) VALUES(?,?,?,?,?,?,?,?);");
\r
71 st.Bind(0,identityid);
\r
72 st.Bind(1,idparts[4]);
\r
75 st.Bind(4,xml.GetUUID());
\r
76 st.Bind(5,xml.GetType());
\r
77 st.Bind(6,xml.GetMimeType());
\r
78 st.Bind(7,xml.GetPuzzleData());
\r
82 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleRequester::HandleAllData parsed IntroductionPuzzle XML file : "+message["Identifier"]);
\r
86 // bad data - mark index
\r
87 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
88 st.Bind(0,identityid);
\r
89 st.Bind(1,idparts[4]);
\r
94 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData error parsing IntroductionPuzzle XML file : "+message["Identifier"]);
\r
97 // remove this identityid from request list
\r
98 RemoveFromRequestList(identityid);
\r
104 const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)
\r
107 SQLite3DB::Statement st;
\r
108 std::vector<std::string> idparts;
\r
113 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
114 StringFunctions::Convert(idparts[1],identityid);
\r
115 StringFunctions::Convert(idparts[2],index);
\r
117 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
118 if(message["Fatal"]=="true")
\r
120 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
121 st.Bind(0,identityid);
\r
122 st.Bind(1,idparts[4]);
\r
127 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
130 // remove this identityid from request list
\r
131 RemoveFromRequestList(identityid);
\r
137 const bool IntroductionPuzzleRequester::HandleMessage(FCPMessage &message)
\r
140 if(message["Identifier"].find("IntroductionPuzzleRequester")==0)
\r
142 if(message.GetName()=="DataFound")
\r
147 if(message.GetName()=="AllData")
\r
149 return HandleAllData(message);
\r
152 if(message.GetName()=="GetFailed")
\r
154 return HandleGetFailed(message);
\r
157 if(message.GetName()=="IdentifierCollision")
\r
159 // remove one of the ids from the requesting list
\r
161 std::vector<std::string> idparts;
\r
162 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
163 StringFunctions::Convert(idparts[1],identityid);
\r
164 RemoveFromRequestList(identityid);
\r
172 void IntroductionPuzzleRequester::Initialize()
\r
174 std::string tempval="";
\r
175 Option::Instance()->Get("MaxIntroductionPuzzleRequests",tempval);
\r
176 StringFunctions::Convert(tempval,m_maxrequests);
\r
177 if(m_maxrequests<1)
\r
180 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
182 if(m_maxrequests>100)
\r
184 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
186 Option::Instance()->Get("MessageBase",m_messagebase);
\r
187 m_tempdate.SetToGMTime();
\r
190 void IntroductionPuzzleRequester::PopulateIDList()
\r
194 std::string limitnum="30";
\r
196 // if we don't have an identity that we haven't seen yet, then set limit to 5
\r
197 SQLite3DB::Statement st=m_db->Prepare("SELECT tblLocalIdentity.LocalIdentityID FROM tblLocalIdentity LEFT JOIN tblIdentity ON tblLocalIdentity.PublicKey=tblIdentity.PublicKey WHERE tblIdentity.IdentityID IS NULL;");
\r
199 if(!st.RowReturned())
\r
207 // select identities that aren't single use and have been seen today ( order by trust DESC and limit to limitnum )
\r
208 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")+"' ORDER BY LocalMessageTrust DESC LIMIT 0,"+limitnum+";");
\r
213 while(st.RowReturned())
\r
215 st.ResultInt(0,id);
\r
221 void IntroductionPuzzleRequester::Process()
\r
223 // max is the smaller of the config value or the total number of identities we will request from
\r
224 long max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;
\r
226 // try to keep up to max requests going
\r
227 if(m_requesting.size()<max)
\r
229 std::map<long,bool>::iterator i=m_ids.begin();
\r
230 while(i!=m_ids.end() && (*i).second==true)
\r
237 StartRequest((*i).first);
\r
241 // we requested from all ids in the list, repopulate the list
\r
245 // 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
246 // this will recheck for ids every minute
\r
249 if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))
\r
256 void IntroductionPuzzleRequester::RegisterWithThread(FreenetMasterThread *thread)
\r
258 thread->RegisterFCPConnected(this);
\r
259 thread->RegisterFCPMessageHandler(this);
\r
260 thread->RegisterPeriodicProcessor(this);
\r
263 void IntroductionPuzzleRequester::RemoveFromRequestList(const long identityid)
\r
265 std::vector<long>::iterator i=m_requesting.begin();
\r
266 while(i!=m_requesting.end() && (*i)!=identityid)
\r
270 if(i!=m_requesting.end())
\r
272 m_requesting.erase(i);
\r
276 void IntroductionPuzzleRequester::StartRequest(const long identityid)
\r
279 FCPMessage message;
\r
280 std::string publickey;
\r
282 std::string indexstr;
\r
283 std::string identityidstr;
\r
285 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
286 st.Bind(0,identityid);
\r
289 if(st.RowReturned())
\r
291 st.ResultText(0,publickey);
\r
295 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblIntroductionPuzzleRequests WHERE Day=? AND IdentityID=?;");
\r
296 st2.Bind(0,now.Format("%Y-%m-%d"));
\r
297 st2.Bind(1,identityid);
\r
301 if(st2.RowReturned())
\r
303 if(st2.ResultNull(0)==false)
\r
305 st2.ResultInt(0,index);
\r
311 StringFunctions::Convert(index,indexstr);
\r
312 StringFunctions::Convert(identityid,identityidstr);
\r
314 message.SetName("ClientGet");
\r
315 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";
\r
316 message["Identifier"]="IntroductionPuzzleRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
317 message["ReturnType"]="direct";
\r
318 message["MaxSize"]="1000000"; // 1 MB
\r
320 m_fcp->SendMessage(message);
\r
322 m_requesting.push_back(identityid);
\r
325 m_ids[identityid]=true;
\r