blob: 6ecb563434ef0a0b1baba00eeb70b20796b2442d [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#!/usr/bin/env ruby
2
3$:.push('../gen-rb')
4
5require 'thrift/transport/tsocket'
6require 'thrift/protocol/tbinaryprotocol'
7require 'thrift/server/tserver'
8
9require 'Calculator'
10require 'shared_types'
11
12class CalculatorHandler
Mark Slee07a3aab2007-03-07 05:45:10 +000013 def initialize()
14 @log = {}
15 end
16
17 def ping()
18 puts "ping()"
19 end
20
21 def add(n1, n2)
22 print "add(", n1, ",", n2, ")\n"
23 return n1 + n2
24 end
Mark Slee8ec70e82008-01-07 21:50:30 +000025
Mark Slee07a3aab2007-03-07 05:45:10 +000026 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
35 if work.num2 == 0
36 x = InvalidOperation.new()
37 x.what = work.op
38 x.why = "Cannot divide by 0"
39 raise x
40 end
41 val = work.num1 / work.num2
42 else
Christopher Pirod795b9d2007-06-28 01:09:22 +000043 x = InvalidOperation.new()
Mark Slee07a3aab2007-03-07 05:45:10 +000044 x.what = work.op
45 x.why = "Invalid operation"
46 raise x
47 end
48
49 entry = SharedStruct.new()
50 entry.key = logid
51 entry.value = "#{val}"
52 @log[logid] = entry
53
54 return val
55
56 end
57
58 def getStruct(key)
59 print "getStruct(", key, ")\n"
60 return @log[key]
61 end
62
63 def zip()
64 print "zip\n"
65 end
66
67end
68
69handler = CalculatorHandler.new()
70processor = Calculator::Processor.new(handler)
71transport = TServerSocket.new(9090)
Mark Slee76791962007-03-14 02:47:35 +000072transportFactory = TBufferedTransportFactory.new()
73server = TSimpleServer.new(processor, transport, transportFactory)
Mark Slee07a3aab2007-03-07 05:45:10 +000074
75puts "Starting the server..."
76server.serve()
77puts "done."