| 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/weak_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" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 // MultiServer creates a new object to handle each message pipe. | 51 // MultiServer creates a new object to handle each message pipe. |
| 52 class MultiServer : public mojo::ApplicationDelegate, | 52 class MultiServer : public mojo::ApplicationDelegate, |
| 53 public mojo::InterfaceFactory<Echo> { | 53 public mojo::InterfaceFactory<Echo> { |
| 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 mojo::ApplicationConnection* connection, const std::string& url) override
{ |
| 60 connection->AddService<Echo>(this); | 60 connection->AddService<Echo>(this); |
| 61 return true; | 61 return true; |
| 62 } | 62 } |
| 63 | 63 |
| 64 // From InterfaceFactory<Echo> | 64 // From InterfaceFactory<Echo> |
| 65 void Create(mojo::ApplicationConnection* connection, | 65 void Create(mojo::ApplicationConnection* connection, |
| 66 mojo::InterfaceRequest<Echo> request) override { | 66 mojo::InterfaceRequest<Echo> request) override { |
| 67 // This object will be deleted automatically because of the use of | 67 // This object will be deleted automatically because of the use of |
| 68 // StrongBinding<> for the declaration of |strong_binding_|. | 68 // StrongBinding<> for the declaration of |strong_binding_|. |
| 69 new StrongBindingEchoImpl(request.Pass()); | 69 new StrongBindingEchoImpl(request.Pass()); |
| 70 } | 70 } |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 // SingletonServer uses the same object to handle all message pipes. Useful | 73 // SingletonServer uses the same object to handle all message pipes. Useful |
| 74 // for stateless operation. | 74 // for stateless operation. |
| 75 class SingletonServer : public mojo::ApplicationDelegate, | 75 class SingletonServer : public mojo::ApplicationDelegate, |
| 76 public mojo::InterfaceFactory<Echo> { | 76 public mojo::InterfaceFactory<Echo> { |
| 77 public: | 77 public: |
| 78 SingletonServer() {} | 78 SingletonServer() {} |
| 79 | 79 |
| 80 // From ApplicationDelegate | 80 // From ApplicationDelegate |
| 81 bool ConfigureIncomingConnection( | 81 bool ConfigureIncomingConnection( |
| 82 mojo::ApplicationConnection* connection) override { | 82 mojo::ApplicationConnection* connection, |
| 83 const std::string& url) override { |
| 83 connection->AddService<Echo>(this); | 84 connection->AddService<Echo>(this); |
| 84 return true; | 85 return true; |
| 85 } | 86 } |
| 86 | 87 |
| 87 // From InterfaceFactory<Echo> | 88 // From InterfaceFactory<Echo> |
| 88 void Create(mojo::ApplicationConnection* connection, | 89 void Create(mojo::ApplicationConnection* connection, |
| 89 mojo::InterfaceRequest<Echo> request) override { | 90 mojo::InterfaceRequest<Echo> request) override { |
| 90 // All channels will connect to this singleton object, so just | 91 // All channels will connect to this singleton object, so just |
| 91 // add the binding to our collection. | 92 // add the binding to our collection. |
| 92 bindings_.AddBinding(&echo_impl_, request.Pass()); | 93 bindings_.AddBinding(&echo_impl_, request.Pass()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 104 // not reliable. There's a race condition because a second client could bind | 105 // 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 | 106 // to the server before the first client called EchoString(). Therefore, this |
| 106 // is an example of how not to write your code. | 107 // is an example of how not to write your code. |
| 107 class OneAtATimeServer : public mojo::ApplicationDelegate, | 108 class OneAtATimeServer : public mojo::ApplicationDelegate, |
| 108 public mojo::InterfaceFactory<Echo> { | 109 public mojo::InterfaceFactory<Echo> { |
| 109 public: | 110 public: |
| 110 OneAtATimeServer() : binding_(&echo_impl_) {} | 111 OneAtATimeServer() : binding_(&echo_impl_) {} |
| 111 | 112 |
| 112 // From ApplicationDelegate | 113 // From ApplicationDelegate |
| 113 bool ConfigureIncomingConnection( | 114 bool ConfigureIncomingConnection( |
| 114 mojo::ApplicationConnection* connection) override { | 115 mojo::ApplicationConnection* connection, const std::string& url) override
{ |
| 115 connection->AddService<Echo>(this); | 116 connection->AddService<Echo>(this); |
| 116 return true; | 117 return true; |
| 117 } | 118 } |
| 118 | 119 |
| 119 // From InterfaceFactory<Echo> | 120 // From InterfaceFactory<Echo> |
| 120 void Create(mojo::ApplicationConnection* connection, | 121 void Create(mojo::ApplicationConnection* connection, |
| 121 mojo::InterfaceRequest<Echo> request) override { | 122 mojo::InterfaceRequest<Echo> request) override { |
| 122 binding_.Bind(request.Pass()); | 123 binding_.Bind(request.Pass()); |
| 123 } | 124 } |
| 124 | 125 |
| 125 private: | 126 private: |
| 126 EchoImpl echo_impl_; | 127 EchoImpl echo_impl_; |
| 127 | 128 |
| 128 mojo::Binding<Echo> binding_; | 129 mojo::Binding<Echo> binding_; |
| 129 }; | 130 }; |
| 130 | 131 |
| 131 } // namespace examples | 132 } // namespace examples |
| 132 } // namespace mojo | 133 } // namespace mojo |
| 133 | 134 |
| 134 MojoResult MojoMain(MojoHandle shell_handle) { | 135 MojoResult MojoMain(MojoHandle shell_handle) { |
| 135 // Uncomment one of the three servers at a time to see it work: | 136 // Uncomment one of the three servers at a time to see it work: |
| 136 mojo::ApplicationRunner runner(new mojo::examples::MultiServer()); | 137 mojo::ApplicationRunner runner(new mojo::examples::MultiServer()); |
| 137 // mojo::ApplicationRunner runner(new mojo::examples::SingletonServer()); | 138 // mojo::ApplicationRunner runner(new mojo::examples::SingletonServer()); |
| 138 // mojo::ApplicationRunner runner(new mojo::examples::OneAtATimeServer()); | 139 // mojo::ApplicationRunner runner(new mojo::examples::OneAtATimeServer()); |
| 139 | 140 |
| 140 return runner.Run(shell_handle); | 141 return runner.Run(shell_handle); |
| 141 } | 142 } |
| OLD | NEW |