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 |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 13 | 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 Slee | 8ec70e8 | 2008-01-07 21:50:30 +0000 | [diff] [blame] | 25 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 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 |
| 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 Piro | d795b9d | 2007-06-28 01:09:22 +0000 | [diff] [blame] | 43 | x = InvalidOperation.new() |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 44 | 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 | |
| 67 | end |
| 68 | |
| 69 | handler = CalculatorHandler.new() |
| 70 | processor = Calculator::Processor.new(handler) |
| 71 | transport = TServerSocket.new(9090) |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 72 | transportFactory = TBufferedTransportFactory.new() |
| 73 | server = TSimpleServer.new(processor, transport, transportFactory) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 74 | |
| 75 | puts "Starting the server..." |
| 76 | server.serve() |
| 77 | puts "done." |