version 0.1.2
[fms.git] / src / freenet / trustlistinserter.cpp
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
5 \r
6 #ifdef XMEM\r
7         #include <xmem.h>\r
8 #endif\r
9 \r
10 TrustListInserter::TrustListInserter()\r
11 {\r
12         Initialize();\r
13 }\r
14 \r
15 TrustListInserter::TrustListInserter(FCPv2 *fcp):IFCPConnected(fcp)\r
16 {\r
17         Initialize();\r
18 }\r
19 \r
20 void TrustListInserter::CheckForNeededInsert()\r
21 {\r
22         DateTime date;\r
23         date.SetToGMTime();\r
24         date.Add(0,0,-1);\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
26 \r
27         if(rs.Empty()==false)\r
28         {\r
29                 StartInsert(rs.GetInt(0),rs.GetField(1));\r
30         }\r
31 }\r
32 \r
33 void TrustListInserter::FCPConnected()\r
34 {\r
35         m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false';");\r
36 }\r
37 \r
38 void TrustListInserter::FCPDisconnected()\r
39 {\r
40 \r
41 }\r
42 \r
43 const bool TrustListInserter::HandleMessage(FCPMessage &message)\r
44 {\r
45 \r
46         if(message["Identifier"].find("TrustListInserter")==0)\r
47         {\r
48                 \r
49                 DateTime now;\r
50                 std::vector<std::string> idparts;\r
51 \r
52                 now.SetToGMTime();\r
53                 StringFunctions::Split(message["Identifier"],"|",idparts);\r
54 \r
55                 // no action for URIGenerated\r
56                 if(message.GetName()=="URIGenerated")\r
57                 {\r
58                         return true;\r
59                 }\r
60 \r
61                 // no action for IdentifierCollision\r
62                 if(message.GetName()=="IdentifierCollision")\r
63                 {\r
64                         return true;\r
65                 }\r
66 \r
67                 if(message.GetName()=="PutSuccessful")\r
68                 {\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
72                         return true;\r
73                 }\r
74 \r
75                 if(message.GetName()=="PutFailed")\r
76                 {\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
79                         \r
80                         // if code 9 (collision), then insert index into inserted table\r
81                         if(message["Code"]=="9")\r
82                         {\r
83                                 m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
84                         }\r
85                         \r
86                         return true;\r
87                 }\r
88 \r
89         }\r
90 \r
91         return false;\r
92 }\r
93 \r
94 void TrustListInserter::Initialize()\r
95 {\r
96         Option::instance()->Get("MessageBase",m_messagebase);\r
97         m_lastchecked.SetToGMTime();\r
98 }\r
99 \r
100 void TrustListInserter::Process()\r
101 {\r
102         DateTime now;\r
103         now.SetToGMTime();\r
104 \r
105         // check every minute\r
106         if(m_lastchecked<=(now-(1.0/1440.0)))\r
107         {\r
108                 CheckForNeededInsert();\r
109                 m_lastchecked=now;\r
110         }\r
111 }\r
112 \r
113 void TrustListInserter::RegisterWithThread(FreenetMasterThread *thread)\r
114 {\r
115         thread->RegisterFCPConnected(this);\r
116         thread->RegisterFCPMessageHandler(this);\r
117         thread->RegisterPeriodicProcessor(this);\r
118 }\r
119 \r
120 void TrustListInserter::StartInsert(const long localidentityid, const std::string &privatekey)\r
121 {\r
122         FCPMessage message;\r
123         TrustListXML xml;\r
124         std::string data;\r
125         std::string datasizestr;\r
126         std::string publickey;\r
127         int messagetrust;\r
128         int trustlisttrust;\r
129         DateTime now,date;\r
130         int index;\r
131         std::string indexstr;\r
132         std::string localidentityidstr;\r
133 \r
134         now.SetToGMTime();\r
135         date.SetToGMTime();\r
136 \r
137         // build the xml file - we only want to add identities that we recently saw, otherwise we could be inserting a ton of identities\r
138         date.Add(0,0,0,-20);    // identities seen in last 20 days\r
139         SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, LocalMessageTrust, LocalTrustListTrust FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LastSeen>=?;");\r
140         st.Bind(0,date.Format("%Y-%m-%d"));\r
141         st.Step();\r
142         while(st.RowReturned())\r
143         {\r
144                 st.ResultText(0,publickey);\r
145                 st.ResultInt(1,messagetrust);\r
146                 st.ResultInt(2,trustlisttrust);\r
147                 xml.AddTrust(publickey,messagetrust,trustlisttrust);\r
148                 st.Step();\r
149         }\r
150 \r
151         // get next insert index\r
152         st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblTrustListInserts WHERE LocalIdentityID=? AND Day=?;");\r
153         st.Bind(0,localidentityid);\r
154         st.Bind(1,now.Format("%Y-%m-%d"));\r
155         st.Step();\r
156 \r
157         index=0;\r
158         if(st.RowReturned() && st.ResultNull(0)==false)\r
159         {\r
160                 st.ResultInt(0,index);\r
161                 index++;\r
162         }\r
163 \r
164         StringFunctions::Convert(localidentityid,localidentityidstr);\r
165         StringFunctions::Convert(index,indexstr);\r
166 \r
167         data=xml.GetXML();\r
168         StringFunctions::Convert(data.size(),datasizestr);\r
169 \r
170         message.SetName("ClientPut");\r
171         message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";\r
172         message["Identifier"]="TrustListInserter|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];\r
173         message["UploadFrom"]="direct";\r
174         message["DataLength"]=datasizestr;\r
175         m_fcp->SendMessage(message);\r
176         m_fcp->SendRaw(data.c_str(),data.size());\r
177 \r
178         m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='true' WHERE LocalIdentityID="+localidentityidstr+";");\r
179 \r
180 }\r