Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package; |
| 21 | |
| 22 | import haxe.ds.IntMap; |
| 23 | |
| 24 | import org.apache.thrift.*; |
| 25 | import org.apache.thrift.protocol.*; |
| 26 | import org.apache.thrift.transport.*; |
| 27 | import org.apache.thrift.server.*; |
| 28 | import org.apache.thrift.meta_data.*; |
| 29 | |
| 30 | import tutorial.*; |
| 31 | import shared.*; |
| 32 | |
| 33 | |
Jens Geyer | 2dcefad | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 34 | class CalculatorHandler implements Calculator_service { |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 35 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 36 | private var log = new IntMap<SharedStruct>(); |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 37 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 38 | public function new() { |
| 39 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 40 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 41 | public function ping() : Void { |
| 42 | trace("ping()"); |
| 43 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 44 | |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 45 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 46 | public function add( num1 : haxe.Int32, num2 : haxe.Int32) : haxe.Int32 { |
| 47 | trace('add( $num1, $num2)'); |
| 48 | return num1 + num2; |
| 49 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 50 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 51 | public function calculate( logid : haxe.Int32, work : Work) : haxe.Int32 { |
| 52 | trace('calculate( $logid, '+work.op+","+work.num1+","+work.num2+")"); |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 53 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 54 | var val : haxe.Int32 = 0; |
| 55 | switch (work.op) |
| 56 | { |
| 57 | case Operation.ADD: |
| 58 | val = work.num1 + work.num2; |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 59 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 60 | case Operation.SUBTRACT: |
| 61 | val = work.num1 - work.num2; |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 62 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 63 | case Operation.MULTIPLY: |
| 64 | val = work.num1 * work.num2; |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 65 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 66 | case Operation.DIVIDE: |
| 67 | if (work.num2 == 0) |
| 68 | { |
| 69 | var io = new InvalidOperation(); |
Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 70 | io.whatOp = work.op; |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 71 | io.why = "Cannot divide by 0"; |
| 72 | throw io; |
| 73 | } |
| 74 | val = Std.int( work.num1 / work.num2); |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 75 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 76 | default: |
| 77 | var io = new InvalidOperation(); |
Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 78 | io.whatOp = work.op; |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 79 | io.why = "Unknown operation"; |
| 80 | throw io; |
| 81 | } |
| 82 | |
| 83 | var entry = new SharedStruct(); |
| 84 | entry.key = logid; |
| 85 | entry.value = '$val'; |
| 86 | log.set(logid, entry); |
| 87 | |
| 88 | return val; |
| 89 | } |
| 90 | |
| 91 | public function getStruct( key : haxe.Int32) : SharedStruct { |
| 92 | trace('getStruct($key)'); |
| 93 | return log.get(key); |
| 94 | } |
| 95 | |
| 96 | // oneway method, no args |
| 97 | public function zip() : Void { |
| 98 | trace("zip()"); |
| 99 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 100 | |
| 101 | } |