Commit 2b1422fe authored by Marco De Marco's avatar Marco De Marco
Browse files

Protocol manager class added

parent c05fc9be
Loading
Loading
Loading
Loading
+33 −33
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ DBManager::DBManager(Tango::DeviceImpl* deviceImpl_p,
//==============================================================================
DBManager::~DBManager()
{
    DEBUG_STREAM << "DBManager::DBManager()" << endl;
    DEBUG_STREAM << "DBManager::~DBManager()" << endl;
}

//==============================================================================
@@ -46,22 +46,22 @@ void DBManager::connect() throw(soci::soci_error)
{
    DEBUG_STREAM << "DBManager::connect()" << endl;

    std::stringstream connection;
    connection << " host=" << m_configuration_sp->getDatabaseHost();
    connection << " port=" << m_configuration_sp->getDatabasePort();
    connection << " user=" << m_configuration_sp->getDatabaseUsername();
    connection << " password=" << m_configuration_sp->getDatabasePassword();

    unsigned int connectionNumber = m_configuration_sp->getDatabaseConnectionNumber();

    for(unsigned int i=0; i<connectionNumber; ++i)
    {
        m_connectionPool_sp->at(i).open(soci::mysql, connection.str());

        #ifdef VERBOSE_DEBUG
            INFO_STREAM << "CONNECTION: " << connection.str() << " -> OPEN" << endl;
        #endif
    }
//    std::stringstream connection;
//    connection << " host=" << m_configuration_sp->getDatabaseHost();
//    connection << " port=" << m_configuration_sp->getDatabasePort();
//    connection << " user=" << m_configuration_sp->getDatabaseUsername();
//    connection << " password=" << m_configuration_sp->getDatabasePassword();
//
//    unsigned int connectionNumber = m_configuration_sp->getDatabaseConnectionNumber();
//
//    for(unsigned int i=0; i<connectionNumber; ++i)
//    {
//        m_connectionPool_sp->at(i).open(soci::mysql, connection.str());
//
//        #ifdef VERBOSE_DEBUG
//            INFO_STREAM << "CONNECTION: " << connection.str() << " -> OPEN" << endl;
//        #endif
//    }
}

//==============================================================================
@@ -71,22 +71,22 @@ void DBManager::disconnect()
{
    DEBUG_STREAM << "DBManager::disconnect()" << endl;

    std::stringstream connection;
    connection << " host=" << m_configuration_sp->getDatabaseHost();
    connection << " port=" << m_configuration_sp->getDatabasePort();
    connection << " user=" << m_configuration_sp->getDatabaseUsername();
    connection << " password=" << m_configuration_sp->getDatabasePassword();

    unsigned int connectionNumber = m_configuration_sp->getDatabaseConnectionNumber();

    for(unsigned int i=0; i<connectionNumber; ++i)
    {
        m_connectionPool_sp->at(i).close();

        #ifdef VERBOSE_DEBUG
            INFO_STREAM << "CONNECTION: " << connection.str() << " -> CLOSE" << endl;
        #endif
    }
//    std::stringstream connection;
//    connection << " host=" << m_configuration_sp->getDatabaseHost();
//    connection << " port=" << m_configuration_sp->getDatabasePort();
//    connection << " user=" << m_configuration_sp->getDatabaseUsername();
//    connection << " password=" << m_configuration_sp->getDatabasePassword();
//
//    unsigned int connectionNumber = m_configuration_sp->getDatabaseConnectionNumber();
//
//    for(unsigned int i=0; i<connectionNumber; ++i)
//    {
//        m_connectionPool_sp->at(i).close();
//
//        #ifdef VERBOSE_DEBUG
//            INFO_STREAM << "CONNECTION: " << connection.str() << " -> CLOSE" << endl;
//        #endif
//    }
}

}   //namespace
+4 −7
Original line number Diff line number Diff line
/*
 * File:   DBManager.h
 * Author: mdm
 *
 * Created on October 24, 2013, 2:57 PM
 */

