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 previous.Add(0,0,0,-m_daysbackward);
\r
34 // 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
35 sql="SELECT tblLocalIdentity.LocalIdentityID ";
\r
36 sql+="FROM tblLocalIdentity INNER JOIN tblMessageInserts ON tblLocalIdentity.LocalIdentityID=tblMessageInserts.LocalIdentityID ";
\r
37 sql+="WHERE tblMessageInserts.Day>=? AND ((tblLocalIdentity.LastInsertedMessageList<=? OR tblLocalIdentity.LastInsertedMessageList IS NULL OR tblLocalIdentity.LastInsertedMessageList='') OR tblLocalIdentity.LocalIdentityID IN (SELECT LocalIdentityID FROM tmpMessageListInsert)) ";
\r
40 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
41 st.Bind(0,previous.Format("%Y-%m-%d"));
\r
42 st.Bind(1,(now-(1.0/48.0)).Format("%Y-%m-%d %H:%M:%S"));
\r
45 while(st.RowReturned() && startedinsert==false)
\r
47 int localidentityid;
\r
48 st.ResultInt(0,localidentityid);
\r
49 startedinsert=StartInsert(localidentityid);
\r
56 const bool MessageListInserter::HandlePutFailed(FCPMessage &message)
\r
58 std::vector<std::string> idparts;
\r
59 long localidentityid;
\r
62 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
63 StringFunctions::Convert(idparts[1],localidentityid);
\r
64 StringFunctions::Convert(idparts[2],index);
\r
66 if(message["Fatal"]=="true" || message["Code"]=="9")
\r
68 SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'false');");
\r
69 st.Bind(0,localidentityid);
\r
70 st.Bind(1,idparts[4]);
\r
75 RemoveFromInsertList(localidentityid);
\r
81 const bool MessageListInserter::HandlePutSuccessful(FCPMessage &message)
\r
84 std::vector<std::string> idparts;
\r
85 long localidentityid;
\r
88 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
89 StringFunctions::Convert(idparts[1],localidentityid);
\r
90 StringFunctions::Convert(idparts[2],index);
\r
92 SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'true');");
\r
93 st.Bind(0,localidentityid);
\r
94 st.Bind(1,idparts[4]);
\r
99 st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedMessageList=? WHERE LocalIdentityID=?;");
\r
100 st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));
\r
101 st.Bind(1,localidentityid);
\r
104 // delete any record from tmpMessageListInsert
\r
105 st=m_db->Prepare("DELETE FROM tmpMessageListInsert WHERE LocalIdentityID=?;");
\r
106 st.Bind(0,localidentityid);
\r
109 RemoveFromInsertList(localidentityid);
\r
111 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageListInserter::HandlePutSuccessful successfully inserted MessageList.");
\r
116 void MessageListInserter::Initialize()
\r
118 std::string tempval;
\r
120 m_fcpuniquename="MessageListInserter";
\r
122 Option::Instance()->Get("MessageListDaysBackward",tempval);
\r
123 StringFunctions::Convert(tempval,m_daysbackward);
\r
126 const bool MessageListInserter::StartInsert(const long &localidentityid)
\r
128 FCPMessage message;
\r
131 std::string privatekey;
\r
132 std::string localidentityidstr;
\r
133 MessageListXML mlxml;
\r
134 MessageXML messxml;
\r
135 std::string xmlstr;
\r
136 std::string xmlsizestr;
\r
138 std::string indexstr;
\r
141 date.SetToGMTime();
\r
142 date.Add(0,0,0,-m_daysbackward);
\r
143 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
145 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
146 st.Bind(0,localidentityid);
\r
147 st.Bind(1,date.Format("%Y-%m-%d"));
\r
150 while(st.RowReturned())
\r
154 std::string xmlstr;
\r
155 std::vector<std::string> boards;
\r
157 st.ResultText(0,day);
\r
158 st.ResultInt(1,index);
\r
159 st.ResultText(2,xmlstr);
\r
160 st.ResultText(3,privatekey);
\r
162 messxml.ParseXML(xmlstr);
\r
164 mlxml.AddMessage(day,index,messxml.GetBoards());
\r
171 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 100;");
\r
172 SQLite3DB::Statement st2=m_db->Prepare("SELECT BoardName FROM tblBoard INNER JOIN tblMessageBoard ON tblBoard.BoardID=tblMessageBoard.BoardID WHERE tblMessageBoard.MessageID=?;");
\r
174 while(st.RowReturned())
\r
178 std::string publickey;
\r
179 std::vector<std::string> boardlist;
\r
182 st.ResultText(0,day);
\r
183 st.ResultInt(1,index);
\r
184 st.ResultText(2,publickey);
\r
185 st.ResultInt(3,messageid);
\r
187 st2.Bind(0,messageid);
\r
189 while(st2.RowReturned())
\r
191 std::string boardname="";
\r
192 st2.ResultText(0,boardname);
\r
193 StringFunctions::LowerCase(boardname,boardname);
\r
194 boardlist.push_back(boardname);
\r
199 mlxml.AddExternalMessage(publickey,day,index,boardlist);
\r
204 // get last inserted messagelist index for this day
\r
206 st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblMessageListInserts WHERE LocalIdentityID=? AND Day=?;");
\r
207 st.Bind(0,localidentityid);
\r
208 st.Bind(1,now.Format("%Y-%m-%d"));
\r
210 if(st.ResultNull(0)==false)
\r
212 st.ResultInt(0,index);
\r
215 StringFunctions::Convert(index,indexstr);
\r
217 // actually insert message
\r
218 xmlstr=mlxml.GetXML();
\r
220 // only insert if the last message this identity inserted is different than this message
\r
221 if(m_lastinsertedxml[localidentityid]!=xmlstr)
\r
223 StringFunctions::Convert(xmlstr.size(),xmlsizestr);
\r
225 message.SetName("ClientPut");
\r
226 message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|MessageList|"+indexstr+".xml";
\r
227 message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];
\r
228 message["UploadFrom"]="direct";
\r
229 message["DataLength"]=xmlsizestr;
\r
230 m_fcp->SendMessage(message);
\r
231 m_fcp->SendRaw(xmlstr.c_str(),xmlstr.size());
\r
233 m_inserting.push_back(localidentityid);
\r
235 m_lastinsertedxml[localidentityid]=xmlstr;
\r