Skip to content
ProtocolManager.cpp 8.28 KiB
Newer Older
#include <ProtocolManager.h>
#include <boost/filesystem/v3/path.hpp>
#include <stdexcept>

namespace DataImporter_ns
{

//==============================================================================
//      ProtocolManager::ProtocolManager()
//==============================================================================
ProtocolManager::ProtocolManager(Tango::DeviceImpl* deviceImpl_p,
    Configuration::SP configuration_sp, DBManager::SP dBManager_sp) :
    Tango::LogAdapter(deviceImpl_p), m_configuration_sp(configuration_sp),
    m_dBManager_sp(dBManager_sp)
{
    DEBUG_STREAM << "ProtocolManager::ProtocolManager()" << endl;
}

//==============================================================================
//      ProtocolManager::ProtocolManager()
//==============================================================================
ProtocolManager::~ProtocolManager()
{
    DEBUG_STREAM << "ProtocolManager::~ProtocolManager()" << endl;
}

//==============================================================================
//      ProtocolManager::ProtocolManager()
//==============================================================================
ProtocolManager::SP ProtocolManager::create(Tango::DeviceImpl* deviceImpl_p,
    Configuration::SP configuration_sp, DBManager::SP dBManager_sp)
{
    ProtocolManager::SP d_sp(new ProtocolManager(deviceImpl_p, configuration_sp,
        dBManager_sp), ProtocolManager::Deleter());

    return d_sp;
}

//==============================================================================
//      ProtocolManager::ProtocolManager()
//==============================================================================
void ProtocolManager::setRemoteEndpoint(std::string remoteEndpoint)
{
    DEBUG_STREAM << "ProtocolManager::setRemoteEndpoint()" << endl;

    m_remoteEndpoint = remoteEndpoint;
}

//==============================================================================
//      ProtocolManager::updateNewFileList()
//==============================================================================
void ProtocolManager::updateNewFileList() throw(soci::soci_error)
    DEBUG_STREAM << "ProtocolManager::updateNewFileList()" << endl;
    boost::posix_time::ptime m_lastTimestamp =
        m_dBManager_sp->retrieveLastTimestamp();
    DEBUG_STREAM << "ProtocolManager::updateNewFileList() last timestamp "
        << boost::posix_time::to_simple_string(m_lastTimestamp) << endl;
    m_newFileRowset_sp = m_dBManager_sp->retrieveNewFiles(m_lastTimestamp);
    m_newFileRowsetIt = m_newFileRowset_sp->begin();
//==============================================================================
//      ProtocolManager::updateFiledFileList()
//==============================================================================
void ProtocolManager::updateFiledFileList() throw(soci::soci_error)
    DEBUG_STREAM << "ProtocolManager::updateFiledFileList()" << endl;
    m_failedFileRowset_sp = m_dBManager_sp->retrieveFailedFiles();
    m_failedFileRowsetIt = m_failedFileRowset_sp->begin();
}

//==============================================================================
//      ProtocolManager::isNewFileListEmpty()
//==============================================================================
bool ProtocolManager::isNewFileListEmpty()
    DEBUG_STREAM << "ProtocolManager::isNewFileListEmpty()" << endl;
    return !(m_newFileRowset_sp &&
        m_newFileRowsetIt != m_newFileRowset_sp->end());
}

//==============================================================================
//      ProtocolManager::isFailedFileListEmpty()
//==============================================================================
bool ProtocolManager::isFailedFileListEmpty()
    DEBUG_STREAM << "ProtocolManager::isFailedFileListEmpty()" << endl;
    return !(m_failedFileRowset_sp &&
        m_failedFileRowsetIt != m_failedFileRowset_sp->end());
