1 #include "../../include/freenet/trustlistrequester.h"
\r
2 #include "../../include/option.h"
\r
3 #include "../../include/stringfunctions.h"
\r
4 #include "../../include/freenet/trustlistxml.h"
\r
10 TrustListRequester::TrustListRequester()
\r
15 TrustListRequester::TrustListRequester(FCPv2 *fcp):IFCPConnected(fcp)
\r
20 void TrustListRequester::FCPConnected()
\r
22 m_requesting.clear();
\r
26 void TrustListRequester::FCPDisconnected()
\r
31 const bool TrustListRequester::HandleAllData(FCPMessage &message)
\r
34 SQLite3DB::Statement st;
\r
35 std::vector<std::string> idparts;
\r
37 std::vector<char> data;
\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("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");
\r
68 // loop through all trust entries in xml and add to database if we don't already know them
\r
69 for(long i=0; i<xml.TrustCount(); i++)
\r
71 std::string identity;
\r
72 identity=xml.GetIdentity(i);
\r
74 //TODO get the trust levels as well
\r
76 st.Bind(0,identity);
\r
78 if(st.RowReturned()==false)
\r
80 m_db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('"+identity+"','"+now.Format("%Y-%m-%d %H:%M:%S")+"');");
\r
85 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
86 st.Bind(0,identityid);
\r
87 st.Bind(1,idparts[4]);
\r
92 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" parsed Identity XML file : "+message["Identifier"]);
\r
96 // bad data - mark index
\r
97 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
98 st.Bind(0,identityid);
\r
99 st.Bind(1,idparts[4]);
\r
104 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,__FUNCTION__" error parsing TrustList XML file : "+message["Identifier"]);
\r
107 // remove this identityid from request list
\r
108 RemoveFromRequestList(identityid);
\r
114 const bool TrustListRequester::HandleGetFailed(FCPMessage &message)
\r
117 SQLite3DB::Statement st;
\r
118 std::vector<std::string> idparts;
\r
123 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
124 StringFunctions::Convert(idparts[1],identityid);
\r
125 StringFunctions::Convert(idparts[2],index);
\r
127 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
128 if(message["Fatal"]=="true")
\r
130 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
131 st.Bind(0,identityid);
\r
132 st.Bind(1,idparts[4]);
\r
137 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,__FUNCTION__" fatal error requesting "+message["Identifier"]);
\r
140 // remove this identityid from request list
\r
141 RemoveFromRequestList(identityid);
\r
147 const bool TrustListRequester::HandleMessage(FCPMessage &message)
\r
150 if(message["Identifier"].find("TrustListRequester")==0)
\r
152 if(message.GetName()=="DataFound")
\r
157 if(message.GetName()=="AllData")
\r
159 return HandleAllData(message);
\r
162 if(message.GetName()=="GetFailed")
\r
164 return HandleGetFailed(message);
\r
167 if(message.GetName()=="IdentifierCollision")
\r
169 // remove one of the ids from the requesting list
\r
171 std::vector<std::string> idparts;
\r
172 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
173 StringFunctions::Convert(idparts[1],identityid);
\r
174 RemoveFromRequestList(identityid);
\r
182 void TrustListRequester::Initialize()
\r
184 std::string tempval="";
\r
185 Option::instance()->Get("MaxIdentityRequests",tempval);
\r
186 StringFunctions::Convert(tempval,m_maxrequests);
\r
187 if(m_maxrequests<1)
\r
190 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxTrustListRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
192 if(m_maxrequests>100)
\r
194 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxTrustListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
196 Option::instance()->Get("MessageBase",m_messagebase);
\r
197 m_tempdate.SetToGMTime();
\r
200 void TrustListRequester::PopulateIDList()
\r
205 date.SetToGMTime();
\r
207 // select identities we want to query (we've seen them today and they are publishing trust list) - sort by their trust level (descending) with secondary sort on how long ago we saw them (ascending)
\r
208 SQLite3DB::Statement st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+date.Format("%Y-%m-%d")+"' AND PublishTrustList='true' AND LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust') ORDER BY LocalMessageTrust+LocalTrustListTrust DESC, LastSeen;");
\r
213 while(st.RowReturned())
\r
215 st.ResultInt(0,id);
\r
221 void TrustListRequester::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_tempdate<(now-(1.0/1440.0)))
\r
257 void TrustListRequester::RegisterWithThread(FreenetMasterThread *thread)
\r
259 thread->RegisterFCPConnected(this);
\r
260 thread->RegisterFCPMessageHandler(this);
\r
261 thread->RegisterPeriodicProcessor(this);
\r
264 void TrustListRequester::RemoveFromRequestList(const long identityid)
\r
266 std::vector<long>::iterator i=m_requesting.begin();
\r
267 while(i!=m_requesting.end() && (*i)!=identityid)
\r
271 if(i!=m_requesting.end())
\r
273 m_requesting.erase(i);
\r
277 void TrustListRequester::StartRequest(const long identityid)
\r
280 FCPMessage message;
\r
281 std::string publickey;
\r
283 std::string indexstr;
\r
284 std::string identityidstr;
\r
286 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
287 st.Bind(0,identityid);
\r
290 if(st.RowReturned())
\r
292 st.ResultText(0,publickey);
\r
296 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");
\r
297 st2.Bind(0,now.Format("%Y-%m-%d"));
\r
298 st2.Bind(1,identityid);
\r
302 if(st2.RowReturned())
\r
304 if(st2.ResultNull(0)==false)
\r
306 st2.ResultInt(0,index);
\r
312 StringFunctions::Convert(index,indexstr);
\r
313 StringFunctions::Convert(identityid,identityidstr);
\r
315 message.SetName("ClientGet");
\r
316 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
317 message["Identifier"]="TrustListRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
318 message["ReturnType"]="direct";
\r
319 message["MaxSize"]="1000000"; // 1 MB
\r
321 m_fcp->SendMessage(message);
\r
323 m_requesting.push_back(identityid);
\r
327 m_ids[identityid]=true;
\r