Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 1 | package main; |
| 2 | |
| 3 | /* |
| 4 | * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | * or more contributor license agreements. See the NOTICE file |
| 6 | * distributed with this work for additional information |
| 7 | * regarding copyright ownership. The ASF licenses this file |
| 8 | * to you under the Apache License, Version 2.0 (the |
| 9 | * "License"); you may not use this file except in compliance |
| 10 | * with the License. You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, |
| 15 | * software distributed under the License is distributed on an |
| 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | * KIND, either express or implied. See the License for the |
| 18 | * specific language governing permissions and limitations |
| 19 | * under the License. |
| 20 | */ |
| 21 | |
| 22 | import ( |
| 23 | "fmt" |
| 24 | "os" |
| 25 | "strconv" |
| 26 | "thriftlib/tutorial" |
| 27 | "thriftlib/shared" |
| 28 | ) |
| 29 | |
| 30 | type CalculatorHandler struct { |
| 31 | log map[int]*shared.SharedStruct |
| 32 | } |
| 33 | |
| 34 | func NewCalculatorHandler() *CalculatorHandler { |
| 35 | return &CalculatorHandler{log:make(map[int]*shared.SharedStruct)} |
| 36 | } |
| 37 | |
| 38 | func (p *CalculatorHandler) Ping() (err os.Error) { |
| 39 | fmt.Print("ping()\n") |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | func (p *CalculatorHandler) Add(num1 int32, num2 int32) (retval17 int32, err os.Error) { |
| 44 | fmt.Print("add(", num1, ",", num2, ")\n") |
| 45 | return num1 + num2, nil |
| 46 | } |
| 47 | |
| 48 | func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val int32, ouch *tutorial.InvalidOperation, err os.Error) { |
| 49 | fmt.Print("calculate(", logid, ", {", w.Op, ",", w.Num1, ",", w.Num2, "})\n") |
| 50 | switch(w.Op) { |
| 51 | case tutorial.ADD: |
| 52 | val = w.Num1 + w.Num2 |
| 53 | break |
| 54 | case tutorial.SUBTRACT: |
| 55 | val = w.Num1 - w.Num2 |
| 56 | break |
| 57 | case tutorial.MULTIPLY: |
| 58 | val = w.Num1 * w.Num2 |
| 59 | break |
| 60 | case tutorial.DIVIDE: |
| 61 | if w.Num2 == 0 { |
| 62 | ouch = tutorial.NewInvalidOperation() |
| 63 | ouch.What = int32(w.Op) |
| 64 | ouch.Why = "Cannot divide by 0" |
| 65 | return |
| 66 | } |
| 67 | val = w.Num1 / w.Num2 |
| 68 | break |
| 69 | default: |
| 70 | ouch = tutorial.NewInvalidOperation() |
| 71 | ouch.What = int32(w.Op) |
| 72 | ouch.Why = "Unknown operation" |
| 73 | return |
| 74 | } |
| 75 | entry := shared.NewSharedStruct() |
| 76 | entry.Key = logid |
| 77 | entry.Value = strconv.Itoa(int(val)) |
| 78 | k := int(logid) |
| 79 | /* |
| 80 | oldvalue, exists := p.log[k] |
| 81 | if exists { |
| 82 | fmt.Print("Replacing ", oldvalue, " with ", entry, " for key ", k, "\n") |
| 83 | } else { |
| 84 | fmt.Print("Adding ", entry, " for key ", k, "\n") |
| 85 | } |
| 86 | */ |
| 87 | p.log[k] = entry, true |
| 88 | return val, ouch, err |
| 89 | } |
| 90 | |
| 91 | func (p *CalculatorHandler) GetStruct(key int32) (*shared.SharedStruct, os.Error) { |
| 92 | fmt.Print("getStruct(", key, ")\n") |
| 93 | v, _ := p.log[int(key)] |
| 94 | return v, nil |
| 95 | } |
| 96 | |
| 97 | func (p *CalculatorHandler) Zip() (err os.Error) { |
| 98 | fmt.Print("zip()\n") |
| 99 | return nil |
| 100 | } |
| 101 | |