Skip to content
Server.cpp 4.26 KiB
Newer Older
#include <Server.h>
#include <WorkerThread.h>
#include <boost/lexical_cast.hpp>

#include <google/protobuf/stubs/common.h>

namespace MetadataExporter_ns
{

//==============================================================================
//      Server::Server()
//==============================================================================
Server::Server(Tango::DeviceImpl* deviceImpl_p, Configuration::SP configuration_sp) :
    Tango::LogAdapter(deviceImpl_p), m_deviceImpl_p(deviceImpl_p),
    m_configuration_sp(configuration_sp)
{
    DEBUG_STREAM << "Server::Server()" << endl;
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    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;

    m_status="Disconnected";
}

//==============================================================================
//      Server::Server()
//==============================================================================
Server::~Server()
{
    DEBUG_STREAM << "Server::~Server()" << endl;
    m_ioService_sp->stop();
    if(m_threadGroup_sp)
    {
        m_threadGroup_sp->interrupt_all();
        m_threadGroup_sp->join_all();

    google::protobuf::ShutdownProtobufLibrary();
}

//==============================================================================
//      Server::start()
//==============================================================================
void Server::start()
{
    DEBUG_STREAM << "Server::start()" << endl;
    m_threadGroup_sp.reset(new boost::thread_group);
    unsigned int workerNumber = m_configuration_sp->getWorkerNumber();
    WorkerThread worker(m_deviceImpl_p, m_ioService_sp);
    for(unsigned int i=0; i<workerNumber; ++i)
        m_threadGroup_sp->add_thread(new boost::thread(&WorkerThread::run, worker));
}

//==============================================================================
//      Server::stop()
//==============================================================================
void Server::stop()
{
    DEBUG_STREAM << "Server::stop()" << endl;
    m_ioService_sp->stop();
    if(m_threadGroup_sp)
    {
        m_threadGroup_sp->interrupt_all();
        m_threadGroup_sp->join_all();
    }
//==============================================================================
//      Client::readState()
//==============================================================================
Tango::DevState Server::readState()
{
    DEBUG_STREAM << "Server::readState()" << endl;

    boost::mutex::scoped_lock stateLock(m_stateMutex);

    return m_state;
}

//==============================================================================
//      Client::getStatus()
//==============================================================================
std::string Server::readStatus()
{
    DEBUG_STREAM << "Server::readStatus()" << endl;

    boost::mutex::scoped_lock statusLock(m_stateMutex);

    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;
    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()
//==============================================================================
void Server::handleAccept(Session::SP session_sp,
    const boost::system::error_code& ec)
{
    DEBUG_STREAM << "Server::handleAccept()" << endl;

    if(!ec)
        session_sp->start();
    else
        WARN_STREAM << "Server::handleAccept() " << ec.message() << endl;

    startAccept();
}

}   //namespace