version 0.0.2
[fms.git] / src / freenet / trustlistinserter.cpp
diff --git a/src/freenet/trustlistinserter.cpp b/src/freenet/trustlistinserter.cpp
new file mode 100644 (file)
index 0000000..c5cc7df
--- /dev/null
@@ -0,0 +1,177 @@
+#include "../../include/freenet/trustlistinserter.h"\r
+#include "../../include/option.h"\r
+#include "../../include/freenet/trustlistxml.h"\r
+#include "../../include/stringfunctions.h"\r
+\r
+#ifdef XMEM\r
+       #include <xmem.h>\r
+#endif\r
+\r
+TrustListInserter::TrustListInserter()\r
+{\r
+       Initialize();\r
+}\r
+\r
+TrustListInserter::TrustListInserter(FCPv2 *fcp):IFCPConnected(fcp)\r
+{\r
+       Initialize();\r
+}\r
+\r
+void TrustListInserter::CheckForNeededInsert()\r
+{\r
+       DateTime date;\r
+       date.SetToGMTime();\r
+       date.Add(0,0,-1);\r
+       SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID, PrivateKey FROM tblLocalIdentity WHERE PrivateKey IS NOT NULL AND PrivateKey <> '' AND PublishTrustList='true' AND InsertingTrustList='false' AND (LastInsertedTrustList<='"+date.Format("%Y-%m-%d %H:%M:%S")+"' OR LastInsertedTrustList IS NULL);");\r
+\r
+       if(rs.Empty()==false)\r
+       {\r
+               StartInsert(rs.GetInt(0),rs.GetField(1));\r
+       }\r
+}\r
+\r
+void TrustListInserter::FCPConnected()\r
+{\r
+       m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false';");\r
+}\r
+\r
+void TrustListInserter::FCPDisconnected()\r
+{\r
+\r
+}\r
+\r
+const bool TrustListInserter::HandleMessage(FCPMessage &message)\r
+{\r
+\r
+       if(message["Identifier"].find("TrustListInserter")==0)\r
+       {\r
+               \r
+               DateTime now;\r
+               std::vector<std::string> idparts;\r
+\r
+               now.SetToGMTime();\r
+               StringFunctions::Split(message["Identifier"],"|",idparts);\r
+\r
+               // no action for URIGenerated\r
+               if(message.GetName()=="URIGenerated")\r
+               {\r
+                       return true;\r
+               }\r
+\r
+               // no action for IdentifierCollision\r
+               if(message.GetName()=="IdentifierCollision")\r
+               {\r
+                       return true;\r
+               }\r
+\r
+               if(message.GetName()=="PutSuccessful")\r
+               {\r
+                       m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false', LastInsertedTrustList='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");\r
+                       m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
+                       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" inserted TrustList xml");\r
+                       return true;\r
+               }\r
+\r
+               if(message.GetName()=="PutFailed")\r
+               {\r
+                       m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false' WHERE LocalIdentityID="+idparts[1]+";");\r
+                       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" failure inserting TrustList xml.  Code="+message["Code"]+" Description="+message["CodeDescription"]);\r
+                       \r
+                       // if code 9 (collision), then insert index into inserted table\r
+                       if(message["Code"]=="9")\r
+                       {\r
+                               m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
+                       }\r
+                       \r
+                       return true;\r
+               }\r
+\r
+       }\r
+\r
+       return false;\r
+}\r
+\r
+void TrustListInserter::Initialize()\r
+{\r
+       Option::instance()->Get("MessageBase",m_messagebase);\r
+       m_lastchecked.SetToGMTime();\r
+}\r
+\r
+void TrustListInserter::Process()\r
+{\r
+       DateTime now;\r
+       now.SetToGMTime();\r
+\r
+       // check every minute\r
+       if(m_lastchecked<=(now-(1.0/1440.0)))\r
+       {\r
+               CheckForNeededInsert();\r
+               m_lastchecked=now;\r
+       }\r
+}\r
+\r
+void TrustListInserter::RegisterWithThread(FreenetMasterThread *thread)\r
+{\r
+       thread->RegisterFCPConnected(this);\r
+       thread->RegisterFCPMessageHandler(this);\r
+       thread->RegisterPeriodicProcessor(this);\r
+}\r
+\r
+void TrustListInserter::StartInsert(const long localidentityid, const std::string &privatekey)\r
+{\r
+       FCPMessage message;\r
+       TrustListXML xml;\r
+       std::string data;\r
+       std::string datasizestr;\r
+       std::string publickey;\r
+       int messagetrust;\r
+       int trustlisttrust;\r
+       DateTime now;\r
+       int index;\r
+       std::string indexstr;\r
+       std::string localidentityidstr;\r
+\r
+       now.SetToGMTime();\r
+       \r
+       // build the xml file\r
+       SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, LocalMessageTrust, LocalTrustListTrust FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey<>'';");\r
+       st.Step();\r
+       while(st.RowReturned())\r
+       {\r
+               st.ResultText(0,publickey);\r
+               st.ResultInt(1,messagetrust);\r
+               st.ResultInt(2,trustlisttrust);\r
+               xml.AddTrust(publickey,messagetrust,trustlisttrust);\r
+               st.Step();\r
+       }\r
+\r
+       // get next insert index\r
+       st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblTrustListInserts WHERE LocalIdentityID=? AND Day=?;");\r
+       st.Bind(0,localidentityid);\r
+       st.Bind(1,now.Format("%Y-%m-%d"));\r
+       st.Step();\r
+\r
+       index=0;\r
+       if(st.RowReturned() && st.ResultNull(0)==false)\r
+       {\r
+               st.ResultInt(0,index);\r
+               index++;\r
+       }\r
+\r
+       StringFunctions::Convert(localidentityid,localidentityidstr);\r
+       StringFunctions::Convert(index,indexstr);\r
+\r
+       data=xml.GetXML();\r
+       StringFunctions::Convert(data.size(),datasizestr);\r
+\r
+       message.SetName("ClientPut");\r
+       message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";\r
+       message["Identifier"]="TrustListInserter|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];\r
+       message["UploadFrom"]="direct";\r
+       message["DataLength"]=datasizestr;\r
+       m_fcp->SendMessage(message);\r
+       m_fcp->SendRaw(data.c_str(),data.size());\r
+\r
+       m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='true' WHERE LocalIdentityID="+localidentityidstr+";");\r
+\r
+}
\ No newline at end of file