From 8000709cdd556f27c0c0c9b92c884961714121d9 Mon Sep 17 00:00:00 2001
From: Marco De Marco <demarco@oats.inaf.it>
Date: Thu, 30 Jan 2014 15:54:08 +0100
Subject: [PATCH] Before protocol manager refactoring

---
 src/Client.cpp          |  6 ++--
 src/Client.h            |  2 +-
 src/ProtocolManager.cpp | 66 ++++++++++++++++++++++++++++++++++-------
 src/ProtocolManager.h   |  5 +++-
 4 files changed, 64 insertions(+), 15 deletions(-)

diff --git a/src/Client.cpp b/src/Client.cpp
index 0294aaf..f43785c 100644
--- a/src/Client.cpp
+++ b/src/Client.cpp
@@ -38,7 +38,7 @@ Client::~Client()
 
     if(m_thread_sp)
     {
-        m_thread_sp->interrupt();
+        //m_thread_sp->interrupt();
 
         m_thread_sp->join();
     }
@@ -82,7 +82,7 @@ void Client::stop()
 
     if(m_thread_sp)
     {
-        m_thread_sp->interrupt();
+        //m_thread_sp->interrupt();
 
         m_thread_sp->join();
     }
@@ -411,6 +411,8 @@ void Client::handleReadData(FileWrapper::SP fileWrapper_sp, std::size_t recvByte
         {
             WARN_STREAM << "Client::handleReadData() bad I/O" << endl;
 
+            fileWrapper_sp->cleanUp();
+
             onTransferFailed();
         }
     }
diff --git a/src/Client.h b/src/Client.h
index b5dfdf7..f45abe8 100644
--- a/src/Client.h
+++ b/src/Client.h
@@ -117,7 +117,7 @@ protected:
         const boost::system::error_code&);
 
 //------------------------------------------------------------------------------
-//  [Protected] Transfer result methods
+//  [Protected] Protocol and transfer result handler methods
 //------------------------------------------------------------------------------
     void onTransferCompleted(FileWrapper::SP);
 
diff --git a/src/ProtocolManager.cpp b/src/ProtocolManager.cpp
index c9fc82b..e76bc7f 100644
--- a/src/ProtocolManager.cpp
+++ b/src/ProtocolManager.cpp
@@ -80,23 +80,35 @@ bool ProtocolManager::hasNextFile()
         m_recoveryMode = false;
         return true;
     }
-    else if(m_failedFileRowset_sp &&
+
+    if(m_failedFileRowset_sp &&
         m_failedFileRowsetIt != m_failedFileRowset_sp->end())
     {
-        DEBUG_STREAM << "ProtocolManager::hasNextFile() from failed list" << endl;
+        boost::posix_time::ptime now(boost::posix_time::second_clock::local_time());
 
-        //FIXME: inhibition of retry for n seconds
+        if(m_recoveryModeTime.is_not_a_date_time())
+            m_recoveryModeTime = now;
 
-        m_recoveryMode = true;
-        return true;
-    }
-    else
-    {
-        DEBUG_STREAM << "ProtocolManager::hasNextFile() lists empty" << endl;
+        boost::posix_time::time_duration diff = now - m_recoveryModeTime;
 
-        m_recoveryMode = false;
-        return false;
+        if(diff.total_seconds() > 30)
+        {
+            DEBUG_STREAM << "ProtocolManager::hasNextFile() from failed list" << endl;
+            m_recoveryModeTime = now;
+            m_recoveryMode = true;
+            return true;
+        }
+        else
+        {
+            DEBUG_STREAM << "ProtocolManager::hasNextFile() "
+                << "wait from failed list" << endl;
+        }
     }
+
+    DEBUG_STREAM << "ProtocolManager::hasNextFile() lists empty" << endl;
+
+    m_recoveryMode = false;
+    return false;
 }
 
 //==============================================================================
@@ -280,6 +292,7 @@ void ProtocolManager::setFileTransfered(FileWrapper::SP fileWrapper_sp)
             throw std::invalid_argument("Empty file name found on failed list");
         string fileName = m_failedFileRowsetIt->get<1>().get();
 
+        //FIXME: not incremented in case of exception!!!
         ++m_failedFileRowsetIt;
 
         DBManager::TransactionSP auxTransaction_sp = m_dBManager_sp->getAuxTransaction();
@@ -315,9 +328,40 @@ void ProtocolManager::setFileFailed() throw(std::logic_error, std::runtime_error
             throw std::invalid_argument("Empty file name found on new list");
         string fileName = m_newFileRowsetIt->get<1>().get();
 
+        if(!m_newFileRowsetIt->get<2>())
+            throw std::invalid_argument("Empty update time found on new list");
+        std::tm currentTm = m_newFileRowsetIt->get<2>().get();
+
+        INFO_STREAM << "ProtocolManager::setFileFailed() file "
+            << fileName << " version " << fileVersion << " transfered" << endl;
+
+        boost::posix_time::ptime currentPtime =
+            boost::posix_time::ptime_from_tm(currentTm);
+
+        boost::posix_time::ptime nextPtime(boost::posix_time::pos_infin);
+
+        //FIXME: not incremented in case of exception!!!
         ++m_newFileRowsetIt;
 
+        if(m_newFileRowsetIt != m_newFileRowset_sp->end())
+        {
+            if(!m_newFileRowsetIt->get<2>())
+                    throw std::invalid_argument("Empty next update time found on new list");
+                std::tm nextTm = m_newFileRowsetIt->get<2>().get();
+
+                nextPtime =boost::posix_time::ptime_from_tm(nextTm);
+        }
+
+        DBManager::TransactionSP auxTransaction_sp = m_dBManager_sp->getAuxTransaction();
+        DBManager::TransactionSP mainTransaction_sp = m_dBManager_sp->getMainTransaction();
+
+        if(nextPtime > currentPtime)
+            m_dBManager_sp->persistLastTimestamp(currentPtime);
+
         m_dBManager_sp->addFailedFile(fileVersion, fileName);
+
+        auxTransaction_sp->commit();
+        mainTransaction_sp->commit();
     }
     else
     {
diff --git a/src/ProtocolManager.h b/src/ProtocolManager.h
index 9cfc991..b6b5a4d 100644
--- a/src/ProtocolManager.h
+++ b/src/ProtocolManager.h
@@ -97,9 +97,12 @@ protected:
     //Address and port of remote endpoint
     std::string m_remoteEndpoint;
 
-    //Processing file from recovery list
+    //Processing file from failed list
     bool m_recoveryMode;
 
+    //Processing file from failed list last timestamp
+    boost::posix_time::ptime m_recoveryModeTime;
+
     //New file list shared pointer
     DBManager::NewFileRowsetSP m_newFileRowset_sp;
 
-- 
GitLab