5 require 'thrift/transport/tsocket'
6 require 'thrift/protocol/tbinaryprotocol'
7 require 'thrift/server/tserver'
10 require 'shared_types'
12 class CalculatorHandler
22 print "add(", n1, ",", n2, ")\n"
26 def calculate(logid, work)
27 print "calculate(", logid, ", {", work.op, ",", work.num1, ",", work.num2,"})\n"
28 if work.op == Operation::ADD
29 val = work.num1 + work.num2
30 elsif work.op == Operation::SUBTRACT
31 val = work.num1 - work.num2
32 elsif work.op == Operation::MULTIPLY
33 val = work.num1 * work.num2
34 elsif work.op == Operation::DIVIDE
36 x = InvalidOperation.new()
38 x.why = "Cannot divide by 0"
41 val = work.num1 / work.num2
43 x = InvalidOperation.new()
45 x.why = "Invalid operation"
49 entry = SharedStruct.new()
51 entry.value = "#{val}"
59 print "getStruct(", key, ")\n"
69 handler = CalculatorHandler.new()
70 processor = Calculator::Processor.new(handler)
71 transport = TServerSocket.new(9090)
72 transportFactory = TBufferedTransportFactory.new()
73 server = TSimpleServer.new(processor, transport, transportFactory)
75 puts "Starting the server..."