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 | |
| 22 | import sys |
| 23 | import glob |
| 24 | sys.path.append('gen-py.tornado') |
| 25 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
| 26 | |
| 27 | from tutorial import Calculator |
| 28 | from tutorial.ttypes import Operation, InvalidOperation |
| 29 | |
| 30 | from shared.ttypes import SharedStruct |
| 31 | |
| 32 | from thrift import TTornado |
| 33 | from thrift.transport import TSocket |
| 34 | from thrift.transport import TTransport |
| 35 | from thrift.protocol import TBinaryProtocol |
| 36 | from thrift.server import TServer |
| 37 | |
| 38 | from tornado import ioloop |
| 39 | |
| 40 | |
| 41 | class CalculatorHandler(object): |
| 42 | def __init__(self): |
| 43 | self.log = {} |
| 44 | |
| 45 | def ping(self, callback): |
| 46 | print "ping()" |
| 47 | callback() |
| 48 | |
| 49 | def add(self, n1, n2, callback): |
| 50 | print "add({}, {})".format(n1, n2) |
| 51 | callback(n1 + n2) |
| 52 | |
| 53 | def calculate(self, logid, work, callback): |
| 54 | print "calculate({}, {})".format(logid, work) |
| 55 | |
| 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() |
| 65 | x.what = work.op |
| 66 | x.why = "Cannot divide by 0" |
| 67 | raise x |
| 68 | val = work.num1 / work.num2 |
| 69 | else: |
| 70 | x = InvalidOperation() |
| 71 | x.what = work.op |
| 72 | 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 |
| 79 | callback(val) |
| 80 | |
| 81 | def getStruct(self, key, callback): |
| 82 | print "getStruct({})".format(key) |
| 83 | callback(self.log[key]) |
| 84 | |
| 85 | def zip(self, callback): |
| 86 | print "zip()" |
| 87 | callback() |
| 88 | |
| 89 | |
| 90 | def main(): |
| 91 | handler = CalculatorHandler() |
| 92 | processor = Calculator.Processor(handler) |
| 93 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
| 94 | server = TTornado.TTornadoServer(processor, pfactory) |
| 95 | |
| 96 | print "Starting the server..." |
| 97 | server.bind(9090) |
| 98 | server.start(1) |
| 99 | ioloop.IOLoop.instance().start() |
| 100 | print "done." |
| 101 | |
| 102 | |
| 103 | if __name__ == "__main__": |
| 104 | main() |