1 #include "../../include/freenet/trustlistinserter.h"
\r
2 #include "../../include/option.h"
\r
3 #include "../../include/freenet/trustlistxml.h"
\r
4 #include "../../include/stringfunctions.h"
\r
10 TrustListInserter::TrustListInserter()
\r
15 TrustListInserter::TrustListInserter(FCPv2 *fcp):IFCPConnected(fcp)
\r
20 void TrustListInserter::CheckForNeededInsert()
\r
25 SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID, PrivateKey FROM tblLocalIdentity WHERE PrivateKey IS NOT NULL AND PrivateKey <> '' AND PublishTrustList='true' AND InsertingTrustList='false' AND (LastInsertedTrustList<='"+date.Format("%Y-%m-%d %H:%M:%S")+"' OR LastInsertedTrustList IS NULL);");
\r
27 if(rs.Empty()==false)
\r
29 StartInsert(rs.GetInt(0),rs.GetField(1));
\r
33 void TrustListInserter::FCPConnected()
\r
35 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false';");
\r
38 void TrustListInserter::FCPDisconnected()
\r
43 const bool TrustListInserter::HandleMessage(FCPMessage &message)
\r
46 if(message["Identifier"].find("TrustListInserter")==0)
\r
50 std::vector<std::string> idparts;
\r
53 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
55 // no action for URIGenerated
\r
56 if(message.GetName()=="URIGenerated")
\r
61 // no action for IdentifierCollision
\r
62 if(message.GetName()=="IdentifierCollision")
\r
67 if(message.GetName()=="PutSuccessful")
\r
69 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false', LastInsertedTrustList='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");
\r
70 m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
71 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage inserted TrustList xml");
\r
75 if(message.GetName()=="PutFailed")
\r
77 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false' WHERE LocalIdentityID="+idparts[1]+";");
\r
78 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage failure inserting TrustList xml. Code="+message["Code"]+" Description="+message["CodeDescription"]);
\r
80 // if code 9 (collision), then insert index into inserted table
\r
81 if(message["Code"]=="9")
\r
83 m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
94 void TrustListInserter::Initialize()
\r
96 Option::instance()->Get("MessageBase",m_messagebase);
\r
97 m_lastchecked.SetToGMTime();
\r
100 void TrustListInserter::Process()
\r
105 // check every minute
\r
106 if(m_lastchecked<=(now-(1.0/1440.0)))
\r
108 CheckForNeededInsert();
\r
113 void TrustListInserter::RegisterWithThread(FreenetMasterThread *thread)
\r
115 thread->RegisterFCPConnected(this);
\r
116 thread->RegisterFCPMessageHandler(this);
\r
117 thread->RegisterPeriodicProcessor(this);
\r
120 void TrustListInserter::StartInsert(const long localidentityid, const std::string &privatekey)
\r
122 FCPMessage message;
\r
125 std::string datasizestr;
\r
126 std::string publickey;
\r
128 int trustlisttrust;
\r
131 std::string indexstr;
\r
132 std::string localidentityidstr;
\r
136 // build the xml file
\r
137 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, LocalMessageTrust, LocalTrustListTrust FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey<>'';");
\r
139 while(st.RowReturned())
\r
141 st.ResultText(0,publickey);
\r
142 st.ResultInt(1,messagetrust);
\r
143 st.ResultInt(2,trustlisttrust);
\r
144 xml.AddTrust(publickey,messagetrust,trustlisttrust);
\r
148 // get next insert index
\r
149 st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblTrustListInserts WHERE LocalIdentityID=? AND Day=?;");
\r
150 st.Bind(0,localidentityid);
\r
151 st.Bind(1,now.Format("%Y-%m-%d"));
\r
155 if(st.RowReturned() && st.ResultNull(0)==false)
\r
157 st.ResultInt(0,index);
\r
161 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
162 StringFunctions::Convert(index,indexstr);
\r
165 StringFunctions::Convert(data.size(),datasizestr);
\r
167 message.SetName("ClientPut");
\r
168 message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
169 message["Identifier"]="TrustListInserter|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];
\r
170 message["UploadFrom"]="direct";
\r
171 message["DataLength"]=datasizestr;
\r
172 m_fcp->SendMessage(message);
\r
173 m_fcp->SendRaw(data.c_str(),data.size());
\r
175 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='true' WHERE LocalIdentityID="+localidentityidstr+";");
\r