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