X-Git-Url: https://git.pterodactylus.net/?p=fms.git;a=blobdiff_plain;f=src%2Ffreenet%2Fmessagelistinserter.cpp;h=eb102a13bfe7e78cdf0d36dac8c348de0b442368;hp=b608145a7a597d232d4f487ab4b7c1d77b8c6247;hb=853f67b0b7b8121d572cff34d40f7b28cac8f65e;hpb=c7fcb4c4bc5012a584add81a9509fc1f84c3c688 diff --git a/src/freenet/messagelistinserter.cpp b/src/freenet/messagelistinserter.cpp index b608145..eb102a1 100644 --- a/src/freenet/messagelistinserter.cpp +++ b/src/freenet/messagelistinserter.cpp @@ -24,28 +24,36 @@ void MessageListInserter::CheckForNeededInsert() std::string sql; DateTime now; DateTime previous; + bool startedinsert=false; now.SetToGMTime(); previous.SetToGMTime(); + // reset the last inserted xml doc to nothing if the day has changed + if(m_lastchecked.GetDay()!=now.GetDay()) + { + m_lastinsertedxml.clear(); + } + previous.Add(0,0,0,-m_daysbackward); // 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) sql="SELECT tblLocalIdentity.LocalIdentityID "; sql+="FROM tblLocalIdentity INNER JOIN tblMessageInserts ON tblLocalIdentity.LocalIdentityID=tblMessageInserts.LocalIdentityID "; sql+="WHERE tblMessageInserts.Day>=? AND ((tblLocalIdentity.LastInsertedMessageList<=? OR tblLocalIdentity.LastInsertedMessageList IS NULL OR tblLocalIdentity.LastInsertedMessageList='') OR tblLocalIdentity.LocalIdentityID IN (SELECT LocalIdentityID FROM tmpMessageListInsert)) "; - sql+=";"; + sql+="GROUP BY tblLocalIdentity.LocalIdentityID;"; SQLite3DB::Statement st=m_db->Prepare(sql); st.Bind(0,previous.Format("%Y-%m-%d")); st.Bind(1,(now-(1.0/48.0)).Format("%Y-%m-%d %H:%M:%S")); st.Step(); - if(st.RowReturned()) + while(st.RowReturned() && startedinsert==false) { int localidentityid; st.ResultInt(0,localidentityid); - StartInsert(localidentityid); + startedinsert=StartInsert(localidentityid); + st.Step(); } } @@ -58,19 +66,31 @@ const bool MessageListInserter::HandlePutFailed(FCPMessage &message) long index; StringFunctions::Split(message["Identifier"],"|",idparts); - StringFunctions::Convert(idparts[1],localidentityid); - StringFunctions::Convert(idparts[2],index); - if(message["Fatal"]=="true" || message["Code"]=="9") + // non USK + if(idparts[0]==m_fcpuniquename) { - SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'false');"); - st.Bind(0,localidentityid); - st.Bind(1,idparts[4]); - st.Bind(2,index); - st.Step(); - } + StringFunctions::Convert(idparts[1],localidentityid); + StringFunctions::Convert(idparts[2],index); + + if(message["Fatal"]=="true" || message["Code"]=="9") + { + SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'false');"); + st.Bind(0,localidentityid); + st.Bind(1,idparts[4]); + st.Bind(2,index); + st.Step(); + } - RemoveFromInsertList(localidentityid); + RemoveFromInsertList(localidentityid); + + // reset the last inserted xml doc to nothing so we will try to insert this one again + m_lastinsertedxml[localidentityid]=""; + } + else + { + m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageListInserter::HandlePutFailed "+message["Identifier"]); + } return true; @@ -84,29 +104,38 @@ const bool MessageListInserter::HandlePutSuccessful(FCPMessage &message) long index; StringFunctions::Split(message["Identifier"],"|",idparts); - StringFunctions::Convert(idparts[1],localidentityid); - StringFunctions::Convert(idparts[2],index); - SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'true');"); - st.Bind(0,localidentityid); - st.Bind(1,idparts[4]); - st.Bind(2,index); - st.Step(); + // non USK + if(idparts[0]==m_fcpuniquename) + { + StringFunctions::Convert(idparts[1],localidentityid); + StringFunctions::Convert(idparts[2],index); - now.SetToGMTime(); - st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedMessageList=? WHERE LocalIdentityID=?;"); - st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S")); - st.Bind(1,localidentityid); - st.Step(); + SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'true');"); + st.Bind(0,localidentityid); + st.Bind(1,idparts[4]); + st.Bind(2,index); + st.Step(); - // delete any record from tmpMessageListInsert - st=m_db->Prepare("DELETE FROM tmpMessageListInsert WHERE LocalIdentityID=?;"); - st.Bind(0,localidentityid); - st.Step(); + now.SetToGMTime(); + st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedMessageList=? WHERE LocalIdentityID=?;"); + st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S")); + st.Bind(1,localidentityid); + st.Step(); - RemoveFromInsertList(localidentityid); + // delete any record from tmpMessageListInsert + st=m_db->Prepare("DELETE FROM tmpMessageListInsert WHERE LocalIdentityID=?;"); + st.Bind(0,localidentityid); + st.Step(); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageListInserter::HandlePutSuccessful successfully inserted MessageList."); + RemoveFromInsertList(localidentityid); + + m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageListInserter::HandlePutSuccessful successfully inserted MessageList."); + } + else + { + m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageListInserter::HandlePutSuccessful inserted USK MessageList "+message["Identifier"]); + } return true; } @@ -121,7 +150,7 @@ void MessageListInserter::Initialize() StringFunctions::Convert(tempval,m_daysbackward); } -void MessageListInserter::StartInsert(const long &localidentityid) +const bool MessageListInserter::StartInsert(const long &localidentityid) { FCPMessage message; DateTime date; @@ -165,6 +194,40 @@ void MessageListInserter::StartInsert(const long &localidentityid) } st.Finalize(); + + 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;"); + SQLite3DB::Statement st2=m_db->Prepare("SELECT BoardName FROM tblBoard INNER JOIN tblMessageBoard ON tblBoard.BoardID=tblMessageBoard.BoardID WHERE tblMessageBoard.MessageID=?;"); + st.Step(); + while(st.RowReturned()) + { + std::string day; + int index; + std::string publickey; + std::vector boardlist; + int messageid; + + st.ResultText(0,day); + st.ResultInt(1,index); + st.ResultText(2,publickey); + st.ResultInt(3,messageid); + + st2.Bind(0,messageid); + st2.Step(); + while(st2.RowReturned()) + { + std::string boardname=""; + st2.ResultText(0,boardname); + StringFunctions::LowerCase(boardname,boardname); + boardlist.push_back(boardname); + st2.Step(); + } + st2.Reset(); + + mlxml.AddExternalMessage(publickey,day,index,boardlist); + + st.Step(); + } + // get last inserted messagelist index for this day index=0; st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblMessageListInserts WHERE LocalIdentityID=? AND Day=?;"); @@ -178,18 +241,41 @@ void MessageListInserter::StartInsert(const long &localidentityid) } StringFunctions::Convert(index,indexstr); - // actually insert message xmlstr=mlxml.GetXML(); - StringFunctions::Convert(xmlstr.size(),xmlsizestr); - - message.SetName("ClientPut"); - message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|MessageList|"+indexstr+".xml"; - message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+indexstr+"|"+message["URI"]; - message["UploadFrom"]="direct"; - message["DataLength"]=xmlsizestr; - m_fcp->SendMessage(message); - m_fcp->SendRaw(xmlstr.c_str(),xmlstr.size()); - m_inserting.push_back(localidentityid); + // only insert if the last message this identity inserted is different than this message + if(m_lastinsertedxml[localidentityid]!=xmlstr) + { + std::string targeturi=""; + StringFunctions::Convert(xmlstr.size(),xmlsizestr); + + message.SetName("ClientPut"); + message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|MessageList|"+indexstr+".xml"; + message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+indexstr+"|"+message["URI"]; + message["UploadFrom"]="direct"; + message["DataLength"]=xmlsizestr; + m_fcp->SendMessage(message); + m_fcp->SendRaw(xmlstr.c_str(),xmlstr.size()); + + message.Reset(); + message.SetName("ClientPutComplexDir"); + message["URI"]="USK"+privatekey.substr(3)+m_messagebase+"|"+now.Format("%Y.%m.%d")+"|MessageList/0/"; + message["Identifier"]=m_fcpuniquename+"USK|"+message["URI"]; + message["DefaultName"]="MessageList.xml"; + message["Files.0.Name"]="MessageList.xml"; + message["Files.0.UploadFrom"]="direct"; + message["Files.0.DataLength"]=xmlsizestr; + m_fcp->SendMessage(message); + m_fcp->SendRaw(xmlstr.c_str(),xmlstr.size()); + + m_inserting.push_back(localidentityid); + m_lastinsertedxml[localidentityid]=xmlstr; + + return true; + } + else + { + return false; + } }