Commit 63ea83fb authored by AdamSimpson's avatar AdamSimpson
Browse files

Begin websocket refactor

parent 9a53340c
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -3,9 +3,9 @@
#include "Messenger.h"
#include "Logger.h"
#include "Messenger.h"
#include <boost/asio/io_service.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/read_until.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/process.hpp>
#include <boost/regex.hpp>
+9 −3
Original line number Diff line number Diff line
@@ -21,14 +21,20 @@ set(SOURCE_FILES_QUEUE
        Common/include/Logger.h
        Common/include/Messenger.h
        BuilderQueue/src/OpenStackBuilder.cpp
        BuilderQueue/include/OpenStackBuilder.h Common/src/BuilderData.cpp Common/src/Messenger.cpp Common/include/ClientData.h)
        BuilderQueue/include/OpenStackBuilder.h
        Common/src/BuilderData.cpp
        Common/src/Messenger.cpp
        Common/include/ClientData.h)

set(SOURCE_FILES_BUILDER
        Builder/src/main.cpp
        Common/include/BuilderData.h
        Common/src/Logger.cpp
        Common/include/Logger.h
        Common/include/Messenger.h Common/src/Messenger.cpp Builder/src/Builder.cpp Builder/include/Builder.h)
        Common/include/Messenger.h
        Common/src/Messenger.cpp
        Builder/src/Builder.cpp
        Builder/include/Builder.h)

# Files related to the API
set(SOURCE_FILES_CLIENT
@@ -82,7 +88,7 @@ target_link_libraries(builder_server ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(container_builder ${CMAKE_THREAD_LIBS_INIT})


find_package(Boost 1.64.0 COMPONENTS system coroutine filesystem serialization regex log log_setup thread program_options REQUIRED)
find_package(Boost 1.66.0 COMPONENTS system coroutine filesystem serialization regex log log_setup thread program_options REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(builder_queue ${Boost_LIBRARIES})
target_link_libraries(builder_server ${Boost_LIBRARIES})
+54 −59
Original line number Diff line number Diff line
@@ -4,99 +4,94 @@
#include <boost/asio.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/filesystem.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/write.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <iostream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
//#include <boost/archive/text_iarchive.hpp>
//#include <boost/archive/text_oarchive.hpp>
#include "BuilderData.h"
#include <boost/progress.hpp>
#include <boost/crc.hpp>
//#include <boost/progress.hpp>
//#include <boost/crc.hpp>
#include <memory.h>
#include "Logger.h"
#include "ClientData.h"

namespace asio = boost::asio;
using asio::ip::tcp;

// Enum to handle message type, used to ensure
enum class MessageType : unsigned char {
    string,
    builder,
    client_data,
    file,
    error
};

// Message header
struct Header {
    std::size_t size;
    MessageType type;
};
namespace websocket = boost::beast::websocket;

class Messenger {

public:
    // Create a client messenger by asyncronously connecting to the specified host
    explicit Messenger(asio::io_service &io_service, const std::string &host, const std::string &port,
                       asio::yield_context yield) : socket(io_service),
                                                    yield(yield) {
    explicit Messenger(asio::io_service &io_service,
                       const std::string &host,
                       const std::string &port,
                       asio::yield_context yield,
                       boost::system::error_code error) : web_socket(io_service) {
        do {
            tcp::resolver queue_resolver(io_service);
            asio::async_connect(socket, queue_resolver.resolve({host, port}), yield[error]);
            asio::async_connect(web_socket.next_layer(), queue_resolver.resolve({host, port}), yield[error]);
        } while (error);

        web_socket.async_handshake(host, "/", yield[error]);
    }

    // Create a server messenger by doing an async block listen on the specified port
    explicit Messenger(asio::io_service &io_service, const std::string &port, asio::yield_context yield) :
            socket(io_service),
            yield(yield) {
    explicit Messenger(asio::io_service &io_service,
                       const std::string &port,
                       asio::yield_context yield,
                       boost::system::error_code error) : web_socket(io_service) {
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), std::stoi(port)));
        acceptor.async_accept(socket, yield[error]);
        acceptor.async_accept(web_socket.next_layer(), yield[error]);
    }

    // Create a server messenger by doing an async block give the socket
    // The messenger will assume ownership of the socket
    explicit Messenger(tcp::socket socket, asio::yield_context yield) : socket(std::move(socket)),
                                                                        yield(yield) {
    explicit Messenger(tcp::acceptor& acceptor,
            asio::io_service &io_service,
                       asio::yield_context yield,
                       boost::system::error_code error) : web_socket(io_service) {
        acceptor.async_accept(web_socket.next_layer(), yield[error]);
    }

    std::string async_receive(MessageType type = MessageType::string);
    std::string async_receive();

    std::string async_receive(MessageType *type);
    void async_send(const std::string &message,
                    asio::yield_context yield,
                    boost::system::error_code error);

    void async_send(const std::string &message, MessageType type = MessageType::string);
    void async_send(asio::streambuf &message_body,
                    asio::yield_context yield,
                    boost::system::error_code error);

    void async_send(asio::streambuf &message_body);
    void async_receive_file(boost::filesystem::path file_path,
                            asio::yield_context yield,
                            boost::system::error_code error,
                            const bool print_progress = false
    );

    void async_receive_file(boost::filesystem::path file_path, const bool print_progress = false);
    void async_send_file(boost::filesystem::path file_path,
                         asio::yield_context yield,
                         boost::system::error_code error,
                         const bool print_progress = false
    );

    void async_send_file(boost::filesystem::path file_path, const bool print_progress = false);
    BuilderData async_receive_builder(asio::yield_context yield,
                                      boost::system::error_code error);

    BuilderData async_receive_builder();
    void async_send(BuilderData builder,
                    asio::yield_context yield,
                    boost::system::error_code error);

    void async_send(BuilderData builder);
    ClientData async_receive_client_data(asio::yield_context yield,
                                         boost::system::error_code error);

    ClientData async_receive_client_data();
    void async_send(ClientData client_data,
                    asio::yield_context yield,
                    boost::system::error_code error);

    void async_send(ClientData client_data);

    tcp::socket socket;
    // asio yield_context.ec_ is private and so i'd prefer to not (ab)use it, as such hold on to a single error variable that gets set
    // by any Messenger failure. Taking the yield_context and error seperately in the constructor is one way to handle this
    // This guards somewhat against passing yield[ec] to one of the messenger functions on accident
    asio::yield_context yield;
    boost::system::error_code error;
private:
    void async_send_header(std::size_t message_size, MessageType type);

    Header async_receive_header();

    static constexpr std::size_t header_size() {
        return sizeof(Header);
    }
    websocket::stream<tcp::socket> web_socket;
};
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
BootStrap: docker
From: ppc64le/ubuntu:zesty
From: ppc64le/ubuntu:artful

%setup
cp /usr/bin/qemu-ppc64le  ${SINGULARITY_ROOTFS}/usr/bin/qemu-ppc64le