]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/tutorial/dart/console_client/bin/main.dart
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / tutorial / dart / console_client / bin / main.dart
CommitLineData
f67539c2
TL
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
18import 'dart:async';
19import 'dart:io';
20
21import 'package:args/args.dart';
22import 'package:logging/logging.dart';
23import 'package:thrift/thrift.dart';
24import 'package:thrift/thrift_console.dart';
25import 'package:tutorial/tutorial.dart';
26
27TTransport _transport;
28Calculator _calculator;
29int logid = 0;
30
31const Map<String, int> operationLookup = const {
32 '+': Operation.ADD,
33 '-': Operation.SUBTRACT,
34 '*': Operation.MULTIPLY,
35 '/': Operation.DIVIDE
36};
37
38main(List<String> args) {
39 Logger.root.level = Level.ALL;
40 Logger.root.onRecord.listen((LogRecord rec) {
41 print('${rec.level.name}: ${rec.time}: ${rec.message}');
42 });
43
44 var parser = new ArgParser();
45 parser.addOption('port', defaultsTo: '9090', help: 'The port to connect to');
46
47 ArgResults results;
48 try {
49 results = parser.parse(args);
50 } catch (e) {
51 results = null;
52 }
53
54 if (results == null) {
55 print(parser.usage);
56 exit(0);
57 }
58
59 int port = int.parse(results['port']);
60
61 _initConnection(port).then((_) => _run());
62}
63
64Future _initConnection(int port) async {
65 var socket = await Socket.connect('127.0.0.1', port);
66 _transport = new TAsyncClientSocketTransport(
67 new TTcpSocket(socket), new TMessageReader(new TBinaryProtocolFactory()));
68 TProtocol protocol = new TBinaryProtocol(_transport);
69 await _transport.open();
70
71 _calculator = new CalculatorClient(protocol);
72}
73
74Future _run() async {
75 _help();
76
77 while (true) {
78 stdout.write("> ");
79 var input = stdin.readLineSync();
80 var parts = input.split(' ');
81 var command = parts[0];
82 var args = parts.length > 1 ? parts.sublist(1) : [];
83
84 switch (command) {
85 case 'ping':
86 await _ping();
87 break;
88
89 case 'add':
90 await _add(int.parse(args[0]), int.parse(args[1]));
91 break;
92
93 case 'calc':
94 int op = operationLookup[args[1]];
95 if (!Operation.VALID_VALUES.contains(op)) {
96 stdout.writeln('Unknown operator ${args[1]}');
97 break;
98 }
99
100 var work = new Work()
101 ..num1 = int.parse(args[0])
102 ..op = op
103 ..num2 = int.parse(args[2])
104 ..comment = args.length > 3 ? args[3] : '';
105
106 await _calc(work);
107 break;
108
109 case 'struct':
110 await _struct(int.parse(args[0]));
111 break;
112
113 case 'help':
114 default:
115 _help();
116 break;
117 }
118 }
119}
120
121void _help() {
122 stdout.writeln('Commands:');
123 stdout.writeln(' help');
124 stdout.writeln(' ping');
125 stdout.writeln(' add x y');
126 stdout.writeln(' calc x op y [comment]');
127 stdout.writeln(' struct id');
128 stdout.writeln('');
129}
130
131Future _ping() async {
132 await _calculator.ping();
133 stdout.writeln('ping succeeded');
134}
135
136Future _add(int x, int y) async {
137 int result = await _calculator.add(x, y);
138 stdout.writeln('= $result');
139}
140
141Future _calc(Work work) async {
142 int result = await _calculator.calculate(logid++, work);
143 stdout.writeln('= $result');
144}
145
146Future _struct(int key) async {
147 var struct = await _calculator.getStruct(key);
148 stdout.writeln(struct.toString());
149}