]> git.mxchange.org Git - simgear.git/blobdiff - simgear/package/Install.cxx
Tweak HTTP code to always sleep.
[simgear.git] / simgear / package / Install.cxx
index b471d965259bf8efe3c8def993426005dfaf41d8..d513786c27e605ebe5dc34e18d2bbebe1cf9d1e3 100644 (file)
@@ -1,4 +1,19 @@
-
+// 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/Install.hxx>
 
@@ -51,30 +66,30 @@ protected:
     
     virtual void responseHeadersComplete()
     {
-        std::cout << "starting download of " << m_owner->package()->id() << " from "
-            << url() << std::endl;
         Dir d(m_extractPath);
         d.create(0755);        
         
-        memset(&m_md5, 0, sizeof(MD5_CTX));
-        MD5Init(&m_md5);
+        memset(&m_md5, 0, sizeof(SG_MD5_CTX));
+        SG_MD5Init(&m_md5);
     }
     
     virtual void gotBodyData(const char* s, int n)
     {
         m_buffer += std::string(s, n);
-        MD5Update(&m_md5, (unsigned char*) s, n);
+        SG_MD5Update(&m_md5, (unsigned char*) s, n);
+        
+        m_owner->installProgress(m_buffer.size(), responseLength());
     }
     
-    virtual void responseComplete()
+    virtual void onDone()
     {
         if (responseCode() != 200) {
             SG_LOG(SG_GENERAL, SG_ALERT, "download failure");
-            doFailure();
+            doFailure(Delegate::FAIL_DOWNLOAD);
             return;
         }
 
-        MD5Final(&m_md5);
+        SG_MD5Final(&m_md5);
     // convert final sum to hex
         const char hexChar[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
         std::stringstream hexMd5;
@@ -88,15 +103,13 @@ protected:
                 << "\t" << hexMd5.str() << "\n\t"
                 << m_owner->package()->md5() << "\n\t"
                 << "downloading from:" << url());
-            doFailure();
+            doFailure(Delegate::FAIL_CHECKSUM);
             return;
-        } else {
-            std::cout << "MD5 checksum is ok" << std::endl;
         }
         
         if (!extractUnzip()) {
             SG_LOG(SG_GENERAL, SG_WARN, "zip extraction failed");
-            doFailure();
+            doFailure(Delegate::FAIL_EXTRACT);
             return;
         }
                   
@@ -107,9 +120,15 @@ protected:
         }
         
         m_extractPath.append(m_owner->package()->id());
-        m_extractPath.rename(m_owner->path());        
+        bool ok = m_extractPath.rename(m_owner->path());
+        if (!ok) {
+            doFailure(Delegate::FAIL_FILESYSTEM);
+            return;
+        }
+        
         m_owner->m_revision = m_owner->package()->revision();
         m_owner->writeRevisionFile();
+        m_owner->installResult(Delegate::FAIL_SUCCESS);
     }
     
 private:
@@ -212,21 +231,24 @@ private:
         return result;
     }
         
-    void doFailure()
+    void doFailure(Delegate::FailureCode aReason)
     {
         Dir dir(m_extractPath);
         dir.remove(true /* recursive */);
+
         if (m_urls.size() == 1) {
-            
+            std::cout << "failure:" << aReason << std::endl;
+            m_owner->installResult(aReason);
             return;
         }
         
+        std::cout << "retrying download" << std::endl;
         m_urls.erase(m_urls.begin()); // pop first URL
     }
     
     Install* m_owner;
     string_list m_urls;
-    MD5_CTX m_md5;
+    SG_MD5_CTX m_md5;
     std::string m_buffer;
     SGPath m_extractPath;
 };
@@ -284,7 +306,8 @@ void Install::startUpdate()
     }
     
     m_download = new PackageArchiveDownloader(this);
-    m_package->catalog()->root()->getHTTPClient()->makeRequest(m_download);
+    m_package->catalog()->root()->makeHTTPRequest(m_download);
+    m_package->catalog()->root()->startInstall(this);
 }
 
 void Install::uninstall()
@@ -294,6 +317,21 @@ void Install::uninstall()
     delete this;
 }
 
+void Install::installResult(Delegate::FailureCode aReason)
+{
+    if (aReason == Delegate::FAIL_SUCCESS) {
+        m_package->catalog()->root()->finishInstall(this);
+    } else {
+        m_package->catalog()->root()->failedInstall(this, aReason);
+    }
+}
+    
+void Install::installProgress(unsigned int aBytes, unsigned int aTotal)
+{
+    m_package->catalog()->root()->installProgress(this, aBytes, aTotal);
+}
+
+    
 } // of namespace pkg
 
 } // of namespace simgear