15e52aa1aba0b57cfde548c27cc170167f09002b
[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 #include <Poco/DateTimeFormatter.h>\r
7 #include <Poco/DateTimeParser.h>\r
8 \r
9 #ifdef XMEM\r
10         #include <xmem.h>\r
11 #endif\r
12 \r
13 IdentityInserter::IdentityInserter()\r
14 {\r
15         Initialize();\r
16 }\r
17 \r
18 IdentityInserter::IdentityInserter(FCPv2::Connection *fcp):IFCPConnected(fcp)\r
19 {\r
20         Initialize();\r
21 }\r
22 \r
23 void IdentityInserter::CheckForNeededInsert()\r
24 {\r
25         Poco::DateTime now;\r
26         Poco::DateTime date;\r
27 \r
28         // set date to 1 hour back\r
29         date-=Poco::Timespan(0,1,0,0,0);\r
30 \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
33         {\r
34                 date=now;\r
35                 date.assign(date.year(),date.month(),date.day(),0,0,0);\r
36         }\r
37 \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
39         \r
40         if(rs.Empty()==false)\r
41         {\r
42                 StartInsert(rs.GetInt(0));\r
43         }\r
44 \r
45 }\r
46 \r
47 void IdentityInserter::FCPConnected()\r
48 {\r
49         m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false';");\r
50 }\r
51 \r
52 \r
53 void IdentityInserter::FCPDisconnected()\r
54 {\r
55         \r
56 }\r
57 \r
58 const bool IdentityInserter::HandleMessage(FCPv2::Message &message)\r
59 {\r
60 \r
61         if(message["Identifier"].find("IdentityInserter")==0)\r
62         {\r
63                 Poco::DateTime now;\r
64                 std::vector<std::string> idparts;\r
65 \r
66                 StringFunctions::Split(message["Identifier"],"|",idparts);\r
67 \r
68                 // no action for URIGenerated\r
69                 if(message.GetName()=="URIGenerated")\r
70                 {\r
71                         return true;\r
72                 }\r
73 \r
74                 // no action for IdentifierCollision\r
75                 if(message.GetName()=="IdentifierCollision")\r
76                 {\r
77                         return true;\r
78                 }\r
79 \r
80                 if(message.GetName()=="PutSuccessful")\r
81                 {\r
82                         // 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
83                         // If this is the case, we will skip updating LastInsertedIdentity so that we can insert this identity again for today\r
84                         Poco::DateTime lastdate;\r
85                         int tzdiff=0;\r
86                         Poco::DateTimeParser::tryParse("%Y-%m-%d",idparts[4],lastdate,tzdiff);\r
87 \r
88                         if(lastdate.day()==now.day())\r
89                         {\r
90                                 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false', LastInsertedIdentity='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");\r
91                         }\r
92                         else\r
93                         {\r
94                                 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");\r
95                         }\r
96                         m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
97                         m_log->debug("IdentityInserter::HandleMessage inserted Identity xml");\r
98                         return true;\r
99                 }\r
100 \r
101                 if(message.GetName()=="PutFailed")\r
102                 {\r
103                         m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");\r
104                         m_log->debug("IdentityInserter::HandleMessage failure inserting Identity xml.  Code="+message["Code"]+" Description="+message["CodeDescription"]);\r
105                         \r
106                         // if code 9 (collision), then insert index into inserted table\r
107                         if(message["Code"]=="9")\r
108                         {\r
109                                 m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
110                         }\r
111                         \r
112                         return true;\r
113                 }\r
114 \r
115         }\r
116 \r
117         return false;\r
118 \r
119 }\r
120 \r
121 void IdentityInserter::Initialize()\r
122 {\r
123         m_lastchecked=Poco::Timestamp();\r
124 }\r
125 \r
126 void IdentityInserter::Process()\r
127 {\r
128         Poco::DateTime now;\r
129 \r
130         if(m_lastchecked<(now-Poco::Timespan(0,0,1,0,0)))\r
131         {\r
132                 CheckForNeededInsert();\r
133                 m_lastchecked=now;\r
134         }\r
135 \r
136 }\r
137 \r
138 void IdentityInserter::RegisterWithThread(FreenetMasterThread *thread)\r
139 {\r
140         thread->RegisterFCPConnected(this);\r
141         thread->RegisterFCPMessageHandler(this);\r
142         thread->RegisterPeriodicProcessor(this);\r
143 }\r
144 \r
145 void IdentityInserter::StartInsert(const long localidentityid)\r
146 {\r
147         Poco::DateTime date;\r
148         std::string idstring;\r
149 \r
150         StringFunctions::Convert(localidentityid,idstring);\r
151 \r
152         SQLite3DB::Recordset rs=m_db->Query("SELECT Name,PrivateKey,SingleUse,PublishTrustList,PublishBoardList,PublishFreesite,FreesiteEdition FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");\r
153 \r
154         if(rs.Empty()==false)\r
155         {\r
156                 IdentityXML idxml;\r
157                 FCPv2::Message mess;\r
158                 Poco::DateTime now;\r
159                 std::string messagebase;\r
160                 std::string data;\r
161                 std::string datasizestr;\r
162                 std::string privatekey;\r
163                 long index=0;\r
164                 std::string indexstr;\r
165                 std::string singleuse="false";\r
166                 std::string publishtrustlist="false";\r
167                 std::string publishboardlist="false";\r
168                 std::string freesiteedition="";\r
169                 int edition=-1;\r
170 \r
171                 now=Poco::Timestamp();\r
172 \r
173                 SQLite3DB::Recordset rs2=m_db->Query("SELECT MAX(InsertIndex) FROM tblLocalIdentityInserts WHERE LocalIdentityID="+idstring+" AND Day='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"';");\r
174                 if(rs2.Empty()==false)\r
175                 {\r
176                         if(rs2.GetField(0)==NULL)\r
177                         {\r
178                                 index=0;\r
179                         }\r
180                         else\r
181                         {\r
182                                 index=rs2.GetInt(0)+1;\r
183                         }\r
184                 }\r
185                 StringFunctions::Convert(index,indexstr);\r
186 \r
187                 Option::Instance()->Get("MessageBase",messagebase);\r
188 \r
189                 if(rs.GetField(0))\r
190                 {\r
191                         idxml.SetName(rs.GetField(0));\r
192                 }\r
193 \r
194                 if(rs.GetField(1))\r
195                 {\r
196                         privatekey=rs.GetField(1);\r
197                 }\r
198 \r
199                 if(rs.GetField(2))\r
200                 {\r
201                         singleuse=rs.GetField(2);\r
202                 }\r
203                 singleuse=="true" ? idxml.SetSingleUse(true) : idxml.SetSingleUse(false);\r
204 \r
205                 if(rs.GetField(3))\r
206                 {\r
207                         publishtrustlist=rs.GetField(3);\r
208                 }\r
209                 publishtrustlist=="true" ? idxml.SetPublishTrustList(true) : idxml.SetPublishTrustList(false);\r
210 \r
211                 if(rs.GetField(4))\r
212                 {\r
213                         publishboardlist=rs.GetField(4);\r
214                 }\r
215                 publishboardlist=="true" ? idxml.SetPublishBoardList(true) : idxml.SetPublishBoardList(false);\r
216 \r
217                 if(rs.GetField(5) && rs.GetField(6))\r
218                 {\r
219                         if(std::string(rs.GetField(5))=="true")\r
220                         {\r
221                                 freesiteedition=rs.GetField(6);\r
222                                 StringFunctions::Convert(freesiteedition,edition);\r
223                                 idxml.SetFreesiteEdition(edition);\r
224                         }\r
225                 }\r
226 \r
227                 data=idxml.GetXML();\r
228                 StringFunctions::Convert(data.size(),datasizestr);\r
229 \r
230                 mess.SetName("ClientPut");\r
231                 mess["URI"]=privatekey+messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|Identity|"+indexstr+".xml";\r
232                 mess["Identifier"]="IdentityInserter|"+idstring+"|"+indexstr+"|"+mess["URI"];\r
233                 mess["UploadFrom"]="direct";\r
234                 mess["DataLength"]=datasizestr;\r
235                 m_fcp->Send(mess);\r
236                 m_fcp->Send(std::vector<char>(data.begin(),data.end()));\r
237 \r
238                 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='true' WHERE LocalIdentityID="+idstring+";");\r
239 \r
240         }\r
241 }\r