X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=src%2Ftype3_AndroidCloud%2Fanbox-master%2Fsrc%2Fanbox%2Fcontainer%2Fclient.cpp;fp=src%2Ftype3_AndroidCloud%2Fanbox-master%2Fsrc%2Fanbox%2Fcontainer%2Fclient.cpp;h=99251e2dc72b78305a8f56ee9433f9c363a585c4;hb=e26c1ec581be598521517829adba8c8dd23a768f;hp=0000000000000000000000000000000000000000;hpb=6699c1aea74eeb0eb400e6299079f0c7576f716f;p=iec.git diff --git a/src/type3_AndroidCloud/anbox-master/src/anbox/container/client.cpp b/src/type3_AndroidCloud/anbox-master/src/anbox/container/client.cpp new file mode 100644 index 0000000..99251e2 --- /dev/null +++ b/src/type3_AndroidCloud/anbox-master/src/anbox/container/client.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2016 Simon Fels + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + */ + +#include "anbox/container/client.h" +#include "anbox/system_configuration.h" +#include "anbox/container/management_api_stub.h" +#include "anbox/logger.h" +#include "anbox/network/local_socket_messenger.h" +#include "anbox/rpc/channel.h" +#include "anbox/rpc/message_processor.h" +#include "anbox/rpc/pending_call_cache.h" + +namespace ba = boost::asio; +namespace bs = boost::system; + +namespace anbox { +namespace container { +Client::Client(const std::shared_ptr &rt) + : messenger_(std::make_shared( + SystemConfiguration::instance().container_socket_path(), rt)), + pending_calls_(std::make_shared()), + rpc_channel_(std::make_shared(pending_calls_, messenger_)), + management_api_(std::make_shared(rpc_channel_)), + processor_( + std::make_shared(messenger_, pending_calls_)) { + read_next_message(); +} + +Client::~Client() {} + +void Client::start(const Configuration &configuration) { + try { + management_api_->start_container(configuration); + } catch (const std::exception &e) { + ERROR("Failed to start container: %s", e.what()); + if (terminate_callback_) + terminate_callback_(); + } +} + +void Client::stop() { + management_api_->stop_container(); +} + +void Client::register_terminate_handler(const TerminateCallback &callback) { + terminate_callback_ = callback; +} + +void Client::read_next_message() { + auto callback = std::bind(&Client::on_read_size, this, std::placeholders::_1, + std::placeholders::_2); + messenger_->async_receive_msg(callback, ba::buffer(buffer_)); +} + +void Client::on_read_size(const boost::system::error_code &error, + std::size_t bytes_read) { + if (error) { + if (terminate_callback_) + terminate_callback_(); + return; + } + + std::vector data(bytes_read); + std::copy(buffer_.data(), buffer_.data() + bytes_read, data.data()); + + if (processor_->process_data(data)) read_next_message(); +} +} // namespace container +} // namespace anbox