X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ffreenet%2Fmessageinserter.cpp;h=d248cd39c0dfc8bfbcd33d65dd123b4a691307da;hb=56f67ecca96efc7b72d03c95c8c42cfb66e31468;hp=3d5af9aa8e4a8ceab2ad6e93c058e84b8e530b47;hpb=52c0819bfc1d083c6e0738f75f0d7eeba521295a;p=fms.git diff --git a/src/freenet/messageinserter.cpp b/src/freenet/messageinserter.cpp index 3d5af9a..d248cd3 100644 --- a/src/freenet/messageinserter.cpp +++ b/src/freenet/messageinserter.cpp @@ -1,6 +1,10 @@ #include "../../include/freenet/messageinserter.h" #include "../../include/freenet/messagexml.h" +#include +#include +#include + MessageInserter::MessageInserter() { Initialize(); @@ -13,17 +17,31 @@ MessageInserter::MessageInserter(FCPv2 *fcp):IIndexInserter(fcp) void MessageInserter::CheckForNeededInsert() { + Poco::DateTime now; + bool didinsert=false; // only do 1 insert at a time if(m_inserting.size()==0) { - SQLite3DB::Statement st=m_db->Prepare("SELECT MessageUUID FROM tblMessageInserts INNER JOIN tblLocalIdentity ON tblMessageInserts.LocalIdentityID=tblLocalIdentity.LocalIdentityID WHERE tblLocalIdentity.PrivateKey IS NOT NULL AND tblLocalIdentity.PrivateKey <> '' AND tblMessageInserts.Inserted='false';"); + SQLite3DB::Statement st=m_db->Prepare("SELECT MessageUUID FROM tblMessageInserts INNER JOIN tblLocalIdentity ON tblMessageInserts.LocalIdentityID=tblLocalIdentity.LocalIdentityID WHERE tblLocalIdentity.PrivateKey IS NOT NULL AND tblLocalIdentity.PrivateKey <> '' AND tblMessageInserts.Inserted='false' AND tblMessageInserts.SendDate<=?;"); + st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S")); st.Step(); - if(st.RowReturned()) + while(st.RowReturned() && m_inserting.size()==0) { - std::string messageuuid; + std::string messageuuid=""; st.ResultText(0,messageuuid); - StartInsert(messageuuid); + + // make sure there are no uninserted files attached to this message + SQLite3DB::Statement st2=m_db->Prepare("SELECT FileInsertID FROM tblFileInserts WHERE Key IS NULL AND MessageUUID=?;"); + st2.Bind(0,messageuuid); + st2.Step(); + + if(st2.RowReturned()==false) + { + StartInsert(messageuuid); + } + + st.Step(); } } } @@ -47,6 +65,8 @@ const bool MessageInserter::HandlePutFailed(FCPMessage &message) st.Step(); } + m_log->trace("MessageInserter::HandlePutFailed error code "+message["Code"]+" fatal="+message["Fatal"]); + RemoveFromInsertList(idparts[1]); return true; @@ -55,10 +75,11 @@ const bool MessageInserter::HandlePutFailed(FCPMessage &message) const bool MessageInserter::HandlePutSuccessful(FCPMessage &message) { MessageXML xml; - DateTime date; + Poco::DateTime date; int localidentityid; int index; std::vector idparts; + StringFunctions::Split(message["Identifier"],"|",idparts); StringFunctions::Convert(idparts[3],index); StringFunctions::Convert(idparts[2],localidentityid); @@ -70,10 +91,10 @@ const bool MessageInserter::HandlePutSuccessful(FCPMessage &message) st.Step(); // insert record into temp table so MessageList will be inserted ASAP - date.SetToGMTime(); + date=Poco::Timestamp(); st=m_db->Prepare("INSERT INTO tmpMessageListInsert(LocalIdentityID,Date) VALUES(?,?);"); st.Bind(0,localidentityid); - st.Bind(1,date.Format("%Y-%m-%d")); + st.Bind(1,Poco::DateTimeFormatter::format(date,"%Y-%m-%d")); st.Step(); // update the messageuuid to the real messageuuid @@ -92,11 +113,17 @@ const bool MessageInserter::HandlePutSuccessful(FCPMessage &message) st2.Bind(1,xml.GetXML()); st2.Bind(2,idparts[1]); st2.Step(); + + //update file insert MessageUUID as well + st2=m_db->Prepare("UPDATE tblFileInserts SET MessageUUID=? WHERE MessageUUID=?;"); + st2.Bind(0,idparts[4]); + st2.Bind(1,idparts[1]); + st2.Step(); } RemoveFromInsertList(idparts[1]); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageInserter::HandlePutSuccessful successfully inserted message "+message["Identifier"]); + m_log->debug("MessageInserter::HandlePutSuccessful successfully inserted message "+message["Identifier"]); return true; } @@ -106,11 +133,10 @@ void MessageInserter::Initialize() m_fcpuniquename="MessageInserter"; } -void MessageInserter::StartInsert(const std::string &messageuuid) +const bool MessageInserter::StartInsert(const std::string &messageuuid) { MessageXML xmlfile; - DateTime now; - now.SetToGMTime(); + Poco::DateTime now; SQLite3DB::Statement st=m_db->Prepare("SELECT MessageXML,PrivateKey,tblLocalIdentity.LocalIdentityID,PublicKey FROM tblMessageInserts INNER JOIN tblLocalIdentity ON tblMessageInserts.LocalIdentityID=tblLocalIdentity.LocalIdentityID WHERE MessageUUID=?;"); st.Bind(0,messageuuid); st.Step(); @@ -134,7 +160,7 @@ void MessageInserter::StartInsert(const std::string &messageuuid) StringFunctions::Convert(localidentityid,idstr); st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblMessageInserts WHERE Day=? AND LocalIdentityID=?;"); - st.Bind(0,now.Format("%Y-%m-%d")); + st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d")); st.Bind(1,localidentityid); st.Step(); @@ -145,9 +171,27 @@ void MessageInserter::StartInsert(const std::string &messageuuid) } StringFunctions::Convert(index,indexstr); + xmlfile.ParseXML(xml); + + // add file attachments to xml - must do this before we change UUID + st=m_db->Prepare("SELECT Key, Size FROM tblFileInserts WHERE MessageUUID=?;"); + st.Bind(0,xmlfile.GetMessageID()); + st.Step(); + while(st.RowReturned()) + { + std::string key=""; + int size; + + st.ResultText(0,key); + st.ResultInt(1,size); + + xmlfile.AddFileAttachment(key,size); + + st.Step(); + } + // recreate messageuuid in xml - UUID of message will not match entry in MessageInserts table until we successfully insert it // see HandlePutSuccessful - xmlfile.ParseXML(xml); // if we don't already have an @sskpart - add it if(xmlfile.GetMessageID().find("@")==std::string::npos) { @@ -160,7 +204,7 @@ void MessageInserter::StartInsert(const std::string &messageuuid) StringFunctions::Convert(xml.size(),xmlsizestr); message.SetName("ClientPut"); - message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|Message|"+indexstr+".xml"; + message["URI"]=privatekey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|Message|"+indexstr+".xml"; message["Identifier"]=m_fcpuniquename+"|"+messageuuid+"|"+idstr+"|"+indexstr+"|"+xmlfile.GetMessageID()+"|"+message["URI"]; message["UploadFrom"]="direct"; message["DataLength"]=xmlsizestr; @@ -169,7 +213,13 @@ void MessageInserter::StartInsert(const std::string &messageuuid) m_inserting.push_back(messageuuid); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageInserter::StartInsert started message insert "+message["URI"]); + m_log->debug("MessageInserter::StartInsert started message insert "+message["URI"]); + + return true; + } + else + { + return false; } }