version 0.0.2
[fms.git] / src / freenet / identityinserter.cpp
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
5 \r
6 #ifdef XMEM\r
7         #include <xmem.h>\r
8 #endif\r
9 \r
10 IdentityInserter::IdentityInserter()\r
11 {\r
12         Initialize();\r
13 }\r
14 \r
15 IdentityInserter::IdentityInserter(FCPv2 *fcp):IFCPConnected(fcp)\r
16 {\r
17         Initialize();\r
18 }\r
19 \r
20 void IdentityInserter::CheckForNeededInsert()\r
21 {\r
22         DateTime now;\r
23         DateTime date;\r
24         now.SetToGMTime();\r
25         date.SetToGMTime();\r
26         // set date to 1 hour back\r
27         date.Add(0,0,-1);\r
28 \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
31         {\r
32                 date=now;\r
33         }\r
34 \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
36         \r
37         if(rs.Empty()==false)\r
38         {\r
39                 StartInsert(rs.GetInt(0));\r
40         }\r
41 \r
42 }\r
43 \r
44 void IdentityInserter::FCPConnected()\r
45 {\r
46         m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false';");\r
47 }\r
48 \r
49 \r
50 void IdentityInserter::FCPDisconnected()\r
51 {\r
52         \r
53 }\r
54 \r
55 const bool IdentityInserter::HandleMessage(FCPMessage &message)\r
56 {\r
57 \r
58         if(message["Identifier"].find("IdentityInserter")==0)\r
59         {\r
60                 DateTime now;\r
61                 std::vector<std::string> idparts;\r
62 \r
63                 now.SetToGMTime();\r
64                 StringFunctions::Split(message["Identifier"],"|",idparts);\r
65 \r
66                 // no action for URIGenerated\r
67                 if(message.GetName()=="URIGenerated")\r
68                 {\r
69                         return true;\r
70                 }\r
71 \r
72                 // no action for IdentifierCollision\r
73                 if(message.GetName()=="IdentifierCollision")\r
74                 {\r
75                         return true;\r
76                 }\r
77 \r
78                 if(message.GetName()=="PutSuccessful")\r
79                 {\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
83                         return true;\r
84                 }\r
85 \r
86                 if(message.GetName()=="PutFailed")\r
87                 {\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
90                         \r
91                         // if code 9 (collision), then insert index into inserted table\r
92                         if(message["Code"]=="9")\r
93                         {\r
94                                 m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
95                         }\r
96                         \r
97                         return true;\r
98                 }\r
99 \r
100         }\r
101 \r
102         return false;\r
103 \r
104 }\r
105 \r
106 void IdentityInserter::Initialize()\r
107 {\r
108         m_lastchecked.SetToGMTime();\r
109 }\r
110 \r
111 void IdentityInserter::Process()\r
112 {\r
113         DateTime now;\r
114         now.SetToGMTime();\r
115 \r
116         if(m_lastchecked<(now-(1.0/1440.0)))\r
117         {\r
118                 CheckForNeededInsert();\r
119                 m_lastchecked=now;\r
120         }\r
121 \r
122 }\r
123 \r
124 void IdentityInserter::RegisterWithThread(FreenetMasterThread *thread)\r
125 {\r
126         thread->RegisterFCPConnected(this);\r
127         thread->RegisterFCPMessageHandler(this);\r
128         thread->RegisterPeriodicProcessor(this);\r
129 }\r
130 \r
131 void IdentityInserter::StartInsert(const long localidentityid)\r
132 {\r
133         DateTime date;\r
134         std::string idstring;\r
135 \r
136         StringFunctions::Convert(localidentityid,idstring);\r
137         date.SetToGMTime();\r
138 \r
139         SQLite3DB::Recordset rs=m_db->Query("SELECT Name,PrivateKey,SingleUse,PublishTrustList,PublishBoardList FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");\r
140 \r
141         if(rs.Empty()==false)\r
142         {\r
143                 IdentityXML idxml;\r
144                 FCPMessage mess;\r
145                 DateTime now;\r
146                 std::string messagebase;\r
147                 std::string data;\r
148                 std::string datasizestr;\r
149                 std::string privatekey;\r
150                 long index=0;\r
151                 std::string indexstr;\r
152                 std::string singleuse="false";\r
153                 std::string publishtrustlist="false";\r
154                 std::string publishboardlist="false";\r
155 \r
156                 now.SetToGMTime();\r
157 \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
160                 {\r
161                         if(rs2.GetField(0)==NULL)\r
162                         {\r
163                                 index=0;\r
164                         }\r
165                         else\r
166                         {\r
167                                 index=rs2.GetInt(0)+1;\r
168                         }\r
169                 }\r
170                 StringFunctions::Convert(index,indexstr);\r
171 \r
172                 Option::instance()->Get("MessageBase",messagebase);\r
173 \r
174                 if(rs.GetField(0))\r
175                 {\r
176                         idxml.SetName(rs.GetField(0));\r
177                 }\r
178 \r
179                 if(rs.GetField(1))\r
180                 {\r
181                         privatekey=rs.GetField(1);\r
182                 }\r
183 \r
184                 if(rs.GetField(2))\r
185                 {\r
186                         singleuse=rs.GetField(2);\r
187                 }\r
188                 singleuse=="true" ? idxml.SetSingleUse(true) : idxml.SetSingleUse(false);\r
189 \r
190                 if(rs.GetField(3))\r
191                 {\r
192                         publishtrustlist=rs.GetField(3);\r
193                 }\r
194                 publishtrustlist=="true" ? idxml.SetPublishTrustList(true) : idxml.SetPublishTrustList(false);\r
195 \r
196                 if(rs.GetField(4))\r
197                 {\r
198                         publishboardlist=rs.GetField(3);\r
199                 }\r
200                 publishboardlist=="true" ? idxml.SetPublishBoardList(true) : idxml.SetPublishBoardList(false);\r
201 \r
202                 data=idxml.GetXML();\r
203                 StringFunctions::Convert(data.size(),datasizestr);\r
204 \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
212 \r
213                 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='true' WHERE LocalIdentityID="+idstring+";");\r
214 \r
215         }\r
216 }\r