version 0.1.2
[fms.git] / src / freenet / identityinserter.cpp
index 33a8085..aa1f4ab 100644 (file)
@@ -17,18 +17,24 @@ IdentityInserter::IdentityInserter(FCPv2 *fcp):IFCPConnected(fcp)
        Initialize();\r
 }\r
 \r
-void IdentityInserter::FCPConnected()\r
-{\r
-       m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false';");\r
-}\r
-\r
 void IdentityInserter::CheckForNeededInsert()\r
 {\r
+       DateTime now;\r
        DateTime date;\r
+       now.SetToGMTime();\r
        date.SetToGMTime();\r
        // set date to 1 hour back\r
        date.Add(0,0,-1);\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
+       {\r
+               date=now;\r
+               date.SetHour(0);\r
+               date.SetMinute(0);\r
+               date.SetSecond(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
        \r
        if(rs.Empty()==false)\r
@@ -38,6 +44,12 @@ void IdentityInserter::CheckForNeededInsert()
 \r
 }\r
 \r
+void IdentityInserter::FCPConnected()\r
+{\r
+       m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false';");\r
+}\r
+\r
+\r
 void IdentityInserter::FCPDisconnected()\r
 {\r
        \r
@@ -68,16 +80,23 @@ 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
+                       // 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
+                       DateTime lastdate;\r
+                       lastdate.Set(idparts[4]);\r
+                       if(lastdate.GetDay()!=now.GetDay())\r
+                       {\r
+                               m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false', LastInsertedIdentity='"+now.Format("%Y-%m-%d %H:%M:%S")+"' 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->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" inserted identity xml");\r
+                       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IdentityInserter::HandleMessage inserted Identity xml");\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
+                       m_log->WriteLog(LogFile::LOGLEVEL_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
@@ -127,7 +146,7 @@ void IdentityInserter::StartInsert(const long localidentityid)
        StringFunctions::Convert(localidentityid,idstring);\r
        date.SetToGMTime();\r
 \r
-       SQLite3DB::Recordset rs=m_db->Query("SELECT Name,PrivateKey,SingleUse FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");\r
+       SQLite3DB::Recordset rs=m_db->Query("SELECT Name,PrivateKey,SingleUse,PublishTrustList,PublishBoardList FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");\r
 \r
        if(rs.Empty()==false)\r
        {\r
@@ -141,6 +160,8 @@ void IdentityInserter::StartInsert(const long localidentityid)
                long index=0;\r
                std::string indexstr;\r
                std::string singleuse="false";\r
+               std::string publishtrustlist="false";\r
+               std::string publishboardlist="false";\r
 \r
                now.SetToGMTime();\r
 \r
@@ -176,6 +197,18 @@ void IdentityInserter::StartInsert(const long localidentityid)
                }\r
                singleuse=="true" ? idxml.SetSingleUse(true) : idxml.SetSingleUse(false);\r
 \r
+               if(rs.GetField(3))\r
+               {\r
+                       publishtrustlist=rs.GetField(3);\r
+               }\r
+               publishtrustlist=="true" ? idxml.SetPublishTrustList(true) : idxml.SetPublishTrustList(false);\r
+\r
+               if(rs.GetField(4))\r
+               {\r
+                       publishboardlist=rs.GetField(4);\r
+               }\r
+               publishboardlist=="true" ? idxml.SetPublishBoardList(true) : idxml.SetPublishBoardList(false);\r
+\r
                data=idxml.GetXML();\r
                StringFunctions::Convert(data.size(),datasizestr);\r
 \r