| 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_delegate.h" | 10 #include "mojo/public/cpp/application/application_impl_base.h" |
| 11 #include "mojo/public/cpp/application/application_runner.h" | 11 #include "mojo/public/cpp/application/run_application.h" |
| 12 #include "mojo/public/cpp/application/service_provider_impl.h" | 12 #include "mojo/public/cpp/application/service_provider_impl.h" |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" | 13 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 14 | 14 |
| 15 namespace mojo { | 15 namespace mojo { |
| 16 namespace examples { | 16 namespace examples { |
| 17 | 17 |
| 18 // This file demonstrates three ways to implement a simple Mojo server. Because | 18 // This file demonstrates three ways to implement a simple Mojo server. Because |
| 19 // there's no state that's saved per-pipe, all three servers appear to the | 19 // there's no state that's saved per-pipe, all three servers appear to the |
| 20 // client to do exactly the same thing. | 20 // client to do exactly the same thing. |
| 21 // | 21 // |
| (...skipping 21 matching lines...) Expand all Loading... |
| 43 class StrongBindingEchoImpl : public EchoImpl { | 43 class StrongBindingEchoImpl : public EchoImpl { |
| 44 public: | 44 public: |
| 45 explicit StrongBindingEchoImpl(InterfaceRequest<Echo> handle) | 45 explicit StrongBindingEchoImpl(InterfaceRequest<Echo> handle) |
| 46 : strong_binding_(this, handle.Pass()) {} | 46 : strong_binding_(this, handle.Pass()) {} |
| 47 | 47 |
| 48 private: | 48 private: |
| 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::ApplicationImplBase { |
| 54 public: | 54 public: |
| 55 MultiServer() {} | 55 MultiServer() {} |
| 56 | 56 |
| 57 // From ApplicationDelegate | 57 // From ApplicationImplBase |
| 58 bool ConfigureIncomingConnection( | 58 bool OnAcceptConnection(ServiceProviderImpl* service_provider_impl) override { |
| 59 ServiceProviderImpl* service_provider_impl) override { | |
| 60 service_provider_impl->AddService<Echo>( | 59 service_provider_impl->AddService<Echo>( |
| 61 [](const mojo::ConnectionContext& connection_context, | 60 [](const mojo::ConnectionContext& connection_context, |
| 62 mojo::InterfaceRequest<Echo> echo_request) { | 61 mojo::InterfaceRequest<Echo> echo_request) { |
| 63 // This object will be deleted automatically because of the use of | 62 // This object will be deleted automatically because of the use of |
| 64 // StrongBinding<> for the declaration of |strong_binding_|. | 63 // StrongBinding<> for the declaration of |strong_binding_|. |
| 65 new StrongBindingEchoImpl(echo_request.Pass()); | 64 new StrongBindingEchoImpl(echo_request.Pass()); |
| 66 }); | 65 }); |
| 67 return true; | 66 return true; |
| 68 } | 67 } |
| 69 }; | 68 }; |
| 70 | 69 |
| 71 // SingletonServer uses the same object to handle all message pipes. Useful | 70 // SingletonServer uses the same object to handle all message pipes. Useful |
| 72 // for stateless operation. | 71 // for stateless operation. |
| 73 class SingletonServer : public mojo::ApplicationDelegate { | 72 class SingletonServer : public mojo::ApplicationImplBase { |
| 74 public: | 73 public: |
| 75 SingletonServer() {} | 74 SingletonServer() {} |
| 76 | 75 |
| 77 // From ApplicationDelegate | 76 // From ApplicationImplBase |
| 78 bool ConfigureIncomingConnection( | 77 bool OnAcceptConnection(ServiceProviderImpl* service_provider_impl) override { |
| 79 ServiceProviderImpl* service_provider_impl) override { | |
| 80 service_provider_impl->AddService<Echo>( | 78 service_provider_impl->AddService<Echo>( |
| 81 [this](const mojo::ConnectionContext& connection_context, | 79 [this](const mojo::ConnectionContext& connection_context, |
| 82 mojo::InterfaceRequest<Echo> echo_request) { | 80 mojo::InterfaceRequest<Echo> echo_request) { |
| 83 // All channels will connect to this singleton object, so just | 81 // All channels will connect to this singleton object, so just |
| 84 // add the binding to our collection. | 82 // add the binding to our collection. |
| 85 bindings_.AddBinding(&echo_impl_, echo_request.Pass()); | 83 bindings_.AddBinding(&echo_impl_, echo_request.Pass()); |
| 86 }); | 84 }); |
| 87 return true; | 85 return true; |
| 88 } | 86 } |
| 89 | 87 |
| 90 private: | 88 private: |
| 91 EchoImpl echo_impl_; | 89 EchoImpl echo_impl_; |
| 92 | 90 |
| 93 mojo::BindingSet<Echo> bindings_; | 91 mojo::BindingSet<Echo> bindings_; |
| 94 }; | 92 }; |
| 95 | 93 |
| 96 // OneAtATimeServer works with only one pipe at a time. When a new pipe tries | 94 // 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 | 95 // 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 | 96 // 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 | 97 // 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 | 98 // to the server before the first client called EchoString(). Therefore, this |
| 101 // is an example of how not to write your code. | 99 // is an example of how not to write your code. |
| 102 class OneAtATimeServer : public mojo::ApplicationDelegate { | 100 class OneAtATimeServer : public mojo::ApplicationImplBase { |
| 103 public: | 101 public: |
| 104 OneAtATimeServer() : binding_(&echo_impl_) {} | 102 OneAtATimeServer() : binding_(&echo_impl_) {} |
| 105 | 103 |
| 106 // From ApplicationDelegate | 104 // From ApplicationImplBase |
| 107 bool ConfigureIncomingConnection( | 105 bool OnAcceptConnection(ServiceProviderImpl* service_provider_impl) override { |
| 108 ServiceProviderImpl* service_provider_impl) override { | |
| 109 service_provider_impl->AddService<Echo>( | 106 service_provider_impl->AddService<Echo>( |
| 110 [this](const mojo::ConnectionContext& connection_context, | 107 [this](const mojo::ConnectionContext& connection_context, |
| 111 mojo::InterfaceRequest<Echo> echo_request) { | 108 mojo::InterfaceRequest<Echo> echo_request) { |
| 112 binding_.Bind(echo_request.Pass()); | 109 binding_.Bind(echo_request.Pass()); |
| 113 }); | 110 }); |
| 114 return true; | 111 return true; |
| 115 } | 112 } |
| 116 | 113 |
| 117 private: | 114 private: |
| 118 EchoImpl echo_impl_; | 115 EchoImpl echo_impl_; |
| 119 | 116 |
| 120 mojo::Binding<Echo> binding_; | 117 mojo::Binding<Echo> binding_; |
| 121 }; | 118 }; |
| 122 | 119 |
| 123 } // namespace examples | 120 } // namespace examples |
| 124 } // namespace mojo | 121 } // namespace mojo |
| 125 | 122 |
| 126 MojoResult MojoMain(MojoHandle application_request) { | 123 MojoResult MojoMain(MojoHandle application_request) { |
| 127 // Uncomment one of the three servers at a time to see it work: | 124 // Uncomment one of the three servers at a time to see it work: |
| 128 mojo::ApplicationRunner runner(std::unique_ptr<mojo::examples::MultiServer>( | 125 mojo::examples::MultiServer server_app; |
| 129 new mojo::examples::MultiServer())); | 126 // mojo::examples::SingletonServer server_app; |
| 130 // mojo::ApplicationRunner runner(new mojo::examples::SingletonServer()); | 127 // mojo::examples::OneAtATimeServer server_app; |
| 131 // mojo::ApplicationRunner runner(new mojo::examples::OneAtATimeServer()); | 128 return mojo::RunMainApplication(application_request, &server_app); |
| 132 | |
| 133 return runner.Run(application_request); | |
| 134 } | 129 } |
| OLD | NEW |