| 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 | 6 |
| 7 import 'package:mojo/application.dart'; | 7 import 'package:mojo/application.dart'; |
| 8 import 'package:mojo/bindings.dart'; | 8 import 'package:mojo/bindings.dart'; |
| 9 import 'package:mojo/core.dart'; | 9 import 'package:mojo/core.dart'; |
| 10 | 10 |
| 11 import 'package:_mojo_for_test_only/test/pingpong_service.mojom.dart'; | 11 import 'package:_mojo_for_test_only/test/pingpong_service.mojom.dart'; |
| 12 | 12 |
| 13 class PingPongClientImpl implements PingPongClient { | 13 class PingPongClientImpl implements PingPongClient { |
| 14 final PingPongClientStub stub; | 14 PingPongClientInterface client; |
| 15 Completer _completer; | 15 Completer _completer; |
| 16 int _count; | 16 int _count; |
| 17 | 17 |
| 18 PingPongClientImpl.unbound(this._count, this._completer) | 18 PingPongClientImpl(this._count, this._completer) { |
| 19 : stub = new PingPongClientStub.unbound() { | 19 client = new PingPongClientInterface(this); |
| 20 stub.impl = this; | |
| 21 } | 20 } |
| 22 | 21 |
| 23 void pong(int pongValue) { | 22 void pong(int pongValue) { |
| 24 if (pongValue == _count) { | 23 if (pongValue == _count) { |
| 25 _completer.complete(null); | 24 _completer.complete(null); |
| 26 stub.close(); | 25 client.close(); |
| 27 } | 26 } |
| 28 } | 27 } |
| 29 } | 28 } |
| 30 | 29 |
| 31 class PingPongServiceImpl implements PingPongService { | 30 class PingPongServiceImpl implements PingPongService { |
| 32 PingPongServiceStub _stub; | 31 PingPongService _service; |
| 33 Application _application; | 32 Application _application; |
| 34 PingPongClientProxy _pingPongClient; | 33 PingPongClient _pingPongClient; |
| 35 | 34 |
| 36 PingPongServiceImpl(this._application, MojoMessagePipeEndpoint endpoint) { | 35 PingPongServiceImpl(this._application, MojoMessagePipeEndpoint endpoint) { |
| 37 _stub = new PingPongServiceStub.fromEndpoint(endpoint, this); | 36 _service = new PingPongServiceInterface.fromEndpoint(endpoint, this); |
| 38 } | 37 } |
| 39 | 38 |
| 40 PingPongServiceImpl.fromStub(this._stub) { | 39 void setClient(PingPongClientInterface client) { |
| 41 _stub.impl = this; | |
| 42 } | |
| 43 | |
| 44 void setClient(Proxy proxy) { | |
| 45 assert(_pingPongClient == null); | 40 assert(_pingPongClient == null); |
| 46 _pingPongClient = proxy; | 41 _pingPongClient = client; |
| 47 } | 42 } |
| 48 | 43 |
| 49 void ping(int pingValue) { | 44 void ping(int pingValue) { |
| 50 if (_pingPongClient != null) { | 45 if (_pingPongClient != null) { |
| 51 _pingPongClient.pong(pingValue + 1); | 46 _pingPongClient.pong(pingValue + 1); |
| 52 } | 47 } |
| 53 } | 48 } |
| 54 | 49 |
| 55 Future pingTargetUrl(String url, int count, | 50 Future pingTargetUrl(String url, int count, |
| 56 [Function responseFactory]) async { | 51 [Function responseFactory]) async { |
| 57 if (_application == null) { | 52 if (_application == null) { |
| 58 return responseFactory(false); | 53 return responseFactory(false); |
| 59 } | 54 } |
| 60 var completer = new Completer(); | 55 var completer = new Completer(); |
| 61 var pingPongService = new PingPongServiceProxy.unbound(); | 56 var pingPongService = new PingPongServiceInterfaceRequest(); |
| 62 _application.connectToService(url, pingPongService); | 57 _application.connectToService(url, pingPongService); |
| 63 | 58 |
| 64 var pingPongClient = new PingPongClientImpl.unbound(count, completer); | 59 var pingPongClient = new PingPongClientImpl(count, completer); |
| 65 pingPongService.setClient(pingPongClient.stub); | 60 pingPongService.setClient(pingPongClient.client); |
| 66 | 61 |
| 67 for (var i = 0; i < count; i++) { | 62 for (var i = 0; i < count; i++) { |
| 68 pingPongService.ping(i); | 63 pingPongService.ping(i); |
| 69 } | 64 } |
| 70 await completer.future; | 65 await completer.future; |
| 71 await pingPongService.close(); | 66 await pingPongService.close(); |
| 72 | 67 |
| 73 return responseFactory(true); | 68 return responseFactory(true); |
| 74 } | 69 } |
| 75 | 70 |
| 76 Future pingTargetService(Proxy proxy, int count, | 71 Future pingTargetService(PingPongServiceInterface service, int count, |
| 77 [Function responseFactory]) async { | 72 [Function responseFactory]) async { |
| 78 var pingPongService = proxy; | 73 var pingPongService = service; |
| 79 var completer = new Completer(); | 74 var completer = new Completer(); |
| 80 var client = new PingPongClientImpl.unbound(count, completer); | 75 var client = new PingPongClientImpl(count, completer); |
| 81 pingPongService.setClient(client.stub); | 76 pingPongService.setClient(client.client); |
| 82 | 77 |
| 83 for (var i = 0; i < count; i++) { | 78 for (var i = 0; i < count; i++) { |
| 84 pingPongService.ping(i); | 79 pingPongService.ping(i); |
| 85 } | 80 } |
| 86 await completer.future; | 81 await completer.future; |
| 87 await pingPongService.close(); | 82 await pingPongService.close(); |
| 88 | 83 |
| 89 return responseFactory(true); | 84 return responseFactory(true); |
| 90 } | 85 } |
| 91 | 86 |
| 92 getPingPongService(PingPongServiceStub serviceStub) { | 87 getPingPongService(PingPongServiceInterfaceRequest service) { |
| 93 var targetServiceProxy = new PingPongServiceProxy.unbound(); | 88 var targetService = new PingPongServiceInterfaceRequest(); |
| 94 _application.connectToService( | 89 _application.connectToService("mojo:dart_pingpong_target", targetService); |
| 95 "mojo:dart_pingpong_target", targetServiceProxy); | |
| 96 | 90 |
| 97 // Pass along the interface request to another implementation of the | 91 // Pass along the interface request to another implementation of the |
| 98 // service. | 92 // service. |
| 99 targetServiceProxy.getPingPongService(serviceStub); | 93 targetService.getPingPongService(service); |
| 100 targetServiceProxy.close(); | 94 targetService.close(); |
| 101 } | 95 } |
| 102 | 96 |
| 103 getPingPongServiceDelayed(PingPongServiceStub serviceStub) { | 97 getPingPongServiceDelayed(PingPongServiceInterfaceRequest service) { |
| 104 Timer.run(() { | 98 Timer.run(() { |
| 105 var endpoint = serviceStub.ctrl.unbind(); | 99 var endpoint = service.ctrl.unbind(); |
| 106 new Timer(const Duration(milliseconds: 10), () { | 100 new Timer(const Duration(milliseconds: 10), () { |
| 107 var targetServiceProxy = new PingPongServiceProxy.unbound(); | 101 var targetService = new PingPongServiceInterfaceRequest(); |
| 108 _application.connectToService( | 102 _application.connectToService( |
| 109 "mojo:dart_pingpong_target", targetServiceProxy); | 103 "mojo:dart_pingpong_target", targetService); |
| 110 | 104 |
| 111 // Pass along the interface request to another implementation of the | 105 // Pass along the interface request to another implementation of the |
| 112 // service. | 106 // service. |
| 113 serviceStub.ctrl.bind(endpoint); | 107 service.ctrl.bind(endpoint); |
| 114 targetServiceProxy.getPingPongService(serviceStub); | 108 targetService.getPingPongService(service); |
| 115 targetServiceProxy.close(); | 109 targetService.close(); |
| 116 }); | 110 }); |
| 117 }); | 111 }); |
| 118 } | 112 } |
| 119 | 113 |
| 120 void quit() {} | 114 void quit() {} |
| 121 } | 115 } |
| 122 | 116 |
| 123 class PingPongApplication extends Application { | 117 class PingPongApplication extends Application { |
| 124 PingPongApplication.fromHandle(MojoHandle handle) : super.fromHandle(handle); | 118 PingPongApplication.fromHandle(MojoHandle handle) : super.fromHandle(handle); |
| 125 | 119 |
| 126 @override | 120 @override |
| 127 void acceptConnection(String requestorUrl, String resolvedUrl, | 121 void acceptConnection(String requestorUrl, String resolvedUrl, |
| 128 ApplicationConnection connection) { | 122 ApplicationConnection connection) { |
| 129 connection.provideService(PingPongService.serviceName, | 123 connection.provideService(PingPongService.serviceName, |
| 130 (endpoint) => new PingPongServiceImpl(this, endpoint), | 124 (endpoint) => new PingPongServiceImpl(this, endpoint), |
| 131 description: PingPongServiceStub.serviceDescription); | 125 description: PingPongService.serviceDescription); |
| 132 } | 126 } |
| 133 } | 127 } |
| 134 | 128 |
| 135 main(List args, Object handleToken) { | 129 main(List args, Object handleToken) { |
| 136 MojoHandle appHandle = new MojoHandle(handleToken); | 130 MojoHandle appHandle = new MojoHandle(handleToken); |
| 137 new PingPongApplication.fromHandle(appHandle) | 131 new PingPongApplication.fromHandle(appHandle) |
| 138 ..onError = ((_) { | 132 ..onError = ((_) { |
| 139 MojoHandle.reportLeakedHandles(); | 133 MojoHandle.reportLeakedHandles(); |
| 140 }); | 134 }); |
| 141 } | 135 } |
| OLD | NEW |