blob: 543d7fb4e01aa7d2bbce8a36c07488f07caab24d [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 (
23 "fmt"
24 "git.apache.org/thrift.git/lib/go/thrift"
25 "tutorial"
Jens Geyer4c835952013-08-13 21:34:17 +020026 "crypto/tls"
Jens Geyer0e87c462013-06-18 22:25:07 +020027)
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
40 quotient, ouch, err := client.Calculate(1, work)
41 if err != nil {
42 fmt.Println("Error during operation:", err)
43 return err
44 } else if ouch != nil {
45 fmt.Println("Invalid operation:", ouch)
46 } else {
47 fmt.Println("Whoa we can divide by 0 with new value:", quotient)
48 }
49
50 work.Op = tutorial.Operation_SUBTRACT
51 work.Num1 = 15
52 work.Num2 = 10
53 diff, ouch, err := client.Calculate(1, work)
54 if err != nil {
55 fmt.Println("Error during operation:", err)
56 return err
57 } else if ouch != nil {
58 fmt.Println("Invalid operation:", ouch)
59 } else {
60 fmt.Print("15-10=", diff, "\n")
61 }
62
63 log, err := client.GetStruct(1)
64 if err != nil {
65 fmt.Println("Unable to get struct:", err)
66 return err
67 } else {
68 fmt.Println("Check log:", log.Value)
69 }
70 return err
71}
72
Jens Geyer4c835952013-08-13 21:34:17 +020073func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error {
Jens Geyer0e87c462013-06-18 22:25:07 +020074 var transport thrift.TTransport
Jens Geyer4c835952013-08-13 21:34:17 +020075 var err error
76 if secure {
77 cfg := new(tls.Config)
78 cfg.InsecureSkipVerify = true
79 transport, err = thrift.NewTSSLSocket(addr, cfg)
80 } else {
81 transport, err = thrift.NewTSocket(addr)
82 }
Jens Geyer0e87c462013-06-18 22:25:07 +020083 if err != nil {
84 fmt.Println("Error opening socket:", err)
85 return err
86 }
87 transport = transportFactory.GetTransport(transport)
88 defer transport.Close()
89 if err := transport.Open(); err != nil {
90 return err
91 }
92 return handleClient(tutorial.NewCalculatorClientFactory(transport, protocolFactory))
93}