blob: 623fde054c835264daf92ac8f9cfc0736d07fd81 [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#!/usr/bin/env ruby
2
David Reissea2cba82009-03-30 21:35:00 +00003#
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
Mark Slee07a3aab2007-03-07 05:45:10 +000022$:.push('../gen-rb')
Kevin Clark2b1e10b2008-06-18 01:00:50 +000023$:.unshift '../../lib/rb/lib'
Mark Slee07a3aab2007-03-07 05:45:10 +000024
Kevin Clark21411532008-06-18 01:03:33 +000025require 'thrift'
26require 'thrift/protocol/binaryprotocol'
Mark Slee07a3aab2007-03-07 05:45:10 +000027
28require 'Calculator'
29
Mark Slee07a3aab2007-03-07 05:45:10 +000030begin
Christopher Piro5f5fdf32007-07-25 22:41:00 +000031 port = ARGV[0] || 9090
David Reiss0c90f6f2008-02-06 22:18:40 +000032
Kevin Clark21411532008-06-18 01:03:33 +000033 transport = Thrift::BufferedTransport.new(Thrift::Socket.new('localhost', port))
34 protocol = Thrift::BinaryProtocol.new(transport)
Mark Slee76791962007-03-14 02:47:35 +000035 client = Calculator::Client.new(protocol)
Christopher Piro5f5fdf32007-07-25 22:41:00 +000036
Mark Slee76791962007-03-14 02:47:35 +000037 transport.open()
Christopher Piro5f5fdf32007-07-25 22:41:00 +000038
Mark Slee76791962007-03-14 02:47:35 +000039 client.ping()
40 print "ping()\n"
Christopher Piro5f5fdf32007-07-25 22:41:00 +000041
Mark Slee76791962007-03-14 02:47:35 +000042 sum = client.add(1,1)
43 print "1+1=", sum, "\n"
Christopher Piro5f5fdf32007-07-25 22:41:00 +000044
45 sum = client.add(1,4)
46 print "1+4=", sum, "\n"
47
Mark Slee76791962007-03-14 02:47:35 +000048 work = Work.new()
Christopher Piro5f5fdf32007-07-25 22:41:00 +000049
50 work.op = Operation::SUBTRACT
51 work.num1 = 15
52 work.num2 = 10
53 diff = client.calculate(1, work)
54 print "15-10=", diff, "\n"
55
56 log = client.getStruct(1)
57 print "Log: ", log.value, "\n"
58
Mark Slee76791962007-03-14 02:47:35 +000059 begin
60 work.op = Operation::DIVIDE
61 work.num1 = 1
62 work.num2 = 0
63 quot = client.calculate(1, work)
64 puts "Whoa, we can divide by 0 now?"
65 rescue InvalidOperation => io
66 print "InvalidOperation: ", io.why, "\n"
67 end
Christopher Piro5f5fdf32007-07-25 22:41:00 +000068
69 client.zip()
70 print "zip\n"
71
Mark Slee76791962007-03-14 02:47:35 +000072 transport.close()
73
Kevin Clark29600442008-06-18 00:54:13 +000074rescue Thrift::Exception => tx
75 print 'Thrift::Exception: ', tx.message, "\n"
Mark Slee07a3aab2007-03-07 05:45:10 +000076end