blob: 319ca3e6eedbaf36ceacb4b99434b87d3c5e9bd6 [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 Geyer38b1a042014-02-04 23:56:39 +010024 "crypto/tls"
Jens Geyer0e87c462013-06-18 22:25:07 +020025 "fmt"
Jens Geyer0e87c462013-06-18 22:25:07 +020026 "tutorial"
D. Can Celasun4f77ab82017-09-21 15:21:00 +020027
D. Can Celasun9ee29512018-10-17 08:44:48 +020028 "github.com/apache/thrift/lib/go/thrift"
Jens Geyer0e87c462013-06-18 22:25:07 +020029)
30
John Boiles57852792018-01-05 14:37:05 -080031var defaultCtx = context.Background()
32
Jens Geyer0e87c462013-06-18 22:25:07 +020033func handleClient(client *tutorial.CalculatorClient) (err error) {
taozle5c302e02017-07-23 15:21:44 +020034 client.Ping(defaultCtx)
Jens Geyer0e87c462013-06-18 22:25:07 +020035 fmt.Println("ping()")
36
taozle5c302e02017-07-23 15:21:44 +020037 sum, _ := client.Add(defaultCtx, 1, 1)
Jens Geyer0e87c462013-06-18 22:25:07 +020038 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
taozle5c302e02017-07-23 15:21:44 +020044 quotient, err := client.Calculate(defaultCtx, 1, work)
Jens Geyer0e87c462013-06-18 22:25:07 +020045 if err != nil {
Jens Geyer56d41eb2014-02-05 21:10:59 +010046 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 Geyer0e87c462013-06-18 22:25:07 +020052 } else {
53 fmt.Println("Whoa we can divide by 0 with new value:", quotient)
54 }
55
56 work.Op = tutorial.Operation_SUBTRACT
57 work.Num1 = 15
58 work.Num2 = 10
taozle5c302e02017-07-23 15:21:44 +020059 diff, err := client.Calculate(defaultCtx, 1, work)
Jens Geyer0e87c462013-06-18 22:25:07 +020060 if err != nil {
Jens Geyer56d41eb2014-02-05 21:10:59 +010061 switch v := err.(type) {
62 case *tutorial.InvalidOperation:
63 fmt.Println("Invalid operation:", v)
64 default:
65 fmt.Println("Error during operation:", err)
66 }
Jens Geyer0e87c462013-06-18 22:25:07 +020067 return err
Jens Geyer0e87c462013-06-18 22:25:07 +020068 } else {
69 fmt.Print("15-10=", diff, "\n")
70 }
71
taozle5c302e02017-07-23 15:21:44 +020072 log, err := client.GetStruct(defaultCtx, 1)
Jens Geyer0e87c462013-06-18 22:25:07 +020073 if err != nil {
74 fmt.Println("Unable to get struct:", err)
75 return err
76 } else {
77 fmt.Println("Check log:", log.Value)
78 }
79 return err
80}
81
Jens Geyer4c835952013-08-13 21:34:17 +020082func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error {
Jens Geyer0e87c462013-06-18 22:25:07 +020083 var transport thrift.TTransport
Jens Geyer4c835952013-08-13 21:34:17 +020084 var err error
85 if secure {
86 cfg := new(tls.Config)
87 cfg.InsecureSkipVerify = true
88 transport, err = thrift.NewTSSLSocket(addr, cfg)
89 } else {
90 transport, err = thrift.NewTSocket(addr)
91 }
Jens Geyer0e87c462013-06-18 22:25:07 +020092 if err != nil {
93 fmt.Println("Error opening socket:", err)
94 return err
95 }
D. Can Celasun8da0e722017-06-02 14:33:32 +020096 transport, err = transportFactory.GetTransport(transport)
97 if err != nil {
98 return err
99 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200100 defer transport.Close()
101 if err := transport.Open(); err != nil {
102 return err
103 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200104 iprot := protocolFactory.GetProtocol(transport)
105 oprot := protocolFactory.GetProtocol(transport)
106 return handleClient(tutorial.NewCalculatorClient(thrift.NewTStandardClient(iprot, oprot)))
Jens Geyer0e87c462013-06-18 22:25:07 +0200107}