blob: 4f8e48656fc2983e7b2d26d8ebbf8497e7f2dc45 [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
Mark Slee07a3aab2007-03-07 05:45:10 +000022$:.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'
26require 'thrift/protocol/binaryprotocol'
Mark Slee07a3aab2007-03-07 05:45:10 +000027require 'thrift/server/tserver'
28
29require 'Calculator'
30require 'shared_types'
31
32class CalculatorHandler
Mark Slee07a3aab2007-03-07 05:45:10 +000033 def initialize()
34 @log = {}
35 end
36
37 def ping()
38 puts "ping()"
39 end
40
41 def add(n1, n2)
42 print "add(", n1, ",", n2, ")\n"
43 return n1 + n2
44 end
Mark Slee8ec70e82008-01-07 21:50:30 +000045
Mark Slee07a3aab2007-03-07 05:45:10 +000046 def calculate(logid, work)
47 print "calculate(", logid, ", {", work.op, ",", work.num1, ",", work.num2,"})\n"
48 if work.op == Operation::ADD
49 val = work.num1 + work.num2
50 elsif work.op == Operation::SUBTRACT
51 val = work.num1 - work.num2
52 elsif work.op == Operation::MULTIPLY
53 val = work.num1 * work.num2
54 elsif work.op == Operation::DIVIDE
55 if work.num2 == 0
56 x = InvalidOperation.new()
57 x.what = work.op
58 x.why = "Cannot divide by 0"
59 raise x
60 end
61 val = work.num1 / work.num2
62 else
Christopher Pirod795b9d2007-06-28 01:09:22 +000063 x = InvalidOperation.new()
Mark Slee07a3aab2007-03-07 05:45:10 +000064 x.what = work.op
65 x.why = "Invalid operation"
66 raise x
67 end
68
69 entry = SharedStruct.new()
70 entry.key = logid
71 entry.value = "#{val}"
72 @log[logid] = entry
73
74 return val
75
76 end
77
78 def getStruct(key)
79 print "getStruct(", key, ")\n"
80 return @log[key]
81 end
82
83 def zip()
84 print "zip\n"
85 end
86
87end
88
89handler = CalculatorHandler.new()
90processor = Calculator::Processor.new(handler)
Kevin Clark21411532008-06-18 01:03:33 +000091transport = Thrift::ServerSocket.new(9090)
92transportFactory = Thrift::BufferedTransportFactory.new()
93server = Thrift::SimpleServer.new(processor, transport, transportFactory)
Mark Slee07a3aab2007-03-07 05:45:10 +000094
95puts "Starting the server..."
96server.serve()
97puts "done."