Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: services/dart/test/pingpong/main.dart

Issue 982673002: Dart: Removes need to call listen(). (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Don't pile up callbacks Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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';
(...skipping 14 matching lines...) Expand all
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(this._application, MojoMessagePipeEndpoint endpoint) {
36 : _application = application { 36 _stub = new PingPongServiceStub.fromEndpoint(endpoint, this);
37 _stub = new PingPongServiceStub.fromEndpoint(endpoint, impl: this);
38 } 37 }
39 38
40 PingPongServiceImpl.fromStub(this._stub) { 39 PingPongServiceImpl.fromStub(this._stub) {
41 _stub.impl = this; 40 _stub.impl = this;
42 } 41 }
43 42
44 listen() => _stub.listen();
45
46 void setClient(ProxyBase proxyBase) { 43 void setClient(ProxyBase proxyBase) {
47 assert(_pingPongClient == null); 44 assert(_pingPongClient == null);
48 _pingPongClient = proxyBase; 45 _pingPongClient = proxyBase;
49 } 46 }
50 47
51 void ping(int pingValue) { 48 void ping(int pingValue) {
52 if (_pingPongClient != null) { 49 if (_pingPongClient != null) {
53 _pingPongClient.ptr.pong(pingValue + 1); 50 _pingPongClient.ptr.pong(pingValue + 1);
54 } 51 }
55 } 52 }
56 53
57 Future pingTargetUrl(String url, int count, Function responseFactory) async { 54 Future pingTargetUrl(String url, int count, Function responseFactory) async {
58 if (_application == null) { 55 if (_application == null) {
59 return responseFactory(false); 56 return responseFactory(false);
60 } 57 }
61 var completer = new Completer(); 58 var completer = new Completer();
62 var pingPongService = new PingPongServiceProxy.unbound(); 59 var pingPongService = new PingPongServiceProxy.unbound();
63 _application.connectToService(url, pingPongService); 60 _application.connectToService(url, pingPongService);
64 61
65 var pingPongClient = new PingPongClientImpl.unbound(count, completer); 62 var pingPongClient = new PingPongClientImpl.unbound(count, completer);
66 pingPongService.ptr.setClient(pingPongClient.stub); 63 pingPongService.ptr.setClient(pingPongClient.stub);
67 pingPongClient.stub.listen();
68 64
69 for (var i = 0; i < count; i++) { 65 for (var i = 0; i < count; i++) {
70 pingPongService.ptr.ping(i); 66 pingPongService.ptr.ping(i);
71 } 67 }
72 await completer.future; 68 await completer.future;
73 pingPongService.ptr.quit(); 69 pingPongService.ptr.quit();
74 pingPongService.close(); 70 pingPongService.close();
75 71
76 return responseFactory(true); 72 return responseFactory(true);
77 } 73 }
78 74
79 Future pingTargetService(ProxyBase proxyBase, int count, 75 Future pingTargetService(ProxyBase proxyBase,
80 Function responseFactory) async { 76 int count,
77 Function responseFactory) async {
81 var pingPongService = proxyBase; 78 var pingPongService = proxyBase;
82 var completer = new Completer(); 79 var completer = new Completer();
83 var client = new PingPongClientImpl.unbound(count, completer); 80 var client = new PingPongClientImpl.unbound(count, completer);
84 pingPongService.ptr.setClient(client.stub); 81 pingPongService.ptr.setClient(client.stub);
85 client.stub.listen();
86 82
87 for (var i = 0; i < count; i++) { 83 for (var i = 0; i < count; i++) {
88 pingPongService.ptr.ping(i); 84 pingPongService.ptr.ping(i);
89 } 85 }
90 await completer.future; 86 await completer.future;
91 pingPongService.ptr.quit(); 87 pingPongService.ptr.quit();
92 pingPongService.close(); 88 pingPongService.close();
93 89
94 return responseFactory(true); 90 return responseFactory(true);
95 } 91 }
96 92
97 getPingPongService(PingPongServiceStub serviceStub) { 93 getPingPongService(PingPongServiceStub serviceStub) {
98 new PingPongServiceImpl.fromStub(serviceStub)..listen(); 94 new PingPongServiceImpl.fromStub(serviceStub);
99 } 95 }
100 96
101 void quit() { 97 void quit() {
102 if (_pingPongClient != null) { 98 if (_pingPongClient != null) {
103 _pingPongClient.close(); 99 _pingPongClient.close();
104 _pingPongClient = null; 100 _pingPongClient = null;
105 } 101 }
106 _stub.close(); 102 _stub.close();
107 _stub = null; 103 _stub = null;
108 } 104 }
109 } 105 }
110 106
111 class PingPongApplication extends Application { 107 class PingPongApplication extends Application {
112 PingPongApplication.fromHandle(MojoHandle handle) : super.fromHandle(handle); 108 PingPongApplication.fromHandle(MojoHandle handle) : super.fromHandle(handle);
113 109
114 @override 110 @override
115 void acceptConnection(String requestorUrl, String resolvedUrl, 111 void acceptConnection(String requestorUrl, String resolvedUrl,
116 ApplicationConnection connection) { 112 ApplicationConnection connection) {
117 connection.provideService( 113 connection.provideService(
118 PingPongServiceName, 114 PingPongServiceName,
119 (endpoint) => new PingPongServiceImpl(this, endpoint)); 115 (endpoint) => new PingPongServiceImpl(this, endpoint));
120 connection.listen();
121 } 116 }
122 } 117 }
123 118
124 main(List args) { 119 main(List args) {
125 MojoHandle appHandle = new MojoHandle(args[0]); 120 MojoHandle appHandle = new MojoHandle(args[0]);
126 String url = args[1]; 121 String url = args[1];
127 new PingPongApplication.fromHandle(appHandle); 122 new PingPongApplication.fromHandle(appHandle);
128 } 123 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698