| 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 PingPongClientStub stub; |
| 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 : stub = new PingPongClientStub.unbound() { |
| 19 stub.delegate = this; | 19 stub.impl = 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 stub.close(); |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 } | 28 } |
| 29 | 29 |
| 30 class PingPongServiceImpl implements PingPongService { | 30 class PingPongServiceImpl implements PingPongService { |
| 31 PingPongServiceStub _stub; | 31 PingPongServiceStub _stub; |
| 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 _stub = new PingPongServiceStub.fromEndpoint(endpoint, impl: this); |
| 38 ..delegate = this | |
| 39 ..listen(); | |
| 40 } | 38 } |
| 41 | 39 |
| 42 PingPongServiceImpl.fromStub(this._stub) { | 40 PingPongServiceImpl.fromStub(this._stub) { |
| 43 _stub.delegate = this; | 41 _stub.impl = this; |
| 44 } | 42 } |
| 45 | 43 |
| 46 listen() => _stub.listen(); | 44 listen() => _stub.listen(); |
| 47 | 45 |
| 48 void setClient(ProxyBase proxyBase) { | 46 void setClient(ProxyBase proxyBase) { |
| 49 assert(_pingPongClient== null); | 47 assert(_pingPongClient == null); |
| 50 _pingPongClient = proxyBase; | 48 _pingPongClient = proxyBase; |
| 51 } | 49 } |
| 52 | 50 |
| 53 void ping(int pingValue) { | 51 void ping(int pingValue) { |
| 54 if (_pingPongClient != null) { | 52 if (_pingPongClient != null) { |
| 55 _pingPongClient.ptr.pong(pingValue + 1); | 53 _pingPongClient.ptr.pong(pingValue + 1); |
| 56 } | 54 } |
| 57 } | 55 } |
| 58 | 56 |
| 59 Future pingTargetUrl(String url, int count, Function responseFactory) async { | 57 Future pingTargetUrl(String url, int count, Function responseFactory) async { |
| 60 if (_application == null) { | 58 if (_application == null) { |
| 61 return responseFactory(false); | 59 return responseFactory(false); |
| 62 } | 60 } |
| 63 var completer = new Completer(); | 61 var completer = new Completer(); |
| 64 var pingPongService= new PingPongServiceProxy.unbound(); | 62 var pingPongService = new PingPongServiceProxy.unbound(); |
| 65 _application.connectToService(url, pingPongService); | 63 _application.connectToService(url, pingPongService); |
| 66 | 64 |
| 67 var pingPongClient = new PingPongClientImpl.unbound(count, completer); | 65 var pingPongClient = new PingPongClientImpl.unbound(count, completer); |
| 68 pingPongService.ptr.setClient(pingPongClient.stub); | 66 pingPongService.ptr.setClient(pingPongClient.stub); |
| 69 pingPongClient.stub.listen(); | 67 pingPongClient.stub.listen(); |
| 70 | 68 |
| 71 for (var i = 0; i < count; i++) { | 69 for (var i = 0; i < count; i++) { |
| 72 pingPongService.ptr.ping(i); | 70 pingPongService.ptr.ping(i); |
| 73 } | 71 } |
| 74 await completer.future; | 72 await completer.future; |
| 75 pingPongService.ptr.quit(); | 73 pingPongService.ptr.quit(); |
| 74 pingPongService.close(); |
| 76 | 75 |
| 77 return responseFactory(true); | 76 return responseFactory(true); |
| 78 } | 77 } |
| 79 | 78 |
| 80 Future pingTargetService(ProxyBase proxyBase, | 79 Future pingTargetService(ProxyBase proxyBase, int count, |
| 81 int count, | 80 Function responseFactory) async { |
| 82 Function responseFactory) async { | |
| 83 var pingPongService = proxyBase; | 81 var pingPongService = proxyBase; |
| 84 var completer = new Completer(); | 82 var completer = new Completer(); |
| 85 var client = new PingPongClientImpl.unbound(count, completer); | 83 var client = new PingPongClientImpl.unbound(count, completer); |
| 86 pingPongService.ptr.setClient(client.stub); | 84 pingPongService.ptr.setClient(client.stub); |
| 87 client.stub.listen(); | 85 client.stub.listen(); |
| 88 | 86 |
| 89 for (var i = 0; i < count; i++) { | 87 for (var i = 0; i < count; i++) { |
| 90 pingPongService.ptr.ping(i); | 88 pingPongService.ptr.ping(i); |
| 91 } | 89 } |
| 92 await completer.future; | 90 await completer.future; |
| 93 pingPongService.ptr.quit(); | 91 pingPongService.ptr.quit(); |
| 92 pingPongService.close(); |
| 94 | 93 |
| 95 return responseFactory(true); | 94 return responseFactory(true); |
| 96 } | 95 } |
| 97 | 96 |
| 98 getPingPongService(PingPongServiceStub serviceStub) { | 97 getPingPongService(PingPongServiceStub serviceStub) { |
| 99 new PingPongServiceImpl.fromStub(serviceStub)..listen(); | 98 new PingPongServiceImpl.fromStub(serviceStub)..listen(); |
| 100 } | 99 } |
| 101 | 100 |
| 102 void quit() { | 101 void quit() { |
| 103 if (_pingPongClient != null) { | 102 if (_pingPongClient != null) { |
| 104 _pingPongClient.close(); | 103 _pingPongClient.close(); |
| 105 _pingPongClient = null; | 104 _pingPongClient = null; |
| 106 } | 105 } |
| 107 _stub.close(); | 106 _stub.close(); |
| 108 if (_application != null) { | 107 _stub = null; |
| 109 _application.close(); | |
| 110 } | |
| 111 } | 108 } |
| 112 } | 109 } |
| 113 | 110 |
| 114 class PingPongApplication extends Application { | 111 class PingPongApplication extends Application { |
| 115 PingPongApplication.fromHandle(MojoHandle handle) : super.fromHandle(handle); | 112 PingPongApplication.fromHandle(MojoHandle handle) : super.fromHandle(handle); |
| 116 | 113 |
| 117 @override | 114 @override |
| 118 void acceptConnection(String requestorUrl, String resolvedUrl, | 115 void acceptConnection(String requestorUrl, String resolvedUrl, |
| 119 ApplicationConnection connection) { | 116 ApplicationConnection connection) { |
| 120 connection.provideService(PingPongServiceName, | 117 connection.provideService( |
| 118 PingPongServiceName, |
| 121 (endpoint) => new PingPongServiceImpl(this, endpoint)); | 119 (endpoint) => new PingPongServiceImpl(this, endpoint)); |
| 122 connection.listen(); | 120 connection.listen(); |
| 123 } | 121 } |
| 124 } | 122 } |
| 125 | 123 |
| 126 main(List args) { | 124 main(List args) { |
| 127 MojoHandle appHandle = new MojoHandle(args[0]); | 125 MojoHandle appHandle = new MojoHandle(args[0]); |
| 128 String url = args[1]; | 126 String url = args[1]; |
| 129 new PingPongApplication.fromHandle(appHandle); | 127 new PingPongApplication.fromHandle(appHandle); |
| 130 } | 128 } |
| OLD | NEW |