blob: 60d019f40300b08178171418908a3df06dbaf683 [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
13 include Calculator::Iface
14
15 def initialize()
16 @log = {}
17 end
18
19 def ping()
20 puts "ping()"
21 end
22
23 def add(n1, n2)
24 print "add(", n1, ",", n2, ")\n"
25 return n1 + n2
26 end
27
28 def calculate(logid, work)
29 print "calculate(", logid, ", {", work.op, ",", work.num1, ",", work.num2,"})\n"
30 if work.op == Operation::ADD
31 val = work.num1 + work.num2
32 elsif work.op == Operation::SUBTRACT
33 val = work.num1 - work.num2
34 elsif work.op == Operation::MULTIPLY
35 val = work.num1 * work.num2
36 elsif work.op == Operation::DIVIDE
37 if work.num2 == 0
38 x = InvalidOperation.new()
39 x.what = work.op
40 x.why = "Cannot divide by 0"
41 raise x
42 end
43 val = work.num1 / work.num2
44 else
Christopher Pirod795b9d2007-06-28 01:09:22 +000045 x = InvalidOperation.new()
Mark Slee07a3aab2007-03-07 05:45:10 +000046 x.what = work.op
47 x.why = "Invalid operation"
48 raise x
49 end
50
51 entry = SharedStruct.new()
52 entry.key = logid
53 entry.value = "#{val}"
54 @log[logid] = entry
55
56 return val
57
58 end
59
60 def getStruct(key)
61 print "getStruct(", key, ")\n"
62 return @log[key]
63 end
64
65 def zip()
66 print "zip\n"
67 end
68
69end
70
71handler = CalculatorHandler.new()
72processor = Calculator::Processor.new(handler)
73transport = TServerSocket.new(9090)
Mark Slee76791962007-03-14 02:47:35 +000074transportFactory = TBufferedTransportFactory.new()
75server = TSimpleServer.new(processor, transport, transportFactory)
Mark Slee07a3aab2007-03-07 05:45:10 +000076
77puts "Starting the server..."
78server.serve()
79puts "done."