blob: 1d707d740cf4e272f2ffa4515296702605af0b1d [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#!/usr/bin/env ruby
2
David Reissea2cba82009-03-30 21:35:00 +00003#
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
Roger Meiered817d02013-06-09 23:10:06 +020022$:.push('gen-rb')
Kevin Clark2b1e10b2008-06-18 01:00:50 +000023$:.unshift '../../lib/rb/lib'
Mark Slee07a3aab2007-03-07 05:45:10 +000024
Kevin Clark21411532008-06-18 01:03:33 +000025require 'thrift'
Mark Slee07a3aab2007-03-07 05:45:10 +000026
Bryan Duxburyb7b8af92009-05-05 18:59:49 +000027require 'calculator'
Mark Slee07a3aab2007-03-07 05:45:10 +000028require 'shared_types'
29
30class CalculatorHandler
Mark Slee07a3aab2007-03-07 05:45:10 +000031 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 Slee8ec70e82008-01-07 21:50:30 +000043
Mark Slee07a3aab2007-03-07 05:45:10 +000044 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()
Konrad Grochowski3b115df2015-05-18 17:58:36 +020055 x.whatOp = work.op
Mark Slee07a3aab2007-03-07 05:45:10 +000056 x.why = "Cannot divide by 0"
57 raise x
58 end
59 val = work.num1 / work.num2
60 else
Christopher Pirod795b9d2007-06-28 01:09:22 +000061 x = InvalidOperation.new()
Konrad Grochowski3b115df2015-05-18 17:58:36 +020062 x.whatOp = work.op
Mark Slee07a3aab2007-03-07 05:45:10 +000063 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
85end
86
87handler = CalculatorHandler.new()
88processor = Calculator::Processor.new(handler)
Kevin Clark21411532008-06-18 01:03:33 +000089transport = Thrift::ServerSocket.new(9090)
90transportFactory = Thrift::BufferedTransportFactory.new()
91server = Thrift::SimpleServer.new(processor, transport, transportFactory)
Mark Slee07a3aab2007-03-07 05:45:10 +000092
93puts "Starting the server..."
94server.serve()
95puts "done."