]> git.mxchange.org Git - simgear.git/blobdiff - simgear/package/Catalog.cxx
Tweak HTTP code to always sleep.
[simgear.git] / simgear / package / Catalog.cxx
index e8a17573b250f623bc0cbab258686a409fa611df..160d2fbf7cc45e917853c4289c93c04c02fa07d4 100644 (file)
@@ -1,8 +1,24 @@
-
+// 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>
 
@@ -34,21 +50,16 @@ public:
     }
     
 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;
         }
         
@@ -59,7 +70,15 @@ protected:
             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;
         }
         
@@ -73,10 +92,20 @@ protected:
         
         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;
 };
@@ -105,7 +134,7 @@ Catalog* Catalog::createFromUrl(Root* aRoot, const std::string& aUrl)
 {
     Catalog* c = new Catalog(aRoot);
     Downloader* dl = new Downloader(c, aUrl);
-    aRoot->getHTTPClient()->makeRequest(dl);
+    aRoot->makeHTTPRequest(dl);
     
     return c;
 }
@@ -126,6 +155,12 @@ Catalog* Catalog::createFromPath(Root* aRoot, const SGPath& aPath)
         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);
@@ -165,7 +200,7 @@ Catalog::packagesNeedingUpdate() const
 void Catalog::refresh()
 {
     Downloader* dl = new Downloader(this, url());
-    m_root->getHTTPClient()->makeRequest(dl);
+    m_root->makeHTTPRequest(dl);
     m_root->catalogRefreshBegin(this);
 }
 
@@ -220,6 +255,11 @@ std::string Catalog::description() const
 {
     return getLocalisedString(m_props, "description");
 }
+    
+SGPropertyNode* Catalog::properties() const
+{
+    return m_props.ptr();
+}
 
 void Catalog::parseTimestamp()
 {
@@ -237,13 +277,20 @@ void Catalog::writeTimestamp()
     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())) {
@@ -256,9 +303,9 @@ std::string Catalog::getLocalisedString(const SGPropertyNode* aRoot, const char*
     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);
 }