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::CheckForNeededInsert()
\r
26 // set date to 1 hour back
\r
29 // 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
30 if(date.GetDay()!=now.GetDay())
\r
38 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
40 if(rs.Empty()==false)
\r
42 StartInsert(rs.GetInt(0));
\r
47 void IdentityInserter::FCPConnected()
\r
49 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false';");
\r
53 void IdentityInserter::FCPDisconnected()
\r
58 const bool IdentityInserter::HandleMessage(FCPMessage &message)
\r
61 if(message["Identifier"].find("IdentityInserter")==0)
\r
64 std::vector<std::string> idparts;
\r
67 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
69 // no action for URIGenerated
\r
70 if(message.GetName()=="URIGenerated")
\r
75 // no action for IdentifierCollision
\r
76 if(message.GetName()=="IdentifierCollision")
\r
81 if(message.GetName()=="PutSuccessful")
\r
83 // 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
84 // If this is the case, we will skip updating LastInsertedIdentity so that we can insert this identity again for today
\r
86 lastdate.Set(idparts[4]);
\r
87 if(lastdate.GetDay()==now.GetDay())
\r
89 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false', LastInsertedIdentity='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");
\r
93 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");
\r
95 m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
96 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IdentityInserter::HandleMessage inserted Identity xml");
\r
100 if(message.GetName()=="PutFailed")
\r
102 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");
\r
103 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IdentityInserter::HandleMessage failure inserting Identity xml. Code="+message["Code"]+" Description="+message["CodeDescription"]);
\r
105 // if code 9 (collision), then insert index into inserted table
\r
106 if(message["Code"]=="9")
\r
108 m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
120 void IdentityInserter::Initialize()
\r
122 m_lastchecked.SetToGMTime();
\r
125 void IdentityInserter::Process()
\r
130 if(m_lastchecked<(now-(1.0/1440.0)))
\r
132 CheckForNeededInsert();
\r
138 void IdentityInserter::RegisterWithThread(FreenetMasterThread *thread)
\r
140 thread->RegisterFCPConnected(this);
\r
141 thread->RegisterFCPMessageHandler(this);
\r
142 thread->RegisterPeriodicProcessor(this);
\r
145 void IdentityInserter::StartInsert(const long localidentityid)
\r
148 std::string idstring;
\r
150 StringFunctions::Convert(localidentityid,idstring);
\r
151 date.SetToGMTime();
\r
153 SQLite3DB::Recordset rs=m_db->Query("SELECT Name,PrivateKey,SingleUse,PublishTrustList,PublishBoardList,PublishFreesite,FreesiteEdition FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");
\r
155 if(rs.Empty()==false)
\r
160 std::string messagebase;
\r
162 std::string datasizestr;
\r
163 std::string privatekey;
\r
165 std::string indexstr;
\r
166 std::string singleuse="false";
\r
167 std::string publishtrustlist="false";
\r
168 std::string publishboardlist="false";
\r
169 std::string freesiteedition="";
\r
174 SQLite3DB::Recordset rs2=m_db->Query("SELECT MAX(InsertIndex) FROM tblLocalIdentityInserts WHERE LocalIdentityID="+idstring+" AND Day='"+now.Format("%Y-%m-%d")+"';");
\r
175 if(rs2.Empty()==false)
\r
177 if(rs2.GetField(0)==NULL)
\r
183 index=rs2.GetInt(0)+1;
\r
186 StringFunctions::Convert(index,indexstr);
\r
188 Option::Instance()->Get("MessageBase",messagebase);
\r
192 idxml.SetName(rs.GetField(0));
\r
197 privatekey=rs.GetField(1);
\r
202 singleuse=rs.GetField(2);
\r
204 singleuse=="true" ? idxml.SetSingleUse(true) : idxml.SetSingleUse(false);
\r
208 publishtrustlist=rs.GetField(3);
\r
210 publishtrustlist=="true" ? idxml.SetPublishTrustList(true) : idxml.SetPublishTrustList(false);
\r
214 publishboardlist=rs.GetField(4);
\r
216 publishboardlist=="true" ? idxml.SetPublishBoardList(true) : idxml.SetPublishBoardList(false);
\r
218 if(rs.GetField(5) && rs.GetField(6))
\r
220 if(std::string(rs.GetField(5))=="true")
\r
222 freesiteedition=rs.GetField(6);
\r
223 StringFunctions::Convert(freesiteedition,edition);
\r
224 idxml.SetFreesiteEdition(edition);
\r
228 data=idxml.GetXML();
\r
229 StringFunctions::Convert(data.size(),datasizestr);
\r
231 mess.SetName("ClientPut");
\r
232 mess["URI"]=privatekey+messagebase+"|"+now.Format("%Y-%m-%d")+"|Identity|"+indexstr+".xml";
\r
233 mess["Identifier"]="IdentityInserter|"+idstring+"|"+indexstr+"|"+mess["URI"];
\r
234 mess["UploadFrom"]="direct";
\r
235 mess["DataLength"]=datasizestr;
\r
236 m_fcp->SendMessage(mess);
\r
237 m_fcp->SendRaw(data.c_str(),data.size());
\r
239 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='true' WHERE LocalIdentityID="+idstring+";");
\r