]>
Commit | Line | Data |
---|---|---|
1 | package 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 | ||
22 | import ( | |
23 | "context" | |
24 | "crypto/tls" | |
25 | "fmt" | |
26 | "tutorial" | |
27 | ||
28 | "github.com/apache/thrift/lib/go/thrift" | |
29 | ) | |
30 | ||
31 | var defaultCtx = context.Background() | |
32 | ||
33 | func handleClient(client *tutorial.CalculatorClient) (err error) { | |
34 | client.Ping(defaultCtx) | |
35 | fmt.Println("ping()") | |
36 | ||
37 | sum, _ := client.Add(defaultCtx, 1, 1) | |
38 | 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 | |
44 | quotient, err := client.Calculate(defaultCtx, 1, work) | |
45 | if err != nil { | |
46 | switch v := err.(type) { | |
47 | case *tutorial.InvalidOperation: | |
48 | fmt.Println("Invalid operation:", v) | |
49 | default: | |
50 | fmt.Println("Error during operation:", err) | |
51 | } | |
52 | return err | |
53 | } 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 | |
60 | diff, err := client.Calculate(defaultCtx, 1, work) | |
61 | if err != nil { | |
62 | switch v := err.(type) { | |
63 | case *tutorial.InvalidOperation: | |
64 | fmt.Println("Invalid operation:", v) | |
65 | default: | |
66 | fmt.Println("Error during operation:", err) | |
67 | } | |
68 | return err | |
69 | } else { | |
70 | fmt.Print("15-10=", diff, "\n") | |
71 | } | |
72 | ||
73 | log, err := client.GetStruct(defaultCtx, 1) | |
74 | 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 | ||
83 | func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error { | |
84 | var transport thrift.TTransport | |
85 | 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 | } | |
93 | if err != nil { | |
94 | fmt.Println("Error opening socket:", err) | |
95 | return err | |
96 | } | |
97 | transport, err = transportFactory.GetTransport(transport) | |
98 | if err != nil { | |
99 | return err | |
100 | } | |
101 | defer transport.Close() | |
102 | if err := transport.Open(); err != nil { | |
103 | return err | |
104 | } | |
105 | iprot := protocolFactory.GetProtocol(transport) | |
106 | oprot := protocolFactory.GetProtocol(transport) | |
107 | return handleClient(tutorial.NewCalculatorClient(thrift.NewTStandardClient(iprot, oprot))) | |
108 | } |