]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/tutorial/dart/server/bin/main.dart
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / tutorial / dart / server / bin / main.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:async';
19 import 'dart:io';
20
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';
27
28 TProtocol _protocol;
29 TProcessor _processor;
30 WebSocket _webSocket;
31
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}');
36 });
37
38 var parser = new ArgParser();
39 parser.addOption('port', defaultsTo: '9090', help: 'The port to listen on');
40 parser.addOption('type',
41 defaultsTo: 'ws',
42 allowed: ['ws', 'tcp'],
43 help: 'The type of socket',
44 allowedHelp: {'ws': 'WebSocket', 'tcp': 'TCP Socket'});
45
46 ArgResults results;
47 try {
48 results = parser.parse(args);
49 } catch (e) {
50 results = null;
51 }
52
53 if (results == null) {
54 print(parser.usage);
55 exit(0);
56 }
57
58 int port = int.parse(results['port']);
59 String socketType = results['type'];
60
61 if (socketType == 'tcp') {
62 _runTcpServer(port);
63 } else if (socketType == 'ws') {
64 _runWebSocketServer(port);
65 }
66 }
67
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');
71
72 httpServer.listen((HttpRequest request) async {
73 if (request.uri.path == '/ws') {
74 _webSocket = await WebSocketTransformer.upgrade(request);
75 await _initProcessor(new TWebSocket(_webSocket));
76 } else {
77 print('Invalid path: ${request.uri.path}');
78 }
79 });
80 }
81
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');
85
86 Socket socket = await serverSocket.first;
87 await _initProcessor(new TTcpSocket(socket));
88 }
89
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();
96
97 print('connected');
98 }
99
100 Future _processMessage(_) async {
101 _processor.process(_protocol, _protocol);
102 }
103
104 class CalculatorServer implements Calculator {
105 final Map<int, SharedStruct> _log = {};
106
107 Future ping() async {
108 print('ping()');
109 }
110
111 Future<int> add(int num1, int num2) async {
112 print('add($num1, $num2)');
113
114 return num1 + num2;
115 }
116
117 Future<int> calculate(int logid, Work work) async {
118 print('calulate($logid, ${work.toString()})');
119
120 int val;
121
122 switch (work.op) {
123 case Operation.ADD:
124 val = work.num1 + work.num2;
125 break;
126
127 case Operation.SUBTRACT:
128 val = work.num1 - work.num2;
129 break;
130
131 case Operation.MULTIPLY:
132 val = work.num1 * work.num2;
133 break;
134
135 case Operation.DIVIDE:
136 if (work.num2 == 0) {
137 var x = new InvalidOperation();
138 x.whatOp = work.op;
139 x.why = 'Cannot divide by 0';
140 throw x;
141 }
142 val = (work.num1 / work.num2).floor();
143 break;
144 }
145
146 var log = new SharedStruct();
147 log.key = logid;
148 log.value = '$val "${work.comment}"';
149 this._log[logid] = log;
150
151 return val;
152 }
153
154 Future zip() async {
155 print('zip()');
156 }
157
158 Future<SharedStruct> getStruct(int key) async {
159 print('getStruct($key)');
160
161 return _log[key];
162 }
163 }