- Proper mono version
[tberman-thrift.git] / tutorial / rb / RubyClient.rb
blobd2b9150f42c221ee54f248a170ac79aeb65a7009
1 #!/usr/bin/env ruby
3 $:.push('../gen-rb')
4 $:.unshift '../../lib/rb/lib'
6 require 'thrift'
7 require 'thrift/protocol/binaryprotocol'
9 require 'Calculator'
11 begin
12   port = ARGV[0] || 9090
14   transport = Thrift::BufferedTransport.new(Thrift::Socket.new('localhost', port))
15   protocol = Thrift::BinaryProtocol.new(transport)
16   client = Calculator::Client.new(protocol)
18   transport.open()
20   client.ping()
21   print "ping()\n"
23   sum = client.add(1,1)
24   print "1+1=", sum, "\n"
26   sum = client.add(1,4)
27   print "1+4=", sum, "\n"
29   work = Work.new()
31   work.op = Operation::SUBTRACT
32   work.num1 = 15
33   work.num2 = 10
34   diff = client.calculate(1, work)
35   print "15-10=", diff, "\n"
37   log = client.getStruct(1)
38   print "Log: ", log.value, "\n"
40   begin
41     work.op = Operation::DIVIDE
42     work.num1 = 1
43     work.num2 = 0
44     quot = client.calculate(1, work)
45     puts "Whoa, we can divide by 0 now?"
46   rescue InvalidOperation => io
47     print "InvalidOperation: ", io.why, "\n"
48   end
50   client.zip()
51   print "zip\n"
53   transport.close()
55 rescue Thrift::Exception => tx
56   print 'Thrift::Exception: ', tx.message, "\n"
57 end