4 $:.unshift '../../lib/rb/lib'
7 require 'thrift/protocol/binaryprotocol'
8 require 'thrift/server/tserver'
11 require 'shared_types'
13 class CalculatorHandler
23 print "add(", n1, ",", n2, ")\n"
27 def calculate(logid, work)
28 print "calculate(", logid, ", {", work.op, ",", work.num1, ",", work.num2,"})\n"
29 if work.op == Operation::ADD
30 val = work.num1 + work.num2
31 elsif work.op == Operation::SUBTRACT
32 val = work.num1 - work.num2
33 elsif work.op == Operation::MULTIPLY
34 val = work.num1 * work.num2
35 elsif work.op == Operation::DIVIDE
37 x = InvalidOperation.new()
39 x.why = "Cannot divide by 0"
42 val = work.num1 / work.num2
44 x = InvalidOperation.new()
46 x.why = "Invalid operation"
50 entry = SharedStruct.new()
52 entry.value = "#{val}"
60 print "getStruct(", key, ")\n"
70 handler = CalculatorHandler.new()
71 processor = Calculator::Processor.new(handler)
72 transport = Thrift::ServerSocket.new(9090)
73 transportFactory = Thrift::BufferedTransportFactory.new()
74 server = Thrift::SimpleServer.new(processor, transport, transportFactory)
76 puts "Starting the server..."