//==============================================================================
//      ProtocolManager::createNewFileRequest()
//==============================================================================
RequestSP ProtocolManager::createNewFileRequest() throw(std::runtime_error)
    DEBUG_STREAM << "ProtocolManager::createNewFileRequest()" << endl;
    if(isNewFileListEmpty())
        throw std::runtime_error("New file list is empty");

    RequestSP request_sp(new Request);

    request_sp->set_username(m_configuration_sp->getDatabaseUsername());
    request_sp->set_password(m_configuration_sp->getDatabasePassword());
    request_sp->set_schema(m_configuration_sp->getDatabaseSchema());
    request_sp->set_table(m_configuration_sp->getDatabaseTable());
    if(!m_newFileRowsetIt->get<0>())
        throw std::runtime_error("Empty file version found");

    int fileVersion = m_newFileRowsetIt->get<0>().get();
    if(!m_newFileRowsetIt->get<1>())
        throw std::runtime_error("Empty file name found");
    std::string fileName = m_newFileRowsetIt->get<1>().get();
    INFO_STREAM << "ProtocolManager::createTransfer() request new file "
        << fileName << " version " << fileVersion << endl;
    request_sp->set_file_version(fileVersion);
    request_sp->set_file_name(fileName);
    if(!request_sp->IsInitialized())
        throw std::runtime_error("Request not initialized");

    return request_sp;
//==============================================================================
//      ProtocolManager::createFailedFileRequest()
//==============================================================================
RequestSP ProtocolManager::createFailedFileRequest() throw(std::runtime_error)
    DEBUG_STREAM << "ProtocolManager::createFailedFileRequest()" << endl;
    if(isFailedFileListEmpty())
        throw std::runtime_error("Failed file list is empty");
    RequestSP request_sp(new Request);
    request_sp->set_username(m_configuration_sp->getDatabaseUsername());
    request_sp->set_password(m_configuration_sp->getDatabasePassword());
    request_sp->set_schema(m_configuration_sp->getDatabaseSchema());
    request_sp->set_table(m_configuration_sp->getDatabaseTable());
    if(!m_failedFileRowsetIt->get<0>())
        throw std::runtime_error("Empty file version found");
    int fileVersion = m_failedFileRowsetIt->get<0>().get();
    if(!m_failedFileRowsetIt->get<1>())
        throw std::runtime_error("Empty file name found");
    std::string fileName = m_failedFileRowsetIt->get<1>().get();
    INFO_STREAM << "ProtocolManager::createTransfer() request new file "
        << fileName << " version " << fileVersion << endl;
    request_sp->set_file_version(fileVersion);
    request_sp->set_file_name(fileName);
    if(!request_sp->IsInitialized())
        throw std::runtime_error("Request not initialized");
    return request_sp;
////==============================================================================
////      ProtocolManager::processData()
////==============================================================================
//void ProtocolManager::processTransfer(ResponseSP response_sp)
//        throw(std::runtime_error, std::out_of_range)
//{
//    DEBUG_STREAM << "ProtocolManager::processData()" << endl;
//
//    const Response::Transfer& transfer = response_sp->transfer();
//
//    if(transfer.state() == Response::Transfer::ACCEPTED)
//    {
//        INFO_STREAM << "ProtocolManager::processValidation() State ACCEPTED "
//            << "status " << transfer.status() << " from " << m_remoteEndpoint << endl;
//
//        INFO_STREAM << "PATH: " << transfer.file_path() << endl;
//        INFO_STREAM << "VERSION: " << transfer.file_version() << endl;
//        INFO_STREAM << "FILE: " << transfer.file_name() << endl;
//        INFO_STREAM << "SIZE: " << transfer.size() << endl;
//
//        m_isTransferRequest = true;
//
//        m_fileSize = transfer.size();
//
//        std::stringstream pathStream;
//        pathStream << "/" << m_configuration_sp->getStoragePath() << "/"
//            << transfer.file_version() << "/" << transfer.file_version();
//
//        m_fileDir = pathStream.str();
//        m_fileName = transfer.file_name();
//    }
//    else
//    {
//        ERROR_STREAM << "ProtocolManager::processValidation() State REJECTED "
//            << "status " << transfer.status() << " from " << m_remoteEndpoint << endl;
//
//        m_isTransferRequest = false;
//
//        throw std::runtime_error(transfer.status());
//    }
//}
//

}   //namespace