]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/tutorial/dart/client/web/client.dart
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / tutorial / dart / client / web / client.dart
1 /// Licensed to the Apache Software Foundation (ASF) under one
2 /// or more contributor license agreements. See the NOTICE file
3 /// distributed with this work for additional information
4 /// regarding copyright ownership. The ASF licenses this file
5 /// to you under the Apache License, Version 2.0 (the
6 /// "License"); you may not use this file except in compliance
7 /// with the License. You may obtain a copy of the License at
8 ///
9 /// http://www.apache.org/licenses/LICENSE-2.0
10 ///
11 /// Unless required by applicable law or agreed to in writing,
12 /// software distributed under the License is distributed on an
13 /// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 /// KIND, either express or implied. See the License for the
15 /// specific language governing permissions and limitations
16 /// under the License.
17
18 import 'dart:html';
19
20 import 'package:thrift/thrift.dart';
21 import 'package:thrift/thrift_browser.dart';
22 import 'package:shared/shared.dart';
23 import 'package:tutorial/tutorial.dart';
24
25 /// Adapted from the AS3 tutorial
26 void main() {
27 new CalculatorUI(querySelector('#output')).start();
28 }
29
30 class CalculatorUI {
31 final DivElement output;
32
33 CalculatorUI(this.output);
34
35 TTransport _transport;
36 Calculator _calculatorClient;
37
38 void start() {
39 _buildInterface();
40 _initConnection();
41 }
42
43 void _validate() {
44 if (!_transport.isOpen) {
45 window.alert("The transport is not open!");
46 }
47 }
48
49 void _initConnection() {
50 _transport = new TAsyncClientSocketTransport(
51 new TWebSocket(Uri.parse('ws://127.0.0.1:9090/ws')),
52 new TMessageReader(new TBinaryProtocolFactory()));
53 TProtocol protocol = new TBinaryProtocol(_transport);
54 _transport.open();
55
56 _calculatorClient = new CalculatorClient(protocol);
57 }
58
59 void _buildInterface() {
60 output.children.forEach((e) {
61 e.remove();
62 });
63
64 _buildPingComponent();
65
66 _buildAddComponent();
67
68 _buildCalculatorComponent();
69
70 _buildGetStructComponent();
71 }
72
73 void _buildPingComponent() {
74 output.append(new HeadingElement.h3()..text = "Ping");
75 ButtonElement pingButton = new ButtonElement()
76 ..text = "PING"
77 ..onClick.listen(_onPingClick);
78 output.append(pingButton);
79 }
80
81 void _onPingClick(MouseEvent e) {
82 _validate();
83
84 _calculatorClient.ping();
85 }
86
87 void _buildAddComponent() {
88 output.append(new HeadingElement.h3()..text = "Add");
89 InputElement num1 = new InputElement()
90 ..id = "add1"
91 ..type = "number"
92 ..style.fontSize = "14px"
93 ..style.width = "50px";
94 output.append(num1);
95 SpanElement op = new SpanElement()
96 ..text = "+"
97 ..style.fontSize = "14px"
98 ..style.marginLeft = "10px";
99 output.append(op);
100 InputElement num2 = new InputElement()
101 ..id = "add2"
102 ..type = "number"
103 ..style.fontSize = "14px"
104 ..style.width = "50px"
105 ..style.marginLeft = "10px";
106 output.append(num2);
107 ButtonElement addButton = new ButtonElement()
108 ..text = "="
109 ..style.fontSize = "14px"
110 ..style.marginLeft = "10px"
111 ..onClick.listen(_onAddClick);
112 output.append(addButton);
113 SpanElement result = new SpanElement()
114 ..id = "addResult"
115 ..style.fontSize = "14px"
116 ..style.marginLeft = "10px";
117 output.append(result);
118 }
119
120 void _onAddClick(MouseEvent e) {
121 _validate();
122
123 InputElement num1 = querySelector("#add1");
124 InputElement num2 = querySelector("#add2");
125 SpanElement result = querySelector("#addResult");
126
127 _calculatorClient
128 .add(int.parse(num1.value), int.parse(num2.value))
129 .then((int n) {
130 result.text = "$n";
131 });
132 }
133
134 void _buildCalculatorComponent() {
135 output.append(new HeadingElement.h3()..text = "Calculator");
136 InputElement num1 = new InputElement()
137 ..id = "calc1"
138 ..type = "number"
139 ..style.fontSize = "14px"
140 ..style.width = "50px";
141 output.append(num1);
142 SelectElement op = new SelectElement()
143 ..id = "calcOp"
144 ..multiple = false
145 ..selectedIndex = 0
146 ..style.fontSize = "16px"
147 ..style.marginLeft = "10px"
148 ..style.width = "50px";
149 OptionElement addOp = new OptionElement()
150 ..text = "+"
151 ..value = Operation.ADD.toString();
152 op.add(addOp, 0);
153 OptionElement subtractOp = new OptionElement()
154 ..text = "-"
155 ..value = Operation.SUBTRACT.toString();
156 op.add(subtractOp, 1);
157 OptionElement multiplyOp = new OptionElement()
158 ..text = "*"
159 ..value = Operation.MULTIPLY.toString();
160 op.add(multiplyOp, 2);
161 OptionElement divideOp = new OptionElement()
162 ..text = "/"
163 ..value = Operation.DIVIDE.toString();
164 op.add(divideOp, 3);
165 output.append(op);
166 InputElement num2 = new InputElement()
167 ..id = "calc2"
168 ..type = "number"
169 ..style.fontSize = "14px"
170 ..style.width = "50px"
171 ..style.marginLeft = "10px";
172 output.append(num2);
173 ButtonElement calcButton = new ButtonElement()
174 ..text = "="
175 ..style.fontSize = "14px"
176 ..style.marginLeft = "10px"
177 ..onClick.listen(_onCalcClick);
178 output.append(calcButton);
179 SpanElement result = new SpanElement()
180 ..id = "calcResult"
181 ..style.fontSize = "14px"
182 ..style.marginLeft = "10px";
183 output.append(result);
184 output.append(new BRElement());
185 output.append(new BRElement());
186 LabelElement logIdLabel = new LabelElement()
187 ..text = "Log ID:"
188 ..style.fontSize = "14px";
189 output.append(logIdLabel);
190 InputElement logId = new InputElement()
191 ..id = "logId"
192 ..type = "number"
193 ..value = "1"
194 ..style.fontSize = "14px"
195 ..style.width = "50px"
196 ..style.marginLeft = "10px";
197 output.append(logId);
198 LabelElement commentLabel = new LabelElement()
199 ..text = "Comment:"
200 ..style.fontSize = "14px"
201 ..style.marginLeft = "10px";
202 output.append(commentLabel);
203 InputElement comment = new InputElement()
204 ..id = "comment"
205 ..style.fontSize = "14px"
206 ..style.width = "100px"
207 ..style.marginLeft = "10px";
208 output.append(comment);
209 }
210
211 void _onCalcClick(MouseEvent e) {
212 _validate();
213
214 InputElement num1 = querySelector("#calc1");
215 InputElement num2 = querySelector("#calc2");
216 SelectElement op = querySelector("#calcOp");
217 SpanElement result = querySelector("#calcResult");
218 InputElement logId = querySelector("#logId");
219 InputElement comment = querySelector("#comment");
220
221 int logIdValue = int.parse(logId.value);
222 logId.value = (logIdValue + 1).toString();
223
224 Work work = new Work();
225 work.num1 = int.parse(num1.value);
226 work.num2 = int.parse(num2.value);
227 work.op = int.parse(op.options[op.selectedIndex].value);
228 work.comment = comment.value;
229
230 _calculatorClient.calculate(logIdValue, work).then((int n) {
231 result.text = "$n";
232 });
233 }
234
235 void _buildGetStructComponent() {
236 output.append(new HeadingElement.h3()..text = "Get Struct");
237 LabelElement logIdLabel = new LabelElement()
238 ..text = "Struct Key:"
239 ..style.fontSize = "14px";
240 output.append(logIdLabel);
241 InputElement logId = new InputElement()
242 ..id = "structKey"
243 ..type = "number"
244 ..value = "1"
245 ..style.fontSize = "14px"
246 ..style.width = "50px"
247 ..style.marginLeft = "10px";
248 output.append(logId);
249 ButtonElement getStructButton = new ButtonElement()
250 ..text = "GET"
251 ..style.fontSize = "14px"
252 ..style.marginLeft = "10px"
253 ..onClick.listen(_onGetStructClick);
254 output.append(getStructButton);
255 output.append(new BRElement());
256 output.append(new BRElement());
257 TextAreaElement result = new TextAreaElement()
258 ..id = "getStructResult"
259 ..style.fontSize = "14px"
260 ..style.width = "300px"
261 ..style.height = "50px"
262 ..style.marginLeft = "10px";
263 output.append(result);
264 }
265
266 void _onGetStructClick(MouseEvent e) {
267 _validate();
268
269 InputElement structKey = querySelector("#structKey");
270 TextAreaElement result = querySelector("#getStructResult");
271
272 _calculatorClient
273 .getStruct(int.parse(structKey.value))
274 .then((SharedStruct s) {
275 result.text = "${s.toString()}";
276 });
277 }
278 }