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 | import logging |
| 28 | |
| 29 | from tutorial import Calculator |
| 30 | from tutorial.ttypes import Operation, Work, InvalidOperation |
| 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 | |
| 37 | from tornado import gen |
| 38 | from tornado import ioloop |
| 39 | |
| 40 | |
| 41 | @gen.engine |
| 42 | def communicate(callback=None): |
| 43 | # create client |
| 44 | transport = TTornado.TTornadoStreamTransport('localhost', 9090) |
| 45 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
| 46 | client = Calculator.Client(transport, pfactory) |
| 47 | |
| 48 | # open the transport, bail on error |
| 49 | try: |
| 50 | yield gen.Task(transport.open) |
| 51 | except TTransport.TTransportException as ex: |
| 52 | logging.error(ex) |
| 53 | if callback: |
| 54 | callback() |
| 55 | return |
| 56 | |
| 57 | # ping |
| 58 | yield gen.Task(client.ping) |
| 59 | print "ping()" |
| 60 | |
| 61 | # add |
| 62 | sum_ = yield gen.Task(client.add, 1, 1) |
| 63 | print "1 + 1 = {}".format(sum_) |
| 64 | |
| 65 | # make a oneway call without a callback (schedule the write and continue |
| 66 | # without blocking) |
| 67 | client.zip() |
| 68 | print "zip() without callback" |
| 69 | |
| 70 | # make a oneway call with a callback (we'll wait for the stream write to |
| 71 | # complete before continuing) |
| 72 | yield gen.Task(client.zip) |
| 73 | print "zip() with callback" |
| 74 | |
| 75 | # calculate 1/0 |
| 76 | work = Work() |
| 77 | work.op = Operation.DIVIDE |
| 78 | work.num1 = 1 |
| 79 | work.num2 = 0 |
| 80 | |
| 81 | try: |
| 82 | quotient = yield gen.Task(client.calculate, 1, work) |
| 83 | print "Whoa? You know how to divide by zero?" |
| 84 | except InvalidOperation as io: |
| 85 | print "InvalidOperation: {}".format(io) |
| 86 | |
| 87 | # calculate 15-10 |
| 88 | work.op = Operation.SUBTRACT |
| 89 | work.num1 = 15 |
| 90 | work.num2 = 10 |
| 91 | |
| 92 | diff = yield gen.Task(client.calculate, 1, work) |
| 93 | print "15 - 10 = {}".format(diff) |
| 94 | |
| 95 | # getStruct |
| 96 | log = yield gen.Task(client.getStruct, 1) |
| 97 | print "Check log: {}".format(log.value) |
| 98 | |
| 99 | # close the transport |
| 100 | client._transport.close() |
| 101 | |
| 102 | if callback: |
| 103 | callback() |
| 104 | |
| 105 | |
| 106 | def main(): |
| 107 | # create an ioloop, do the above, then stop |
| 108 | io_loop = ioloop.IOLoop.instance() |
| 109 | def this_joint(): |
| 110 | communicate(callback=io_loop.stop) |
| 111 | io_loop.add_callback(this_joint) |
| 112 | io_loop.start() |
| 113 | |
| 114 | |
| 115 | if __name__ == "__main__": |
| 116 | main() |