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
6 #include <Poco/DateTime.h>
\r
7 #include <Poco/Timestamp.h>
\r
8 #include <Poco/Timespan.h>
\r
9 #include <Poco/DateTimeFormatter.h>
\r
15 IdentityRequester::IdentityRequester()
\r
20 IdentityRequester::IdentityRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)
\r
25 const bool IdentityRequester::HandleAllData(FCPMessage &message)
\r
28 SQLite3DB::Statement st;
\r
29 std::vector<std::string> idparts;
\r
31 std::vector<char> data;
\r
35 std::string name="";
\r
37 now=Poco::Timestamp();
\r
38 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
39 StringFunctions::Convert(message["DataLength"],datalength);
\r
40 StringFunctions::Convert(idparts[1],identityid);
\r
41 StringFunctions::Convert(idparts[2],index);
\r
43 // wait for all data to be received from connection
\r
44 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
49 // if we got disconnected- return immediately
\r
50 if(m_fcp->Connected()==false)
\r
56 data.resize(datalength);
\r
59 m_fcp->ReceiveRaw(&data[0],datalength);
\r
62 // parse file into xml and update the database
\r
63 if(data.size()>0 && xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
66 st=m_db->Prepare("UPDATE tblIdentity SET Name=?, SingleUse=?, LastSeen=?, PublishTrustList=?, PublishBoardList=?, FreesiteEdition=? WHERE IdentityID=?");
\r
73 if(xml.GetSingleUse()==true)
\r
81 st.Bind(2,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));
\r
82 if(xml.GetPublishTrustList()==true)
\r
90 if(xml.GetPublishBoardList()==true)
\r
98 if(xml.GetFreesiteEdition()>=0)
\r
100 st.Bind(5,xml.GetFreesiteEdition());
\r
106 st.Bind(6,identityid);
\r
110 st=m_db->Prepare("INSERT INTO tblIdentityRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
111 st.Bind(0,identityid);
\r
112 st.Bind(1,idparts[4]);
\r
117 m_log->debug(m_fcpuniquename+"::HandleAllData parsed Identity XML file : "+message["Identifier"]);
\r
121 // bad data - mark index
\r
122 st=m_db->Prepare("INSERT INTO tblIdentityRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
123 st.Bind(0,identityid);
\r
124 st.Bind(1,idparts[4]);
\r
129 m_log->error(m_fcpuniquename+"::HandleAllData error parsing Identity XML file : "+message["Identifier"]);
\r
132 // remove this identityid from request list
\r
133 RemoveFromRequestList(identityid);
\r
139 const bool IdentityRequester::HandleGetFailed(FCPMessage &message)
\r
141 SQLite3DB::Statement st;
\r
142 std::vector<std::string> idparts;
\r
146 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
147 StringFunctions::Convert(idparts[1],identityid);
\r
148 StringFunctions::Convert(idparts[2],index);
\r
150 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
151 if(message["Fatal"]=="true")
\r
153 st=m_db->Prepare("INSERT INTO tblIdentityRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
154 st.Bind(0,identityid);
\r
155 st.Bind(1,idparts[4]);
\r
160 m_log->error(m_fcpuniquename+"::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
163 // remove this identityid from request list
\r
164 RemoveFromRequestList(identityid);
\r
170 void IdentityRequester::Initialize()
\r
172 m_fcpuniquename="KnownIdentityRequester";
\r
173 Option::Instance()->GetInt("MaxIdentityRequests",m_maxrequests);
\r
175 // known identities get 4/5 + any remaining if not evenly divisible - unknown identities get 1/5 of the max requests option
\r
176 m_maxrequests=((m_maxrequests*4)/5)+(m_maxrequests%5);
\r
178 if(m_maxrequests<1)
\r
181 m_log->error("Option MaxIdentityRequests is currently set at less than 1. It must be 1 or greater.");
\r
183 if(m_maxrequests>100)
\r
185 m_log->warning("Option MaxIdentityRequests is currently set at more than 100. This value might be incorrectly configured.");
\r
189 void IdentityRequester::PopulateIDList()
\r
191 Poco::DateTime date;
\r
194 date.assign(date.year(),date.month(),date.day(),0,0,0);
\r
196 // select identities we want to query (haven't seen yet today) - sort by their trust level (descending) with secondary sort on how long ago we saw them (ascending)
\r
197 SQLite3DB::Statement st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen IS NOT NULL AND LastSeen<'"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d %H:%M:%S")+"' ORDER BY LocalMessageTrust+LocalTrustListTrust DESC, LastSeen;");
\r
202 while(st.RowReturned())
\r
204 st.ResultInt(0,id);
\r
210 void IdentityRequester::StartRequest(const long &identityid)
\r
212 Poco::DateTime now;
\r
213 FCPMessage message;
\r
214 std::string publickey;
\r
216 std::string indexstr;
\r
217 std::string identityidstr;
\r
219 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
220 st.Bind(0,identityid);
\r
223 if(st.RowReturned())
\r
225 st.ResultText(0,publickey);
\r
227 now=Poco::Timestamp();
\r
229 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblIdentityRequests WHERE Day=? AND IdentityID=?;");
\r
230 st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));
\r
231 st2.Bind(1,identityid);
\r
235 if(st2.RowReturned())
\r
237 if(st2.ResultNull(0)==false)
\r
239 st2.ResultInt(0,index);
\r
245 StringFunctions::Convert(index,indexstr);
\r
246 StringFunctions::Convert(identityid,identityidstr);
\r
248 message.SetName("ClientGet");
\r
249 message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|Identity|"+indexstr+".xml";
\r
250 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
251 message["ReturnType"]="direct";
\r
252 message["MaxSize"]="10000"; // 10 KB
\r
254 m_fcp->SendMessage(message);
\r
256 m_requesting.push_back(identityid);
\r
260 m_ids[identityid]=true;
\r