1 #include "../../include/freenet/identityrequester.h"
\r
2 #include "../../include/freenet/identityxml.h"
\r
3 #include "../../include/stringfunctions.h"
\r
4 #include "../../include/option.h"
\r
5 #include "../../include/datetime.h"
\r
11 IdentityRequester::IdentityRequester()
\r
16 IdentityRequester::IdentityRequester(FCPv2 *fcp):IFCPConnected(fcp)
\r
21 void IdentityRequester::FCPConnected()
\r
23 m_requesting.clear();
\r
27 void IdentityRequester::FCPDisconnected()
\r
32 const bool IdentityRequester::HandleAllData(FCPMessage &message)
\r
35 SQLite3DB::Statement st;
\r
36 std::vector<std::string> idparts;
\r
38 std::vector<char> data;
\r
44 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
45 StringFunctions::Convert(message["DataLength"],datalength);
\r
46 StringFunctions::Convert(idparts[1],identityid);
\r
47 StringFunctions::Convert(idparts[2],index);
\r
49 // wait for all data to be received from connection
\r
50 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
55 // if we got disconnected- return immediately
\r
56 if(m_fcp->Connected()==false)
\r
62 data.resize(datalength);
\r
63 m_fcp->ReceiveRaw(&data[0],datalength);
\r
65 // parse file into xml and update the database
\r
66 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
69 st=m_db->Prepare("UPDATE tblIdentity SET Name=?, SingleUse=?, LastSeen=?, PublishTrustList=?, PublishBoardList=? WHERE IdentityID=?");
\r
70 st.Bind(0,xml.GetName());
\r
71 if(xml.GetSingleUse()==true)
\r
79 st.Bind(2,now.Format("%Y-%m-%d %H:%M:%S"));
\r
80 if(xml.GetPublishTrustList()==true)
\r
88 if(xml.GetPublishBoardList()==true)
\r
96 st.Bind(5,identityid);
\r
100 st=m_db->Prepare("INSERT INTO tblIdentityRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
101 st.Bind(0,identityid);
\r
102 st.Bind(1,idparts[4]);
\r
107 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IdentityRequester::HandleAllData parsed Identity XML file : "+message["Identifier"]);
\r
111 // bad data - mark index
\r
112 st=m_db->Prepare("INSERT INTO tblIdentityRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
113 st.Bind(0,identityid);
\r
114 st.Bind(1,idparts[4]);
\r
119 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IdentityRequester::HandleAllData error parsing Identity XML file : "+message["Identifier"]);
\r
122 // remove this identityid from request list
\r
123 RemoveFromRequestList(identityid);
\r
129 const bool IdentityRequester::HandleGetFailed(FCPMessage &message)
\r
132 SQLite3DB::Statement st;
\r
133 std::vector<std::string> idparts;
\r
138 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
139 StringFunctions::Convert(idparts[1],identityid);
\r
140 StringFunctions::Convert(idparts[2],index);
\r
142 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
143 if(message["Fatal"]=="true")
\r
145 st=m_db->Prepare("INSERT INTO tblIdentityRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
146 st.Bind(0,identityid);
\r
147 st.Bind(1,idparts[4]);
\r
152 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IdentityRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
155 // remove this identityid from request list
\r
156 RemoveFromRequestList(identityid);
\r
162 const bool IdentityRequester::HandleMessage(FCPMessage &message)
\r
165 if(message["Identifier"].find("IdentityRequester")==0)
\r
167 if(message.GetName()=="DataFound")
\r
172 if(message.GetName()=="AllData")
\r
174 return HandleAllData(message);
\r
177 if(message.GetName()=="GetFailed")
\r
179 return HandleGetFailed(message);
\r
182 if(message.GetName()=="IdentifierCollision")
\r
184 // remove one of the ids from the requesting list
\r
186 std::vector<std::string> idparts;
\r
187 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
188 StringFunctions::Convert(idparts[1],identityid);
\r
189 RemoveFromRequestList(identityid);
\r
197 void IdentityRequester::Initialize()
\r
199 std::string tempval="";
\r
200 Option::Instance()->Get("MaxIdentityRequests",tempval);
\r
201 StringFunctions::Convert(tempval,m_maxrequests);
\r
202 if(m_maxrequests<1)
\r
205 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxIdentityRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
207 if(m_maxrequests>100)
\r
209 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxIdentityRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
211 Option::Instance()->Get("MessageBase",m_messagebase);
\r
212 m_tempdate.SetToGMTime();
\r
215 void IdentityRequester::PopulateIDList()
\r
220 date.SetToGMTime();
\r
223 // select identities we want to query (haven't seen in last hour) - sort by their trust level (descending) with secondary sort on how long ago we saw them (ascending)
\r
224 SQLite3DB::Statement st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND (LastSeen IS NULL OR LastSeen<='"+date.Format("%Y-%m-%d %H:%M:%S")+"') ORDER BY LocalMessageTrust+LocalTrustListTrust DESC, LastSeen;");
\r
229 while(st.RowReturned())
\r
231 st.ResultInt(0,id);
\r
237 void IdentityRequester::Process()
\r
239 // max is the smaller of the config value or the total number of identities we will request from
\r
240 long max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;
\r
242 // try to keep up to max requests going
\r
243 if(m_requesting.size()<max)
\r
245 std::map<long,bool>::iterator i=m_ids.begin();
\r
246 while(i!=m_ids.end() && (*i).second==true)
\r
253 StartRequest((*i).first);
\r
257 // we requested from all ids in the list, repopulate the list
\r
261 // 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
262 // this will recheck for ids every minute
\r
265 if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))
\r
273 void IdentityRequester::RemoveFromRequestList(const long identityid)
\r
275 std::vector<long>::iterator i=m_requesting.begin();
\r
276 while(i!=m_requesting.end() && (*i)!=identityid)
\r
280 if(i!=m_requesting.end())
\r
282 m_requesting.erase(i);
\r
286 void IdentityRequester::RegisterWithThread(FreenetMasterThread *thread)
\r
288 thread->RegisterFCPConnected(this);
\r
289 thread->RegisterFCPMessageHandler(this);
\r
290 thread->RegisterPeriodicProcessor(this);
\r
293 void IdentityRequester::StartRequest(const long identityid)
\r
296 FCPMessage message;
\r
297 std::string publickey;
\r
299 std::string indexstr;
\r
300 std::string identityidstr;
\r
302 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
303 st.Bind(0,identityid);
\r
306 if(st.RowReturned())
\r
308 st.ResultText(0,publickey);
\r
312 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblIdentityRequests WHERE Day=? AND IdentityID=?;");
\r
313 st2.Bind(0,now.Format("%Y-%m-%d"));
\r
314 st2.Bind(1,identityid);
\r
318 if(st2.RowReturned())
\r
320 if(st2.ResultNull(0)==false)
\r
322 st2.ResultInt(0,index);
\r
328 StringFunctions::Convert(index,indexstr);
\r
329 StringFunctions::Convert(identityid,identityidstr);
\r
331 message.SetName("ClientGet");
\r
332 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|Identity|"+indexstr+".xml";
\r
333 message["Identifier"]="IdentityRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
334 message["ReturnType"]="direct";
\r
335 message["MaxSize"]="10000"; // 10 KB
\r
337 m_fcp->SendMessage(message);
\r
339 m_requesting.push_back(identityid);
\r
343 m_ids[identityid]=true;
\r