From a1fb492be1537b03204aff3838085aa096343452 Mon Sep 17 00:00:00 2001
From: Marco De Marco <demarco@oats.inaf.it>
Date: Fri, 24 Jan 2014 16:02:40 +0100
Subject: [PATCH] Modified list update loop and minor fix

---
 src/Client.cpp          | 33 +++++++++++++++--------
 src/Client.h            |  2 +-
 src/DBManager.cpp       |  8 +++---
 src/DataImporter.cpp    |  2 +-
 src/PlainClient.cpp     |  1 +
 src/ProtocolManager.cpp | 60 ++++++++++++++++-------------------------
 src/ProtocolManager.h   |  5 ----
 src/SSLClient.cpp       |  1 +
 8 files changed, 53 insertions(+), 59 deletions(-)

diff --git a/src/Client.cpp b/src/Client.cpp
index 45f3e48..0294aaf 100644
--- a/src/Client.cpp
+++ b/src/Client.cpp
@@ -208,26 +208,41 @@ void Client::startUpdateLists()
         writeStatus("Unknown error");
     }
 
-    handleUpdateLists();
+    if(readState() != Tango::ALARM && m_protocolManager_sp->hasNextFile())
+    {
+        startResolve();
+    }
+    else
+    {
+        m_listsUpdateTimer.expires_from_now(
+            boost::posix_time::seconds(m_configuration_sp->getRefreshTime()));
+
+        m_listsUpdateTimer.async_wait(boost::bind(&Client::handleUpdateLists,
+            this, boost::asio::placeholders::error));
+    }
 }
 
 //==============================================================================
 //      Client::handleUpdateLists()
 //==============================================================================
-void Client::handleUpdateLists()
+void Client::handleUpdateLists(const boost::system::error_code& errorCode)
 {
     DEBUG_STREAM << "Client::handleUpdateLists()" << endl;
 
-    if(readState() != Tango::ALARM && m_protocolManager_sp->hasNextFile())
+    if(!errorCode)
     {
-        startResolve();
+        startUpdateLists();
+    }
+    else if(errorCode == boost::asio::error::operation_aborted)
+    {
+        DEBUG_STREAM << "Client::handleUpdateLists() STOP" << endl;
     }
     else
     {
-        m_listsUpdateTimer.expires_from_now(
-            boost::posix_time::seconds(m_configuration_sp->getRefreshTime()));
+        ERROR_STREAM << "Client::handleResolve() " << errorCode.message() << endl;
 
-        m_listsUpdateTimer.async_wait(boost::bind(&Client::handleUpdateLists, this));
+        writeState(Tango::ALARM);
+        writeStatus(errorCode.message());
     }
 }
 
