blob: f944cf8a66a4e03802cf06cb027fb7c31adc6b4d [file] [log] [blame]
Jens Geyer0e87c462013-06-18 22:25:07 +02001package 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
22import (
John Boiles57852792018-01-05 14:37:05 -080023 "context"
Jens Geyer0e87c462013-06-18 22:25:07 +020024 "fmt"
D. Can Celasun4f77ab82017-09-21 15:21:00 +020025
D. Can Celasun9ee29512018-10-17 08:44:48 +020026 "github.com/apache/thrift/lib/go/thrift"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070027 "github.com/apache/thrift/tutorial/go/gen-go/tutorial"
Jens Geyer0e87c462013-06-18 22:25:07 +020028)
29
John Boiles57852792018-01-05 14:37:05 -080030var defaultCtx = context.Background()
31
Jens Geyer0e87c462013-06-18 22:25:07 +020032func handleClient(client *tutorial.CalculatorClient) (err error) {
taozle5c302e02017-07-23 15:21:44 +020033 client.Ping(defaultCtx)
Jens Geyer0e87c462013-06-18 22:25:07 +020034 fmt.Println("ping()")
35
taozle5c302e02017-07-23 15:21:44 +020036 sum, _ := client.Add(defaultCtx, 1, 1)
Jens Geyer0e87c462013-06-18 22:25:07 +020037 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
taozle5c302e02017-07-23 15:21:44 +020043 quotient, err := client.Calculate(defaultCtx, 1, work)
Jens Geyer0e87c462013-06-18 22:25:07 +020044 if err != nil {
Jens Geyer56d41eb2014-02-05 21:10:59 +010045 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 Geyer0e87c462013-06-18 22:25:07 +020051 } 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
taozle5c302e02017-07-23 15:21:44 +020058 diff, err := client.Calculate(defaultCtx, 1, work)
Jens Geyer0e87c462013-06-18 22:25:07 +020059 if err != nil {
Jens Geyer56d41eb2014-02-05 21:10:59 +010060 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 Geyer0e87c462013-06-18 22:25:07 +020066 return err
Jens Geyer0e87c462013-06-18 22:25:07 +020067 } else {
68 fmt.Print("15-10=", diff, "\n")
69 }
70
taozle5c302e02017-07-23 15:21:44 +020071 log, err := client.GetStruct(defaultCtx, 1)
Jens Geyer0e87c462013-06-18 22:25:07 +020072 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' Wang17373a32021-08-26 11:04:27 -070081func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool, cfg *thrift.TConfiguration) error {
Jens Geyer0e87c462013-06-18 22:25:07 +020082 var transport thrift.TTransport
Jens Geyer4c835952013-08-13 21:34:17 +020083 if secure {
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070084 transport = thrift.NewTSSLSocketConf(addr, cfg)
Jens Geyer4c835952013-08-13 21:34:17 +020085 } else {
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070086 transport = thrift.NewTSocketConf(addr, cfg)
Jens Geyer4c835952013-08-13 21:34:17 +020087 }
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070088 transport, err := transportFactory.GetTransport(transport)
D. Can Celasun8da0e722017-06-02 14:33:32 +020089 if err != nil {
90 return err
91 }
Jens Geyer0e87c462013-06-18 22:25:07 +020092 defer transport.Close()
93 if err := transport.Open(); err != nil {
94 return err
95 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +020096 iprot := protocolFactory.GetProtocol(transport)
97 oprot := protocolFactory.GetProtocol(transport)
98 return handleClient(tutorial.NewCalculatorClient(thrift.NewTStandardClient(iprot, oprot)))
Jens Geyer0e87c462013-06-18 22:25:07 +020099}