Commit 7db4f17c authored by AdamSimpson's avatar AdamSimpson
Browse files

Add a progress bar(maybe) for file transfers

parent 45b4bb33
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ int main(int argc, char *argv[]) {
                        std::cout << "Sending definition: " << definition_path << std::endl;

                        // Send the definition file
                        builder_messenger.async_send_file(definition_path, yield);
                        builder_messenger.async_send_file(definition_path, yield, true);

                        std::cout << "Start reading builder output:" << std::endl;

@@ -95,7 +95,7 @@ int main(int argc, char *argv[]) {
                        // Read the container image
                        std::cout << "Begin receive of container: " << container_path << std::endl;

                        builder_messenger.async_receive_file(container_path, yield);
                        builder_messenger.async_receive_file(container_path, yield, true);

                        std::cout << "End receive of container: " << container_path << std::endl;

+22 −2
Original line number Diff line number Diff line
@@ -12,6 +12,8 @@
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "Builder.h"
#include <boost/progress.hpp>
#include <memory.h>

namespace asio = boost::asio;
using asio::ip::tcp;
@@ -56,7 +58,7 @@ public:

    // TODO set chunk size to the size of the socket receive buffer?
    template <typename Handler>
    void async_receive_file(boost::filesystem::path file_path, const Handler& handler) {
    void async_receive_file(boost::filesystem::path file_path, const Handler& handler, const bool print_progress=false) {
        std::ofstream file;

        // Get the socket receive buffer size and use that as the chunk size
@@ -73,6 +75,12 @@ public:
        auto header = async_receive_header(MessageType::file, handler);
        auto total_size = header.size;

        // If requested initialize a progress bar
        std::shared_ptr<boost::progress_display> progress_bar;
        if(print_progress) {
            progress_bar = std::make_shared<boost::progress_display>(total_size / chunk_size);
        }

        // Receive file in chunk_size messages
        auto bytes_remaining = total_size;
        std::vector<char> buffer_storage(chunk_size);
@@ -87,6 +95,8 @@ public:

            bytes_remaining -= bytes_received;

            ++(*progress_bar);

        } while (bytes_remaining);

        file.close();
@@ -150,7 +160,7 @@ public:
    // TODO set chunk size to the size of the socket receive buffer?
    // Send a file as a message asynchronously
    template <typename Handler>
    void async_send_file(boost::filesystem::path file_path, const Handler& handler) {
    void async_send_file(boost::filesystem::path file_path, const Handler& handler, const bool print_progress=false) {
        std::ifstream file;

        // Get the socket receive buffer size and use that as the chunk size
@@ -167,6 +177,12 @@ public:

        async_send_header(file_size, MessageType::file, handler);

        // If requested initialize a progress bar
        std::shared_ptr<boost::progress_display> progress_bar;
        if(print_progress) {
            progress_bar = std::make_shared<boost::progress_display>(file_size / chunk_size);
        }

        // Send file in chunk_size bits
        auto bytes_remaining = file_size;
        std::vector<char> buffer_storage(chunk_size);
@@ -179,6 +195,10 @@ public:
            auto bytes_sent = asio::async_write(socket, buffer, asio::transfer_exactly(bytes_to_send), handler);

            bytes_remaining -= bytes_sent;

            if(print_progress) {
                ++(*progress_bar);
            }
        } while (bytes_remaining);

        file.close();