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 | |
| 22 | import sys |
| 23 | sys.path.append('../gen-py') |
| 24 | |
| 25 | from tutorial import Calculator |
| 26 | from tutorial.ttypes import * |
| 27 | |
| 28 | from twisted.internet.defer import inlineCallbacks |
| 29 | from twisted.internet import reactor |
| 30 | from twisted.internet.protocol import ClientCreator |
| 31 | |
| 32 | from thrift import Thrift |
| 33 | from thrift.transport import TTwisted |
| 34 | from thrift.protocol import TBinaryProtocol |
| 35 | |
| 36 | @inlineCallbacks |
| 37 | def main(client): |
| 38 | yield client.ping() |
| 39 | print 'ping()' |
| 40 | |
| 41 | sum = yield client.add(1,1) |
| 42 | print '1+1=%d' % (sum) |
| 43 | |
| 44 | work = Work() |
| 45 | |
| 46 | work.op = Operation.DIVIDE |
| 47 | work.num1 = 1 |
| 48 | work.num2 = 0 |
| 49 | |
| 50 | try: |
| 51 | quotient = yield client.calculate(1, work) |
| 52 | print 'Whoa? You know how to divide by zero?' |
| 53 | except InvalidOperation, io: |
| 54 | print 'InvalidOperation: %r' % io |
| 55 | |
| 56 | work.op = Operation.SUBTRACT |
| 57 | work.num1 = 15 |
| 58 | work.num2 = 10 |
| 59 | |
| 60 | diff = yield client.calculate(1, work) |
| 61 | print '15-10=%d' % (diff) |
| 62 | |
| 63 | log = yield client.getStruct(1) |
| 64 | print 'Check log: %s' % (log.value) |
| 65 | reactor.stop() |
| 66 | |
| 67 | if __name__ == '__main__': |
| 68 | d = ClientCreator(reactor, |
| 69 | TTwisted.ThriftClientProtocol, |
| 70 | Calculator.Client, |
| 71 | TBinaryProtocol.TBinaryProtocolFactory(), |
| 72 | ).connectTCP("127.0.0.1", 9090) |
| 73 | d.addCallback(lambda conn: conn.client) |
| 74 | d.addCallback(main) |
| 75 | |
| 76 | reactor.run() |