Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [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 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 22 | import glob |
| 23 | import sys |
Roger Meier | 1d66d06 | 2012-10-26 21:46:18 +0000 | [diff] [blame] | 24 | sys.path.append('gen-py.twisted') |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 25 | sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0]) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 26 | |
| 27 | from tutorial import Calculator |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 28 | from tutorial.ttypes import InvalidOperation, Operation, Work |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 29 | |
| 30 | from twisted.internet.defer import inlineCallbacks |
| 31 | from twisted.internet import reactor |
| 32 | from twisted.internet.protocol import ClientCreator |
| 33 | |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 34 | from thrift.transport import TTwisted |
| 35 | from thrift.protocol import TBinaryProtocol |
| 36 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 37 | |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 38 | @inlineCallbacks |
| 39 | def main(client): |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 40 | yield client.ping() |
| 41 | print('ping()') |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 42 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 43 | sum = yield client.add(1, 1) |
| 44 | print(('1+1=%d' % (sum))) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 45 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 46 | work = Work() |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 47 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 48 | work.op = Operation.DIVIDE |
| 49 | work.num1 = 1 |
| 50 | work.num2 = 0 |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 51 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 52 | try: |
| 53 | quotient = yield client.calculate(1, work) |
| 54 | print('Whoa? You know how to divide by zero?') |
| 55 | print('FYI the answer is %d' % quotient) |
| 56 | except InvalidOperation as e: |
| 57 | print(('InvalidOperation: %r' % e)) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 58 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 59 | work.op = Operation.SUBTRACT |
| 60 | work.num1 = 15 |
| 61 | work.num2 = 10 |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 62 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 63 | diff = yield client.calculate(1, work) |
| 64 | print(('15-10=%d' % (diff))) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 65 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 66 | log = yield client.getStruct(1) |
| 67 | print(('Check log: %s' % (log.value))) |
| 68 | reactor.stop() |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 69 | |
James E. King III | 3ec4031 | 2019-01-31 18:35:51 -0500 | [diff] [blame] | 70 | |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 71 | if __name__ == '__main__': |
| 72 | d = ClientCreator(reactor, |
| 73 | TTwisted.ThriftClientProtocol, |
| 74 | Calculator.Client, |
| 75 | TBinaryProtocol.TBinaryProtocolFactory(), |
| 76 | ).connectTCP("127.0.0.1", 9090) |
| 77 | d.addCallback(lambda conn: conn.client) |
| 78 | d.addCallback(main) |
| 79 | |
| 80 | reactor.run() |