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 | |
| 34 | class CalculatorHandler implements Calculator { |
| 35 | |
| 36 | private var log = new IntMap<SharedStruct>(); |
| 37 | |
| 38 | public function new() { |
| 39 | } |
| 40 | |
| 41 | public function ping() : Void { |
| 42 | trace("ping()"); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | public function add( num1 : haxe.Int32, num2 : haxe.Int32) : haxe.Int32 { |
| 47 | trace('add( $num1, $num2)'); |
| 48 | return num1 + num2; |
| 49 | } |
| 50 | |
| 51 | public function calculate( logid : haxe.Int32, work : Work) : haxe.Int32 { |
| 52 | trace('calculate( $logid, '+work.op+","+work.num1+","+work.num2+")"); |
| 53 | |
| 54 | var val : haxe.Int32 = 0; |
| 55 | switch (work.op) |
| 56 | { |
| 57 | case Operation.ADD: |
| 58 | val = work.num1 + work.num2; |
| 59 | |
| 60 | case Operation.SUBTRACT: |
| 61 | val = work.num1 - work.num2; |
| 62 | |
| 63 | case Operation.MULTIPLY: |
| 64 | val = work.num1 * work.num2; |
| 65 | |
| 66 | case Operation.DIVIDE: |
| 67 | if (work.num2 == 0) |
| 68 | { |
| 69 | var io = new InvalidOperation(); |
| 70 | io.what = work.op; |
| 71 | io.why = "Cannot divide by 0"; |
| 72 | throw io; |
| 73 | } |
| 74 | val = Std.int( work.num1 / work.num2); |
| 75 | |
| 76 | default: |
| 77 | var io = new InvalidOperation(); |
| 78 | io.what = work.op; |
| 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 | } |
| 100 | |
| 101 | } |