#ifndef DBMANAGER_H
#define	DBMANAGER_H

@@ -13,6 +6,7 @@
#include <tango.h>

#include <boost/scoped_ptr.hpp>
#include <boost/thread/mutex.hpp>

#include <soci/error.h>
#include <soci/session.h>
@@ -69,6 +63,9 @@ protected:
    //Configuration shared pointer
    Configuration::SP m_configuration_sp;

    //Connection pool mutex
    boost::mutex m_connectionPoolMutex;

    //Database connection pool scoped pointer
    boost::scoped_ptr<soci::connection_pool> m_connectionPool_sp;
};
+2 −2
Original line number Diff line number Diff line
@@ -194,6 +194,7 @@ void MetadataExporter::get_device_property()

    //Authorised user map [user password]
    std::map<const std::string, const std::string> authorisedUsersMap;

	/*----- PROTECTED REGION END -----*/	//	MetadataExporter::get_device_property_before


@@ -654,8 +655,7 @@ void MetadataExporter::checkIfFileExists(std::string fileName)
    if(!boost::filesystem::exists(path))
    {
        std::stringstream errorStream;
        errorStream << "File " << fileName
            << " not exists" << std::endl;
        errorStream << "File " << fileName << " not exists" << std::endl;
        throw std::invalid_argument(errorStream.str());
    }

+29 −26
Original line number Diff line number Diff line
@@ -26,8 +26,6 @@ PlainSession::~PlainSession()
{
    DEBUG_STREAM << "PlainSession::~PlainSession()" << endl;

    INFO_STREAM << m_plainSocket.remote_endpoint() << " CONNECTED" << endl;

    boost::system::error_code errorCode;

    m_plainSocket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, errorCode);
@@ -65,7 +63,7 @@ void PlainSession::start()
{
    DEBUG_STREAM << "PlainSession::start()" << endl;

    INFO_STREAM << m_plainSocket.remote_endpoint() << " DISCONNECTED" << endl;
    INFO_STREAM << m_plainSocket.remote_endpoint() << " CONNECTED" << endl;

    startReadRequestHeader();
}
@@ -80,10 +78,8 @@ void PlainSession::startReadRequestHeader()
        m_readBuff.resize(HEADER_SIZE);

        boost::asio::async_read(m_plainSocket, boost::asio::buffer(m_readBuff),
            m_strand.wrap(
                boost::bind(
                    &PlainSession::handleReadRequestHeader, shared_from_this(),
                    boost::asio::placeholders::error)));
            m_strand.wrap(boost::bind(&PlainSession::handleReadRequestHeader,
            shared_from_this(), boost::asio::placeholders::error)));
}

//==============================================================================
@@ -102,10 +98,8 @@ void PlainSession::startReadRequestBody(boost::uint32_t bodySize)
        boost::asio::buffer(&m_readBuff[HEADER_SIZE], bodySize);

    boost::asio::async_read(m_plainSocket, mutableBuffer,
        m_strand.wrap(
            boost::bind(
                &PlainSession::handleReadRequestBody, shared_from_this(),
                boost::asio::placeholders::error)));
        m_strand.wrap(boost::bind(&PlainSession::handleReadRequestBody,
        shared_from_this(), boost::asio::placeholders::error)));
}

//==============================================================================
@@ -115,11 +109,13 @@ void PlainSession::startWriteResponse()
{
    DEBUG_STREAM << "PlainSession::startWriteResponse()" << endl;

    try
    {
        RequestSP request_sp(new Request);

        request_sp->ParseFromArray(&m_readBuff[HEADER_SIZE], m_readBuff.size() - HEADER_SIZE);

    ResponseSP response_sp = prepareResponse(request_sp);
        ResponseSP response_sp = m_protocolManager_sp->prepareResponse(request_sp);

        boost::uint32_t bodySize = response_sp->ByteSize();

@@ -134,10 +130,17 @@ void PlainSession::startWriteResponse()
        response_sp->SerializeToArray(&writeBuff[HEADER_SIZE], bodySize);

        boost::asio::async_write(m_plainSocket, boost::asio::buffer(writeBuff),
        m_strand.wrap(
            boost::bind(
                &PlainSession::handleWriteResponse, shared_from_this(),
                boost::asio::placeholders::error)));
            m_strand.wrap(boost::bind(&PlainSession::handleWriteResponse,
                shared_from_this(), boost::asio::placeholders::error)));
    }
    catch(std::runtime_error& ec)
    {
        ERROR_STREAM << "SSLSession::startWriteResponse() " << ec.what() << endl;
    }
    catch(...)
    {
        ERROR_STREAM << "SSLSession::startWriteResponse() unknown error" << endl;
    }
}

}   //namespace
 No newline at end of file
