Commit 4da44754 authored by Marco De Marco's avatar Marco De Marco
Browse files

Plain connection basic test works

parent 21ceb825
Loading
Loading
Loading
Loading
+69 −0
Original line number Diff line number Diff line
#include <PlainSession.h>
#include <boost/bind.hpp>

namespace MetadataExporter_ns
{
@@ -49,6 +50,74 @@ boost::asio::ip::tcp::socket& PlainSession::getSocket()
void PlainSession::start()
{
    DEBUG_STREAM << "PlainSession::start()" << endl;

    INFO_STREAM << "CONNECTED TO " << m_socket.remote_endpoint() << endl;

    BufferSP recvBuffer_sp( new Buffer );

    m_socket.async_read_some(boost::asio::buffer(*recvBuffer_sp),
        m_strand.wrap(boost::bind(&PlainSession::handleRequest,
        shared_from_this(), recvBuffer_sp, boost::asio::placeholders::bytes_transferred,
        boost::asio::placeholders::error)));
}

//==============================================================================
//      PlainSession::handleRequest()
//==============================================================================
void PlainSession::handleRequest(BufferSP recvBuffer_sp, std::size_t size,
    const boost::system::error_code& ec)
{
    DEBUG_STREAM << "PlainSession::handleRequest()" << endl;

    if(!ec)
    {
        BufferSP sendBuffer_sp( new Buffer );

        for(std::size_t i=0; i<size; ++i)
        {
            (*sendBuffer_sp)[i] = toupper( (*recvBuffer_sp)[i] );
        }

        boost::asio::async_write(m_socket, boost::asio::buffer(*sendBuffer_sp, size),
            m_strand.wrap(boost::bind(&PlainSession::handleResponse,
            shared_from_this(), sendBuffer_sp, boost::asio::placeholders::bytes_transferred,
            boost::asio::placeholders::error)));
    }
    else if(ec == boost::asio::error::eof)
    {
        INFO_STREAM << "DICONNECTED FROM " << m_socket.remote_endpoint() << endl;
    }
    else
    {
        WARN_STREAM << "ERROR FROM " << m_socket.remote_endpoint() << endl;
    }
}

//==============================================================================
//      PlainSession::handleResponse()
//==============================================================================
void PlainSession::handleResponse(BufferSP sendBuffer_sp, std::size_t size,
    const boost::system::error_code& ec)
{
    DEBUG_STREAM << "PlainSession::handleResponse()" << endl;

    if(!ec)
    {
        BufferSP recvBuffer_sp( new Buffer );

        m_socket.async_read_some(boost::asio::buffer(*recvBuffer_sp),
            m_strand.wrap(boost::bind(&PlainSession::handleRequest,
            shared_from_this(), recvBuffer_sp, boost::asio::placeholders::bytes_transferred,
            boost::asio::placeholders::error)));
    }
    else if(ec == boost::asio::error::eof)
    {
        INFO_STREAM << "DICONNECTED FROM " << m_socket.remote_endpoint() << endl;
    }
    else
    {
        WARN_STREAM << "ERROR FROM " << m_socket.remote_endpoint() << endl;
    }
}

}   //namespace
 No newline at end of file
+9 −1
Original line number Diff line number Diff line
@@ -5,10 +5,13 @@

#include <tango.h>

#include <boost/enable_shared_from_this.hpp>

