blob: a497d7f8b19a3e691786a7d8e634013de220c6bb [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) {
30 client.Ping()
31 fmt.Println("ping()")
32
33 sum, _ := client.Add(1, 1)
34 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
Jens Geyer38b1a042014-02-04 23:56:39 +010040 quotient, err := client.Calculate(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
Jens Geyer38b1a042014-02-04 23:56:39 +010056 diff, err := client.Calculate(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
69 log, err := client.GetStruct(1)
70 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 }
93 transport = transportFactory.GetTransport(transport)
94 defer transport.Close()
95 if err := transport.Open(); err != nil {
96 return err
97 }
98 return handleClient(tutorial.NewCalculatorClientFactory(transport, protocolFactory))
99}