blob: 65027ea0510d9663edcf3bf39b25baca8abbc6ea [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"
25 "git.apache.org/thrift.git/lib/go/thrift"
26 "tutorial"
27)
28
29func handleClient(client *tutorial.CalculatorClient) (err error) {
taozle5c302e02017-07-23 15:21:44 +020030 client.Ping(defaultCtx)
Jens Geyer0e87c462013-06-18 22:25:07 +020031 fmt.Println("ping()")
32
taozle5c302e02017-07-23 15:21:44 +020033 sum, _ := client.Add(defaultCtx, 1, 1)
Jens Geyer0e87c462013-06-18 22:25:07 +020034 fmt.Print("1+1=", sum, "\n")
35
36 work := tutorial.NewWork()
37 work.Op = tutorial.Operation_DIVIDE
38 work.Num1 = 1
39 work.Num2 = 0
taozle5c302e02017-07-23 15:21:44 +020040 quotient, err := client.Calculate(defaultCtx, 1, work)
Jens Geyer0e87c462013-06-18 22:25:07 +020041 if err != nil {
Jens Geyer56d41eb2014-02-05 21:10:59 +010042 switch v := err.(type) {
43 case *tutorial.InvalidOperation:
44 fmt.Println("Invalid operation:", v)
45 default:
46 fmt.Println("Error during operation:", err)
47 }
Jens Geyer0e87c462013-06-18 22:25:07 +020048 return err
Jens Geyer0e87c462013-06-18 22:25:07 +020049 } else {
50 fmt.Println("Whoa we can divide by 0 with new value:", quotient)
51 }
52
53 work.Op = tutorial.Operation_SUBTRACT
54 work.Num1 = 15
55 work.Num2 = 10
taozle5c302e02017-07-23 15:21:44 +020056 diff, err := client.Calculate(defaultCtx, 1, work)
Jens Geyer0e87c462013-06-18 22:25:07 +020057 if err != nil {
Jens Geyer56d41eb2014-02-05 21:10:59 +010058 switch v := err.(type) {
59 case *tutorial.InvalidOperation:
60 fmt.Println("Invalid operation:", v)
61 default:
62 fmt.Println("Error during operation:", err)
63 }
Jens Geyer0e87c462013-06-18 22:25:07 +020064 return err
Jens Geyer0e87c462013-06-18 22:25:07 +020065 } else {
66 fmt.Print("15-10=", diff, "\n")
67 }
68
taozle5c302e02017-07-23 15:21:44 +020069 log, err := client.GetStruct(defaultCtx, 1)
Jens Geyer0e87c462013-06-18 22:25:07 +020070 if err != nil {
71 fmt.Println("Unable to get struct:", err)
72 return err
73 } else {
74 fmt.Println("Check log:", log.Value)
75 }
76 return err
77}
78
Jens Geyer4c835952013-08-13 21:34:17 +020079func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error {
Jens Geyer0e87c462013-06-18 22:25:07 +020080 var transport thrift.TTransport
Jens Geyer4c835952013-08-13 21:34:17 +020081 var err error
82 if secure {
83 cfg := new(tls.Config)
84 cfg.InsecureSkipVerify = true
85 transport, err = thrift.NewTSSLSocket(addr, cfg)
86 } else {
87 transport, err = thrift.NewTSocket(addr)
88 }
Jens Geyer0e87c462013-06-18 22:25:07 +020089 if err != nil {
90 fmt.Println("Error opening socket:", err)
91 return err
92 }
D. Can Celasun8da0e722017-06-02 14:33:32 +020093 transport, err = transportFactory.GetTransport(transport)
94 if err != nil {
95 return err
96 }
Jens Geyer0e87c462013-06-18 22:25:07 +020097 defer transport.Close()
98 if err := transport.Open(); err != nil {
99 return err
100 }
101 return handleClient(tutorial.NewCalculatorClientFactory(transport, protocolFactory))
102}