blob: 1d658b9542fd7308439c2ae391d7f5a04c53915c [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
28 "git.apache.org/thrift.git/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 return err
Jens Geyer0e87c462013-06-18 22:25:07 +020053 } 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
taozle5c302e02017-07-23 15:21:44 +020060 diff, err := client.Calculate(defaultCtx, 1, work)
Jens Geyer0e87c462013-06-18 22:25:07 +020061 if err != nil {
Jens Geyer56d41eb2014-02-05 21:10:59 +010062 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 Geyer0e87c462013-06-18 22:25:07 +020068 return err
Jens Geyer0e87c462013-06-18 22:25:07 +020069 } else {
70 fmt.Print("15-10=", diff, "\n")
71 }
72
taozle5c302e02017-07-23 15:21:44 +020073 log, err := client.GetStruct(defaultCtx, 1)
Jens Geyer0e87c462013-06-18 22:25:07 +020074 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 Geyer4c835952013-08-13 21:34:17 +020083func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error {
Jens Geyer0e87c462013-06-18 22:25:07 +020084 var transport thrift.TTransport
Jens Geyer4c835952013-08-13 21:34:17 +020085 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 Geyer0e87c462013-06-18 22:25:07 +020093 if err != nil {
94 fmt.Println("Error opening socket:", err)
95 return err
96 }
D. Can Celasun8da0e722017-06-02 14:33:32 +020097 transport, err = transportFactory.GetTransport(transport)
98 if err != nil {
99 return err
100 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200101 defer transport.Close()
102 if err := transport.Open(); err != nil {
103 return err
104 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200105 iprot := protocolFactory.GetProtocol(transport)
106 oprot := protocolFactory.GetProtocol(transport)
107 return handleClient(tutorial.NewCalculatorClient(thrift.NewTStandardClient(iprot, oprot)))
Jens Geyer0e87c462013-06-18 22:25:07 +0200108}