1#!/usr/bin/env ruby
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$:.push('gen-rb')
23$:.unshift '../../lib/rb/lib'
24
25require 'thrift'
26
27require 'calculator'
28
29begin
30  port = ARGV[0] || 9090
31
32  transport = Thrift::BufferedTransport.new(Thrift::Socket.new('localhost', port))
33  protocol = Thrift::BinaryProtocol.new(transport)
34  client = Calculator::Client.new(protocol)
35
36  transport.open()
37
38  client.ping()
39  print "ping()\n"
40
41  sum = client.add(1,1)
42  print "1+1=", sum, "\n"
43
44  sum = client.add(1,4)
45  print "1+4=", sum, "\n"
46
47  work = Work.new()
48
49  work.op = Operation::SUBTRACT
50  work.num1 = 15
51  work.num2 = 10
52  diff = client.calculate(1, work)
53  print "15-10=", diff, "\n"
54
55  log = client.getStruct(1)
56  print "Log: ", log.value, "\n"
57
58  begin
59    work.op = Operation::DIVIDE
60    work.num1 = 1
61    work.num2 = 0
62    quot = client.calculate(1, work)
63    puts "Whoa, we can divide by 0 now?"
64  rescue InvalidOperation => io
65    print "InvalidOperation: ", io.why, "\n"
66  end
67
68  client.zip()
69  print "zip\n"
70
71  transport.close()
72
73rescue Thrift::Exception => tx
74  print 'Thrift::Exception: ', tx.message, "\n"
75end
76