#include "../../include/freenet/messagexml.h"\r
#include "../../include/freenet/messagelistxml.h"\r
\r
+#include <Poco/DateTime.h>\r
+#include <Poco/Timespan.h>\r
+#include <Poco/DateTimeFormatter.h>\r
+\r
#ifdef XMEM\r
#include <xmem.h>\r
#endif\r
// only do 1 insert at a time\r
if(m_inserting.size()==0)\r
{\r
- DateTime now;\r
- DateTime previous;\r
+ std::string sql;\r
+ Poco::DateTime now;\r
+ Poco::DateTime previous;\r
+ bool startedinsert=false;\r
+\r
+ // reset the last inserted xml doc to nothing if the day has changed\r
+ if(m_lastchecked.day()!=now.day())\r
+ {\r
+ m_lastinsertedxml.clear();\r
+ }\r
\r
- now.SetToGMTime();\r
- previous.SetToGMTime();\r
+ previous-=Poco::Timespan(m_daysbackward,0,0,0,0);\r
\r
- previous.Add(0,0,0,-m_daysbackward);\r
+ // query for identities that have messages in the past X days and (we haven't inserted lists for in the past 30 minutes OR identity has a record in tmpMessageListInsert)\r
+ sql="SELECT tblLocalIdentity.LocalIdentityID ";\r
+ sql+="FROM tblLocalIdentity INNER JOIN tblMessageInserts ON tblLocalIdentity.LocalIdentityID=tblMessageInserts.LocalIdentityID ";\r
+ sql+="WHERE tblMessageInserts.Day>=? AND ((tblLocalIdentity.LastInsertedMessageList<=? OR tblLocalIdentity.LastInsertedMessageList IS NULL OR tblLocalIdentity.LastInsertedMessageList='') OR tblLocalIdentity.LocalIdentityID IN (SELECT LocalIdentityID FROM tmpMessageListInsert)) ";\r
+ sql+="GROUP BY tblLocalIdentity.LocalIdentityID;";\r
\r
- // query for identities that have messages in the past X days and we haven't inserted lists for in the past hour\r
- SQLite3DB::Statement st=m_db->Prepare("SELECT tblLocalIdentity.LocalIdentityID FROM tblLocalIdentity INNER JOIN tblMessageInserts ON tblLocalIdentity.LocalIdentityID=tblMessageInserts.LocalIdentityID WHERE tblMessageInserts.Day>=? AND (tblLocalIdentity.LastInsertedMessageList<=? OR tblLocalIdentity.LastInsertedMessageList IS NULL OR tblLocalIdentity.LastInsertedMessageList='');");\r
- st.Bind(0,previous.Format("%Y-%m-%d"));\r
- st.Bind(1,(now-(1.0/24.0)).Format("%Y-%m-%d %H:%M:%S"));\r
+ SQLite3DB::Statement st=m_db->Prepare(sql);\r
+ st.Bind(0,Poco::DateTimeFormatter::format(previous,"%Y-%m-%d"));\r
+ st.Bind(1,Poco::DateTimeFormatter::format((now-Poco::Timespan(0,0,30,0,0)),"%Y-%m-%d %H:%M:%S"));\r
st.Step();\r
\r
- if(st.RowReturned())\r
+ while(st.RowReturned() && startedinsert==false)\r
{\r
int localidentityid;\r
st.ResultInt(0,localidentityid);\r
- StartInsert(localidentityid);\r
+ startedinsert=StartInsert(localidentityid);\r
+ st.Step();\r
}\r
}\r
\r
long index;\r
\r
StringFunctions::Split(message["Identifier"],"|",idparts);\r
- StringFunctions::Convert(idparts[1],localidentityid);\r
- StringFunctions::Convert(idparts[2],index);\r
\r
- if(message["Fatal"]=="true" || message["Code"]=="9")\r
+ // non USK\r
+ if(idparts[0]==m_fcpuniquename)\r
{\r
- SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'false');");\r
- st.Bind(0,localidentityid);\r
- st.Bind(1,idparts[4]);\r
- st.Bind(2,index);\r
- st.Step();\r
- }\r
+ StringFunctions::Convert(idparts[1],localidentityid);\r
+ StringFunctions::Convert(idparts[2],index);\r
\r
- RemoveFromInsertList(localidentityid);\r
+ if(message["Fatal"]=="true" || message["Code"]=="9")\r
+ {\r
+ SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'false');");\r
+ st.Bind(0,localidentityid);\r
+ st.Bind(1,idparts[4]);\r
+ st.Bind(2,index);\r
+ st.Step();\r
+ }\r
+\r
+ RemoveFromInsertList(localidentityid);\r
+\r
+ // reset the last inserted xml doc to nothing so we will try to insert this one again\r
+ m_lastinsertedxml[localidentityid]="";\r
+ }\r
+ else\r
+ {\r
+ m_log->debug("MessageListInserter::HandlePutFailed "+message["Identifier"]);\r
+ }\r
\r
return true;\r
\r
\r
const bool MessageListInserter::HandlePutSuccessful(FCPMessage &message)\r
{\r
- DateTime now;\r
+ Poco::DateTime now;\r
std::vector<std::string> idparts;\r
long localidentityid;\r
long index;\r
\r
StringFunctions::Split(message["Identifier"],"|",idparts);\r
- StringFunctions::Convert(idparts[1],localidentityid);\r
- StringFunctions::Convert(idparts[2],index);\r
\r
- SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'true');");\r
- st.Bind(0,localidentityid);\r
- st.Bind(1,idparts[4]);\r
- st.Bind(2,index);\r
- st.Step();\r
+ // non USK\r
+ if(idparts[0]==m_fcpuniquename)\r
+ {\r
+ StringFunctions::Convert(idparts[1],localidentityid);\r
+ StringFunctions::Convert(idparts[2],index);\r
\r
- now.SetToGMTime();\r
- st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedMessageList=? WHERE LocalIdentityID=?;");\r
- st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));\r
- st.Bind(1,localidentityid);\r
- st.Step();\r
+ SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'true');");\r
+ st.Bind(0,localidentityid);\r
+ st.Bind(1,idparts[4]);\r
+ st.Bind(2,index);\r
+ st.Step();\r
+\r
+ now=Poco::Timestamp();\r
+ st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedMessageList=? WHERE LocalIdentityID=?;");\r
+ st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));\r
+ st.Bind(1,localidentityid);\r
+ st.Step();\r
+\r
+ // delete any record from tmpMessageListInsert\r
+ st=m_db->Prepare("DELETE FROM tmpMessageListInsert WHERE LocalIdentityID=?;");\r
+ st.Bind(0,localidentityid);\r
+ st.Step();\r
\r
- RemoveFromInsertList(localidentityid);\r
+ RemoveFromInsertList(localidentityid);\r
+\r
+ m_log->debug("MessageListInserter::HandlePutSuccessful successfully inserted MessageList.");\r
+ }\r
+ else\r
+ {\r
+ m_log->debug("MessageListInserter::HandlePutSuccessful inserted USK MessageList "+message["Identifier"]);\r
+ }\r
\r
return true;\r
}\r
\r
m_fcpuniquename="MessageListInserter";\r
m_daysbackward=0;\r
- Option::instance()->Get("MessageListDaysBackward",tempval);\r
+ Option::Instance()->Get("MessageListDaysBackward",tempval);\r
StringFunctions::Convert(tempval,m_daysbackward);\r
}\r
\r
-void MessageListInserter::StartInsert(const long &localidentityid)\r
+const bool MessageListInserter::StartInsert(const long &localidentityid)\r
{\r
FCPMessage message;\r
- DateTime date;\r
- DateTime now;\r
+ Poco::DateTime date;\r
+ Poco::DateTime now;\r
std::string privatekey;\r
std::string localidentityidstr;\r
MessageListXML mlxml;\r
int index;\r
std::string indexstr;\r
\r
- now.SetToGMTime();\r
- date.SetToGMTime();\r
- date.Add(0,0,0,-m_daysbackward);\r
+ date-=Poco::Timespan(m_daysbackward,0,0,0,0);\r
StringFunctions::Convert(localidentityid,localidentityidstr);\r
\r
SQLite3DB::Statement st=m_db->Prepare("SELECT Day, InsertIndex, MessageXML, PrivateKey FROM tblMessageInserts INNER JOIN tblLocalIdentity ON tblMessageInserts.LocalIdentityID=tblLocalIdentity.LocalIdentityID WHERE tblLocalIdentity.LocalIdentityID=? AND Day>=?;");\r
st.Bind(0,localidentityid);\r
- st.Bind(1,date.Format("%Y-%m-%d"));\r
+ st.Bind(1,Poco::DateTimeFormatter::format(date,"%Y-%m-%d"));\r
st.Step();\r
\r
while(st.RowReturned())\r
}\r
st.Finalize();\r
\r
+\r
+ st=m_db->Prepare("SELECT MessageDate, MessageIndex, PublicKey, MessageID FROM tblMessage INNER JOIN tblIdentity ON tblMessage.IdentityID=tblIdentity.IdentityID WHERE MessageIndex IS NOT NULL ORDER BY MessageDate DESC, MessageTime DESC LIMIT 200;");\r
+ SQLite3DB::Statement st2=m_db->Prepare("SELECT BoardName FROM tblBoard INNER JOIN tblMessageBoard ON tblBoard.BoardID=tblMessageBoard.BoardID WHERE tblMessageBoard.MessageID=?;");\r
+ st.Step();\r
+ while(st.RowReturned())\r
+ {\r
+ std::string day;\r
+ int index;\r
+ std::string publickey;\r
+ std::vector<std::string> boardlist;\r
+ int messageid;\r
+ \r
+ st.ResultText(0,day);\r
+ st.ResultInt(1,index);\r
+ st.ResultText(2,publickey);\r
+ st.ResultInt(3,messageid);\r
+\r
+ st2.Bind(0,messageid);\r
+ st2.Step();\r
+ while(st2.RowReturned())\r
+ {\r
+ std::string boardname="";\r
+ st2.ResultText(0,boardname);\r
+ StringFunctions::LowerCase(boardname,boardname);\r
+ boardlist.push_back(boardname);\r
+ st2.Step();\r
+ }\r
+ st2.Reset();\r
+\r
+ mlxml.AddExternalMessage(publickey,day,index,boardlist);\r
+\r
+ st.Step();\r
+ }\r
+\r
// get last inserted messagelist index for this day\r
index=0;\r
st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblMessageListInserts WHERE LocalIdentityID=? AND Day=?;");\r
st.Bind(0,localidentityid);\r
- st.Bind(1,now.Format("%Y-%m-%d"));\r
+ st.Bind(1,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));\r
st.Step();\r
if(st.ResultNull(0)==false)\r
{\r
}\r
StringFunctions::Convert(index,indexstr);\r
\r
- // actually insert message\r
xmlstr=mlxml.GetXML();\r
- StringFunctions::Convert(xmlstr.size(),xmlsizestr);\r
-\r
- message.SetName("ClientPut");\r
- message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|MessageList|"+indexstr+".xml";\r
- message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];\r
- message["UploadFrom"]="direct";\r
- message["DataLength"]=xmlsizestr;\r
- m_fcp->SendMessage(message);\r
- m_fcp->SendRaw(xmlstr.c_str(),xmlstr.size());\r
\r
- m_inserting.push_back(localidentityid);\r
+ // only insert if the last message this identity inserted is different than this message\r
+ if(m_lastinsertedxml[localidentityid]!=xmlstr)\r
+ {\r
+ std::string targeturi="";\r
+ StringFunctions::Convert(xmlstr.size(),xmlsizestr);\r
+\r
+ message.SetName("ClientPut");\r
+ message["URI"]=privatekey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|MessageList|"+indexstr+".xml";\r
+ message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];\r
+ message["UploadFrom"]="direct";\r
+ message["DataLength"]=xmlsizestr;\r
+ m_fcp->SendMessage(message);\r
+ m_fcp->SendRaw(xmlstr.c_str(),xmlstr.size());\r
+\r
+ message.Reset();\r
+ message.SetName("ClientPutComplexDir");\r
+ message["URI"]="USK"+privatekey.substr(3)+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y.%m.%d")+"|MessageList/0/";\r
+ message["Identifier"]=m_fcpuniquename+"USK|"+message["URI"];\r
+ message["DefaultName"]="MessageList.xml";\r
+ message["Files.0.Name"]="MessageList.xml";\r
+ message["Files.0.UploadFrom"]="direct";\r
+ message["Files.0.DataLength"]=xmlsizestr;\r
+ m_fcp->SendMessage(message);\r
+ m_fcp->SendRaw(xmlstr.c_str(),xmlstr.size());\r
+\r
+ m_inserting.push_back(localidentityid);\r
+ m_lastinsertedxml[localidentityid]=xmlstr;\r
+\r
+ return true;\r
+ }\r
+ else\r
+ {\r
+ return false;\r
+ }\r
\r
}\r