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