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
6 #include <Poco/DateTimeFormatter.h>
\r
7 #include <Poco/DateTimeParser.h>
\r
13 IdentityInserter::IdentityInserter(SQLite3DB::DB *db):IDatabase(db)
\r
18 IdentityInserter::IdentityInserter(SQLite3DB::DB *db, FCPv2::Connection *fcp):IDatabase(db),IFCPConnected(fcp)
\r
23 void IdentityInserter::CheckForNeededInsert()
\r
26 Poco::DateTime date;
\r
28 // set date to 1 hour back
\r
29 date-=Poco::Timespan(0,1,0,0,0);
\r
31 // 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
32 if(date.day()!=now.day())
\r
35 date.assign(date.year(),date.month(),date.day(),0,0,0);
\r
38 SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PrivateKey IS NOT NULL AND PrivateKey <> '' AND InsertingIdentity='false' AND (LastInsertedIdentity<'"+Poco::DateTimeFormatter::format(date,"%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(FCPv2::Message &message)
\r
61 if(message["Identifier"].find("IdentityInserter")==0)
\r
64 std::vector<std::string> idparts;
\r
66 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
68 // no action for URIGenerated
\r
69 if(message.GetName()=="URIGenerated")
\r
74 // no action for IdentifierCollision
\r
75 if(message.GetName()=="IdentifierCollision")
\r
80 if(message.GetName()=="PutSuccessful")
\r
83 // do check to make sure this is the non-editioned SSK - we ignore failure/success for editioned SSK for now
\r
84 if(message["Identifier"].find(".xml")!=std::string::npos)
\r
86 // 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
87 // If this is the case, we will skip updating LastInsertedIdentity so that we can insert this identity again for today
\r
88 Poco::DateTime lastdate;
\r
90 Poco::DateTimeParser::tryParse("%Y-%m-%d",idparts[4],lastdate,tzdiff);
\r
91 if(lastdate.day()==now.day())
\r
93 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false', LastInsertedIdentity='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");
\r
97 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");
\r
99 m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
100 m_log->debug("IdentityInserter::HandleMessage inserted Identity xml");
\r
104 m_log->trace("IdentityInserter::HandleMessage inserted editioned Identity xml");
\r
109 if(message.GetName()=="PutFailed")
\r
111 // do check to make sure this is the non-editioned SSK - we ignore failure/success for editioned SSK for now
\r
112 if(message["Identifier"].find(".xml")!=std::string::npos)
\r
114 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");
\r
115 m_log->debug("IdentityInserter::HandleMessage failure inserting Identity xml. Code="+message["Code"]+" Description="+message["CodeDescription"]);
\r
117 // if code 9 (collision), then insert index into inserted table
\r
118 if(message["Code"]=="9")
\r
120 m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
125 m_log->trace("IdentityInserter::HandleMessage PutFailed for editioned SSK error code "+message["Code"]+ " id "+message["Identifier"]);
\r
137 void IdentityInserter::Initialize()
\r
139 m_lastchecked=Poco::Timestamp();
\r
142 void IdentityInserter::Process()
\r
144 Poco::DateTime now;
\r
146 if(m_lastchecked<(now-Poco::Timespan(0,0,1,0,0)))
\r
148 CheckForNeededInsert();
\r
154 void IdentityInserter::RegisterWithThread(FreenetMasterThread *thread)
\r
156 thread->RegisterFCPConnected(this);
\r
157 thread->RegisterFCPMessageHandler(this);
\r
158 thread->RegisterPeriodicProcessor(this);
\r
161 void IdentityInserter::StartInsert(const long localidentityid)
\r
163 Poco::DateTime date;
\r
164 std::string idstring;
\r
166 StringFunctions::Convert(localidentityid,idstring);
\r
168 SQLite3DB::Recordset rs=m_db->Query("SELECT Name,PrivateKey,SingleUse,PublishTrustList,PublishBoardList,PublishFreesite,FreesiteEdition FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");
\r
170 if(rs.Empty()==false)
\r
173 FCPv2::Message mess;
\r
174 Poco::DateTime now;
\r
175 std::string messagebase;
\r
177 std::string datasizestr;
\r
178 std::string privatekey;
\r
180 std::string indexstr;
\r
181 std::string singleuse="false";
\r
182 std::string publishtrustlist="false";
\r
183 std::string publishboardlist="false";
\r
184 std::string freesiteedition="";
\r
187 now=Poco::Timestamp();
\r
189 SQLite3DB::Recordset rs2=m_db->Query("SELECT MAX(InsertIndex) FROM tblLocalIdentityInserts WHERE LocalIdentityID="+idstring+" AND Day='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"';");
\r
190 if(rs2.Empty()==false)
\r
192 if(rs2.GetField(0)==NULL)
\r
198 index=rs2.GetInt(0)+1;
\r
201 StringFunctions::Convert(index,indexstr);
\r
203 Option option(m_db);
\r
204 option.Get("MessageBase",messagebase);
\r
208 idxml.SetName(rs.GetField(0));
\r
213 privatekey=rs.GetField(1);
\r
218 singleuse=rs.GetField(2);
\r
220 singleuse=="true" ? idxml.SetSingleUse(true) : idxml.SetSingleUse(false);
\r
224 publishtrustlist=rs.GetField(3);
\r
226 publishtrustlist=="true" ? idxml.SetPublishTrustList(true) : idxml.SetPublishTrustList(false);
\r
230 publishboardlist=rs.GetField(4);
\r
232 publishboardlist=="true" ? idxml.SetPublishBoardList(true) : idxml.SetPublishBoardList(false);
\r
234 if(rs.GetField(5) && rs.GetField(6))
\r
236 if(std::string(rs.GetField(5))=="true")
\r
238 freesiteedition=rs.GetField(6);
\r
239 StringFunctions::Convert(freesiteedition,edition);
\r
240 idxml.SetFreesiteEdition(edition);
\r
244 data=idxml.GetXML();
\r
245 StringFunctions::Convert(data.size(),datasizestr);
\r
247 mess.SetName("ClientPut");
\r
248 mess["URI"]=privatekey+messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|Identity|"+indexstr+".xml";
\r
249 mess["Identifier"]="IdentityInserter|"+idstring+"|"+indexstr+"|"+mess["URI"];
\r
250 mess["UploadFrom"]="direct";
\r
251 mess["DataLength"]=datasizestr;
\r
253 m_fcp->Send(std::vector<char>(data.begin(),data.end()));
\r
255 // test insert as editioned SSK
\r
257 mess.SetName("ClientPut");
\r
258 mess["URI"]=privatekey+messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|Identity-"+indexstr;
\r
259 mess["Identifier"]="IdentityInserter|"+mess["URI"];
\r
260 mess["UploadFrom"]="direct";
\r
261 mess["DataLength"]=datasizestr;
\r
263 m_fcp->Send(std::vector<char>(data.begin(),data.end()));
\r
265 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='true' WHERE LocalIdentityID="+idstring+";");
\r