blob: 25616bf4ecc192dfcd9ae939540c3d494e391f13 [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 (
Jens Geyer38b1a042014-02-04 23:56:39 +010023 "crypto/tls"
Jens Geyer0e87c462013-06-18 22:25:07 +020024 "fmt"
Jens Geyer0e87c462013-06-18 22:25:07 +020025 "tutorial"
D. Can Celasun4f77ab82017-09-21 15:21:00 +020026
27 "git.apache.org/thrift.git/lib/go/thrift"
Jens Geyer0e87c462013-06-18 22:25:07 +020028)
29
30func handleClient(client *tutorial.CalculatorClient) (err error) {
taozle5c302e02017-07-23 15:21:44 +020031 client.Ping(defaultCtx)
Jens Geyer0e87c462013-06-18 22:25:07 +020032 fmt.Println("ping()")
33
taozle5c302e02017-07-23 15:21:44 +020034 sum, _ := client.Add(defaultCtx, 1, 1)
Jens Geyer0e87c462013-06-18 22:25:07 +020035 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
taozle5c302e02017-07-23 15:21:44 +020041 quotient, err := client.Calculate(defaultCtx, 1, work)
Jens Geyer0e87c462013-06-18 22:25:07 +020042 if err != nil {
Jens Geyer56d41eb2014-02-05 21:10:59 +010043 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 Geyer0e87c462013-06-18 22:25:07 +020049 return err
Jens Geyer0e87c462013-06-18 22:25:07 +020050 } 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
taozle5c302e02017-07-23 15:21:44 +020057 diff, err := client.Calculate(defaultCtx, 1, work)
Jens Geyer0e87c462013-06-18 22:25:07 +020058 if err != nil {
Jens Geyer56d41eb2014-02-05 21:10:59 +010059 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 Geyer0e87c462013-06-18 22:25:07 +020065 return err
Jens Geyer0e87c462013-06-18 22:25:07 +020066 } else {
67 fmt.Print("15-10=", diff, "\n")
68 }
69
taozle5c302e02017-07-23 15:21:44 +020070 log, err := client.GetStruct(defaultCtx, 1)
Jens Geyer0e87c462013-06-18 22:25:07 +020071 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 Geyer4c835952013-08-13 21:34:17 +020080func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error {
Jens Geyer0e87c462013-06-18 22:25:07 +020081 var transport thrift.TTransport
Jens Geyer4c835952013-08-13 21:34:17 +020082 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 Geyer0e87c462013-06-18 22:25:07 +020090 if err != nil {
91 fmt.Println("Error opening socket:", err)
92 return err
93 }
D. Can Celasun8da0e722017-06-02 14:33:32 +020094 transport, err = transportFactory.GetTransport(transport)
95 if err != nil {
96 return err
97 }
Jens Geyer0e87c462013-06-18 22:25:07 +020098 defer transport.Close()
99 if err := transport.Open(); err != nil {
100 return err
101 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200102 iprot := protocolFactory.GetProtocol(transport)
103 oprot := protocolFactory.GetProtocol(transport)
104 return handleClient(tutorial.NewCalculatorClient(thrift.NewTStandardClient(iprot, oprot)))
Jens Geyer0e87c462013-06-18 22:25:07 +0200105}