Thrift Tutorial
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665051 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/tutorial/rb/RubyClient.rb b/tutorial/rb/RubyClient.rb
new file mode 100755
index 0000000..e0b47b6
--- /dev/null
+++ b/tutorial/rb/RubyClient.rb
@@ -0,0 +1,43 @@
+#!/usr/bin/env ruby
+
+$:.push('../gen-rb')
+
+require 'thrift/transport/tsocket'
+require 'thrift/protocol/tbinaryprotocol'
+
+require 'Calculator'
+
+transport = TSocket.new('localhost', 9090)
+protocol = TBinaryProtocol.new(transport)
+client = Calculator::Client.new(protocol)
+
+transport.open()
+
+client.ping()
+print "ping()\n"
+
+sum = client.add(1,1)
+print "1+1=", sum, "\n"
+
+work = Work.new()
+
+begin
+ work.op = Operation::DIVIDE
+ work.num1 = 1
+ work.num2 = 0
+ quot = client.calculate(1, work)
+ puts "Whoa, we can divide by 0 now?"
+rescue InvalidOperation => io
+ print "InvalidOperation: ", io.why, "\n"
+end
+
+work.op = Operation::SUBTRACT
+work.num1 = 15
+work.num2 = 10
+diff = client.calculate(1, work)
+print "15-10=", diff, "\n"
+
+log = client.getStruct(1)
+print "Log: ", log.value, "\n"
+
+transport.close()
diff --git a/tutorial/rb/RubyServer.rb b/tutorial/rb/RubyServer.rb
new file mode 100755
index 0000000..2e9145d
--- /dev/null
+++ b/tutorial/rb/RubyServer.rb
@@ -0,0 +1,78 @@
+#!/usr/bin/env ruby
+
+$:.push('../gen-rb')
+
+require 'thrift/transport/tsocket'
+require 'thrift/protocol/tbinaryprotocol'
+require 'thrift/server/tserver'
+
+require 'Calculator'
+require 'shared_types'
+
+class CalculatorHandler
+ include Calculator::Iface
+
+ def initialize()
+ @log = {}
+ end
+
+ def ping()
+ puts "ping()"
+ end
+
+ def add(n1, n2)
+ print "add(", n1, ",", n2, ")\n"
+ return n1 + n2
+ end
+
+ def calculate(logid, work)
+ print "calculate(", logid, ", {", work.op, ",", work.num1, ",", work.num2,"})\n"
+ if work.op == Operation::ADD
+ val = work.num1 + work.num2
+ elsif work.op == Operation::SUBTRACT
+ val = work.num1 - work.num2
+ elsif work.op == Operation::MULTIPLY
+ val = work.num1 * work.num2
+ elsif work.op == Operation::DIVIDE
+ if work.num2 == 0
+ x = InvalidOperation.new()
+ x.what = work.op
+ x.why = "Cannot divide by 0"
+ raise x
+ end
+ val = work.num1 / work.num2
+ else
+ x = InvalidOperation()
+ x.what = work.op
+ x.why = "Invalid operation"
+ raise x
+ end
+
+ entry = SharedStruct.new()
+ entry.key = logid
+ entry.value = "#{val}"
+ @log[logid] = entry
+
+ return val
+
+ end
+
+ def getStruct(key)
+ print "getStruct(", key, ")\n"
+ return @log[key]
+ end
+
+ def zip()
+ print "zip\n"
+ end
+
+end
+
+handler = CalculatorHandler.new()
+processor = Calculator::Processor.new(handler)
+transport = TServerSocket.new(9090)
+server = TSimpleServer.new(processor, transport)
+
+puts "Starting the server..."
+server.serve()
+puts "done."