blob: 7b750e488ce9c7b378d8f0dd649de2569a99e2e5 [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
34from thrift.transport import TSocket
35from thrift.transport import TTransport
36from thrift.protocol import TBinaryProtocol
37from thrift.server import TServer
38
39from tornado import ioloop
40
41
42class CalculatorHandler(object):
43 def __init__(self):
44 self.log = {}
45
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090046 def ping(self):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090047 print("ping()")
Chris Piro20c81ad2013-03-07 11:32:48 -050048
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090049 def add(self, n1, n2):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090050 print("add({}, {})".format(n1, n2))
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090051 return n1 + n2
Chris Piro20c81ad2013-03-07 11:32:48 -050052
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090053 def calculate(self, logid, work):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090054 print("calculate({}, {})".format(logid, work))
Chris Piro20c81ad2013-03-07 11:32:48 -050055
56 if work.op == Operation.ADD:
57 val = work.num1 + work.num2
58 elif work.op == Operation.SUBTRACT:
59 val = work.num1 - work.num2
60 elif work.op == Operation.MULTIPLY:
61 val = work.num1 * work.num2
62 elif work.op == Operation.DIVIDE:
63 if work.num2 == 0:
64 x = InvalidOperation()
Konrad Grochowski3b115df2015-05-18 17:58:36 +020065 x.whatOp = work.op
Chris Piro20c81ad2013-03-07 11:32:48 -050066 x.why = "Cannot divide by 0"
67 raise x
68 val = work.num1 / work.num2
69 else:
70 x = InvalidOperation()
Konrad Grochowski3b115df2015-05-18 17:58:36 +020071 x.whatOp = work.op
Chris Piro20c81ad2013-03-07 11:32:48 -050072 x.why = "Invalid operation"
73 raise x
74
75 log = SharedStruct()
76 log.key = logid
77 log.value = '%d' % (val)
78 self.log[logid] = log
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090079 return val
Chris Piro20c81ad2013-03-07 11:32:48 -050080
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090081 def getStruct(self, key):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090082 print("getStruct({})".format(key))
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090083 return self.log[key]
Chris Piro20c81ad2013-03-07 11:32:48 -050084
Nobuaki Sukegawa2e00c992015-12-01 23:46:58 +090085 def zip(self):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090086 print("zip()")
Chris Piro20c81ad2013-03-07 11:32:48 -050087
88
89def main():
90 handler = CalculatorHandler()
91 processor = Calculator.Processor(handler)
92 pfactory = TBinaryProtocol.TBinaryProtocolFactory()
93 server = TTornado.TTornadoServer(processor, pfactory)
94
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090095 print("Starting the server...")
Chris Piro20c81ad2013-03-07 11:32:48 -050096 server.bind(9090)
97 server.start(1)
98 ioloop.IOLoop.instance().start()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090099 print("done.")
Chris Piro20c81ad2013-03-07 11:32:48 -0500100
101
102if __name__ == "__main__":
103 main()