blob: e80c0fc0963f52097479c235ba7fdd8d52a03f08 [file] [log] [blame]
Roger Meiercd9bc462011-01-03 20:19:07 +00001#!/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
Roger Meier1d66d062012-10-26 21:46:18 +000022import sys, glob
23sys.path.append('gen-py.twisted')
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090024sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0])
Roger Meiercd9bc462011-01-03 20:19:07 +000025
26from tutorial import Calculator
27from tutorial.ttypes import *
28
29from twisted.internet.defer import inlineCallbacks
30from twisted.internet import reactor
31from twisted.internet.protocol import ClientCreator
32
33from thrift import Thrift
34from thrift.transport import TTwisted
35from thrift.protocol import TBinaryProtocol
36
37@inlineCallbacks
38def main(client):
39 yield client.ping()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090040 print('ping()')
Roger Meiercd9bc462011-01-03 20:19:07 +000041
42 sum = yield client.add(1,1)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090043 print(('1+1=%d' % (sum)))
Roger Meiercd9bc462011-01-03 20:19:07 +000044
45 work = Work()
46
47 work.op = Operation.DIVIDE
48 work.num1 = 1
49 work.num2 = 0
50
51 try:
52 quotient = yield client.calculate(1, work)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090053 print('Whoa? You know how to divide by zero?')
54 except InvalidOperation as e:
55 print(('InvalidOperation: %r' % e))
Roger Meiercd9bc462011-01-03 20:19:07 +000056
57 work.op = Operation.SUBTRACT
58 work.num1 = 15
59 work.num2 = 10
60
61 diff = yield client.calculate(1, work)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090062 print(('15-10=%d' % (diff)))
Roger Meiercd9bc462011-01-03 20:19:07 +000063
64 log = yield client.getStruct(1)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090065 print(('Check log: %s' % (log.value)))
Roger Meiercd9bc462011-01-03 20:19:07 +000066 reactor.stop()
67
68if __name__ == '__main__':
69 d = ClientCreator(reactor,
70 TTwisted.ThriftClientProtocol,
71 Calculator.Client,
72 TBinaryProtocol.TBinaryProtocolFactory(),
73 ).connectTCP("127.0.0.1", 9090)
74 d.addCallback(lambda conn: conn.client)
75 d.addCallback(main)
76
77 reactor.run()