From b02d811affe8da98de30c3387ab0cc5c4d995ec8 Mon Sep 17 00:00:00 2001
From: Marco De Marco <demarco@oats.inaf.it>
Date: Thu, 29 May 2014 16:54:17 +0200
Subject: [PATCH] Counter logic implemente and some improvements

---
 src/Client.cpp          | 31 +++++++++++++++------
 src/DBManager.h         |  8 +++---
 src/DataImporter.cpp    | 38 +++----------------------
 src/DataImporter.h      |  1 -
 src/PlainClient.cpp     |  2 +-
 src/ProtocolManager.cpp | 61 +++++++++++++++++++++++------------------
 src/SSLClient.cpp       |  2 +-
 7 files changed, 67 insertions(+), 76 deletions(-)

diff --git a/src/Client.cpp b/src/Client.cpp
index 7942dd2..f07a457 100644
--- a/src/Client.cpp
+++ b/src/Client.cpp
@@ -54,18 +54,33 @@ void Client::start()
 {
     DEBUG_STREAM << "Client::start()" << endl;
 
-    m_dBManager_sp->connectAll();
+    try
+    {
+        m_dBManager_sp->connectAll();
 
-    m_protocolManager_sp = ProtocolManager::create(m_dataImporter_p,
-        m_configuration_sp, m_dBManager_sp);
+        m_protocolManager_sp = ProtocolManager::create(m_dataImporter_p,
+            m_configuration_sp, m_dBManager_sp);
 
-    m_ioService.reset();
+        m_ioService.reset();
 
-    m_work_sp.reset(new boost::asio::io_service::work(m_ioService));
+        m_work_sp.reset(new boost::asio::io_service::work(m_ioService));
 
-    m_thread_sp.reset(new boost::thread(boost::bind(&Client::run, this)));
+        m_thread_sp.reset(new boost::thread(boost::bind(&Client::run, this)));
 
-    Client::startUpdateLists();
+        Client::startUpdateLists();
+    }
+    catch(std::exception& ex)
+    {
+        writeState(Tango::ALARM);
+        std::stringstream error_stream;
+        error_stream << "Client::start() " << ex.what() << std::endl;
+        writeStatus(error_stream.str());
+    }
+    catch(...)
+    {
+        writeState(Tango::ALARM);
+        writeStatus("Client::start() unknown error");
+    }
 }
 
 //==============================================================================
@@ -363,7 +378,7 @@ void Client::handleReadResponseBody(const boost::system::error_code& errorCode)
 
             onTransferFailed();
         }
-        catch(std::runtime_error& ec)
+        catch(std::exception& ec)
         {
             ERROR_STREAM << "Client::handleReadResponseBody() " << ec.what() << endl;
 
diff --git a/src/DBManager.h b/src/DBManager.h
index e1f5980..14f389f 100644
--- a/src/DBManager.h
+++ b/src/DBManager.h
@@ -83,8 +83,8 @@ public:
         throw(soci::soci_error);
 
     virtual void persistLastTimestamp(boost::posix_time::ptime)
-        throw(soci::soci_error);    
-    
+        throw(soci::soci_error);
+
 //------------------------------------------------------------------------------
 //  [Public] File row set definition
 //------------------------------------------------------------------------------
@@ -94,8 +94,8 @@ public:
 
     typedef soci::rowset< FileRow > FileRowset;
 
-    typedef boost::shared_ptr< FileRowset > FileRowsetSP;    
-    
+    typedef boost::shared_ptr< FileRowset > FileRowsetSP;
+
 //------------------------------------------------------------------------------
 //  [Public] New file method
 //------------------------------------------------------------------------------
diff --git a/src/DataImporter.cpp b/src/DataImporter.cpp
index 1d4180a..a9d2648 100644
--- a/src/DataImporter.cpp
+++ b/src/DataImporter.cpp
@@ -716,23 +716,8 @@ void DataImporter::on()
 	DEBUG_STREAM << "DataImporter::On()  - " << device_name << endl;
 	/*----- PROTECTED REGION ID(DataImporter::on) ENABLED START -----*/
 
-    try
-    {
-        if(m_client_sp)
-            m_client_sp->start();
-    }
-    catch(std::exception& ex)
-    {
-        set_state(Tango::ALARM);
-        std::stringstream error_stream;
-        error_stream << "DataImporter::On() " << ex.what() << std::endl;
-        set_status(error_stream.str());
-    }
-    catch(...)
-    {
-        set_state(Tango::ALARM);
-        set_status("DataImporter::On() unknown error");
-    }
+    if(m_client_sp)
+        m_client_sp->start();
 
 	/*----- PROTECTED REGION END -----*/	//	DataImporter::on
 }
