blob: 684d05a4a5ae54090c8011eeda31a7b2e6be2485 [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#!/usr/bin/env ruby
2
3$:.push('../gen-rb')
Kevin Clark2b1e10b2008-06-18 01:00:50 +00004$:.unshift '../../lib/rb/lib'
Mark Slee07a3aab2007-03-07 05:45:10 +00005
Kevin Clark21411532008-06-18 01:03:33 +00006require 'thrift'
7require 'thrift/protocol/binaryprotocol'
Mark Slee07a3aab2007-03-07 05:45:10 +00008require 'thrift/server/tserver'
9
10require 'Calculator'
11require 'shared_types'
12
13class CalculatorHandler
Mark Slee07a3aab2007-03-07 05:45:10 +000014 def initialize()
15 @log = {}
16 end
17
18 def ping()
19 puts "ping()"
20 end
21
22 def add(n1, n2)
23 print "add(", n1, ",", n2, ")\n"
24 return n1 + n2
25 end
Mark Slee8ec70e82008-01-07 21:50:30 +000026
Mark Slee07a3aab2007-03-07 05:45:10 +000027 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
36 if work.num2 == 0
37 x = InvalidOperation.new()
38 x.what = work.op
39 x.why = "Cannot divide by 0"
40 raise x
41 end
42 val = work.num1 / work.num2
43 else
Christopher Pirod795b9d2007-06-28 01:09:22 +000044 x = InvalidOperation.new()
Mark Slee07a3aab2007-03-07 05:45:10 +000045 x.what = work.op
46 x.why = "Invalid operation"
47 raise x
48 end
49
50 entry = SharedStruct.new()
51 entry.key = logid
52 entry.value = "#{val}"
53 @log[logid] = entry
54
55 return val
56
57 end
58
59 def getStruct(key)
60 print "getStruct(", key, ")\n"
61 return @log[key]
62 end
63
64 def zip()
65 print "zip\n"
66 end
67
68end
69
70handler = CalculatorHandler.new()
71processor = Calculator::Processor.new(handler)
Kevin Clark21411532008-06-18 01:03:33 +000072transport = Thrift::ServerSocket.new(9090)
73transportFactory = Thrift::BufferedTransportFactory.new()
74server = Thrift::SimpleServer.new(processor, transport, transportFactory)
Mark Slee07a3aab2007-03-07 05:45:10 +000075
76puts "Starting the server..."
77server.serve()
78puts "done."