1 #include "../../include/freenet/messagelistinserter.h"
\r
2 #include "../../include/freenet/messagexml.h"
\r
3 #include "../../include/freenet/messagelistxml.h"
\r
9 MessageListInserter::MessageListInserter()
\r
14 MessageListInserter::MessageListInserter(FCPv2 *fcp):IIndexInserter<long>(fcp)
\r
19 void MessageListInserter::CheckForNeededInsert()
\r
21 // only do 1 insert at a time
\r
22 if(m_inserting.size()==0)
\r
27 bool startedinsert=false;
\r
30 previous.SetToGMTime();
\r
32 // reset the last inserted xml doc to nothing if the day has changed
\r
33 if(m_lastchecked.GetDay()!=now.GetDay())
\r
35 m_lastinsertedxml.clear();
\r
38 previous.Add(0,0,0,-m_daysbackward);
\r
40 // query for identities that have messages in the past X days and (we haven't inserted lists for in the past 30 minutes OR identity has a record in tmpMessageListInsert)
\r
41 sql="SELECT tblLocalIdentity.LocalIdentityID ";
\r
42 sql+="FROM tblLocalIdentity INNER JOIN tblMessageInserts ON tblLocalIdentity.LocalIdentityID=tblMessageInserts.LocalIdentityID ";
\r
43 sql+="WHERE tblMessageInserts.Day>=? AND ((tblLocalIdentity.LastInsertedMessageList<=? OR tblLocalIdentity.LastInsertedMessageList IS NULL OR tblLocalIdentity.LastInsertedMessageList='') OR tblLocalIdentity.LocalIdentityID IN (SELECT LocalIdentityID FROM tmpMessageListInsert)) ";
\r
44 sql+="GROUP BY tblLocalIdentity.LocalIdentityID;";
\r
46 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
47 st.Bind(0,previous.Format("%Y-%m-%d"));
\r
48 st.Bind(1,(now-(1.0/48.0)).Format("%Y-%m-%d %H:%M:%S"));
\r
51 while(st.RowReturned() && startedinsert==false)
\r
53 int localidentityid;
\r
54 st.ResultInt(0,localidentityid);
\r
55 startedinsert=StartInsert(localidentityid);
\r
62 const bool MessageListInserter::HandlePutFailed(FCPMessage &message)
\r
64 std::vector<std::string> idparts;
\r
65 long localidentityid;
\r
68 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
71 if(idparts[0]==m_fcpuniquename)
\r
73 StringFunctions::Convert(idparts[1],localidentityid);
\r
74 StringFunctions::Convert(idparts[2],index);
\r
76 if(message["Fatal"]=="true" || message["Code"]=="9")
\r
78 SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'false');");
\r
79 st.Bind(0,localidentityid);
\r
80 st.Bind(1,idparts[4]);
\r
85 RemoveFromInsertList(localidentityid);
\r
87 // reset the last inserted xml doc to nothing so we will try to insert this one again
\r
88 m_lastinsertedxml[localidentityid]="";
\r
92 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageListInserter::HandlePutFailed "+message["Identifier"]);
\r
99 const bool MessageListInserter::HandlePutSuccessful(FCPMessage &message)
\r
102 std::vector<std::string> idparts;
\r
103 long localidentityid;
\r
106 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
109 if(idparts[0]==m_fcpuniquename)
\r
111 StringFunctions::Convert(idparts[1],localidentityid);
\r
112 StringFunctions::Convert(idparts[2],index);
\r
114 SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'true');");
\r
115 st.Bind(0,localidentityid);
\r
116 st.Bind(1,idparts[4]);
\r
121 st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedMessageList=? WHERE LocalIdentityID=?;");
\r
122 st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));
\r
123 st.Bind(1,localidentityid);
\r
126 // delete any record from tmpMessageListInsert
\r
127 st=m_db->Prepare("DELETE FROM tmpMessageListInsert WHERE LocalIdentityID=?;");
\r
128 st.Bind(0,localidentityid);
\r
131 RemoveFromInsertList(localidentityid);
\r
133 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageListInserter::HandlePutSuccessful successfully inserted MessageList.");
\r
137 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageListInserter::HandlePutSuccessful inserted USK MessageList "+message["Identifier"]);
\r
143 void MessageListInserter::Initialize()
\r
145 std::string tempval;
\r
147 m_fcpuniquename="MessageListInserter";
\r
149 Option::Instance()->Get("MessageListDaysBackward",tempval);
\r
150 StringFunctions::Convert(tempval,m_daysbackward);
\r
153 const bool MessageListInserter::StartInsert(const long &localidentityid)
\r
155 FCPMessage message;
\r
158 std::string privatekey;
\r
159 std::string localidentityidstr;
\r
160 MessageListXML mlxml;
\r
161 MessageXML messxml;
\r
162 std::string xmlstr;
\r
163 std::string xmlsizestr;
\r
165 std::string indexstr;
\r
168 date.SetToGMTime();
\r
169 date.Add(0,0,0,-m_daysbackward);
\r
170 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
172 SQLite3DB::Statement st=m_db->Prepare("SELECT Day, InsertIndex, MessageXML, PrivateKey FROM tblMessageInserts INNER JOIN tblLocalIdentity ON tblMessageInserts.LocalIdentityID=tblLocalIdentity.LocalIdentityID WHERE tblLocalIdentity.LocalIdentityID=? AND Day>=?;");
\r
173 st.Bind(0,localidentityid);
\r
174 st.Bind(1,date.Format("%Y-%m-%d"));
\r
177 while(st.RowReturned())
\r
181 std::string xmlstr;
\r
182 std::vector<std::string> boards;
\r
184 st.ResultText(0,day);
\r
185 st.ResultInt(1,index);
\r
186 st.ResultText(2,xmlstr);
\r
187 st.ResultText(3,privatekey);
\r
189 messxml.ParseXML(xmlstr);
\r
191 mlxml.AddMessage(day,index,messxml.GetBoards());
\r
198 st=m_db->Prepare("SELECT MessageDate, MessageIndex, PublicKey, MessageID FROM tblMessage INNER JOIN tblIdentity ON tblMessage.IdentityID=tblIdentity.IdentityID WHERE MessageIndex IS NOT NULL ORDER BY MessageDate DESC, MessageTime DESC LIMIT 200;");
\r
199 SQLite3DB::Statement st2=m_db->Prepare("SELECT BoardName FROM tblBoard INNER JOIN tblMessageBoard ON tblBoard.BoardID=tblMessageBoard.BoardID WHERE tblMessageBoard.MessageID=?;");
\r
201 while(st.RowReturned())
\r
205 std::string publickey;
\r
206 std::vector<std::string> boardlist;
\r
209 st.ResultText(0,day);
\r
210 st.ResultInt(1,index);
\r
211 st.ResultText(2,publickey);
\r
212 st.ResultInt(3,messageid);
\r
214 st2.Bind(0,messageid);
\r
216 while(st2.RowReturned())
\r
218 std::string boardname="";
\r
219 st2.ResultText(0,boardname);
\r
220 StringFunctions::LowerCase(boardname,boardname);
\r
221 boardlist.push_back(boardname);
\r
226 mlxml.AddExternalMessage(publickey,day,index,boardlist);
\r
231 // get last inserted messagelist index for this day
\r
233 st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblMessageListInserts WHERE LocalIdentityID=? AND Day=?;");
\r
234 st.Bind(0,localidentityid);
\r
235 st.Bind(1,now.Format("%Y-%m-%d"));
\r
237 if(st.ResultNull(0)==false)
\r
239 st.ResultInt(0,index);
\r
242 StringFunctions::Convert(index,indexstr);
\r
244 xmlstr=mlxml.GetXML();
\r
246 // only insert if the last message this identity inserted is different than this message
\r
247 if(m_lastinsertedxml[localidentityid]!=xmlstr)
\r
249 std::string targeturi="";
\r
250 StringFunctions::Convert(xmlstr.size(),xmlsizestr);
\r
252 message.SetName("ClientPut");
\r
253 message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|MessageList|"+indexstr+".xml";
\r
254 message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];
\r
255 message["UploadFrom"]="direct";
\r
256 message["DataLength"]=xmlsizestr;
\r
257 m_fcp->SendMessage(message);
\r
258 m_fcp->SendRaw(xmlstr.c_str(),xmlstr.size());
\r
261 message.SetName("ClientPutComplexDir");
\r
262 message["URI"]="USK"+privatekey.substr(3)+m_messagebase+"|"+now.Format("%Y.%m.%d")+"|MessageList/0/";
\r
263 message["Identifier"]=m_fcpuniquename+"USK|"+message["URI"];
\r
264 message["DefaultName"]="MessageList.xml";
\r
265 message["Files.0.Name"]="MessageList.xml";
\r
266 message["Files.0.UploadFrom"]="direct";
\r
267 message["Files.0.DataLength"]=xmlsizestr;
\r
268 m_fcp->SendMessage(message);
\r
269 m_fcp->SendRaw(xmlstr.c_str(),xmlstr.size());
\r
271 m_inserting.push_back(localidentityid);
\r
272 m_lastinsertedxml[localidentityid]=xmlstr;
\r