blob: 8fb9357afcf8c156db939613bc90bd433fa66123 [file] [log] [blame]
Vardhan Mudunuru119fc1f2016-03-15 16:20:39 -07001// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This example demonstrates the simple use of SynchronousInterfacePtr<>, which
6// is the blocking, synchronous version of mojom interface calls (typically used
7// via InterfacePtr<>).
8
9#include <memory>
10#include <utility>
11
12#include "examples/echo/echo.mojom-sync.h"
Vardhan Mudunuru119fc1f2016-03-15 16:20:39 -070013#include "mojo/public/c/system/main.h"
14#include "mojo/public/cpp/application/application_delegate.h"
15#include "mojo/public/cpp/application/application_impl.h"
16#include "mojo/public/cpp/application/application_runner.h"
Viet-Trung Luu8972b622016-04-25 18:06:17 -070017#include "mojo/public/cpp/application/connect.h"
Vardhan Mudunuru119fc1f2016-03-15 16:20:39 -070018#include "mojo/public/cpp/bindings/synchronous_interface_ptr.h"
19#include "mojo/public/cpp/environment/logging.h"
20#include "mojo/public/cpp/utility/run_loop.h"
21
22namespace mojo {
23namespace examples {
24
25class EchoClientDelegate : public ApplicationDelegate {
26 public:
27 void Initialize(ApplicationImpl* app) override {
Viet-Trung Luu8972b622016-04-25 18:06:17 -070028 SynchronousInterfacePtr<Echo> echo;
29 ConnectToService(app->shell(), "mojo:echo_server",
30 GetSynchronousProxy(&echo));
Vardhan Mudunuru119fc1f2016-03-15 16:20:39 -070031
32 mojo::String out = "yo!";
Viet-Trung Luu8972b622016-04-25 18:06:17 -070033 MOJO_CHECK(echo->EchoString("hello", &out));
Vardhan Mudunuru119fc1f2016-03-15 16:20:39 -070034 MOJO_LOG(INFO) << "Got response: " << out;
35 RunLoop::current()->Quit();
36 }
37};
38
39} // namespace examples
40} // namespace mojo
41
42MojoResult MojoMain(MojoHandle application_request) {
43 mojo::ApplicationRunner runner(
44 std::unique_ptr<mojo::examples::EchoClientDelegate>(
45 new mojo::examples::EchoClientDelegate()));
46 return runner.Run(application_request);
47}