Commit 5a5de31f authored by Marco De Marco's avatar Marco De Marco
Browse files

Resolver moved in server base class

parent 70c3666b
Loading
Loading
Loading
Loading
+0 −10
Original line number Diff line number Diff line
@@ -13,16 +13,6 @@ PlainServer::PlainServer(Tango::DeviceImpl* deviceImpl_p,
{
    DEBUG_STREAM << "PlainServer::PlainServer()" << endl;

    boost::asio::ip::tcp::endpoint endpoint = resolveEndpoint();

    m_acceptor_sp->open(endpoint.protocol());

    m_acceptor_sp->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));

    m_acceptor_sp->bind(endpoint);

    m_acceptor_sp->listen();

    startAccept();
}

+15 −40
Original line number Diff line number Diff line
#include <SSLServer.h>
#include <SSLSession.h>

#include <boost/filesystem.hpp>
#include <boost/bind.hpp>

namespace MetadataExporter_ns
@@ -16,30 +15,6 @@ SSLServer::SSLServer(Tango::DeviceImpl* deviceImpl_p,
{
    DEBUG_STREAM << "SSLServer::SSLServer()" << endl;

    boost::filesystem::path certificatePath = m_configuration_sp->getCertificateFile();

    DEBUG_STREAM << "SSLServer::SSLServer() certificate "
        << certificatePath.string() << endl;

    if(!boost::filesystem::exists(certificatePath))
        throw std::runtime_error("Certificate file not exists");

    boost::filesystem::path privateKeyPath = m_configuration_sp->getPrivateKeyFile();

    DEBUG_STREAM << "SSLServer::SSLServer() private key "
        << privateKeyPath.string() << endl;

    if(!boost::filesystem::exists(privateKeyPath))
        throw std::runtime_error("Private key file not exists");

    boost::filesystem::path dHTempPath = m_configuration_sp->getDHTempFile();

    DEBUG_STREAM << "SSLServer::SSLServer() Diffie Hellman file "
        << dHTempPath.string() << endl;

    if(!boost::filesystem::exists(dHTempPath))
        throw std::runtime_error("Diffie Hellman file not exists");

    m_context_sp.reset(new boost::asio::ssl::context(*m_ioService_sp,
        boost::asio::ssl::context::sslv23));

@@ -47,24 +22,25 @@ SSLServer::SSLServer(Tango::DeviceImpl* deviceImpl_p,
        boost::asio::ssl::context::no_sslv2 |
        boost::asio::ssl::context::single_dh_use);

    //@todo: ssl certificate password
    m_context_sp->set_password_callback(boost::bind(&SSLServer::getPassword, this));

    m_context_sp->use_certificate_chain_file(certificatePath.string());

    m_context_sp->use_private_key_file(privateKeyPath.string(),
        boost::asio::ssl::context::pem);

    m_context_sp->use_tmp_dh_file(dHTempPath.string());
    std::string certificateFile = m_configuration_sp->getCertificateFile();
    std::string privateKey = m_configuration_sp->getPrivateKeyFile();
    std::string dHTempFile = m_configuration_sp->getDHTempFile();

    boost::asio::ip::tcp::endpoint endpoint = resolveEndpoint();
    #ifdef VERBOSE_DEBUG
        INFO_STREAM << "CERTIFICATE FILE: " << certificateFile << endl;
        INFO_STREAM << "-------------------------------------------------" << endl;
        INFO_STREAM << "PRIVATE KEY FILE: " << privateKey << endl;
        INFO_STREAM << "-------------------------------------------------" << endl;
        INFO_STREAM << "DH TEMPORARY FILE: " << dHTempFile << endl;
        INFO_STREAM << "-------------------------------------------------" << endl;
    #endif

    m_acceptor_sp->open(endpoint.protocol());

    m_acceptor_sp->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));

    m_acceptor_sp->bind(endpoint);

    m_acceptor_sp->listen();
    m_context_sp->use_certificate_chain_file(certificateFile);
    m_context_sp->use_private_key_file(privateKey, boost::asio::ssl::context::pem);
    m_context_sp->use_tmp_dh_file(dHTempFile);

    startAccept();
}
@@ -111,7 +87,6 @@ std::string SSLServer::getPassword()
{
    DEBUG_STREAM << "SSLServer::getPassword()" << endl;

    //@todo: ssl certificate password
    return "test";
}

