version 0.1.10
[fms.git] / src / freenet / introductionpuzzlerequester.cpp
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
5 \r
6 #ifdef XMEM\r
7         #include <xmem.h>\r
8 #endif\r
9 \r
10 IntroductionPuzzleRequester::IntroductionPuzzleRequester()\r
11 {\r
12         Initialize();\r
13 }\r
14 \r
15 IntroductionPuzzleRequester::IntroductionPuzzleRequester(FCPv2 *fcp):IFCPConnected(fcp)\r
16 {\r
17         Initialize();\r
18 }\r
19 \r
20 void IntroductionPuzzleRequester::FCPConnected()\r
21 {\r
22         m_requesting.clear();\r
23         PopulateIDList();\r
24 }\r
25 \r
26 void IntroductionPuzzleRequester::FCPDisconnected()\r
27 {\r
28         \r
29 }\r
30 \r
31 const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)\r
32 {\r
33         DateTime now;\r
34         SQLite3DB::Statement st;\r
35         std::vector<std::string> idparts;\r
36         long datalength;\r
37         std::vector<char> data;\r
38         IntroductionPuzzleXML xml;\r
39         long identityid;\r
40         long index;\r
41 \r
42         now.SetToGMTime();\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
47 \r
48         // wait for all data to be received from connection\r
49         while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)\r
50         {\r
51                 m_fcp->Update(1);\r
52         }\r
53 \r
54         // if we got disconnected- return immediately\r
55         if(m_fcp->Connected()==false)\r
56         {\r
57                 return false;\r
58         }\r
59 \r
60         // receive the file\r
61         data.resize(datalength);\r
62         m_fcp->ReceiveRaw(&data[0],datalength);\r
63 \r
64         // parse file into xml and update the database\r
65         if(xml.ParseXML(std::string(data.begin(),data.end()))==true)\r
66         {\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
70                 st.Bind(2,index);\r
71                 st.Bind(3,"true");\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
76                 st.Step();\r
77                 st.Finalize();\r
78 \r
79                 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleRequester::HandleAllData parsed IntroductionPuzzle XML file : "+message["Identifier"]);\r
80         }\r
81         else\r
82         {\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
87                 st.Bind(2,index);\r
88                 st.Step();\r
89                 st.Finalize();\r
90 \r
91                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData error parsing IntroductionPuzzle XML file : "+message["Identifier"]);\r
92         }\r
93 \r
94         // remove this identityid from request list\r
95         RemoveFromRequestList(identityid);\r
96 \r
97         return true;\r
98 \r
99 }\r
100 \r
101 const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)\r
102 {\r
103         DateTime now;\r
104         SQLite3DB::Statement st;\r
105         std::vector<std::string> idparts;\r
106         long identityid;\r
107         long index;\r
108 \r
109         now.SetToGMTime();\r
110         StringFunctions::Split(message["Identifier"],"|",idparts);\r
111         StringFunctions::Convert(idparts[1],identityid);\r
112         StringFunctions::Convert(idparts[2],index);     \r
113 \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
116         {\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
120                 st.Bind(2,index);\r
121                 st.Step();\r
122                 st.Finalize();\r
123 \r
124                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
125         }\r
126 \r
127         // remove this identityid from request list\r
128         RemoveFromRequestList(identityid);\r
129 \r
130         return true;\r
131 \r
132 }\r
133 \r
134 const bool IntroductionPuzzleRequester::HandleMessage(FCPMessage &message)\r
135 {\r
136 \r
137         if(message["Identifier"].find("IntroductionPuzzleRequester")==0)\r
138         {\r
139                 if(message.GetName()=="DataFound")\r
140                 {\r
141                         return true;\r
142                 }\r
143 \r
144                 if(message.GetName()=="AllData")\r
145                 {\r
146                         return HandleAllData(message);\r
147                 }\r
148 \r
149                 if(message.GetName()=="GetFailed")\r
150                 {\r
151                         return HandleGetFailed(message);\r
152                 }\r
153                 \r
154                 if(message.GetName()=="IdentifierCollision")\r
155                 {\r
156                         // remove one of the ids from the requesting list\r
157                         long identityid=0;\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
162                         return true;\r
163                 }\r
164         }\r
165 \r
166         return false;\r
167 }\r
168 \r
169 void IntroductionPuzzleRequester::Initialize()\r
170 {\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
175         {\r
176                 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
178         }\r
179         if(m_maxrequests>100)\r
180         {\r
181                 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+".  This value might be incorrectly configured.");\r
182         }\r
183         Option::Instance()->Get("MessageBase",m_messagebase);\r
184         m_tempdate.SetToGMTime();\r
185 }\r
186 \r
187 void IntroductionPuzzleRequester::PopulateIDList()\r
188 {\r
189         DateTime now;\r
190         int id;\r
191         std::string limitnum="30";\r
192 \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
195         st.Step();\r
196         if(!st.RowReturned())\r
197         {\r
198                 limitnum="5";\r
199         }\r
200         st.Finalize();\r
201 \r
202         now.SetToGMTime();\r
203 \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 DESC LIMIT 0,"+limitnum+";");\r
206         st.Step();\r
207 \r
208         m_ids.clear();\r
209 \r
210         while(st.RowReturned())\r
211         {\r
212                 st.ResultInt(0,id);\r
213                 m_ids[id]=false;\r
214                 st.Step();\r
215         }\r
216 }\r
217 \r
218 void IntroductionPuzzleRequester::Process()\r
219 {\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
222 \r
223         // try to keep up to max requests going\r
224         if(m_requesting.size()<max)\r
225         {\r
226                 std::map<long,bool>::iterator i=m_ids.begin();\r
227                 while(i!=m_ids.end() && (*i).second==true)\r
228                 {\r
229                         i++;\r
230                 }\r
231 \r
232                 if(i!=m_ids.end())\r
233                 {\r
234                         StartRequest((*i).first);\r
235                 }\r
236                 else\r
237                 {\r
238                         // we requested from all ids in the list, repopulate the list\r
239                         PopulateIDList();\r
240                 }\r
241         }\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
244         DateTime now;\r
245         now.SetToGMTime();\r
246         if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))\r
247         {\r
248                 PopulateIDList();\r
249                 m_tempdate=now;\r
250         }\r
251 }\r
252 \r
253 void IntroductionPuzzleRequester::RegisterWithThread(FreenetMasterThread *thread)\r
254 {\r
255         thread->RegisterFCPConnected(this);\r
256         thread->RegisterFCPMessageHandler(this);\r
257         thread->RegisterPeriodicProcessor(this);\r
258 }\r
259 \r
260 void IntroductionPuzzleRequester::RemoveFromRequestList(const long identityid)\r
261 {\r
262         std::vector<long>::iterator i=m_requesting.begin();\r
263         while(i!=m_requesting.end() && (*i)!=identityid)\r
264         {\r
265                 i++;\r
266         }\r
267         if(i!=m_requesting.end())\r
268         {\r
269                 m_requesting.erase(i);\r
270         }\r
271 }\r
272 \r
273 void IntroductionPuzzleRequester::StartRequest(const long identityid)\r
274 {\r
275         DateTime now;\r
276         FCPMessage message;\r
277         std::string publickey;\r
278         int index;\r
279         std::string indexstr;\r
280         std::string identityidstr;\r
281 \r
282         SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");\r
283         st.Bind(0,identityid);\r
284         st.Step();\r
285 \r
286         if(st.RowReturned())\r
287         {\r
288                 st.ResultText(0,publickey);\r
289 \r
290                 now.SetToGMTime();\r
291 \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
295                 st2.Step();\r
296 \r
297                 index=0;\r
298                 if(st2.RowReturned())\r
299                 {\r
300                         if(st2.ResultNull(0)==false)\r
301                         {\r
302                                 st2.ResultInt(0,index);\r
303                                 index++;\r
304                         }\r
305                 }\r
306                 st2.Finalize();\r
307 \r
308                 StringFunctions::Convert(index,indexstr);\r
309                 StringFunctions::Convert(identityid,identityidstr);\r
310 \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
316 \r
317                 m_fcp->SendMessage(message);\r
318                 \r
319                 m_requesting.push_back(identityid);\r
320         }\r
321 \r
322         m_ids[identityid]=true;\r
323 }\r