mirror of
https://github.com/valitydev/thrift.git
synced 2024-11-07 10:48:51 +00:00
8ec70e88e6
Reviewed By: mcslee Test Plan: The Ruby tutorial is runnable with this fix Other Notes: Submitted by Eric Wong git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665413 13f79535-47bb-0310-9956-ffa450edef68
78 lines
1.6 KiB
Ruby
Executable File
78 lines
1.6 KiB
Ruby
Executable File
#!/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
|
|
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.new()
|
|
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)
|
|
transportFactory = TBufferedTransportFactory.new()
|
|
server = TSimpleServer.new(processor, transport, transportFactory)
|
|
|
|
puts "Starting the server..."
|
|
server.serve()
|
|
puts "done."
|