OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "examples/echo/echo.mojom.h" | 5 #include "examples/echo/echo.mojom.h" |
6 #include "mojo/common/weak_binding_set.h" | 6 #include "mojo/common/binding_set.h" |
7 #include "mojo/public/c/system/main.h" | 7 #include "mojo/public/c/system/main.h" |
8 #include "mojo/public/cpp/application/application_connection.h" | 8 #include "mojo/public/cpp/application/application_connection.h" |
9 #include "mojo/public/cpp/application/application_delegate.h" | 9 #include "mojo/public/cpp/application/application_delegate.h" |
10 #include "mojo/public/cpp/application/application_runner.h" | 10 #include "mojo/public/cpp/application/application_runner.h" |
11 #include "mojo/public/cpp/application/interface_factory.h" | 11 #include "mojo/public/cpp/application/interface_factory.h" |
12 #include "mojo/public/cpp/bindings/strong_binding.h" | 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
13 | 13 |
14 namespace mojo { | 14 namespace mojo { |
15 namespace examples { | 15 namespace examples { |
16 | 16 |
17 // This file demonstrates three ways to implement a simple Mojo server. Because | 17 // This file demonstrates three ways to implement a simple Mojo server. Because |
18 // there's no state that's saved per-pipe, all three servers appear to the | 18 // there's no state that's saved per-pipe, all three servers appear to the |
19 // client to do exactly the same thing. | 19 // client to do exactly the same thing. |
20 // | 20 // |
21 // To choose one of the them, update MojoMain() at the end of this file. | 21 // To choose one of the them, update MojoMain() at the end of this file. |
22 // 1. MultiServer - creates a new object for each connection. Cleans up by using | 22 // 1. MultiServer - creates a new object for each connection. Cleans up by using |
23 // StrongBinding. | 23 // StrongBinding. |
24 // 2. SingletonServer -- all requests are handled by one object. Connections are | 24 // 2. SingletonServer -- all requests are handled by one object. Connections are |
25 // tracked using WeakBindingSet. | 25 // tracked using BindingSet. |
26 // 3. OneAtATimeServer -- each Create call from InterfaceFactory<> closes the | 26 // 3. OneAtATimeServer -- each Create call from InterfaceFactory<> closes the |
27 // old interface pipe and binds the new interface pipe. | 27 // old interface pipe and binds the new interface pipe. |
28 | 28 |
29 // EchoImpl defines our implementation of the Echo interface. | 29 // EchoImpl defines our implementation of the Echo interface. |
30 // It is used by all three server variants. | 30 // It is used by all three server variants. |
31 class EchoImpl : public Echo { | 31 class EchoImpl : public Echo { |
32 public: | 32 public: |
33 void EchoString(const mojo::String& value, | 33 void EchoString(const mojo::String& value, |
34 const Callback<void(mojo::String)>& callback) override { | 34 const Callback<void(mojo::String)>& callback) override { |
35 callback.Run(value); | 35 callback.Run(value); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 void Create(mojo::ApplicationConnection* connection, | 88 void Create(mojo::ApplicationConnection* connection, |
89 mojo::InterfaceRequest<Echo> request) override { | 89 mojo::InterfaceRequest<Echo> request) override { |
90 // All channels will connect to this singleton object, so just | 90 // All channels will connect to this singleton object, so just |
91 // add the binding to our collection. | 91 // add the binding to our collection. |
92 bindings_.AddBinding(&echo_impl_, request.Pass()); | 92 bindings_.AddBinding(&echo_impl_, request.Pass()); |
93 } | 93 } |
94 | 94 |
95 private: | 95 private: |
96 EchoImpl echo_impl_; | 96 EchoImpl echo_impl_; |
97 | 97 |
98 mojo::WeakBindingSet<Echo> bindings_; | 98 mojo::BindingSet<Echo> bindings_; |
99 }; | 99 }; |
100 | 100 |
101 // OneAtATimeServer works with only one pipe at a time. When a new pipe tries | 101 // OneAtATimeServer works with only one pipe at a time. When a new pipe tries |
102 // to bind, the previous pipe is closed. This would seem to be useful when | 102 // to bind, the previous pipe is closed. This would seem to be useful when |
103 // clients are expected to make a single call and then go away, but in fact it's | 103 // clients are expected to make a single call and then go away, but in fact it's |
104 // not reliable. There's a race condition because a second client could bind | 104 // not reliable. There's a race condition because a second client could bind |
105 // to the server before the first client called EchoString(). Therefore, this | 105 // to the server before the first client called EchoString(). Therefore, this |
106 // is an example of how not to write your code. | 106 // is an example of how not to write your code. |
107 class OneAtATimeServer : public mojo::ApplicationDelegate, | 107 class OneAtATimeServer : public mojo::ApplicationDelegate, |
108 public mojo::InterfaceFactory<Echo> { | 108 public mojo::InterfaceFactory<Echo> { |
(...skipping 23 matching lines...) Expand all Loading... |
132 } // namespace mojo | 132 } // namespace mojo |
133 | 133 |
134 MojoResult MojoMain(MojoHandle application_request) { | 134 MojoResult MojoMain(MojoHandle application_request) { |
135 // Uncomment one of the three servers at a time to see it work: | 135 // Uncomment one of the three servers at a time to see it work: |
136 mojo::ApplicationRunner runner(new mojo::examples::MultiServer()); | 136 mojo::ApplicationRunner runner(new mojo::examples::MultiServer()); |
137 // mojo::ApplicationRunner runner(new mojo::examples::SingletonServer()); | 137 // mojo::ApplicationRunner runner(new mojo::examples::SingletonServer()); |
138 // mojo::ApplicationRunner runner(new mojo::examples::OneAtATimeServer()); | 138 // mojo::ApplicationRunner runner(new mojo::examples::OneAtATimeServer()); |
139 | 139 |
140 return runner.Run(application_request); | 140 return runner.Run(application_request); |
141 } | 141 } |
OLD | NEW |