X-Git-Url: https://git.pterodactylus.net/?p=fms.git;a=blobdiff_plain;f=src%2Ffreenet%2Fintroductionpuzzleinserter.cpp;h=a38108415dc8dcdd680983754b4d8d628f1d1a22;hp=9bb4da8a32a8cf1ac2096b935467d0fb7a0fdacc;hb=6836fbb5db8464f56e682989996b2210b14231d0;hpb=7ddb1aeb0b3dc7384597e75f7b3557f2d8f6d14c diff --git a/src/freenet/introductionpuzzleinserter.cpp b/src/freenet/introductionpuzzleinserter.cpp index 9bb4da8..a381084 100644 --- a/src/freenet/introductionpuzzleinserter.cpp +++ b/src/freenet/introductionpuzzleinserter.cpp @@ -10,53 +10,47 @@ #include #endif -IntroductionPuzzleInserter::IntroductionPuzzleInserter() +IntroductionPuzzleInserter::IntroductionPuzzleInserter():IIndexInserter() { Initialize(); } -IntroductionPuzzleInserter::IntroductionPuzzleInserter(FCPv2 *fcp):IFCPConnected(fcp) +IntroductionPuzzleInserter::IntroductionPuzzleInserter(FCPv2 *fcp):IIndexInserter(fcp) { Initialize(); } void IntroductionPuzzleInserter::CheckForNeededInsert() { - // select all local ids that aren't single use and that aren't currently inserting a puzzle and are publishing a trust list - SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PublishTrustList='true' AND SingleUse='false' AND InsertingPuzzle='false' AND PrivateKey IS NOT NULL AND PrivateKey <> '' ORDER BY LastInsertedPuzzle;"); - - while(!rs.AtEnd()) + // only do 1 insert at a time + if(m_inserting.size()==0) { - std::string localidentityidstr; - DateTime now; - now.SetToGMTime(); - - if(rs.GetField(0)) + // select all local ids that aren't single use and that aren't currently inserting a puzzle and are publishing a trust list + SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PublishTrustList='true' AND SingleUse='false' AND PrivateKey IS NOT NULL AND PrivateKey <> '' ORDER BY LastInsertedPuzzle;"); + + while(!rs.AtEnd()) { - localidentityidstr=rs.GetField(0); - } + std::string localidentityidstr; + DateTime now; + now.SetToGMTime(); - // if this identity has any non-solved puzzles for today, we don't need to insert a new puzzle - SQLite3DB::Recordset rs2=m_db->Query("SELECT UUID FROM tblIntroductionPuzzleInserts WHERE Day='"+now.Format("%Y-%m-%d")+"' AND FoundSolution='false' AND LocalIdentityID="+localidentityidstr+";"); + if(rs.GetField(0)) + { + localidentityidstr=rs.GetField(0); + } - // identity doesn't have any non-solved puzzles for today - start a new insert - if(rs2.Empty()==true) - { - StartInsert(rs.GetInt(0)); - } + // if this identity has any non-solved puzzles for today, we don't need to insert a new puzzle + SQLite3DB::Recordset rs2=m_db->Query("SELECT UUID FROM tblIntroductionPuzzleInserts WHERE Day='"+now.Format("%Y-%m-%d")+"' AND FoundSolution='false' AND LocalIdentityID="+localidentityidstr+";"); - rs.Next(); - } -} + // identity doesn't have any non-solved puzzles for today - start a new insert + if(rs2.Empty()==true) + { + StartInsert(rs.GetInt(0)); + } -void IntroductionPuzzleInserter::FCPConnected() -{ - m_db->Execute("UPDATE tblLocalIdentity SET InsertingPuzzle='false';"); -} - -void IntroductionPuzzleInserter::FCPDisconnected() -{ - + rs.Next(); + } + } } void IntroductionPuzzleInserter::GenerateCaptcha(std::string &encodeddata, std::string &solution) @@ -77,33 +71,6 @@ void IntroductionPuzzleInserter::GenerateCaptcha(std::string &encodeddata, std:: } -const bool IntroductionPuzzleInserter::HandleMessage(FCPMessage &message) -{ - - if(message["Identifier"].find("IntroductionPuzzleInserter")==0) - { - - // ignore URIGenerated message - if(message.GetName()=="URIGenerated") - { - return true; - } - - if(message.GetName()=="PutSuccessful") - { - return HandlePutSuccessful(message); - } - - if(message.GetName()=="PutFailed") - { - return HandlePutFailed(message); - } - - } - - return false; -} - const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message) { SQLite3DB::Statement st; @@ -113,18 +80,19 @@ const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message) StringFunctions::Split(message["Identifier"],"|",idparts); StringFunctions::Convert(idparts[1],localidentityid); - st=m_db->Prepare("UPDATE tblLocalIdentity SET InsertingPuzzle='false' WHERE LocalIdentityID=?;"); - st.Bind(0,localidentityid); - st.Step(); - st.Finalize(); - - // if fatal error or collision - mark index - if(message["Fatal"]=="true" || message["Code"]=="9") + // non USK + if(idparts[0]==m_fcpuniquename) { - m_db->Execute("UPDATE tblIntroductionPuzzleInserts SET Day='"+idparts[5]+"', InsertIndex="+idparts[2]+", FoundSolution='true' WHERE UUID='"+idparts[3]+"';"); - } + // if fatal error or collision - mark index + if(message["Fatal"]=="true" || message["Code"]=="9") + { + m_db->Execute("UPDATE tblIntroductionPuzzleInserts SET Day='"+idparts[5]+"', InsertIndex="+idparts[2]+", FoundSolution='true' WHERE UUID='"+idparts[3]+"';"); + } - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutFailed failed to insert puzzle "+idparts[3]); + RemoveFromInsertList(localidentityid); + + m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutFailed failed to insert puzzle "+idparts[3]); + } return true; } @@ -139,55 +107,40 @@ const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message) now.SetToGMTime(); StringFunctions::Split(message["Identifier"],"|",idparts); - StringFunctions::Convert(idparts[1],localidentityid); - StringFunctions::Convert(idparts[2],insertindex); - - st=m_db->Prepare("UPDATE tblIntroductionPuzzleInserts SET Day=?, InsertIndex=? WHERE UUID=?;"); - st.Bind(0,idparts[5]); - st.Bind(1,insertindex); - st.Bind(2,idparts[3]); - st.Step(); - st.Finalize(); - st=m_db->Prepare("UPDATE tblLocalIdentity SET InsertingPuzzle='false', LastInsertedPuzzle=? WHERE LocalIdentityID=?;"); - st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S")); - st.Bind(1,localidentityid); - st.Step(); - st.Finalize(); - - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutSuccessful inserted puzzle "+idparts[3]); - - return true; -} - -void IntroductionPuzzleInserter::Initialize() -{ - m_lastchecked.SetToGMTime(); -} + // non USK + if(idparts[0]==m_fcpuniquename) + { + StringFunctions::Convert(idparts[1],localidentityid); + StringFunctions::Convert(idparts[2],insertindex); -void IntroductionPuzzleInserter::Process() -{ + st=m_db->Prepare("UPDATE tblIntroductionPuzzleInserts SET Day=?, InsertIndex=? WHERE UUID=?;"); + st.Bind(0,idparts[5]); + st.Bind(1,insertindex); + st.Bind(2,idparts[3]); + st.Step(); + st.Finalize(); - DateTime now; + st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedPuzzle=? WHERE LocalIdentityID=?;"); + st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S")); + st.Bind(1,localidentityid); + st.Step(); + st.Finalize(); - now.SetToGMTime(); + RemoveFromInsertList(localidentityid); - if(m_lastchecked<(now-(1.0/1440.0))) - { - CheckForNeededInsert(); - m_lastchecked=now; + m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutSuccessful inserted puzzle "+idparts[3]); } + return true; } -void IntroductionPuzzleInserter::RegisterWithThread(FreenetMasterThread *thread) +void IntroductionPuzzleInserter::Initialize() { - thread->RegisterFCPConnected(this); - thread->RegisterFCPMessageHandler(this); - thread->RegisterPeriodicProcessor(this); + m_fcpuniquename="IntroductionPuzzleInserter"; } -void IntroductionPuzzleInserter::StartInsert(const long localidentityid) +const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) { DateTime now; std::string idstring; @@ -248,15 +201,30 @@ void IntroductionPuzzleInserter::StartInsert(const long localidentityid) message.SetName("ClientPut"); message["URI"]=privatekey+messagebase+"|"+now.Format("%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml"; - message["Identifier"]="IntroductionPuzzleInserter|"+idstring+"|"+indexstr+"|"+xml.GetUUID()+"|"+message["URI"]; + message["Identifier"]=m_fcpuniquename+"|"+idstring+"|"+indexstr+"|"+xml.GetUUID()+"|"+message["URI"]; message["UploadFrom"]="direct"; message["DataLength"]=xmldatasizestr; m_fcp->SendMessage(message); m_fcp->SendRaw(xmldata.c_str(),xmldata.size()); - m_db->Execute("UPDATE tblLocalIdentity SET InsertingPuzzle='true' WHERE LocalIdentityID="+idstring+";"); + // insert to USK + message.Reset(); + message.SetName("ClientPutComplexDir"); + message["URI"]="USK"+privatekey.substr(3)+messagebase+"|"+now.Format("%Y.%m.%d")+"|IntroductionPuzzle/0/"; + message["Identifier"]=m_fcpuniquename+"USK|"+message["URI"]; + message["DefaultName"]="IntroductionPuzzle.xml"; + message["Files.0.Name"]="IntroductionPuzzle.xml"; + message["Files.0.UplaodFrom"]="direct"; + message["Files.0.DataLength"]=xmldatasizestr; + m_fcp->SendMessage(message); + m_fcp->SendRaw(xmldata.c_str(),xmldata.size()); + m_db->Execute("INSERT INTO tblIntroductionPuzzleInserts(UUID,Type,MimeType,LocalIdentityID,PuzzleData,PuzzleSolution) VALUES('"+xml.GetUUID()+"','captcha','image/bmp',"+idstring+",'"+encodedpuzzle+"','"+solutionstring+"');"); + m_inserting.push_back(localidentityid); + m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::StartInsert started insert for id "+idstring); + return true; + }