1 #include "../../include/freenet/fmsversionrequester.h"
\r
2 #include "../../include/freenet/fmsversionxml.h"
\r
3 #include "../../include/option.h"
\r
4 #include "../../include/stringfunctions.h"
\r
6 #include <Poco/Timestamp.h>
\r
7 #include <Poco/Timespan.h>
\r
9 FMSVersionRequester::FMSVersionRequester()
\r
14 FMSVersionRequester::FMSVersionRequester(FCPv2 *fcp):IFCPConnected(fcp)
\r
19 const bool FMSVersionRequester::HandleAllData(FCPMessage &message)
\r
21 std::vector<char> data;
\r
25 StringFunctions::Convert(message["DataLength"],datalength);
\r
27 // wait for all data to be received from connection
\r
28 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
33 // if we got disconnected- return immediately
\r
34 if(m_fcp->Connected()==false)
\r
40 data.resize(datalength);
\r
41 m_fcp->ReceiveRaw(&data[0],datalength);
\r
43 // update latest edition #
\r
44 std::vector<std::string> parts;
\r
45 StringFunctions::Split(message["Identifier"],"/",parts);
\r
48 std::string editionstr=parts[2];
\r
49 Option::Instance()->Set("FMSVersionEdition",editionstr);
\r
52 // parse file into xml and update the database
\r
53 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
56 SQLite3DB::Statement st=m_db->Prepare("REPLACE INTO tblFMSVersion(Major,Minor,Release,Notes,Changes,PageKey,SourceKey) VALUES(?,?,?,?,?,?,?);");
\r
57 st.Bind(0,xml.GetMajor());
\r
58 st.Bind(1,xml.GetMinor());
\r
59 st.Bind(2,xml.GetRelease());
\r
60 st.Bind(3,xml.GetNotes());
\r
61 st.Bind(4,xml.GetChanges());
\r
62 st.Bind(5,xml.GetPageKey());
\r
63 st.Bind(6,xml.GetSourceKey());
\r
66 m_log->debug("FMSVersionRequester::HandleAllData parsed FMSVersion XML file : "+message["Identifier"]);
\r
70 m_log->error("FMSVersionRequester::HandleAllData error parsing FMSVersion XML file : "+message["Identifier"]);
\r
76 const bool FMSVersionRequester::HandleGetFailed(FCPMessage &message)
\r
78 std::vector<std::string> parts;
\r
79 StringFunctions::Split(message["Identifier"],"/",parts);
\r
81 // fatal error - don't try to download again
\r
82 if(message["Fatal"]=="true")
\r
86 std::string editionstr=parts[2];
\r
87 Option::Instance()->Set("FMSVersionEdition",editionstr);
\r
89 m_log->debug("FMSVersionRequester::HandleGetFailed Fatal GetFailed for "+message["Identifier"]);
\r
95 const bool FMSVersionRequester::HandleMessage(FCPMessage &message)
\r
97 if(message["Identifier"].find(m_fcpuniquename)==0)
\r
100 // ignore DataFound
\r
101 if(message.GetName()=="DataFound")
\r
106 if(message.GetName()=="AllData")
\r
108 return HandleAllData(message);
\r
111 if(message.GetName()=="GetFailed")
\r
113 return HandleGetFailed(message);
\r
116 if(message.GetName()=="IdentifierCollision")
\r
126 void FMSVersionRequester::Initialize()
\r
128 m_fcpuniquename="FMSVersionRequester";
\r
129 m_lastchecked=Poco::Timestamp();
\r
130 m_lastchecked-=Poco::Timespan(0,6,0,0,0);
\r
133 void FMSVersionRequester::Process()
\r
135 Poco::DateTime now;
\r
137 // check every 6 hours
\r
138 if((m_lastchecked+Poco::Timespan(0,5,45,0,0))<=now)
\r
145 void FMSVersionRequester::RegisterWithThread(FreenetMasterThread *thread)
\r
147 thread->RegisterFCPConnected(this);
\r
148 thread->RegisterFCPMessageHandler(this);
\r
149 thread->RegisterPeriodicProcessor(this);
\r
152 void FMSVersionRequester::StartRequest()
\r
154 FCPMessage message;
\r
155 std::string key="";
\r
156 std::string editionstr="0";
\r
159 Option::Instance()->Get("FMSVersionKey",key);
\r
160 if(Option::Instance()->Get("FMSVersionEdition",editionstr))
\r
162 StringFunctions::Convert(editionstr,edition);
\r
164 StringFunctions::Convert(edition,editionstr);
\r
168 message.SetName("ClientGet");
\r
169 message["URI"]=key+editionstr+"/FMSVersion.xml";
\r
170 message["Identifier"]=m_fcpuniquename+"|"+message["URI"];
\r
171 message["ReturnType"]="direct";
\r
172 message["MaxSize"]="30000"; // 30K
\r
174 m_fcp->SendMessage(message);
\r