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