@@ -420,8 +435,6 @@ void Client::onTransferCompleted(FileWrapper::SP fileWrapper_sp)
     {
         m_protocolManager_sp->setFileTransfered(fileWrapper_sp);
 
-        m_protocolManager_sp->nextFile();
-
         if(m_protocolManager_sp->hasNextFile())
         {
             startWriteRequest();
@@ -460,8 +473,6 @@ void Client::onTransferFailed()
     {
         m_protocolManager_sp->setFileFailed();
 
-        m_protocolManager_sp->nextFile();
-
         if(m_protocolManager_sp->hasNextFile())
         {
             startWriteRequest();
diff --git a/src/Client.h b/src/Client.h
index bfb901e..b5dfdf7 100644
--- a/src/Client.h
+++ b/src/Client.h
@@ -69,7 +69,7 @@ protected:
 //------------------------------------------------------------------------------
     virtual void startUpdateLists();
 
-    virtual void handleUpdateLists();
+    virtual void handleUpdateLists(const boost::system::error_code&);
 
 //------------------------------------------------------------------------------
 //  [Protected] Endpoint resolution methods
diff --git a/src/DBManager.cpp b/src/DBManager.cpp
index 8d09bee..98c8ba0 100644
--- a/src/DBManager.cpp
+++ b/src/DBManager.cpp
@@ -26,7 +26,7 @@ DBManager::DBManager(Tango::DeviceImpl* deviceImpl_p,
 }
 
 //==============================================================================
-//      DBManager::DBManager()
+//      DBManager::~DBManager()
 //==============================================================================
 DBManager::~DBManager()
 {
@@ -38,7 +38,7 @@ DBManager::~DBManager()
 }
 
 //==============================================================================
-//      DBManager::DBManager()
+//      DBManager::create()
 //==============================================================================
 DBManager::SP DBManager::create(Tango::DeviceImpl* deviceImpl_p,
     Configuration::SP configuration_sp)
@@ -66,7 +66,7 @@ void DBManager::connectAll() throw(soci::soci_error)
     connection << " password=" << m_configuration_sp->getDatabasePassword();
 
     #ifdef VERBOSE_DEBUG
-    INFO_STREAM << "MAIN CONNECTION: " << connection.str() << endl;
+        INFO_STREAM << "MAIN CONNECTION: " << connection.str() << endl;
     #endif
 
     m_mainSession_sp->open(soci::mysql, connection.str());
@@ -79,7 +79,7 @@ void DBManager::connectAll() throw(soci::soci_error)
     connection << " password=" << m_configuration_sp->getAuxDatabasePassword();
 
     #ifdef VERBOSE_DEBUG
-    INFO_STREAM << "AUX CONNECTION: " << connection.str() << endl;
+        INFO_STREAM << "AUX CONNECTION: " << connection.str() << endl;
     #endif
 
     m_auxSession_sp->open(soci::mysql, connection.str());
diff --git a/src/DataImporter.cpp b/src/DataImporter.cpp
index 23aa870..bc540d7 100644
--- a/src/DataImporter.cpp
+++ b/src/DataImporter.cpp
@@ -610,7 +610,7 @@ void DataImporter::get_device_property()
 //--------------------------------------------------------
 void DataImporter::always_executed_hook()
 {
-	INFO_STREAM << "DataImporter::always_executed_hook()  " << device_name << endl;
+	DEBUG_STREAM << "DataImporter::always_executed_hook()  " << device_name << endl;
 	/*----- PROTECTED REGION ID(DataImporter::always_executed_hook) ENABLED START -----*/
 
     if(get_state() != Tango::FAULT)
diff --git a/src/PlainClient.cpp b/src/PlainClient.cpp
index a244659..e6b5708 100644
--- a/src/PlainClient.cpp
+++ b/src/PlainClient.cpp
@@ -242,6 +242,7 @@ void PlainClient::closeConnection()
 
     INFO_STREAM << "PlainClient::closeConnection() " << infoStream.str() << endl;
 
+    m_listsUpdateTimer.expires_at(boost::posix_time::pos_infin);
     m_resetConnectionTimer.expires_at(boost::posix_time::pos_infin);
 
     boost::system::error_code errorCode;
diff --git a/src/ProtocolManager.cpp b/src/ProtocolManager.cpp
index b2d4b87..66154e9 100644
--- a/src/ProtocolManager.cpp
+++ b/src/ProtocolManager.cpp
@@ -1,6 +1,5 @@
 #include <ProtocolManager.h>
 #include <boost/filesystem.hpp>
-#include <stdexcept>
 
 namespace DataImporter_ns
 {
@@ -98,35 +97,6 @@ bool ProtocolManager::hasNextFile()
     }
 }
 
-//==============================================================================
-//      ProtocolManager::nextFile()
-//==============================================================================
-void ProtocolManager::nextFile()
-{
-    DEBUG_STREAM << "ProtocolManager::nextFile()" << endl;
-
-    if(!m_recoveryMode)
-    {
-        if(m_newFileRowset_sp &&
-            m_newFileRowsetIt != m_newFileRowset_sp->end())
-        {
-            DEBUG_STREAM << "ProtocolManager::nextFile() new list" << endl;
-
-            ++m_newFileRowsetIt;
-        }
-    }
-    else
-    {
-        if(m_failedFileRowset_sp &&
-                m_failedFileRowsetIt != m_failedFileRowset_sp->end())
-        {
-            DEBUG_STREAM << "ProtocolManager::nextFile() from failed list" << endl;
-
-            ++m_failedFileRowsetIt;
-        }
-    }
-}
-
 //==============================================================================
 //      ProtocolManager::createRequest()
 //==============================================================================
@@ -261,21 +231,33 @@ void ProtocolManager::setFileTransfered(FileWrapper::SP fileWrapper_sp)
 
         if(!m_newFileRowsetIt->get<2>())
             throw std::invalid_argument("Empty update time found on new list");
-        std::tm update_time = m_newFileRowsetIt->get<2>().get();
+        std::tm currentTm = m_newFileRowsetIt->get<2>().get();
 
         INFO_STREAM << "ProtocolManager::setFileTransfered() file "
             << fileName << " version " << fileVersion << " transfered" << endl;
 
-        boost::posix_time::ptime newPtime = boost::posix_time::ptime_from_tm(update_time);
+        boost::posix_time::ptime currentPtime =
+            boost::posix_time::ptime_from_tm(currentTm);
+
+        boost::posix_time::ptime nextPtime(boost::posix_time::pos_infin);
 
-        if(m_currentPtime.is_not_a_date_time())
-            m_currentPtime = newPtime;
+        //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(newPtime > m_currentPtime)
-            m_dBManager_sp->persistLastTimestamp(newPtime);
+        if(nextPtime > currentPtime)
+            m_dBManager_sp->persistLastTimestamp(currentPtime);
 
         m_dBManager_sp->updateNewFilePath(storagePath, filePath, fileVersion, fileName);
 
@@ -296,6 +278,8 @@ 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();
 
+        ++m_failedFileRowsetIt;
+
         DBManager::TransactionSP auxTransaction_sp = m_dBManager_sp->getAuxTransaction();
         DBManager::TransactionSP mainTransaction_sp = m_dBManager_sp->getMainTransaction();
 
@@ -329,11 +313,13 @@ 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();
 
+        ++m_newFileRowsetIt;
+
         m_dBManager_sp->addFailedFile(fileVersion, fileName);
     }
     else
     {
-        //TODO: file failed again -> what to do?
+        ++m_failedFileRowsetIt;
     }
 }
 
diff --git a/src/ProtocolManager.h b/src/ProtocolManager.h
index cd8e679..9cfc991 100644
--- a/src/ProtocolManager.h
+++ b/src/ProtocolManager.h
@@ -63,8 +63,6 @@ public:
 
     virtual bool hasNextFile();
 
-    virtual void nextFile();
-
 //------------------------------------------------------------------------------
 //	[Public] Request response methods
 //------------------------------------------------------------------------------
@@ -102,9 +100,6 @@ protected:
     //Processing file from recovery list
     bool m_recoveryMode;
 
-    //New file list current timestamp index
-    boost::posix_time::ptime m_currentPtime;
-
     //New file list shared pointer
     DBManager::NewFileRowsetSP m_newFileRowset_sp;
 
diff --git a/src/SSLClient.cpp b/src/SSLClient.cpp
index 35201d5..62ae061 100644
--- a/src/SSLClient.cpp
+++ b/src/SSLClient.cpp
@@ -295,6 +295,7 @@ void SSLClient::closeConnection()
 
     INFO_STREAM << "SSLClient::closeConnection() " << infoStream.str() << endl;
 
+    m_listsUpdateTimer.expires_at(boost::posix_time::pos_infin);
     m_resetConnectionTimer.expires_at(boost::posix_time::pos_infin);
 
     boost::system::error_code errorCode;
-- 
GitLab