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 logging |
| 24 | import sys |
| 25 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 26 | sys.path.append('gen-py.tornado') |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 27 | sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0]) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 28 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 29 | from tutorial import Calculator |
| 30 | from tutorial.ttypes import Operation, Work, InvalidOperation |
| 31 | |
| 32 | from thrift import TTornado |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 33 | from thrift.transport import TTransport |
| 34 | from thrift.protocol import TBinaryProtocol |
| 35 | |
| 36 | from tornado import gen |
| 37 | from tornado import ioloop |
| 38 | |
| 39 | |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 40 | @gen.coroutine |
| 41 | def communicate(): |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 42 | # create client |
| 43 | transport = TTornado.TTornadoStreamTransport('localhost', 9090) |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 44 | # open the transport, bail on error |
| 45 | try: |
| 46 | yield transport.open() |
| 47 | print('Transport is opened') |
| 48 | except TTransport.TTransportException as ex: |
| 49 | logging.error(ex) |
| 50 | raise gen.Return() |
| 51 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 52 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
| 53 | client = Calculator.Client(transport, pfactory) |
| 54 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 55 | # ping |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 56 | yield client.ping() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 57 | print("ping()") |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 58 | |
| 59 | # add |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 60 | sum_ = yield client.add(1, 1) |
| 61 | print("1 + 1 = {0}".format(sum_)) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 62 | |
| 63 | # make a oneway call without a callback (schedule the write and continue |
| 64 | # without blocking) |
| 65 | client.zip() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 66 | print("zip() without callback") |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 67 | |
| 68 | # make a oneway call with a callback (we'll wait for the stream write to |
| 69 | # complete before continuing) |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 70 | client.zip() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 71 | print("zip() with callback") |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 72 | |
| 73 | # calculate 1/0 |
| 74 | work = Work() |
| 75 | work.op = Operation.DIVIDE |
| 76 | work.num1 = 1 |
| 77 | work.num2 = 0 |
| 78 | |
| 79 | try: |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 80 | quotient = yield client.calculate(1, work) |
| 81 | print("Whoa? You know how to divide by zero ? -> {0}".format(quotient)) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 82 | except InvalidOperation as io: |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 83 | print("InvalidOperation: {0}".format(io)) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 84 | |
| 85 | # calculate 15-10 |
| 86 | work.op = Operation.SUBTRACT |
| 87 | work.num1 = 15 |
| 88 | work.num2 = 10 |
| 89 | |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 90 | diff = yield client.calculate(1, work) |
| 91 | print("15 - 10 = {0}".format(diff)) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 92 | |
| 93 | # getStruct |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 94 | log = yield client.getStruct(1) |
| 95 | print("Check log: {0}".format(log.value)) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 96 | |
| 97 | # close the transport |
| 98 | client._transport.close() |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 99 | raise gen.Return() |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 100 | |
| 101 | |
| 102 | def main(): |
| 103 | # create an ioloop, do the above, then stop |
Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 104 | ioloop.IOLoop.current().run_sync(communicate) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 105 | |
| 106 | |
| 107 | if __name__ == "__main__": |
| 108 | main() |