Skip to content
ProtocolManager.cpp 6.79 KiB
Newer Older
#include <ProtocolManager.h>

#include <boost/date_time.hpp>

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;

    m_isAuthorised = false;
    m_hasMoreData = true;
}

//==============================================================================
//      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::waitBeforeRequest()
//==============================================================================
bool ProtocolManager::waitBeforeRequest()
{
    DEBUG_STREAM << "ProtocolManager::waitBeforeRequest()" << endl;

    return !m_hasMoreData;
}

//==============================================================================
//      ProtocolManager::resetProtocolStatus()
//==============================================================================
void ProtocolManager::resetProtocolStatus()
{
    DEBUG_STREAM << "ProtocolManager::resetProtocolStatus()" << endl;

    m_isAuthorised = false;
    m_hasMoreData = true;
}

//==============================================================================
//      ProtocolManager::createRequest()
//==============================================================================
RequestSP ProtocolManager::createRequest()
    throw(std::runtime_error, std::out_of_range)
{
    DEBUG_STREAM << "ProtocolManager::createRequest()" << endl;

    RequestSP request_sp;

    if(!m_isAuthorised)
    {
        request_sp = createAuthroisation();
        request_sp = createData();
    }

    if(!request_sp->IsInitialized())
        throw std::runtime_error("Not initialized request!");

    return request_sp;
}

//==============================================================================
//      ProtocolManager::processResponse()
//==============================================================================
void ProtocolManager::processResponse(ResponseSP response_sp)
    throw(std::runtime_error, std::out_of_range)
{
    DEBUG_STREAM << "ProtocolManager::processResponse()" << endl;

    if(!response_sp->IsInitialized())
        throw std::runtime_error("Not initialized response!");

    switch(response_sp->type())
    {
        case Response::AUTHORIZATION:
        {
            processAuthroisation(response_sp);
            break;
        }
        case Response::DATA:
        {
            processData(response_sp);
            break;
        }
        default:
            throw std::runtime_error("Unknown response type");
    }
}

//==============================================================================
//      ProtocolManager::createAuthroisation()
//==============================================================================
RequestSP ProtocolManager::createAuthroisation() throw(std::runtime_error)
{
    DEBUG_STREAM << "ProtocolManager::createAuthroisation()" << endl;

    RequestSP request_sp(new Request);

    request_sp->set_type(Request::AUTHORIZATION);

    std::string user = m_configuration_sp->getRemoteUsername();
    std::string password = m_configuration_sp->getRemotePassword();

    #ifdef VERBOSE_DEBUG
        INFO_STREAM << "ProtocolManager::createAuthroisation() Send username "
            << user << " password " << password << " to " << m_remoteEndpoint << endl;
    #else
        INFO_STREAM << "ProtocolManager::createAuthroisation() Send to "
            << m_remoteEndpoint << endl;
    #endif

    Request::Authorization* authorization = request_sp->mutable_authorization();
    authorization->set_username(user);
    authorization->set_password(password);

    return request_sp;
}

//==============================================================================
//      ProtocolManager::createData()
//==============================================================================
RequestSP ProtocolManager::createData() throw(std::runtime_error, std::out_of_range)
{
    DEBUG_STREAM << "ProtocolManager::createData()" << endl;
//==============================================================================
//      ProtocolManager::processAuthroisation()
//==============================================================================
void ProtocolManager::processAuthroisation(ResponseSP response_sp)
    throw(std::runtime_error)
{
    DEBUG_STREAM << "ProtocolManager::processAuthroisation()" << endl;

    const Response::Authorization& authorization = response_sp->authorization();

    if(authorization.state() == Response::Authorization::ACCEPTED)
    {
        INFO_STREAM << "ProtocolManager::processAuthroisation() State ACCEPTED "
            << "status " << authorization.status() << " from " << m_remoteEndpoint << endl;

        m_isAuthorised = true;
    }
    else
    {
        ERROR_STREAM << "ProtocolManager::processAuthroisation() State REJECTED "
            << "status " << authorization.status() << " from " << m_remoteEndpoint << endl;

        throw std::runtime_error(authorization.status());
    }
}

//==============================================================================
//      ProtocolManager::processData()
//==============================================================================
void ProtocolManager::processData(ResponseSP response_sp)
        throw(std::runtime_error, std::out_of_range)
{
    DEBUG_STREAM << "ProtocolManager::processData()" << endl;
}

}   //namespace