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