| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'mojo:application'; | 6 import 'mojo:application'; |
| 7 import 'mojo:bindings'; | 7 import 'mojo:bindings'; |
| 8 import 'mojo:core'; | 8 import 'mojo:core'; |
| 9 | 9 |
| 10 import 'package:services/dart/test/pingpong_service.mojom.dart'; | 10 import 'package:services/dart/test/pingpong_service.mojom.dart'; |
| 11 | 11 |
| 12 class PingPongClientImpl implements PingPongClient { | 12 class PingPongClientImpl implements PingPongClient { |
| 13 final PingPongClientStub stub; | 13 final PingPongClientBinding binding; |
| 14 Completer _completer; | 14 Completer _completer; |
| 15 int _count; | 15 int _count; |
| 16 | 16 |
| 17 PingPongClientImpl.unbound(this._count, this._completer) | 17 PingPongClientImpl.unbound(this._count, this._completer) |
| 18 : stub = new PingPongClientStub.unbound() { | 18 : binding = new PingPongClientBinding.unbound() { |
| 19 stub.delegate = this; | 19 binding.delegate = this; |
| 20 } | 20 } |
| 21 | 21 |
| 22 void pong(int pongValue) { | 22 void pong(int pongValue) { |
| 23 if (pongValue == _count) { | 23 if (pongValue == _count) { |
| 24 _completer.complete(null); | 24 _completer.complete(null); |
| 25 stub.close(); | 25 binding.close(); |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 } | 28 } |
| 29 | 29 |
| 30 class PingPongServiceImpl implements PingPongService { | 30 class PingPongServiceImpl implements PingPongService { |
| 31 PingPongServiceStub _stub; | 31 PingPongServiceBinding _binding; |
| 32 Application _application; | 32 Application _application; |
| 33 PingPongClientProxy _pingPongClient; | 33 PingPongClientProxy _pingPongClient; |
| 34 | 34 |
| 35 PingPongServiceImpl(Application application, MojoMessagePipeEndpoint endpoint) | 35 PingPongServiceImpl(Application application, MojoMessagePipeEndpoint endpoint) |
| 36 : _application = application { | 36 : _application = application { |
| 37 _stub = new PingPongServiceStub.fromEndpoint(endpoint) | 37 _binding = new PingPongServiceBinding.fromEndpoint( |
| 38 ..delegate = this | 38 endpoint, delegate: this); |
| 39 ..listen(); | |
| 40 } | 39 } |
| 41 | 40 |
| 42 PingPongServiceImpl.fromStub(this._stub) { | 41 PingPongServiceImpl.fromBinding(this._binding) { |
| 43 _stub.delegate = this; | 42 _binding.delegate = this; |
| 44 } | 43 } |
| 45 | 44 |
| 46 listen() => _stub.listen(); | 45 listen() => _binding.listen(); |
| 47 | 46 |
| 48 void setClient(ProxyBase proxyBase) { | 47 void setClient(ProxyBase proxyBase) { |
| 49 assert(_pingPongClient== null); | 48 assert(_pingPongClient== null); |
| 50 _pingPongClient = proxyBase; | 49 _pingPongClient = proxyBase; |
| 51 } | 50 } |
| 52 | 51 |
| 53 void ping(int pingValue) { | 52 void ping(int pingValue) { |
| 54 if (_pingPongClient != null) { | 53 if (_pingPongClient != null) { |
| 55 _pingPongClient.ptr.pong(pingValue + 1); | 54 _pingPongClient.ptr.pong(pingValue + 1); |
| 56 } | 55 } |
| 57 } | 56 } |
| 58 | 57 |
| 59 Future pingTargetUrl(String url, int count, Function responseFactory) async { | 58 Future pingTargetUrl(String url, int count, Function responseFactory) async { |
| 60 if (_application == null) { | 59 if (_application == null) { |
| 61 return responseFactory(false); | 60 return responseFactory(false); |
| 62 } | 61 } |
| 63 var completer = new Completer(); | 62 var completer = new Completer(); |
| 64 var pingPongService= new PingPongServiceProxy.unbound(); | 63 var pingPongService = new PingPongServiceProxy.unbound(); |
| 65 _application.connectToService(url, pingPongService); | 64 _application.connectToService(url, pingPongService); |
| 66 | 65 |
| 67 var pingPongClient = new PingPongClientImpl.unbound(count, completer); | 66 var pingPongClient = new PingPongClientImpl.unbound(count, completer); |
| 68 pingPongService.ptr.setClient(pingPongClient.stub); | 67 pingPongService.ptr.setClient(pingPongClient.binding); |
| 69 pingPongClient.stub.listen(); | 68 pingPongClient.binding.listen(); |
| 70 | 69 |
| 71 for (var i = 0; i < count; i++) { | 70 for (var i = 0; i < count; i++) { |
| 72 pingPongService.ptr.ping(i); | 71 pingPongService.ptr.ping(i); |
| 73 } | 72 } |
| 74 await completer.future; | 73 await completer.future; |
| 75 pingPongService.ptr.quit(); | 74 pingPongService.ptr.quit(); |
| 75 pingPongService.close(); |
| 76 | 76 |
| 77 return responseFactory(true); | 77 return responseFactory(true); |
| 78 } | 78 } |
| 79 | 79 |
| 80 Future pingTargetService(ProxyBase proxyBase, | 80 Future pingTargetService(ProxyBase proxyBase, |
| 81 int count, | 81 int count, |
| 82 Function responseFactory) async { | 82 Function responseFactory) async { |
| 83 var pingPongService = proxyBase; | 83 var pingPongService = proxyBase; |
| 84 var completer = new Completer(); | 84 var completer = new Completer(); |
| 85 var client = new PingPongClientImpl.unbound(count, completer); | 85 var client = new PingPongClientImpl.unbound(count, completer); |
| 86 pingPongService.ptr.setClient(client.stub); | 86 pingPongService.ptr.setClient(client.binding); |
| 87 client.stub.listen(); | 87 client.binding.listen(); |
| 88 | 88 |
| 89 for (var i = 0; i < count; i++) { | 89 for (var i = 0; i < count; i++) { |
| 90 pingPongService.ptr.ping(i); | 90 pingPongService.ptr.ping(i); |
| 91 } | 91 } |
| 92 await completer.future; | 92 await completer.future; |
| 93 pingPongService.ptr.quit(); | 93 pingPongService.ptr.quit(); |
| 94 pingPongService.close(); |
| 94 | 95 |
| 95 return responseFactory(true); | 96 return responseFactory(true); |
| 96 } | 97 } |
| 97 | 98 |
| 98 getPingPongService(PingPongServiceStub serviceStub) { | 99 getPingPongService(PingPongServiceBinding serviceBinding) { |
| 99 new PingPongServiceImpl.fromStub(serviceStub)..listen(); | 100 new PingPongServiceImpl.fromBinding(serviceBinding)..listen(); |
| 100 } | 101 } |
| 101 | 102 |
| 102 void quit() { | 103 void quit() { |
| 103 if (_pingPongClient != null) { | 104 if (_pingPongClient != null) { |
| 104 _pingPongClient.close(); | 105 _pingPongClient.close(); |
| 105 _pingPongClient = null; | 106 _pingPongClient = null; |
| 106 } | 107 } |
| 107 _stub.close(); | 108 _binding.close(); |
| 108 if (_application != null) { | 109 _binding = null; |
| 109 _application.close(); | |
| 110 } | |
| 111 } | 110 } |
| 112 } | 111 } |
| 113 | 112 |
| 114 class PingPongApplication extends Application { | 113 class PingPongApplication extends Application { |
| 115 PingPongApplication.fromHandle(MojoHandle handle) : super.fromHandle(handle); | 114 PingPongApplication.fromHandle(MojoHandle handle) : super.fromHandle(handle); |
| 116 | 115 |
| 117 void acceptConnection(String requestorUrl, ApplicationConnection connection) { | 116 void acceptConnection(String requestorUrl, |
| 117 ApplicationConnection connection, |
| 118 String resolvedUrl) { |
| 118 connection.provideService(PingPongServiceName, | 119 connection.provideService(PingPongServiceName, |
| 119 (endpoint) => new PingPongServiceImpl(this, endpoint)); | 120 (endpoint) => new PingPongServiceImpl(this, endpoint)); |
| 120 connection.listen(); | 121 connection.listen(); |
| 121 } | 122 } |
| 122 } | 123 } |
| 123 | 124 |
| 124 main(List args) { | 125 main(List args) { |
| 125 MojoHandle appHandle = new MojoHandle(args[0]); | 126 MojoHandle appHandle = new MojoHandle(args[0]); |
| 126 String url = args[1]; | 127 String url = args[1]; |
| 127 new PingPongApplication.fromHandle(appHandle); | 128 new PingPongApplication.fromHandle(appHandle); |
| 128 } | 129 } |
| OLD | NEW |