| 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 <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "examples/echo/echo.mojom.h" | 7 #include "examples/echo/echo.mojom.h" |
| 8 #include "mojo/common/binding_set.h" | 8 #include "mojo/common/binding_set.h" |
| 9 #include "mojo/public/c/system/main.h" | 9 #include "mojo/public/c/system/main.h" |
| 10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 mojo::StrongBinding<Echo> strong_binding_; | 49 mojo::StrongBinding<Echo> strong_binding_; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 // MultiServer creates a new object to handle each message pipe. | 52 // MultiServer creates a new object to handle each message pipe. |
| 53 class MultiServer : public mojo::ApplicationDelegate { | 53 class MultiServer : public mojo::ApplicationDelegate { |
| 54 public: | 54 public: |
| 55 MultiServer() {} | 55 MultiServer() {} |
| 56 | 56 |
| 57 // From ApplicationDelegate | 57 // From ApplicationDelegate |
| 58 bool ConfigureIncomingConnection( | 58 bool ConfigureIncomingConnection( |
| 59 mojo::ApplicationConnection* connection) override { | 59 ServiceProviderImpl* service_provider_impl) override { |
| 60 connection->GetServiceProviderImpl().AddService<Echo>( | 60 service_provider_impl->AddService<Echo>( |
| 61 [](const mojo::ConnectionContext& connection_context, | 61 [](const mojo::ConnectionContext& connection_context, |
| 62 mojo::InterfaceRequest<Echo> echo_request) { | 62 mojo::InterfaceRequest<Echo> echo_request) { |
| 63 // This object will be deleted automatically because of the use of | 63 // This object will be deleted automatically because of the use of |
| 64 // StrongBinding<> for the declaration of |strong_binding_|. | 64 // StrongBinding<> for the declaration of |strong_binding_|. |
| 65 new StrongBindingEchoImpl(echo_request.Pass()); | 65 new StrongBindingEchoImpl(echo_request.Pass()); |
| 66 }); | 66 }); |
| 67 return true; | 67 return true; |
| 68 } | 68 } |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 // SingletonServer uses the same object to handle all message pipes. Useful | 71 // SingletonServer uses the same object to handle all message pipes. Useful |
| 72 // for stateless operation. | 72 // for stateless operation. |
| 73 class SingletonServer : public mojo::ApplicationDelegate { | 73 class SingletonServer : public mojo::ApplicationDelegate { |
| 74 public: | 74 public: |
| 75 SingletonServer() {} | 75 SingletonServer() {} |
| 76 | 76 |
| 77 // From ApplicationDelegate | 77 // From ApplicationDelegate |
| 78 bool ConfigureIncomingConnection( | 78 bool ConfigureIncomingConnection( |
| 79 mojo::ApplicationConnection* connection) override { | 79 ServiceProviderImpl* service_provider_impl) override { |
| 80 connection->GetServiceProviderImpl().AddService<Echo>( | 80 service_provider_impl->AddService<Echo>( |
| 81 [this](const mojo::ConnectionContext& connection_context, | 81 [this](const mojo::ConnectionContext& connection_context, |
| 82 mojo::InterfaceRequest<Echo> echo_request) { | 82 mojo::InterfaceRequest<Echo> echo_request) { |
| 83 // All channels will connect to this singleton object, so just | 83 // All channels will connect to this singleton object, so just |
| 84 // add the binding to our collection. | 84 // add the binding to our collection. |
| 85 bindings_.AddBinding(&echo_impl_, echo_request.Pass()); | 85 bindings_.AddBinding(&echo_impl_, echo_request.Pass()); |
| 86 }); | 86 }); |
| 87 return true; | 87 return true; |
| 88 } | 88 } |
| 89 | 89 |
| 90 private: | 90 private: |
| 91 EchoImpl echo_impl_; | 91 EchoImpl echo_impl_; |
| 92 | 92 |
| 93 mojo::BindingSet<Echo> bindings_; | 93 mojo::BindingSet<Echo> bindings_; |
| 94 }; | 94 }; |
| 95 | 95 |
| 96 // OneAtATimeServer works with only one pipe at a time. When a new pipe tries | 96 // OneAtATimeServer works with only one pipe at a time. When a new pipe tries |
| 97 // to bind, the previous pipe is closed. This would seem to be useful when | 97 // to bind, the previous pipe is closed. This would seem to be useful when |
| 98 // clients are expected to make a single call and then go away, but in fact it's | 98 // clients are expected to make a single call and then go away, but in fact it's |
| 99 // not reliable. There's a race condition because a second client could bind | 99 // not reliable. There's a race condition because a second client could bind |
| 100 // to the server before the first client called EchoString(). Therefore, this | 100 // to the server before the first client called EchoString(). Therefore, this |
| 101 // is an example of how not to write your code. | 101 // is an example of how not to write your code. |
| 102 class OneAtATimeServer : public mojo::ApplicationDelegate { | 102 class OneAtATimeServer : public mojo::ApplicationDelegate { |
| 103 public: | 103 public: |
| 104 OneAtATimeServer() : binding_(&echo_impl_) {} | 104 OneAtATimeServer() : binding_(&echo_impl_) {} |
| 105 | 105 |
| 106 // From ApplicationDelegate | 106 // From ApplicationDelegate |
| 107 bool ConfigureIncomingConnection( | 107 bool ConfigureIncomingConnection( |
| 108 mojo::ApplicationConnection* connection) override { | 108 ServiceProviderImpl* service_provider_impl) override { |
| 109 connection->GetServiceProviderImpl().AddService<Echo>( | 109 service_provider_impl->AddService<Echo>( |
| 110 [this](const mojo::ConnectionContext& connection_context, | 110 [this](const mojo::ConnectionContext& connection_context, |
| 111 mojo::InterfaceRequest<Echo> echo_request) { | 111 mojo::InterfaceRequest<Echo> echo_request) { |
| 112 binding_.Bind(echo_request.Pass()); | 112 binding_.Bind(echo_request.Pass()); |
| 113 }); | 113 }); |
| 114 return true; | 114 return true; |
| 115 } | 115 } |
| 116 | 116 |
| 117 private: | 117 private: |
| 118 EchoImpl echo_impl_; | 118 EchoImpl echo_impl_; |
| 119 | 119 |
| 120 mojo::Binding<Echo> binding_; | 120 mojo::Binding<Echo> binding_; |
| 121 }; | 121 }; |
| 122 | 122 |
| 123 } // namespace examples | 123 } // namespace examples |
| 124 } // namespace mojo | 124 } // namespace mojo |
| 125 | 125 |
| 126 MojoResult MojoMain(MojoHandle application_request) { | 126 MojoResult MojoMain(MojoHandle application_request) { |
| 127 // Uncomment one of the three servers at a time to see it work: | 127 // Uncomment one of the three servers at a time to see it work: |
| 128 mojo::ApplicationRunner runner(std::unique_ptr<mojo::examples::MultiServer>( | 128 mojo::ApplicationRunner runner(std::unique_ptr<mojo::examples::MultiServer>( |
| 129 new mojo::examples::MultiServer())); | 129 new mojo::examples::MultiServer())); |
| 130 // mojo::ApplicationRunner runner(new mojo::examples::SingletonServer()); | 130 // mojo::ApplicationRunner runner(new mojo::examples::SingletonServer()); |
| 131 // mojo::ApplicationRunner runner(new mojo::examples::OneAtATimeServer()); | 131 // mojo::ApplicationRunner runner(new mojo::examples::OneAtATimeServer()); |
| 132 | 132 |
| 133 return runner.Run(application_request); | 133 return runner.Run(application_request); |
| 134 } | 134 } |
| OLD | NEW |