| 30 |
ashish |
1 |
#!/usr/bin/env ruby
|
|
|
2 |
|
|
|
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 |
|
|
|
22 |
$:.push('../gen-rb')
|
|
|
23 |
$:.unshift '../../lib/rb/lib'
|
|
|
24 |
|
|
|
25 |
require 'thrift'
|
|
|
26 |
|
|
|
27 |
require 'calculator'
|
|
|
28 |
require 'shared_types'
|
|
|
29 |
|
|
|
30 |
class CalculatorHandler
|
|
|
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
|
|
|
43 |
|
|
|
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
|
|
|
61 |
x = InvalidOperation.new()
|
|
|
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)
|
|
|
89 |
transport = Thrift::ServerSocket.new(9090)
|
|
|
90 |
transportFactory = Thrift::BufferedTransportFactory.new()
|
|
|
91 |
server = Thrift::SimpleServer.new(processor, transport, transportFactory)
|
|
|
92 |
|
|
|
93 |
puts "Starting the server..."
|
|
|
94 |
server.serve()
|
|
|
95 |
puts "done."
|