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