+23 −23
Original line number Diff line number Diff line
@@ -19,14 +19,29 @@ Server::Server(Tango::DeviceImpl* deviceImpl_p, Configuration::SP configuration_

    GOOGLE_PROTOBUF_VERIFY_VERSION;

    m_ioService_sp.reset(new boost::asio::io_service);
    m_dBManager_sp = DBManager::create(deviceImpl_p, configuration_sp);

    m_ioService_sp.reset(new boost::asio::io_service);
    m_work_sp.reset(new boost::asio::io_service::work(*m_ioService_sp));

    m_acceptor_sp.reset(new boost::asio::ip::tcp::acceptor(*m_ioService_sp));

    m_state = Tango::OFF;
    std::string localHost = m_configuration_sp->getLocalHost();
    unsigned int localPort = m_configuration_sp->getLocalPort();

    INFO_STREAM << "Listen on " << localHost << ":" << localPort << endl;

    boost::asio::ip::tcp::resolver::query query(localHost,
        boost::lexical_cast<std::string>(localPort));

    boost::asio::ip::tcp::resolver resolver(*m_ioService_sp);
    boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);

    m_acceptor_sp->open(endpoint.protocol());
    m_acceptor_sp->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
    m_acceptor_sp->bind(endpoint);
    m_acceptor_sp->listen();

    m_state = Tango::OFF;
    m_status="Disconnected";
}

@@ -37,6 +52,11 @@ Server::~Server()
{
    DEBUG_STREAM << "Server::~Server()" << endl;

    m_state = Tango::OFF;
    m_status="Disconnected";

    m_acceptor_sp->close();

    m_ioService_sp->stop();

    if(m_threadGroup_sp)
@@ -106,26 +126,6 @@ std::string Server::readStatus()
    return m_status;
}

//==============================================================================
//      Server::resolveEndpoint()
//==============================================================================
boost::asio::ip::tcp::endpoint Server::resolveEndpoint()
{
    DEBUG_STREAM << "Server::resolveEndpoint()" << endl;

    DEBUG_STREAM << "Server::resolveEndpoint() " << m_configuration_sp->getLocalHost()
        << ":" << m_configuration_sp->getLocalPort() << endl;

    //TODO: bad_lexical_cast
    boost::asio::ip::tcp::resolver::query query(m_configuration_sp->getLocalHost(),
        boost::lexical_cast<std::string>(m_configuration_sp->getLocalPort()));

    boost::asio::ip::tcp::resolver resolver(*m_ioService_sp);

    //TODO: system::system_error (runtime_error)
    return *resolver.resolve(query);
}

//==============================================================================
//      Server::handleAccept()
//==============================================================================
+4 −5
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@

#include <Configuration.h>
#include <Session.h>
#include <DBManager.h>

#include <tango.h>

@@ -45,11 +46,6 @@ public:
    virtual std::string readStatus();

protected:
//------------------------------------------------------------------------------
//  [Protected] Endpoint resolution method
//------------------------------------------------------------------------------
    virtual boost::asio::ip::tcp::endpoint resolveEndpoint();

//------------------------------------------------------------------------------
//  [Protected] Incoming connection methods
//------------------------------------------------------------------------------
@@ -66,6 +62,9 @@ protected:
    //Contains configuration parameters
    Configuration::SP m_configuration_sp;

    //Database data access class
    DBManager::SP m_dBManager_sp;

    //Io service used for async operations
    boost::shared_ptr<boost::asio::io_service> m_ioService_sp;