Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: examples/echo/echo_server.cc

Issue 1250463002: Renames WeakBindingSet to BindingSet. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Rebasing Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « apps/moterm/moterm_view.h ('k') | examples/echo_terminal/main.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 }
OLDNEW
« no previous file with comments | « apps/moterm/moterm_view.h ('k') | examples/echo_terminal/main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698