Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame^] | 1 | package main; |
| 2 | |
| 3 | |
| 4 | /* |
| 5 | * Licensed to the Apache Software Foundation (ASF) under one |
| 6 | * or more contributor license agreements. See the NOTICE file |
| 7 | * distributed with this work for additional information |
| 8 | * regarding copyright ownership. The ASF licenses this file |
| 9 | * to you under the Apache License, Version 2.0 (the |
| 10 | * "License"); you may not use this file except in compliance |
| 11 | * with the License. You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, |
| 16 | * software distributed under the License is distributed on an |
| 17 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 18 | * KIND, either express or implied. See the License for the |
| 19 | * specific language governing permissions and limitations |
| 20 | * under the License. |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | import ( |
| 25 | "fmt" |
| 26 | "net" |
| 27 | "os" |
| 28 | "thrift" |
| 29 | "thriftlib/tutorial" |
| 30 | ) |
| 31 | |
| 32 | func Perform(client *tutorial.CalculatorClient) (err os.Error) { |
| 33 | client.Ping() |
| 34 | fmt.Print("ping()\n") |
| 35 | |
| 36 | sum, _ := client.Add(1, 1) |
| 37 | fmt.Print("1+1=", sum, "\n") |
| 38 | |
| 39 | work := tutorial.NewWork() |
| 40 | work.Op = tutorial.DIVIDE |
| 41 | work.Num1 = 1 |
| 42 | work.Num2 = 0 |
| 43 | quotient, ouch, err := client.Calculate(1, work) |
| 44 | if err != nil { |
| 45 | fmt.Print("Error during operation: ", err.String(), "\n") |
| 46 | return err |
| 47 | } else if ouch != nil { |
| 48 | fmt.Print("Invalid operation: ", ouch.String(), "\n") |
| 49 | } else { |
| 50 | fmt.Print("Whoa we can divide by 0 with new value: ", quotient, "\n") |
| 51 | } |
| 52 | |
| 53 | work.Op = tutorial.SUBTRACT |
| 54 | work.Num1 = 15 |
| 55 | work.Num2 = 10 |
| 56 | diff, ouch, err := client.Calculate(1, work) |
| 57 | if err != nil { |
| 58 | fmt.Print("Error during operation: ", err.String(), "\n") |
| 59 | return err |
| 60 | } else if ouch != nil { |
| 61 | fmt.Print("Invalid operation: ", ouch.String(), "\n") |
| 62 | } else { |
| 63 | fmt.Print("15-10=", diff, "\n") |
| 64 | } |
| 65 | |
| 66 | log, err := client.GetStruct(1) |
| 67 | if err != nil { |
| 68 | fmt.Print("Unable to get struct: ", err.String(), "\n") |
| 69 | return err |
| 70 | } else { |
| 71 | fmt.Print("Check log: ", log.Value, "\n") |
| 72 | } |
| 73 | return err |
| 74 | } |
| 75 | |
| 76 | |
| 77 | func RunClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory) os.Error { |
| 78 | addr, err := net.ResolveTCPAddr("localhost:9090") |
| 79 | if err != nil { |
| 80 | fmt.Print("Error resolving address: ", err.String(), "\n") |
| 81 | return err |
| 82 | } |
| 83 | transport := thrift.NewTSocketAddr(addr) |
| 84 | if err = transport.Open(); err != nil { |
| 85 | fmt.Print("Error opening connection for protocol ", addr.Network(), " to ", addr.String(), ": ", err.String(), "\n") |
| 86 | return err |
| 87 | } |
| 88 | useTransport := transportFactory.GetTransport(transport) |
| 89 | client := tutorial.NewCalculatorClientFactory(useTransport, protocolFactory) |
| 90 | Perform(client) |
| 91 | return transport.Close() |
| 92 | } |