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