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
191 std::string limitnum="30";
\r
193 // if we don't have an identity that we haven't seen yet, then set limit to 5
\r
194 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
196 if(!st.RowReturned())
\r
204 // select identities that aren't single use and have been seen today ( order by trust DESC and limit to limitnum )
\r
205 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 LIMIT 0,"+limitnum+";");
\r
210 while(st.RowReturned())
\r
212 st.ResultInt(0,id);
\r
218 void IntroductionPuzzleRequester::Process()
\r
220 // max is the smaller of the config value or the total number of identities we will request from
\r
221 long max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;
\r
223 // try to keep up to max requests going
\r
224 if(m_requesting.size()<max)
\r
226 std::map<long,bool>::iterator i=m_ids.begin();
\r
227 while(i!=m_ids.end() && (*i).second==true)
\r
234 StartRequest((*i).first);
\r
238 // we requested from all ids in the list, repopulate the list
\r
242 // 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
243 // this will recheck for ids every minute
\r
246 if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))
\r
253 void IntroductionPuzzleRequester::RegisterWithThread(FreenetMasterThread *thread)
\r
255 thread->RegisterFCPConnected(this);
\r
256 thread->RegisterFCPMessageHandler(this);
\r
257 thread->RegisterPeriodicProcessor(this);
\r
260 void IntroductionPuzzleRequester::RemoveFromRequestList(const long identityid)
\r
262 std::vector<long>::iterator i=m_requesting.begin();
\r
263 while(i!=m_requesting.end() && (*i)!=identityid)
\r
267 if(i!=m_requesting.end())
\r
269 m_requesting.erase(i);
\r
273 void IntroductionPuzzleRequester::StartRequest(const long identityid)
\r
276 FCPMessage message;
\r
277 std::string publickey;
\r
279 std::string indexstr;
\r
280 std::string identityidstr;
\r
282 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
283 st.Bind(0,identityid);
\r
286 if(st.RowReturned())
\r
288 st.ResultText(0,publickey);
\r
292 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblIntroductionPuzzleRequests WHERE Day=? AND IdentityID=?;");
\r
293 st2.Bind(0,now.Format("%Y-%m-%d"));
\r
294 st2.Bind(1,identityid);
\r
298 if(st2.RowReturned())
\r
300 if(st2.ResultNull(0)==false)
\r
302 st2.ResultInt(0,index);
\r
308 StringFunctions::Convert(index,indexstr);
\r
309 StringFunctions::Convert(identityid,identityidstr);
\r
311 message.SetName("ClientGet");
\r
312 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";
\r
313 message["Identifier"]="IntroductionPuzzleRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
314 message["ReturnType"]="direct";
\r
315 message["MaxSize"]="1000000"; // 1 MB
\r
317 m_fcp->SendMessage(message);
\r
319 m_requesting.push_back(identityid);
\r
322 m_ids[identityid]=true;
\r