Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 1 | #!/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 Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 22 | import glob |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 23 | import sys |
| 24 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 25 | sys.path.append('gen-py.tornado') |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 26 | sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0]) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 27 | |
| 28 | from tutorial import Calculator |
| 29 | from tutorial.ttypes import Operation, InvalidOperation |
| 30 | |
| 31 | from shared.ttypes import SharedStruct |
| 32 | |
| 33 | from thrift import TTornado |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 34 | from thrift.protocol import TBinaryProtocol |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 35 | |
| 36 | from tornado import ioloop |
| 37 | |
| 38 | |
| 39 | class CalculatorHandler(object): |
| 40 | def __init__(self): |
| 41 | self.log = {} |
| 42 | |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 43 | def ping(self): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 44 | print("ping()") |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 45 | |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 46 | def add(self, n1, n2): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 47 | print("add({}, {})".format(n1, n2)) |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 48 | return n1 + n2 |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 49 | |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 50 | def calculate(self, logid, work): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 51 | print("calculate({}, {})".format(logid, work)) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 52 | |
| 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: |
| 61 | x = InvalidOperation() |
Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 62 | x.whatOp = work.op |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 63 | x.why = "Cannot divide by 0" |
| 64 | raise x |
| 65 | val = work.num1 / work.num2 |
| 66 | else: |
| 67 | x = InvalidOperation() |
Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 68 | x.whatOp = work.op |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 69 | x.why = "Invalid operation" |
| 70 | raise x |
| 71 | |
| 72 | log = SharedStruct() |
| 73 | log.key = logid |
| 74 | log.value = '%d' % (val) |
| 75 | self.log[logid] = log |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 76 | return val |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 77 | |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 78 | def getStruct(self, key): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 79 | print("getStruct({})".format(key)) |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 80 | return self.log[key] |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 81 | |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 82 | def zip(self): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 83 | print("zip()") |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 84 | |
| 85 | |
| 86 | def main(): |
| 87 | handler = CalculatorHandler() |
| 88 | processor = Calculator.Processor(handler) |
| 89 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
| 90 | server = TTornado.TTornadoServer(processor, pfactory) |
| 91 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 92 | print("Starting the server...") |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 93 | server.bind(9090) |
| 94 | server.start(1) |
| 95 | ioloop.IOLoop.instance().start() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 96 | print("done.") |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 97 | |
| 98 | |
| 99 | if __name__ == "__main__": |
| 100 | main() |