@@ -748,23 +733,8 @@ void DataImporter::off()
 	DEBUG_STREAM << "DataImporter::Off()  - " << device_name << endl;
 	/*----- PROTECTED REGION ID(DataImporter::off) ENABLED START -----*/
 
-    try
-    {
-        if(m_client_sp)
-            m_client_sp->stop();
-    }
-    catch(std::exception& ex)
-    {
-        set_state(Tango::ALARM);
-        std::stringstream error_stream;
-        error_stream << "DataImporter::Off() " << ex.what() << std::endl;
-        set_status(error_stream.str());
-    }
-    catch(...)
-    {
-        set_state(Tango::ALARM);
-        set_status("DataImporter::Off() unknown error");
-    }
+    if(m_client_sp)
+        m_client_sp->stop();
 
 	/*----- PROTECTED REGION END -----*/	//	DataImporter::off
 }
diff --git a/src/DataImporter.h b/src/DataImporter.h
index 2e3cca7..dbdb4f5 100644
--- a/src/DataImporter.h
+++ b/src/DataImporter.h
@@ -269,7 +269,6 @@ public:
 //------------------------------------------------------------------------------
 //  [Public] Counters methods
 //------------------------------------------------------------------------------
-
     virtual void incrementRegularCounter();
 
     virtual void incrementFailedCounter();
diff --git a/src/PlainClient.cpp b/src/PlainClient.cpp
index 8e9be20..34409e5 100644
--- a/src/PlainClient.cpp
+++ b/src/PlainClient.cpp
@@ -145,7 +145,7 @@ void PlainClient::startWriteRequest()
             boost::bind(&PlainClient::handleWriteRequest, this,
             boost::asio::placeholders::error));
     }
-    catch(std::runtime_error& ec)
+    catch(std::exception& ec)
     {
         ERROR_STREAM << "PlainClient::startWriteRequest() " << ec.what() << endl;
 
diff --git a/src/ProtocolManager.cpp b/src/ProtocolManager.cpp
index fc91f6c..7dd1178 100644
--- a/src/ProtocolManager.cpp
+++ b/src/ProtocolManager.cpp
@@ -53,7 +53,7 @@ void ProtocolManager::setRemoteEndpoint(std::string remoteEndpoint)
 void ProtocolManager::retrieveFiles() throw(std::runtime_error)
 {
     DEBUG_STREAM << "ProtocolManager::retrieveFiles()" << endl;
-    
+
     boost::posix_time::ptime m_lastTimestamp =
         m_dBManager_sp->retrieveLastTimestamp();
 
@@ -61,12 +61,12 @@ void ProtocolManager::retrieveFiles() throw(std::runtime_error)
         << boost::posix_time::to_simple_string(m_lastTimestamp) << endl;
 
     m_newFileRowset_sp = m_dBManager_sp->retrieveNewFiles(m_lastTimestamp);
-    
+
     m_newFileRowsetIt = m_newFileRowset_sp->begin();
 
     m_failedFileRowset_sp = m_dBManager_sp->retrieveFailedFiles();
-    
-    m_failedFileRowsetIt = m_failedFileRowset_sp->begin();    
+
+    m_failedFileRowsetIt = m_failedFileRowset_sp->begin();
 }
 
 //==============================================================================
@@ -75,24 +75,24 @@ void ProtocolManager::retrieveFiles() throw(std::runtime_error)
 bool ProtocolManager::hasFilesToTransfer()
 {
     DEBUG_STREAM << "ProtocolManager::hasFilesToTransfer()" << endl;
-    
+
     if(m_newFileRowset_sp &&
         m_newFileRowsetIt != m_newFileRowset_sp->end())
-    {   
+    {
         INFO_STREAM << "ProtocolManager::hasFilesToTransfer() in new list" << endl;
-        return true;    
-    }   
+        return true;
+    }
     else if(m_failedFileRowset_sp &&
         m_failedFileRowsetIt != m_failedFileRowset_sp->end())
-    {   
+    {
         if(isRecoveryTimeElapsed())
-        {   
+        {
             INFO_STREAM << "ProtocolManager::hasFilesToTransfer() in failed list" << endl;
-            return true;    
-        }   
-    }   
-    
-    return false;    
+            return true;
+        }
+    }
+
+    return false;
 }
 
 //==============================================================================
@@ -115,7 +115,7 @@ bool ProtocolManager::hasNextFile()
     else
     {
         return false;
-    }    
+    }
 }
 
 //==============================================================================
