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
9 /// http://www.apache.org/licenses/LICENSE-2.0
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.
21 import 'package:args/args.dart';
22 import 'package:logging/logging.dart';
23 import 'package:thrift/thrift.dart';
24 import 'package:thrift/thrift_console.dart';
25 import 'package:tutorial/tutorial.dart';
26 import 'package:shared/shared.dart';
29 TProcessor _processor;
32 main(List<String> args) {
33 Logger.root.level = Level.ALL;
34 Logger.root.onRecord.listen((LogRecord rec) {
35 print('${rec.level.name}: ${rec.time}: ${rec.message}');
38 var parser = new ArgParser();
39 parser.addOption('port', defaultsTo: '9090', help: 'The port to listen on');
40 parser.addOption('type',
42 allowed: ['ws', 'tcp'],
43 help: 'The type of socket',
44 allowedHelp: {'ws': 'WebSocket', 'tcp': 'TCP Socket'});
48 results = parser.parse(args);
53 if (results == null) {
58 int port = int.parse(results['port']);
59 String socketType = results['type'];
61 if (socketType == 'tcp') {
63 } else if (socketType == 'ws') {
64 _runWebSocketServer(port);
68 Future _runWebSocketServer(int port) async {
69 var httpServer = await HttpServer.bind('127.0.0.1', port);
70 print('listening for WebSocket connections on $port');
72 httpServer.listen((HttpRequest request) async {
73 if (request.uri.path == '/ws') {
74 _webSocket = await WebSocketTransformer.upgrade(request);
75 await _initProcessor(new TWebSocket(_webSocket));
77 print('Invalid path: ${request.uri.path}');
82 Future _runTcpServer(int port) async {
83 var serverSocket = await ServerSocket.bind('127.0.0.1', port);
84 print('listening for TCP connections on $port');
86 Socket socket = await serverSocket.first;
87 await _initProcessor(new TTcpSocket(socket));
90 Future _initProcessor(TSocket socket) async {
91 TServerSocketTransport transport = new TServerSocketTransport(socket);
92 transport.onIncomingMessage.listen(_processMessage);
93 _processor = new CalculatorProcessor(new CalculatorServer());
94 _protocol = new TBinaryProtocol(transport);
95 await _protocol.transport.open();
100 Future _processMessage(_) async {
101 _processor.process(_protocol, _protocol);
104 class CalculatorServer implements Calculator {
105 final Map<int, SharedStruct> _log = {};
107 Future ping() async {
111 Future<int> add(int num1, int num2) async {
112 print('add($num1, $num2)');
117 Future<int> calculate(int logid, Work work) async {
118 print('calulate($logid, ${work.toString()})');
124 val = work.num1 + work.num2;
127 case Operation.SUBTRACT:
128 val = work.num1 - work.num2;
131 case Operation.MULTIPLY:
132 val = work.num1 * work.num2;
135 case Operation.DIVIDE:
136 if (work.num2 == 0) {
137 var x = new InvalidOperation();
139 x.why = 'Cannot divide by 0';
142 val = (work.num1 / work.num2).floor();
146 var log = new SharedStruct();
148 log.value = '$val "${work.comment}"';
149 this._log[logid] = log;
158 Future<SharedStruct> getStruct(int key) async {
159 print('getStruct($key)');