namespace MetadataExporter_ns
{

class PlainSession : public Session
class PlainSession : public Session,
    public boost::enable_shared_from_this<PlainSession>
{
//------------------------------------------------------------------------------
//	[Protected] Constructor destructor deleter
@@ -41,6 +44,11 @@ protected:
//------------------------------------------------------------------------------
//  [Protected] Utilities methods
//------------------------------------------------------------------------------
    virtual void handleRequest(BufferSP, std::size_t,
        const boost::system::error_code&);

    virtual void handleResponse(BufferSP, std::size_t,
        const boost::system::error_code&);

//------------------------------------------------------------------------------
//	[Protected] Class variables
+64 −1
Original line number Diff line number Diff line
#include <SSLSession.h>
#include <boost/asio/ssl/stream.hpp>
#include <boost/bind.hpp>

namespace MetadataExporter_ns
{
@@ -55,4 +55,67 @@ void SSLSession::start()
    DEBUG_STREAM << "SSLSession::start()" << endl;
}

//==============================================================================
//      SSLSession::handleRequest()
//==============================================================================
void SSLSession::handleRequest(BufferSP recvBuffer_sp, std::size_t size,
    const boost::system::error_code& ec)
{
    DEBUG_STREAM << "SSLSession::handleRequest()" << endl;

    if(!ec)
    {
        BufferSP sendBuffer_sp( new Buffer );

        for(std::size_t i=0; i<size; ++i)
        {
            (*sendBuffer_sp)[i] = toupper( (*recvBuffer_sp)[i] );
        }

        boost::asio::async_write(m_sslSocket, boost::asio::buffer(*sendBuffer_sp, size),
            m_strand.wrap(boost::bind(&SSLSession::handleResponse,
            shared_from_this(), sendBuffer_sp, boost::asio::placeholders::bytes_transferred,
            boost::asio::placeholders::error)));
    }
    else if(ec == boost::asio::error::eof)
    {
        INFO_STREAM << "DICONNECTED FROM "
            << m_sslSocket.lowest_layer().remote_endpoint() << endl;
    }
    else
    {
        WARN_STREAM << "ERROR FROM "
            << m_sslSocket.lowest_layer().remote_endpoint() << endl;
    }
}

//==============================================================================
//      SSLSession::handleResponse()
//==============================================================================
void SSLSession::handleResponse(BufferSP sendBuffer_sp, std::size_t size,
        const boost::system::error_code& ec)
{
    DEBUG_STREAM << "SSLSession::handleResponse()" << endl;

    if(!ec)
    {
        BufferSP recvBuffer_sp( new Buffer );

        m_sslSocket.async_read_some(boost::asio::buffer(*recvBuffer_sp),
            m_strand.wrap(boost::bind(&SSLSession::handleRequest,
            shared_from_this(), recvBuffer_sp, boost::asio::placeholders::bytes_transferred,
            boost::asio::placeholders::error)));
    }
    else if(ec == boost::asio::error::eof)
    {
        INFO_STREAM << "DICONNECTED FROM "
            << m_sslSocket.lowest_layer().remote_endpoint() << endl;
    }
    else
    {
        WARN_STREAM << "ERROR FROM "
            << m_sslSocket.lowest_layer().remote_endpoint() << endl;
    }
}

}   //namespace
+8 −1
Original line number Diff line number Diff line
@@ -5,12 +5,14 @@

#include <tango.h>

#include <boost/enable_shared_from_this.hpp>
#include <boost/asio/ssl.hpp>

namespace MetadataExporter_ns
{

class SSLSession : public Session
class SSLSession : public Session,
        public boost::enable_shared_from_this<SSLSession>
{
//------------------------------------------------------------------------------
//	[Protected] Constructor destructor deleter
@@ -45,6 +47,11 @@ protected:
//------------------------------------------------------------------------------
//  [Protected] Utilities methods
//------------------------------------------------------------------------------
    virtual void handleRequest(BufferSP, std::size_t,
        const boost::system::error_code&);

    virtual void handleResponse(BufferSP, std::size_t,
        const boost::system::error_code&);

//------------------------------------------------------------------------------
//	[Protected] Class variables
+19 −2
Original line number Diff line number Diff line
@@ -3,8 +3,9 @@

#include <tango.h>

#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/shared_ptr.hpp>

namespace MetadataExporter_ns
{
@@ -12,6 +13,13 @@ namespace MetadataExporter_ns
class Session : public Tango::LogAdapter
{
public:
//------------------------------------------------------------------------------
//  [Public] Shared pointer typedef
//------------------------------------------------------------------------------
typedef boost::array< char, 256 > Buffer;

typedef boost::shared_ptr< Buffer > BufferSP;

//------------------------------------------------------------------------------
//  [Public] Shared pointer typedef
//------------------------------------------------------------------------------
@@ -35,10 +43,19 @@ public:
    virtual void start() = 0;

protected:
//------------------------------------------------------------------------------
//  [Protected] Utilities methods
//------------------------------------------------------------------------------
    virtual void handleRequest(BufferSP, std::size_t,
        const boost::system::error_code&) = 0;

    virtual void handleResponse(BufferSP, std::size_t,
        const boost::system::error_code&) = 0;

//------------------------------------------------------------------------------
//	[Protected] Class variables
//------------------------------------------------------------------------------
    //Sychronization mechanism
    //Synchronization mechanism
    boost::asio::io_service::strand m_strand;
};