@@ -124,7 +124,7 @@ bool ProtocolManager::hasNextFile()
 RequestSP ProtocolManager::createtRequest() throw(std::runtime_error)
 {
     DEBUG_STREAM << "ProtocolManager::createtRequest()" << endl;
-    
+
     if(m_newFileRowset_sp &&
         m_newFileRowsetIt != m_newFileRowset_sp->end())
     {
@@ -138,7 +138,7 @@ RequestSP ProtocolManager::createtRequest() throw(std::runtime_error)
     else
     {
         throw std::runtime_error("Lists not initialized");
-    }    
+    }
 }
 
 //==============================================================================
@@ -196,7 +196,7 @@ void ProtocolManager::setCurrentFileDownloaded(FileWrapper::SP fileWrapper_sp)
 
     std::string storagePath = fileWrapper_sp->getStoragePath();
     std::string filePath = fileWrapper_sp->getFilePath();
-    
+
     if(m_newFileRowset_sp &&
         m_newFileRowsetIt != m_newFileRowset_sp->end())
     {
@@ -240,7 +240,9 @@ void ProtocolManager::setCurrentFileDownloaded(FileWrapper::SP fileWrapper_sp)
         m_dBManager_sp->updateNewFilePath(storagePath, filePath, fileVersion, fileName);
 
         auxTransaction_sp->commit();
-        mainTransaction_sp->commit(); 
+        mainTransaction_sp->commit();
+
+        m_dataImporter_p->incrementRegularCounter();
     }
     else if(m_failedFileRowset_sp &&
         m_failedFileRowsetIt != m_failedFileRowset_sp->end())
@@ -263,12 +265,15 @@ void ProtocolManager::setCurrentFileDownloaded(FileWrapper::SP fileWrapper_sp)
         m_dBManager_sp->updateNewFilePath(storagePath, filePath, fileVersion, fileName);
 
         auxTransaction_sp->commit();
-        mainTransaction_sp->commit();        
+        mainTransaction_sp->commit();
+
+        m_dataImporter_p->decrementFailedCounter();
+        m_dataImporter_p->incrementRegularCounter();
     }
     else
     {
         throw std::runtime_error("Lists not initialized");
-    }   
+    }
 }
 
 //==============================================================================
@@ -321,17 +326,19 @@ void ProtocolManager::setCurrentFileFailed() throw(std::runtime_error)
         m_dBManager_sp->addFailedFile(fileVersion, fileName);
 
         auxTransaction_sp->commit();
-        mainTransaction_sp->commit();      
+        mainTransaction_sp->commit();
+
+        m_dataImporter_p->incrementFailedCounter();
     }
     else if(m_failedFileRowset_sp &&
         m_failedFileRowsetIt != m_failedFileRowset_sp->end())
     {
-        ++m_failedFileRowsetIt;        
+        ++m_failedFileRowsetIt;
     }
     else
     {
         throw std::runtime_error("Lists not initialized");
-    }    
+    }
 }
 
 //==============================================================================
@@ -369,7 +376,7 @@ RequestSP ProtocolManager::fillRequest(DBManager::FileRowset::const_iterator it)
     throw(std::runtime_error)
 {
     DEBUG_STREAM << "ProtocolManager::fillRequest()" << endl;
-    
+
     RequestSP request_sp(new Request);
 
     request_sp->set_username(m_configuration_sp->getDatabaseUsername());
@@ -395,7 +402,7 @@ RequestSP ProtocolManager::fillRequest(DBManager::FileRowset::const_iterator it)
     if(!request_sp->IsInitialized())
         throw std::runtime_error("Request not initialized");
 
-    return request_sp;    
+    return request_sp;
 }
 
 }   //namespace
diff --git a/src/SSLClient.cpp b/src/SSLClient.cpp
index e3a8b09..40b46df 100644
--- a/src/SSLClient.cpp
+++ b/src/SSLClient.cpp
@@ -198,7 +198,7 @@ void SSLClient::startWriteRequest()
             boost::bind(&SSLClient::handleWriteRequest, this,
             boost::asio::placeholders::error));
     }
-    catch(std::runtime_error& ec)
+    catch(std::exception& ec)
     {
         ERROR_STREAM << "SSLClient::startWriteRequest() " << ec.what() << endl;
 
-- 
GitLab