Use events instead of method invokes for window state updates and others

This commit is contained in:
Simon Fels 2016-11-25 08:50:43 +01:00
commit a40fe51654
14 changed files with 152 additions and 133 deletions

View file

@ -33,28 +33,15 @@ PlatformApiSkeleton::PlatformApiSkeleton(const std::shared_ptr<rpc::PendingCallC
PlatformApiSkeleton::~PlatformApiSkeleton() {
}
void PlatformApiSkeleton::boot_finished(anbox::protobuf::rpc::Void const *request,
anbox::protobuf::rpc::Void *response,
google::protobuf::Closure *done) {
(void) request;
(void) response;
void PlatformApiSkeleton::handle_boot_finished_event(const anbox::protobuf::bridge::BootFinishedEvent &event) {
(void) event;
if (on_boot_finished_action_)
on_boot_finished_action_();
done->Run();
if (boot_finished_handler_)
boot_finished_handler_();
}
void PlatformApiSkeleton::update_window_state(anbox::protobuf::bridge::WindowStateUpdate const *request,
anbox::protobuf::rpc::Void *response,
google::protobuf::Closure *done) {
(void) response;
auto convert_window_state = [](const ::anbox::protobuf::bridge::WindowStateUpdate_WindowState &window) {
DEBUG("Window: display=%d has_surface=%d frame={%d,%d,%d,%d} package=%s task=%d stack=-1",
window.display_id(), window.has_surface(),
window.frame_left(), window.frame_top(), window.frame_right(), window.frame_bottom(),
window.package_name(), window.task_id());
void PlatformApiSkeleton::handle_window_state_update_event(const anbox::protobuf::bridge::WindowStateUpdateEvent &event) {
auto convert_window_state = [](const ::anbox::protobuf::bridge::WindowStateUpdateEvent_WindowState &window) {
return wm::WindowState(
wm::Display::Id(window.display_id()),
window.has_surface(),
@ -65,24 +52,22 @@ void PlatformApiSkeleton::update_window_state(anbox::protobuf::bridge::WindowSta
};
wm::WindowState::List updated;
for (int n = 0; n < request->windows_size(); n++) {
const auto window = request->windows(n);
for (int n = 0; n < event.windows_size(); n++) {
const auto window = event.windows(n);
updated.push_back(convert_window_state(window));
}
wm::WindowState::List removed;
for (int n = 0; n < request->removed_windows_size(); n++) {
const auto window = request->removed_windows(n);
for (int n = 0; n < event.removed_windows_size(); n++) {
const auto window = event.removed_windows(n);
removed.push_back(convert_window_state(window));
}
window_manager_->apply_window_state_update(updated, removed);
done->Run();
}
void PlatformApiSkeleton::on_boot_finished(const std::function<void()> &action) {
on_boot_finished_action_ = action;
void PlatformApiSkeleton::register_boot_finished_handler(const std::function<void()> &action) {
boot_finished_handler_ = action;
}
} // namespace bridge
} // namespace anbox

View file

@ -32,8 +32,8 @@ namespace rpc {
class Void;
} // namespace rpc
namespace bridge {
class Notification;
class WindowStateUpdate;
class BootFinishedEvent;
class WindowStateUpdateEvent;
} // namespace bridge
} // namespace protobuf
namespace rpc {
@ -49,20 +49,15 @@ public:
const std::shared_ptr<wm::Manager> &window_manager);
virtual ~PlatformApiSkeleton();
void boot_finished(anbox::protobuf::rpc::Void const *request,
anbox::protobuf::rpc::Void *response,
google::protobuf::Closure *done);
void handle_boot_finished_event(const anbox::protobuf::bridge::BootFinishedEvent &event);
void handle_window_state_update_event(const anbox::protobuf::bridge::WindowStateUpdateEvent &event);
void update_window_state(anbox::protobuf::bridge::WindowStateUpdate const *request,
anbox::protobuf::rpc::Void *response,
google::protobuf::Closure *done);
void on_boot_finished(const std::function<void()> &action);
void register_boot_finished_handler(const std::function<void()> &action);
private:
std::shared_ptr<rpc::PendingCallCache> pending_calls_;
std::shared_ptr<wm::Manager> window_manager_;
std::function<void()> on_boot_finished_action_;
std::function<void()> boot_finished_handler_;
};
} // namespace bridge
} // namespace anbox

View file

@ -18,6 +18,7 @@
#include "anbox/bridge/platform_message_processor.h"
#include "anbox/bridge/platform_api_skeleton.h"
#include "anbox/rpc/template_message_processor.h"
#include "anbox/logger.h"
#include "anbox_bridge.pb.h"
@ -34,13 +35,20 @@ PlatformMessageProcessor::~PlatformMessageProcessor() {
}
void PlatformMessageProcessor::dispatch(rpc::Invocation const& invocation) {
if (invocation.method_name() == "boot_finished")
invoke(this, server_.get(), &PlatformApiSkeleton::boot_finished, invocation);
else if (invocation.method_name() == "update_window_state")
invoke(this, server_.get(), &PlatformApiSkeleton::update_window_state, invocation);
}
void PlatformMessageProcessor::process_event_sequence(const std::string&) {
void PlatformMessageProcessor::process_event_sequence(const std::string &raw_events) {
anbox::protobuf::bridge::EventSequence seq;
if (!seq.ParseFromString(raw_events)) {
WARNING("Failed to parse events from raw string");
return;
}
if (seq.has_window_state_update())
server_->handle_window_state_update_event(seq.window_state_update());
if (seq.has_boot_finished())
server_->handle_boot_finished_event(seq.boot_finished());
}
} // namespace anbox
} // namespace network

View file

@ -130,7 +130,8 @@ anbox::cmds::Run::Run(const BusFactory& bus_factory)
android_api_stub->set_rpc_channel(rpc_channel);
auto server = std::make_shared<bridge::PlatformApiSkeleton>(pending_calls, window_manager);
server->on_boot_finished([&]() {
server->register_boot_finished_handler([&]() {
DEBUG("Android successfully booted");
dispatcher->dispatch([&]() {
// FIXME make this configurable or once we have a bridge let the host
// act as a DNS proxy.

View file

@ -2,6 +2,11 @@ option optimize_for = LITE_RUNTIME;
package anbox.protobuf.bridge;
message StructuredError {
optional uint32 domain = 1;
optional uint32 code = 2;
}
message Notification {
required string package_name = 1;
required string category = 2;
@ -27,7 +32,11 @@ message SetDnsServers {
repeated Server servers = 2;
}
message WindowStateUpdate {
message BootFinishedEvent {
}
message WindowStateUpdateEvent {
message WindowState {
required int32 display_id = 1;
required bool has_surface = 2;
@ -42,3 +51,11 @@ message WindowStateUpdate {
repeated WindowState windows = 1;
repeated WindowState removed_windows = 2;
}
message EventSequence {
optional BootFinishedEvent boot_finished = 1;
optional WindowStateUpdateEvent window_state_update = 2;
optional string error = 127;
optional StructuredError structured_error = 128;
}

View file

@ -12,6 +12,7 @@ message Invocation {
message Result {
optional uint32 id = 1;
optional bytes response = 2;
repeated bytes events = 3;
}
message StructuredError {

View file

@ -42,7 +42,17 @@ void Channel::call_method(std::string const& method_name,
google::protobuf::Closure *complete) {
auto const &invocation = invocation_for(method_name, parameters);
pending_calls_->save_completion_details(invocation, response, complete);
send_message(invocation);
send_message(MessageType::invocation, invocation);
}
void Channel::send_event(google::protobuf::MessageLite const& event) {
VariableLengthArray<2048> buffer{static_cast<size_t>(event.ByteSize())};
event.SerializeWithCachedSizesToArray(buffer.data());
anbox::protobuf::rpc::Result response;
response.add_events(buffer.data(), buffer.size());
send_message(MessageType::response, response);
}
protobuf::rpc::Invocation Channel::invocation_for(std::string const& method_name,
@ -62,17 +72,17 @@ protobuf::rpc::Invocation Channel::invocation_for(std::string const& method_name
return invoke;
}
void Channel::send_message(anbox::protobuf::rpc::Invocation const& invocation) {
const size_t size = invocation.ByteSize();
void Channel::send_message(const std::uint8_t &type, google::protobuf::MessageLite const& message) {
const size_t size = message.ByteSize();
const unsigned char header_bytes[header_size] = {
static_cast<unsigned char>((size >> 8) & 0xff),
static_cast<unsigned char>((size >> 0) & 0xff),
MessageType::invocation,
type,
};
std::vector<std::uint8_t> send_buffer(sizeof(header_bytes) + size);
std::copy(header_bytes, header_bytes + sizeof(header_bytes), send_buffer.begin());
invocation.SerializeToArray(send_buffer.data() + sizeof(header_bytes), size);
message.SerializeToArray(send_buffer.data() + sizeof(header_bytes), size);
try {
std::lock_guard<std::mutex> lock(write_mutex_);

View file

@ -52,11 +52,13 @@ public:
google::protobuf::MessageLite *response,
google::protobuf::Closure *complete);
void send_event(google::protobuf::MessageLite const& event);
private:
protobuf::rpc::Invocation invocation_for(
std::string const& method_name,
google::protobuf::MessageLite const* request);
void send_message(anbox::protobuf::rpc::Invocation const& invocation);
void send_message(const std::uint8_t &type, google::protobuf::MessageLite const& message);
int next_id();
void notify_disconnected();

View file

@ -78,7 +78,11 @@ bool MessageProcessor::process_data(const std::vector<std::uint8_t> &data) {
buffer_.erase(buffer_.begin(), buffer_.begin() + message_size);
pending_calls_->complete_response(*result);
if (result->has_id())
pending_calls_->complete_response(*result);
for (int n = 0; n < result->events_size(); n++)
process_event_sequence(result->events(n));
}
return true;