version 0.3.33
[fms.git] / src / freenet / identityinserter.cpp
index 4fac82f..988aa25 100644 (file)
@@ -3,39 +3,39 @@
 #include "../../include/stringfunctions.h"\r
 #include "../../include/option.h"\r
 \r
+#include <Poco/DateTimeFormatter.h>\r
+#include <Poco/DateTimeParser.h>\r
+\r
 #ifdef XMEM\r
        #include <xmem.h>\r
 #endif\r
 \r
-IdentityInserter::IdentityInserter()\r
+IdentityInserter::IdentityInserter(SQLite3DB::DB *db):IDatabase(db)\r
 {\r
        Initialize();\r
 }\r
 \r
-IdentityInserter::IdentityInserter(FCPv2 *fcp):IFCPConnected(fcp)\r
+IdentityInserter::IdentityInserter(SQLite3DB::DB *db, FCPv2::Connection *fcp):IDatabase(db),IFCPConnected(fcp)\r
 {\r
        Initialize();\r
 }\r
 \r
 void IdentityInserter::CheckForNeededInsert()\r
 {\r
-       DateTime now;\r
-       DateTime date;\r
-       now.SetToGMTime();\r
-       date.SetToGMTime();\r
+       Poco::DateTime now;\r
+       Poco::DateTime date;\r
+\r
        // set date to 1 hour back\r
-       date.Add(0,0,-1);\r
+       date-=Poco::Timespan(0,1,0,0,0);\r
 \r
        // Because of importance of Identity.xml, if we are now at the next day we immediately want to insert identities so change the date back to 12:00 AM so we find all identities not inserted yet today\r
-       if(date.GetDay()!=now.GetDay())\r
+       if(date.day()!=now.day())\r
        {\r
                date=now;\r
-               date.SetHour(0);\r
-               date.SetMinute(0);\r
-               date.SetSecond(0);\r
+               date.assign(date.year(),date.month(),date.day(),0,0,0);\r
        }\r
 \r
-       SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PrivateKey IS NOT NULL AND PrivateKey <> '' AND InsertingIdentity='false' AND (LastInsertedIdentity<'"+date.Format("%Y-%m-%d %H:%M:%S")+"' OR LastInsertedIdentity IS NULL) ORDER BY LastInsertedIdentity;");\r
+       SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PrivateKey IS NOT NULL AND PrivateKey <> '' AND InsertingIdentity='false' AND (LastInsertedIdentity<'"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d %H:%M:%S")+"' OR LastInsertedIdentity IS NULL) ORDER BY LastInsertedIdentity;");\r
        \r
        if(rs.Empty()==false)\r
        {\r
@@ -55,16 +55,16 @@ void IdentityInserter::FCPDisconnected()
        \r
 }\r
 \r
-const bool IdentityInserter::HandleMessage(FCPMessage &message)\r
+const bool IdentityInserter::HandleMessage(FCPv2::Message &message)\r
 {\r
 \r
        if(message["Identifier"].find("IdentityInserter")==0)\r
        {\r
-               DateTime now;\r
+               Poco::DateTime now;\r
                std::vector<std::string> idparts;\r
 \r
-               now.SetToGMTime();\r
                StringFunctions::Split(message["Identifier"],"|",idparts);\r
+               m_lastreceivedmessage=now;\r
 \r
                // no action for URIGenerated\r
                if(message.GetName()=="URIGenerated")\r
@@ -80,21 +80,50 @@ const bool IdentityInserter::HandleMessage(FCPMessage &message)
 \r
                if(message.GetName()=="PutSuccessful")\r
                {\r
-                       m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false', LastInsertedIdentity='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");\r
-                       m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
-                       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" inserted Identity xml");\r
+       \r
+                       // do check to make sure this is the non-editioned SSK - we ignore failure/success for editioned SSK for now\r
+                       if(message["Identifier"].find(".xml")!=std::string::npos)\r
+                       {\r
+                               // a little hack here - if we just inserted index yesterday and it is now the next day - we would have inserted todays date not yesterdays as LastInsertedIdentity.\r
+                               // If this is the case, we will skip updating LastInsertedIdentity so that we can insert this identity again for today\r
+                               Poco::DateTime lastdate;\r
+                               int tzdiff=0;\r
+                               Poco::DateTimeParser::tryParse("%Y-%m-%d",idparts[4],lastdate,tzdiff);\r
+                               if(lastdate.day()==now.day())\r
+                               {\r
+                                       m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false', LastInsertedIdentity='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");\r
+                               }\r
+                               else\r
+                               {\r
+                                       m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");\r
+                               }\r
+                               m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
+                               m_log->debug("IdentityInserter::HandleMessage inserted Identity xml");\r
+                       }\r
+                       else\r
+                       {\r
+                               m_log->trace("IdentityInserter::HandleMessage inserted editioned Identity xml");\r
+                       }\r
                        return true;\r
                }\r
 \r
                if(message.GetName()=="PutFailed")\r
                {\r
-                       m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");\r
-                       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" failure inserting identity 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
+                       // do check to make sure this is the non-editioned SSK - we ignore failure/success for editioned SSK for now\r
+                       if(message["Identifier"].find(".xml")!=std::string::npos)\r
                        {\r
-                               m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
+                               m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");\r
+                               m_log->debug("IdentityInserter::HandleMessage failure inserting Identity 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 tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
+                               }\r
+                       }\r
+                       else\r
+                       {\r
+                               m_log->trace("IdentityInserter::HandleMessage PutFailed for editioned SSK error code "+message["Code"]+ " id "+message["Identifier"]);\r
                        }\r
                        \r
                        return true;\r
@@ -108,20 +137,31 @@ const bool IdentityInserter::HandleMessage(FCPMessage &message)
 \r
 void IdentityInserter::Initialize()\r
 {\r
-       m_lastchecked.SetToGMTime();\r
+       m_lastchecked=Poco::Timestamp();\r
+       m_lastreceivedmessage=Poco::Timestamp();\r
 }\r
 \r
 void IdentityInserter::Process()\r
 {\r
-       DateTime now;\r
-       now.SetToGMTime();\r
+       Poco::DateTime now;\r
 \r
-       if(m_lastchecked<(now-(1.0/1440.0)))\r
+       if(m_lastchecked<(now-Poco::Timespan(0,0,1,0,0)))\r
        {\r
                CheckForNeededInsert();\r
                m_lastchecked=now;\r
        }\r
 \r
+       if(m_lastreceivedmessage<(now-Poco::Timespan(0,0,10,0,0)))\r
+       {\r
+               SQLite3DB::Statement st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE InsertingIdentity='true';");\r
+               st.Step();\r
+               if(st.RowReturned())\r
+               {\r
+                       m_log->debug("IdentityInserter::Process 10 minutes have passed without an insert response from the node.  Restarting inserts.");\r
+                       m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false';");\r
+               }\r
+       }\r
+\r
 }\r
 \r
 void IdentityInserter::RegisterWithThread(FreenetMasterThread *thread)\r
@@ -133,19 +173,18 @@ void IdentityInserter::RegisterWithThread(FreenetMasterThread *thread)
 \r
 void IdentityInserter::StartInsert(const long localidentityid)\r
 {\r
-       DateTime date;\r
+       Poco::DateTime date;\r
        std::string idstring;\r
 \r
        StringFunctions::Convert(localidentityid,idstring);\r
-       date.SetToGMTime();\r
 \r
-       SQLite3DB::Recordset rs=m_db->Query("SELECT Name,PrivateKey,SingleUse,PublishTrustList,PublishBoardList FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");\r
+       SQLite3DB::Recordset rs=m_db->Query("SELECT Name,PrivateKey,SingleUse,PublishTrustList,PublishBoardList,PublishFreesite,FreesiteEdition FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");\r
 \r
        if(rs.Empty()==false)\r
        {\r
                IdentityXML idxml;\r
-               FCPMessage mess;\r
-               DateTime now;\r
+               FCPv2::Message mess;\r
+               Poco::DateTime now;\r
                std::string messagebase;\r
                std::string data;\r
                std::string datasizestr;\r
@@ -155,10 +194,12 @@ void IdentityInserter::StartInsert(const long localidentityid)
                std::string singleuse="false";\r
                std::string publishtrustlist="false";\r
                std::string publishboardlist="false";\r
+               std::string freesiteedition="";\r
+               int edition=-1;\r
 \r
-               now.SetToGMTime();\r
+               now=Poco::Timestamp();\r
 \r
-               SQLite3DB::Recordset rs2=m_db->Query("SELECT MAX(InsertIndex) FROM tblLocalIdentityInserts WHERE LocalIdentityID="+idstring+" AND Day='"+now.Format("%Y-%m-%d")+"';");\r
+               SQLite3DB::Recordset rs2=m_db->Query("SELECT MAX(InsertIndex) FROM tblLocalIdentityInserts WHERE LocalIdentityID="+idstring+" AND Day='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"';");\r
                if(rs2.Empty()==false)\r
                {\r
                        if(rs2.GetField(0)==NULL)\r
@@ -172,7 +213,8 @@ void IdentityInserter::StartInsert(const long localidentityid)
                }\r
                StringFunctions::Convert(index,indexstr);\r
 \r
-               Option::instance()->Get("MessageBase",messagebase);\r
+               Option option(m_db);\r
+               option.Get("MessageBase",messagebase);\r
 \r
                if(rs.GetField(0))\r
                {\r
@@ -202,16 +244,36 @@ void IdentityInserter::StartInsert(const long localidentityid)
                }\r
                publishboardlist=="true" ? idxml.SetPublishBoardList(true) : idxml.SetPublishBoardList(false);\r
 \r
+               if(rs.GetField(5) && rs.GetField(6))\r
+               {\r
+                       if(std::string(rs.GetField(5))=="true")\r
+                       {\r
+                               freesiteedition=rs.GetField(6);\r
+                               StringFunctions::Convert(freesiteedition,edition);\r
+                               idxml.SetFreesiteEdition(edition);\r
+                       }\r
+               }\r
+\r
                data=idxml.GetXML();\r
                StringFunctions::Convert(data.size(),datasizestr);\r
 \r
                mess.SetName("ClientPut");\r
-               mess["URI"]=privatekey+messagebase+"|"+now.Format("%Y-%m-%d")+"|Identity|"+indexstr+".xml";\r
+               mess["URI"]=privatekey+messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|Identity|"+indexstr+".xml";\r
                mess["Identifier"]="IdentityInserter|"+idstring+"|"+indexstr+"|"+mess["URI"];\r
                mess["UploadFrom"]="direct";\r
                mess["DataLength"]=datasizestr;\r
-               m_fcp->SendMessage(mess);\r
-               m_fcp->SendRaw(data.c_str(),data.size());\r
+               m_fcp->Send(mess);\r
+               m_fcp->Send(std::vector<char>(data.begin(),data.end()));\r
+\r
+               // test insert as editioned SSK\r
+               mess.Clear();\r
+               mess.SetName("ClientPut");\r
+               mess["URI"]=privatekey+messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|Identity-"+indexstr;\r
+               mess["Identifier"]="IdentityInserter|"+mess["URI"];\r
+               mess["UploadFrom"]="direct";\r
+               mess["DataLength"]=datasizestr;\r
+               m_fcp->Send(mess);\r
+               m_fcp->Send(std::vector<char>(data.begin(),data.end()));\r
 \r
                m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='true' WHERE LocalIdentityID="+idstring+";");\r
 \r