Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [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 ( |
Jens Geyer | 38b1a04 | 2014-02-04 23:56:39 +0100 | [diff] [blame] | 23 | "crypto/tls" |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 24 | "fmt" |
| 25 | "git.apache.org/thrift.git/lib/go/thrift" |
| 26 | "tutorial" |
| 27 | ) |
| 28 | |
| 29 | func handleClient(client *tutorial.CalculatorClient) (err error) { |
| 30 | client.Ping() |
| 31 | fmt.Println("ping()") |
| 32 | |
| 33 | sum, _ := client.Add(1, 1) |
| 34 | fmt.Print("1+1=", sum, "\n") |
| 35 | |
| 36 | work := tutorial.NewWork() |
| 37 | work.Op = tutorial.Operation_DIVIDE |
| 38 | work.Num1 = 1 |
| 39 | work.Num2 = 0 |
Jens Geyer | 38b1a04 | 2014-02-04 23:56:39 +0100 | [diff] [blame] | 40 | quotient, err := client.Calculate(1, work) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 41 | if err != nil { |
Jens Geyer | 56d41eb | 2014-02-05 21:10:59 +0100 | [diff] [blame] | 42 | switch v := err.(type) { |
| 43 | case *tutorial.InvalidOperation: |
| 44 | fmt.Println("Invalid operation:", v) |
| 45 | default: |
| 46 | fmt.Println("Error during operation:", err) |
| 47 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 48 | return err |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 49 | } else { |
| 50 | fmt.Println("Whoa we can divide by 0 with new value:", quotient) |
| 51 | } |
| 52 | |
| 53 | work.Op = tutorial.Operation_SUBTRACT |
| 54 | work.Num1 = 15 |
| 55 | work.Num2 = 10 |
Jens Geyer | 38b1a04 | 2014-02-04 23:56:39 +0100 | [diff] [blame] | 56 | diff, err := client.Calculate(1, work) |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 57 | if err != nil { |
Jens Geyer | 56d41eb | 2014-02-05 21:10:59 +0100 | [diff] [blame] | 58 | switch v := err.(type) { |
| 59 | case *tutorial.InvalidOperation: |
| 60 | fmt.Println("Invalid operation:", v) |
| 61 | default: |
| 62 | fmt.Println("Error during operation:", err) |
| 63 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 64 | return err |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 65 | } else { |
| 66 | fmt.Print("15-10=", diff, "\n") |
| 67 | } |
| 68 | |
| 69 | log, err := client.GetStruct(1) |
| 70 | if err != nil { |
| 71 | fmt.Println("Unable to get struct:", err) |
| 72 | return err |
| 73 | } else { |
| 74 | fmt.Println("Check log:", log.Value) |
| 75 | } |
| 76 | return err |
| 77 | } |
| 78 | |
Jens Geyer | 4c83595 | 2013-08-13 21:34:17 +0200 | [diff] [blame] | 79 | func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error { |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 80 | var transport thrift.TTransport |
Jens Geyer | 4c83595 | 2013-08-13 21:34:17 +0200 | [diff] [blame] | 81 | var err error |
| 82 | if secure { |
| 83 | cfg := new(tls.Config) |
| 84 | cfg.InsecureSkipVerify = true |
| 85 | transport, err = thrift.NewTSSLSocket(addr, cfg) |
| 86 | } else { |
| 87 | transport, err = thrift.NewTSocket(addr) |
| 88 | } |
Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 89 | if err != nil { |
| 90 | fmt.Println("Error opening socket:", err) |
| 91 | return err |
| 92 | } |
| 93 | transport = transportFactory.GetTransport(transport) |
| 94 | defer transport.Close() |
| 95 | if err := transport.Open(); err != nil { |
| 96 | return err |
| 97 | } |
| 98 | return handleClient(tutorial.NewCalculatorClientFactory(transport, protocolFactory)) |
| 99 | } |