1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
/// Licensed to the Apache Software Foundation (ASF) under one
/// or more contributor license agreements. See the NOTICE file
/// distributed with this work for additional information
/// regarding copyright ownership. The ASF licenses this file
/// to you under the Apache License, Version 2.0 (the
/// "License"); you may not use this file except in compliance
/// with the License. You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing,
/// software distributed under the License is distributed on an
/// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
/// KIND, either express or implied. See the License for the
/// specific language governing permissions and limitations
/// under the License.
import 'dart:async';
import 'dart:io';
import 'package:args/args.dart';
import 'package:logging/logging.dart';
import 'package:thrift/thrift.dart';
import 'package:thrift/thrift_console.dart';
import 'package:tutorial/tutorial.dart';
TTransport _transport;
Calculator _calculator;
int logid = 0;
const Map<String, int> operationLookup = const {
'+': Operation.ADD,
'-': Operation.SUBTRACT,
'*': Operation.MULTIPLY,
'/': Operation.DIVIDE
};
main(List<String> args) {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((LogRecord rec) {
print('${rec.level.name}: ${rec.time}: ${rec.message}');
});
var parser = new ArgParser();
parser.addOption('port', defaultsTo: '9090', help: 'The port to connect to');
ArgResults results;
try {
results = parser.parse(args);
} catch (e) {
results = null;
}
if (results == null) {
print(parser.usage);
exit(0);
}
int port = int.parse(results['port']);
_initConnection(port).then((_) => _run());
}
Future _initConnection(int port) async {
var socket = await Socket.connect('127.0.0.1', port);
_transport = new TAsyncClientSocketTransport(
new TTcpSocket(socket), new TMessageReader(new TBinaryProtocolFactory()));
TProtocol protocol = new TBinaryProtocol(_transport);
await _transport.open();
_calculator = new CalculatorClient(protocol);
}
Future _run() async {
_help();
while (true) {
stdout.write("> ");
var input = stdin.readLineSync();
var parts = input.split(' ');
var command = parts[0];
var args = parts.length > 1 ? parts.sublist(1) : [];
switch (command) {
case 'ping':
await _ping();
break;
case 'add':
await _add(int.parse(args[0]), int.parse(args[1]));
break;
case 'calc':
int op = operationLookup[args[1]];
if (!Operation.VALID_VALUES.contains(op)) {
stdout.writeln('Unknown operator ${args[1]}');
break;
}
var work = new Work()
..num1 = int.parse(args[0])
..op = op
..num2 = int.parse(args[2])
..comment = args.length > 3 ? args[3] : '';
await _calc(work);
break;
case 'struct':
await _struct(int.parse(args[0]));
break;
case 'help':
default:
_help();
break;
}
}
}
void _help() {
stdout.writeln('Commands:');
stdout.writeln(' help');
stdout.writeln(' ping');
stdout.writeln(' add x y');
stdout.writeln(' calc x op y [comment]');
stdout.writeln(' struct id');
stdout.writeln('');
}
Future _ping() async {
await _calculator.ping();
stdout.writeln('ping succeeded');
}
Future _add(int x, int y) async {
int result = await _calculator.add(x, y);
stdout.writeln('= $result');
}
Future _calc(Work work) async {
int result = await _calculator.calculate(logid++, work);
stdout.writeln('= $result');
}
Future _struct(int key) async {
var struct = await _calculator.getStruct(key);
stdout.writeln(struct.toString());
}
|