+188 −0
Original line number Diff line number Diff line
#include <ProtocolManager.h>

namespace MetadataExporter_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_isAuthenticated = false;
    m_isValidated = false;
}

//==============================================================================
//      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::prepareResponse()
//==============================================================================
ResponseSP ProtocolManager::prepareResponse(RequestSP request_sp)
    throw(std::runtime_error)
{
    DEBUG_STREAM << "ProtocolManager::prepareResponse()" << endl;

    ResponseSP response_sp;

    switch(request_sp->type())
    {
        case Request::AUTHORIZATION:
        {
            response_sp = prepareAuthroisation(request_sp);
            break;
        }
        case Request::VALIDATION:
        {
            response_sp = prepareValidation(request_sp);
            break;
        }
        case Request::METADATA:
        {
            response_sp = prepareMetadata(request_sp);
            break;
        }
        default:
            throw std::runtime_error("Unknown request type!");
    }

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

    return response_sp;
}

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

    ResponseSP response_sp(new Response());

    response_sp->set_type(Response::AUTHORIZATION);

    Response::Authorization* auth_resp = response_sp->mutable_authorization();

    if(!m_isAuthenticated)
    {
        const Request::Authorization& auth_req = request_sp->authorization();
        std::string username =  auth_req.username();
        std::string password = auth_req.password();

        if(m_configuration_sp->isUserAuthorized(username, password))
        {
            INFO_STREAM << "ProtocolManager::prepareAuthroisation() "
                << "Authorization accepted" << endl;

            m_isAuthenticated = true;

            auth_resp->set_state(Response::Authorization::ACCEPTED);
            auth_resp->set_status("Authorization accepted");
        }
        else
        {
            WARN_STREAM << "ProtocolManager::prepareAuthroisation() "
                << "Invalid username or password" << endl;

            m_isAuthenticated = false;

            auth_resp->set_state(Response::Authorization::REJECTED);
            auth_resp->set_status("Invalid username or password");
        }
    }
    else
    {
        WARN_STREAM << "ProtocolManager::prepareAuthroisation() "
            << "Already authorized" << endl;

        auth_resp->set_state(Response::Authorization::REJECTED);
        auth_resp->set_status("Already authorized");
    }

    return response_sp;
}

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

    ResponseSP response_sp(new Response());

    response_sp->set_type(Response::VALIDATION);

    Response::Validation* validation = response_sp->mutable_validation();

    if(!m_isValidated)
    {
        INFO_STREAM << "ProtocolManager::prepareValidation() "
            << "Validation accepted" << endl;

        m_isValidated = true;

        validation->set_state(Response::Validation::ACCEPTED);
        validation->set_status("Validation accepted");
    }
    else
    {
        WARN_STREAM << "ProtocolManager::prepareValidation() "
            << "Already validated" << endl;

        validation->set_state(Response::Validation::REJECTED);
        validation->set_status("Already validated");
    }

    return response_sp;
}

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

    ResponseSP response_sp(new Response());

    response_sp->set_type(Response::METADATA);

    Response::Metadata* metadata = response_sp->mutable_metadata();

    metadata->set_state(Response::Metadata::ACCEPTED);
    metadata->set_status("Metadata ready");
    metadata->set_partial(1);
    metadata->set_total(1);

    return response_sp;
}

}   //namespace
Loading