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 now
\r
30 if(date.GetDay()!=now.GetDay())
\r
35 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
37 if(rs.Empty()==false)
\r
39 StartInsert(rs.GetInt(0));
\r
44 void IdentityInserter::FCPConnected()
\r
46 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false';");
\r
50 void IdentityInserter::FCPDisconnected()
\r
55 const bool IdentityInserter::HandleMessage(FCPMessage &message)
\r
58 if(message["Identifier"].find("IdentityInserter")==0)
\r
61 std::vector<std::string> idparts;
\r
64 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
66 // no action for URIGenerated
\r
67 if(message.GetName()=="URIGenerated")
\r
72 // no action for IdentifierCollision
\r
73 if(message.GetName()=="IdentifierCollision")
\r
78 if(message.GetName()=="PutSuccessful")
\r
80 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false', LastInsertedIdentity='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");
\r
81 m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
82 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" inserted identity xml");
\r
86 if(message.GetName()=="PutFailed")
\r
88 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");
\r
89 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" failure inserting identity xml. Code="+message["Code"]+" Description="+message["CodeDescription"]);
\r
91 // if code 9 (collision), then insert index into inserted table
\r
92 if(message["Code"]=="9")
\r
94 m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
106 void IdentityInserter::Initialize()
\r
108 m_lastchecked.SetToGMTime();
\r
111 void IdentityInserter::Process()
\r
116 if(m_lastchecked<(now-(1.0/1440.0)))
\r
118 CheckForNeededInsert();
\r
124 void IdentityInserter::RegisterWithThread(FreenetMasterThread *thread)
\r
126 thread->RegisterFCPConnected(this);
\r
127 thread->RegisterFCPMessageHandler(this);
\r
128 thread->RegisterPeriodicProcessor(this);
\r
131 void IdentityInserter::StartInsert(const long localidentityid)
\r
134 std::string idstring;
\r
136 StringFunctions::Convert(localidentityid,idstring);
\r
137 date.SetToGMTime();
\r
139 SQLite3DB::Recordset rs=m_db->Query("SELECT Name,PrivateKey,SingleUse,PublishTrustList,PublishBoardList FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");
\r
141 if(rs.Empty()==false)
\r
146 std::string messagebase;
\r
148 std::string datasizestr;
\r
149 std::string privatekey;
\r
151 std::string indexstr;
\r
152 std::string singleuse="false";
\r
153 std::string publishtrustlist="false";
\r
154 std::string publishboardlist="false";
\r
158 SQLite3DB::Recordset rs2=m_db->Query("SELECT MAX(InsertIndex) FROM tblLocalIdentityInserts WHERE LocalIdentityID="+idstring+" AND Day='"+now.Format("%Y-%m-%d")+"';");
\r
159 if(rs2.Empty()==false)
\r
161 if(rs2.GetField(0)==NULL)
\r
167 index=rs2.GetInt(0)+1;
\r
170 StringFunctions::Convert(index,indexstr);
\r
172 Option::instance()->Get("MessageBase",messagebase);
\r
176 idxml.SetName(rs.GetField(0));
\r
181 privatekey=rs.GetField(1);
\r
186 singleuse=rs.GetField(2);
\r
188 singleuse=="true" ? idxml.SetSingleUse(true) : idxml.SetSingleUse(false);
\r
192 publishtrustlist=rs.GetField(3);
\r
194 publishtrustlist=="true" ? idxml.SetPublishTrustList(true) : idxml.SetPublishTrustList(false);
\r
198 publishboardlist=rs.GetField(3);
\r
200 publishboardlist=="true" ? idxml.SetPublishBoardList(true) : idxml.SetPublishBoardList(false);
\r
202 data=idxml.GetXML();
\r
203 StringFunctions::Convert(data.size(),datasizestr);
\r
205 mess.SetName("ClientPut");
\r
206 mess["URI"]=privatekey+messagebase+"|"+now.Format("%Y-%m-%d")+"|Identity|"+indexstr+".xml";
\r
207 mess["Identifier"]="IdentityInserter|"+idstring+"|"+indexstr+"|"+mess["URI"];
\r
208 mess["UploadFrom"]="direct";
\r
209 mess["DataLength"]=datasizestr;
\r
210 m_fcp->SendMessage(mess);
\r
211 m_fcp->SendRaw(data.c_str(),data.size());
\r
213 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='true' WHERE LocalIdentityID="+idstring+";");
\r