blob: 0c036266e895f2b9508d47618bab3b3a3aefc71e [file] [log] [blame]
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <fuchsia/examples/cpp/fidl.h>
#include <lib/async-loop/cpp/loop.h>
#include <lib/async-loop/default.h>
#include <lib/fidl/cpp/binding_set.h>
#include <lib/sys/cpp/component_context.h>
#include <lib/sys/cpp/outgoing_directory.h>
#include <lib/sys/service/cpp/service_handler.h>
#include <lib/syslog/cpp/log_settings.h>
#include <lib/syslog/cpp/macros.h>
class EchoImpl : public fuchsia::examples::Echo {
public:
explicit EchoImpl(bool reverse) : reverse_(reverse) {}
void SendString(std::string value) override {}
void EchoString(std::string value, EchoStringCallback callback) override {
FX_LOGS(INFO) << "Got echo request: " << value;
if (reverse_) {
std::reverse(value.begin(), value.end());
}
callback(value);
}
private:
const bool reverse_;
};
int main(int argc, const char** argv) {
FX_LOGS(INFO) << "Starting Echo service server";
async::Loop loop(&kAsyncLoopConfigAttachToCurrentThread);
auto context = sys::ComponentContext::CreateAndServeOutgoingDirectory();
sys::ServiceHandler handler;
fuchsia::examples::EchoService::Handler my_service(&handler);
EchoImpl regular_impl(false);
fidl::BindingSet<fuchsia::examples::Echo> regular_echo_bindings;
my_service.add_regular_echo(regular_echo_bindings.GetHandler(&regular_impl));
EchoImpl reversed_impl(true);
fidl::BindingSet<fuchsia::examples::Echo> reversed_echo_bindings;
my_service.add_reversed_echo(reversed_echo_bindings.GetHandler(&reversed_impl));
context->outgoing()->AddService<fuchsia::examples::EchoService>(std::move(handler));
FX_LOGS(INFO) << "Running Echo service server";
loop.Run();
return 0;
}