1 #include "../../include/freenet/identityinserter.h"
\r
2 #include "../../include/freenet/identityxml.h"
\r
3 #include "../../include/stringfunctions.h"
\r
4 #include "../../include/option.h"
\r
10 IdentityInserter::IdentityInserter()
\r
15 IdentityInserter::IdentityInserter(FCPv2 *fcp):IFCPConnected(fcp)
\r
20 void IdentityInserter::FCPConnected()
\r
22 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false';");
\r
25 void IdentityInserter::CheckForNeededInsert()
\r
29 // set date to 1 hour back
\r
32 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
34 if(rs.Empty()==false)
\r
36 StartInsert(rs.GetInt(0));
\r
41 void IdentityInserter::FCPDisconnected()
\r
46 const bool IdentityInserter::HandleMessage(FCPMessage &message)
\r
49 if(message["Identifier"].find("IdentityInserter")==0)
\r
52 std::vector<std::string> idparts;
\r
55 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
57 // no action for URIGenerated
\r
58 if(message.GetName()=="URIGenerated")
\r
63 // no action for IdentifierCollision
\r
64 if(message.GetName()=="IdentifierCollision")
\r
69 if(message.GetName()=="PutSuccessful")
\r
71 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false', LastInsertedIdentity='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");
\r
72 m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
73 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" inserted identity xml");
\r
77 if(message.GetName()=="PutFailed")
\r
79 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");
\r
80 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" failure inserting identity xml. Code="+message["Code"]+" Description="+message["CodeDescription"]);
\r
82 // if code 9 (collision), then insert index into inserted table
\r
83 if(message["Code"]=="9")
\r
85 m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
97 void IdentityInserter::Initialize()
\r
99 m_lastchecked.SetToGMTime();
\r
102 void IdentityInserter::Process()
\r
107 if(m_lastchecked<(now-(1.0/1440.0)))
\r
109 CheckForNeededInsert();
\r
115 void IdentityInserter::RegisterWithThread(FreenetMasterThread *thread)
\r
117 thread->RegisterFCPConnected(this);
\r
118 thread->RegisterFCPMessageHandler(this);
\r
119 thread->RegisterPeriodicProcessor(this);
\r
122 void IdentityInserter::StartInsert(const long localidentityid)
\r
125 std::string idstring;
\r
127 StringFunctions::Convert(localidentityid,idstring);
\r
128 date.SetToGMTime();
\r
130 SQLite3DB::Recordset rs=m_db->Query("SELECT Name,PrivateKey,SingleUse FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");
\r
132 if(rs.Empty()==false)
\r
137 std::string messagebase;
\r
139 std::string datasizestr;
\r
140 std::string privatekey;
\r
142 std::string indexstr;
\r
143 std::string singleuse="false";
\r
147 SQLite3DB::Recordset rs2=m_db->Query("SELECT MAX(InsertIndex) FROM tblLocalIdentityInserts WHERE LocalIdentityID="+idstring+" AND Day='"+now.Format("%Y-%m-%d")+"';");
\r
148 if(rs2.Empty()==false)
\r
150 if(rs2.GetField(0)==NULL)
\r
156 index=rs2.GetInt(0)+1;
\r
159 StringFunctions::Convert(index,indexstr);
\r
161 Option::instance()->Get("MessageBase",messagebase);
\r
165 idxml.SetName(rs.GetField(0));
\r
170 privatekey=rs.GetField(1);
\r
175 singleuse=rs.GetField(2);
\r
177 singleuse=="true" ? idxml.SetSingleUse(true) : idxml.SetSingleUse(false);
\r
179 data=idxml.GetXML();
\r
180 StringFunctions::Convert(data.size(),datasizestr);
\r
182 mess.SetName("ClientPut");
\r
183 mess["URI"]=privatekey+messagebase+"|"+now.Format("%Y-%m-%d")+"|Identity|"+indexstr+".xml";
\r
184 mess["Identifier"]="IdentityInserter|"+idstring+"|"+indexstr+"|"+mess["URI"];
\r
185 mess["UploadFrom"]="direct";
\r
186 mess["DataLength"]=datasizestr;
\r
187 m_fcp->SendMessage(mess);
\r
188 m_fcp->SendRaw(data.c_str(),data.size());
\r
190 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='true' WHERE LocalIdentityID="+idstring+";");
\r