33a80854c820e037d8bb1834ba56e79f10826606
[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::FCPConnected()\r
21 {\r
22         m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false';");\r
23 }\r
24 \r
25 void IdentityInserter::CheckForNeededInsert()\r
26 {\r
27         DateTime date;\r
28         date.SetToGMTime();\r
29         // set date to 1 hour back\r
30         date.Add(0,0,-1);\r
31 \r
32         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
33         \r
34         if(rs.Empty()==false)\r
35         {\r
36                 StartInsert(rs.GetInt(0));\r
37         }\r
38 \r
39 }\r
40 \r
41 void IdentityInserter::FCPDisconnected()\r
42 {\r
43         \r
44 }\r
45 \r
46 const bool IdentityInserter::HandleMessage(FCPMessage &message)\r
47 {\r
48 \r
49         if(message["Identifier"].find("IdentityInserter")==0)\r
50         {\r
51                 DateTime now;\r
52                 std::vector<std::string> idparts;\r
53 \r
54                 now.SetToGMTime();\r
55                 StringFunctions::Split(message["Identifier"],"|",idparts);\r
56 \r
57                 // no action for URIGenerated\r
58                 if(message.GetName()=="URIGenerated")\r
59                 {\r
60                         return true;\r
61                 }\r
62 \r
63                 // no action for IdentifierCollision\r
64                 if(message.GetName()=="IdentifierCollision")\r
65                 {\r
66                         return true;\r
67                 }\r
68 \r
69                 if(message.GetName()=="PutSuccessful")\r
70                 {\r
71                         m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false', LastInsertedIdentity='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");\r
72                         m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
73                         m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" inserted identity xml");\r
74                         return true;\r
75                 }\r
76 \r
77                 if(message.GetName()=="PutFailed")\r
78                 {\r
79                         m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='false' WHERE LocalIdentityID="+idparts[1]+";");\r
80                         m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" failure inserting identity xml.  Code="+message["Code"]+" Description="+message["CodeDescription"]);\r
81                         \r
82                         // if code 9 (collision), then insert index into inserted table\r
83                         if(message["Code"]=="9")\r
84                         {\r
85                                 m_db->Execute("INSERT INTO tblLocalIdentityInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
86                         }\r
87                         \r
88                         return true;\r
89                 }\r
90 \r
91         }\r
92 \r
93         return false;\r
94 \r
95 }\r
96 \r
97 void IdentityInserter::Initialize()\r
98 {\r
99         m_lastchecked.SetToGMTime();\r
100 }\r
101 \r
102 void IdentityInserter::Process()\r
103 {\r
104         DateTime now;\r
105         now.SetToGMTime();\r
106 \r
107         if(m_lastchecked<(now-(1.0/1440.0)))\r
108         {\r
109                 CheckForNeededInsert();\r
110                 m_lastchecked=now;\r
111         }\r
112 \r
113 }\r
114 \r
115 void IdentityInserter::RegisterWithThread(FreenetMasterThread *thread)\r
116 {\r
117         thread->RegisterFCPConnected(this);\r
118         thread->RegisterFCPMessageHandler(this);\r
119         thread->RegisterPeriodicProcessor(this);\r
120 }\r
121 \r
122 void IdentityInserter::StartInsert(const long localidentityid)\r
123 {\r
124         DateTime date;\r
125         std::string idstring;\r
126 \r
127         StringFunctions::Convert(localidentityid,idstring);\r
128         date.SetToGMTime();\r
129 \r
130         SQLite3DB::Recordset rs=m_db->Query("SELECT Name,PrivateKey,SingleUse FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");\r
131 \r
132         if(rs.Empty()==false)\r
133         {\r
134                 IdentityXML idxml;\r
135                 FCPMessage mess;\r
136                 DateTime now;\r
137                 std::string messagebase;\r
138                 std::string data;\r
139                 std::string datasizestr;\r
140                 std::string privatekey;\r
141                 long index=0;\r
142                 std::string indexstr;\r
143                 std::string singleuse="false";\r
144 \r
145                 now.SetToGMTime();\r
146 \r
147                 SQLite3DB::Recordset rs2=m_db->Query("SELECT MAX(InsertIndex) FROM tblLocalIdentityInserts WHERE LocalIdentityID="+idstring+" AND Day='"+now.Format("%Y-%m-%d")+"';");\r
148                 if(rs2.Empty()==false)\r
149                 {\r
150                         if(rs2.GetField(0)==NULL)\r
151                         {\r
152                                 index=0;\r
153                         }\r
154                         else\r
155                         {\r
156                                 index=rs2.GetInt(0)+1;\r
157                         }\r
158                 }\r
159                 StringFunctions::Convert(index,indexstr);\r
160 \r
161                 Option::instance()->Get("MessageBase",messagebase);\r
162 \r
163                 if(rs.GetField(0))\r
164                 {\r
165                         idxml.SetName(rs.GetField(0));\r
166                 }\r
167 \r
168                 if(rs.GetField(1))\r
169                 {\r
170                         privatekey=rs.GetField(1);\r
171                 }\r
172 \r
173                 if(rs.GetField(2))\r
174                 {\r
175                         singleuse=rs.GetField(2);\r
176                 }\r
177                 singleuse=="true" ? idxml.SetSingleUse(true) : idxml.SetSingleUse(false);\r
178 \r
179                 data=idxml.GetXML();\r
180                 StringFunctions::Convert(data.size(),datasizestr);\r
181 \r
182                 mess.SetName("ClientPut");\r
183                 mess["URI"]=privatekey+messagebase+"|"+now.Format("%Y-%m-%d")+"|Identity|"+indexstr+".xml";\r
184                 mess["Identifier"]="IdentityInserter|"+idstring+"|"+indexstr+"|"+mess["URI"];\r
185                 mess["UploadFrom"]="direct";\r
186                 mess["DataLength"]=datasizestr;\r
187                 m_fcp->SendMessage(mess);\r
188                 m_fcp->SendRaw(data.c_str(),data.size());\r
189 \r
190                 m_db->Execute("UPDATE tblLocalIdentity SET InsertingIdentity='true' WHERE LocalIdentityID="+idstring+";");\r
191 \r
192         }\r
193 }\r