blob: 114de19f98ab65a808ea73a58ed46a022005872a [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"
26)
27
28func handleClient(client *tutorial.CalculatorClient) (err error) {
29 client.Ping()
30 fmt.Println("ping()")
31
32 sum, _ := client.Add(1, 1)
33 fmt.Print("1+1=", sum, "\n")
34
35 work := tutorial.NewWork()
36 work.Op = tutorial.Operation_DIVIDE
37 work.Num1 = 1
38 work.Num2 = 0
39 quotient, ouch, err := client.Calculate(1, work)
40 if err != nil {
41 fmt.Println("Error during operation:", err)
42 return err
43 } else if ouch != nil {
44 fmt.Println("Invalid operation:", ouch)
45 } else {
46 fmt.Println("Whoa we can divide by 0 with new value:", quotient)
47 }
48
49 work.Op = tutorial.Operation_SUBTRACT
50 work.Num1 = 15
51 work.Num2 = 10
52 diff, ouch, err := client.Calculate(1, work)
53 if err != nil {
54 fmt.Println("Error during operation:", err)
55 return err
56 } else if ouch != nil {
57 fmt.Println("Invalid operation:", ouch)
58 } else {
59 fmt.Print("15-10=", diff, "\n")
60 }
61
62 log, err := client.GetStruct(1)
63 if err != nil {
64 fmt.Println("Unable to get struct:", err)
65 return err
66 } else {
67 fmt.Println("Check log:", log.Value)
68 }
69 return err
70}
71
72func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory) error {
73 var transport thrift.TTransport
74 transport, err := thrift.NewTSocket("localhost:9090")
75 if err != nil {
76 fmt.Println("Error opening socket:", err)
77 return err
78 }
79 transport = transportFactory.GetTransport(transport)
80 defer transport.Close()
81 if err := transport.Open(); err != nil {
82 return err
83 }
84 return handleClient(tutorial.NewCalculatorClientFactory(transport, protocolFactory))
85}