Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
| 3 | $:.push('../gen-rb') |
| 4 | |
| 5 | require 'thrift/transport/tsocket' |
| 6 | require 'thrift/protocol/tbinaryprotocol' |
| 7 | require 'thrift/server/tserver' |
| 8 | |
| 9 | require 'Calculator' |
| 10 | require 'shared_types' |
| 11 | |
| 12 | class 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 Piro | d795b9d | 2007-06-28 01:09:22 +0000 | [diff] [blame] | 45 | x = InvalidOperation.new() |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 46 | 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 | |
| 69 | end |
| 70 | |
| 71 | handler = CalculatorHandler.new() |
| 72 | processor = Calculator::Processor.new(handler) |
| 73 | transport = TServerSocket.new(9090) |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 74 | transportFactory = TBufferedTransportFactory.new() |
| 75 | server = TSimpleServer.new(processor, transport, transportFactory) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 76 | |
| 77 | puts "Starting the server..." |
| 78 | server.serve() |
| 79 | puts "done." |