-
+// Copyright (C) 2013 James Turner - zakalawe@mac.com
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Library General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
#include <simgear/package/Catalog.hxx>
#include <boost/foreach.hpp>
+#include <algorithm>
#include <fstream>
#include <cstring>
}
protected:
- virtual void responseHeadersComplete()
- {
-
- }
-
virtual void gotBodyData(const char* s, int n)
{
m_buffer += std::string(s, n);
}
- virtual void responseComplete()
+ virtual void onDone()
{
if (responseCode() != 200) {
SG_LOG(SG_GENERAL, SG_ALERT, "catalog download failure:" << m_owner->url());
- m_owner->refreshComplete(false);
+ m_owner->refreshComplete(Delegate::FAIL_DOWNLOAD);
return;
}
m_owner->parseProps(props);
} catch (sg_exception& e) {
SG_LOG(SG_GENERAL, SG_ALERT, "catalog parse failure:" << m_owner->url());
- m_owner->refreshComplete(false);
+ m_owner->refreshComplete(Delegate::FAIL_EXTRACT);
+ return;
+ }
+
+ std::string ver(m_owner->root()->catalogVersion());
+ if (!checkVersion(ver, props)) {
+ SG_LOG(SG_GENERAL, SG_WARN, "downloaded catalog " << m_owner->url() << ", version mismatch:\n\t"
+ << props->getStringValue("version") << " vs required " << ver);
+ m_owner->refreshComplete(Delegate::FAIL_VERSION);
return;
}
time(&m_owner->m_retrievedTime);
m_owner->writeTimestamp();
- m_owner->refreshComplete(true);
+ m_owner->refreshComplete(Delegate::FAIL_SUCCESS);
}
private:
+ bool checkVersion(const std::string& aVersion, SGPropertyNode* aProps)
+ {
+ BOOST_FOREACH(SGPropertyNode* v, aProps->getChildren("version")) {
+ if (v->getStringValue() == aVersion) {
+ return true;
+ }
+ }
+ return false;
+ }
+
Catalog* m_owner;
std::string m_buffer;
};
{
Catalog* c = new Catalog(aRoot);
Downloader* dl = new Downloader(c, aUrl);
- aRoot->getHTTPClient()->makeRequest(dl);
+ aRoot->makeHTTPRequest(dl);
return c;
}
return NULL;
}
+ if (props->getStringValue("version") != aRoot->catalogVersion()) {
+ SG_LOG(SG_GENERAL, SG_WARN, "skipping catalog at " << aPath << ", version mismatch:\n\t"
+ << props->getStringValue("version") << " vs required " << aRoot->catalogVersion());
+ return NULL;
+ }
+
Catalog* c = new Catalog(aRoot);
c->m_installRoot = aPath;
c->parseProps(props);
void Catalog::refresh()
{
Downloader* dl = new Downloader(this, url());
- m_root->getHTTPClient()->makeRequest(dl);
+ m_root->makeHTTPRequest(dl);
m_root->catalogRefreshBegin(this);
}
{
return getLocalisedString(m_props, "description");
}
+
+SGPropertyNode* Catalog::properties() const
+{
+ return m_props.ptr();
+}
void Catalog::parseTimestamp()
{
f << m_retrievedTime << std::endl;
}
-int Catalog::ageInSeconds() const
+unsigned int Catalog::ageInSeconds() const
{
time_t now;
time(&now);
- return ::difftime(now, m_retrievedTime);
+ int diff = ::difftime(now, m_retrievedTime);
+ return (diff < 0) ? 0 : diff;
}
+bool Catalog::needsRefresh() const
+{
+ unsigned int maxAge = m_props->getIntValue("max-age-sec", m_root->maxAgeSeconds());
+ return (ageInSeconds() > maxAge);
+}
+
std::string Catalog::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
{
if (aRoot->hasChild(m_root->getLocale())) {
return aRoot->getStringValue(aName);
}
-void Catalog::refreshComplete(bool aSuccess)
+void Catalog::refreshComplete(Delegate::FailureCode aReason)
{
- m_root->catalogRefreshComplete(this, aSuccess);
+ m_root->catalogRefreshComplete(this, aReason);
}