blob: b472195a498bcaadd5993e13183400b187d87825 [file] [log] [blame]
Chris Piro20c81ad2013-03-07 11:32:48 -05001#!/usr/bin/env python
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
Chris Piro20c81ad2013-03-07 11:32:48 -050022import glob
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090023import sys
24
Chris Piro20c81ad2013-03-07 11:32:48 -050025sys.path.append('gen-py.tornado')
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090026sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0])
Chris Piro20c81ad2013-03-07 11:32:48 -050027
28from tutorial import Calculator
29from tutorial.ttypes import Operation, InvalidOperation
30
31from shared.ttypes import SharedStruct
32
33from thrift import TTornado
Chris Piro20c81ad2013-03-07 11:32:48 -050034from thrift.protocol import TBinaryProtocol
Chris Piro20c81ad2013-03-07 11:32:48 -050035
36from tornado import ioloop
37
38
39class CalculatorHandler(object):
40 def __init__(self):
41 self.log = {}
42
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090043 def ping(self):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090044 print("ping()")
Chris Piro20c81ad2013-03-07 11:32:48 -050045
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090046 def add(self, n1, n2):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090047 print("add({}, {})".format(n1, n2))
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090048 return n1 + n2
Chris Piro20c81ad2013-03-07 11:32:48 -050049
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090050 def calculate(self, logid, work):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090051 print("calculate({}, {})".format(logid, work))
Chris Piro20c81ad2013-03-07 11:32:48 -050052
53 if work.op == Operation.ADD:
54 val = work.num1 + work.num2
55 elif work.op == Operation.SUBTRACT:
56 val = work.num1 - work.num2
57 elif work.op == Operation.MULTIPLY:
58 val = work.num1 * work.num2
59 elif work.op == Operation.DIVIDE:
60 if work.num2 == 0:
Kengo Seki46554d02019-12-27 07:56:12 +090061 raise InvalidOperation(work.op, "Cannot divide by 0")
Chris Piro20c81ad2013-03-07 11:32:48 -050062 val = work.num1 / work.num2
63 else:
Kengo Seki46554d02019-12-27 07:56:12 +090064 raise InvalidOperation(work.op, "Invalid operation")
Chris Piro20c81ad2013-03-07 11:32:48 -050065
66 log = SharedStruct()
67 log.key = logid
68 log.value = '%d' % (val)
69 self.log[logid] = log
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090070 return val
Chris Piro20c81ad2013-03-07 11:32:48 -050071
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090072 def getStruct(self, key):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090073 print("getStruct({})".format(key))
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090074 return self.log[key]
Chris Piro20c81ad2013-03-07 11:32:48 -050075
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090076 def zip(self):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090077 print("zip()")
Chris Piro20c81ad2013-03-07 11:32:48 -050078
79
80def main():
81 handler = CalculatorHandler()
82 processor = Calculator.Processor(handler)
83 pfactory = TBinaryProtocol.TBinaryProtocolFactory()
84 server = TTornado.TTornadoServer(processor, pfactory)
85
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090086 print("Starting the server...")
Chris Piro20c81ad2013-03-07 11:32:48 -050087 server.bind(9090)
88 server.start(1)
89 ioloop.IOLoop.instance().start()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090090 print("done.")
Chris Piro20c81ad2013-03-07 11:32:48 -050091
92
93if __name__ == "__main__":
94 main()