Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 4 | # Licensed to the Apache Software Foundation (ASF) under one |
| 5 | # or more contributor license agreements. See the NOTICE file |
| 6 | # distributed with this work for additional information |
| 7 | # regarding copyright ownership. The ASF licenses this file |
| 8 | # to you under the Apache License, Version 2.0 (the |
| 9 | # "License"); you may not use this file except in compliance |
| 10 | # with the License. You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, |
| 15 | # software distributed under the License is distributed on an |
| 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | # KIND, either express or implied. See the License for the |
| 18 | # specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 22 | $:.push('../gen-rb') |
Kevin Clark | 2b1e10b | 2008-06-18 01:00:50 +0000 | [diff] [blame] | 23 | $:.unshift '../../lib/rb/lib' |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 24 | |
Kevin Clark | 2141153 | 2008-06-18 01:03:33 +0000 | [diff] [blame] | 25 | require 'thrift' |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 26 | |
Bryan Duxbury | b7b8af9 | 2009-05-05 18:59:49 +0000 | [diff] [blame] | 27 | require 'calculator' |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 28 | require 'shared_types' |
| 29 | |
| 30 | class CalculatorHandler |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 31 | def initialize() |
| 32 | @log = {} |
| 33 | end |
| 34 | |
| 35 | def ping() |
| 36 | puts "ping()" |
| 37 | end |
| 38 | |
| 39 | def add(n1, n2) |
| 40 | print "add(", n1, ",", n2, ")\n" |
| 41 | return n1 + n2 |
| 42 | end |
Mark Slee | 8ec70e8 | 2008-01-07 21:50:30 +0000 | [diff] [blame] | 43 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 44 | def calculate(logid, work) |
| 45 | print "calculate(", logid, ", {", work.op, ",", work.num1, ",", work.num2,"})\n" |
| 46 | if work.op == Operation::ADD |
| 47 | val = work.num1 + work.num2 |
| 48 | elsif work.op == Operation::SUBTRACT |
| 49 | val = work.num1 - work.num2 |
| 50 | elsif work.op == Operation::MULTIPLY |
| 51 | val = work.num1 * work.num2 |
| 52 | elsif work.op == Operation::DIVIDE |
| 53 | if work.num2 == 0 |
| 54 | x = InvalidOperation.new() |
| 55 | x.what = work.op |
| 56 | x.why = "Cannot divide by 0" |
| 57 | raise x |
| 58 | end |
| 59 | val = work.num1 / work.num2 |
| 60 | else |
Christopher Piro | d795b9d | 2007-06-28 01:09:22 +0000 | [diff] [blame] | 61 | x = InvalidOperation.new() |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 62 | x.what = work.op |
| 63 | x.why = "Invalid operation" |
| 64 | raise x |
| 65 | end |
| 66 | |
| 67 | entry = SharedStruct.new() |
| 68 | entry.key = logid |
| 69 | entry.value = "#{val}" |
| 70 | @log[logid] = entry |
| 71 | |
| 72 | return val |
| 73 | |
| 74 | end |
| 75 | |
| 76 | def getStruct(key) |
| 77 | print "getStruct(", key, ")\n" |
| 78 | return @log[key] |
| 79 | end |
| 80 | |
| 81 | def zip() |
| 82 | print "zip\n" |
| 83 | end |
| 84 | |
| 85 | end |
| 86 | |
| 87 | handler = CalculatorHandler.new() |
| 88 | processor = Calculator::Processor.new(handler) |
Kevin Clark | 2141153 | 2008-06-18 01:03:33 +0000 | [diff] [blame] | 89 | transport = Thrift::ServerSocket.new(9090) |
| 90 | transportFactory = Thrift::BufferedTransportFactory.new() |
| 91 | server = Thrift::SimpleServer.new(processor, transport, transportFactory) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 92 | |
| 93 | puts "Starting the server..." |
| 94 | server.serve() |
| 95 | puts "done." |