blob: 2976495e314d6312625190c0d078e139154b92fb [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
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090022import glob
23import sys
Roger Meier1d66d062012-10-26 21:46:18 +000024sys.path.append('gen-py.twisted')
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090025sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0])
Roger Meiercd9bc462011-01-03 20:19:07 +000026
27from tutorial import Calculator
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090028from tutorial.ttypes import InvalidOperation, Operation, Work
Roger Meiercd9bc462011-01-03 20:19:07 +000029
30from twisted.internet.defer import inlineCallbacks
31from twisted.internet import reactor
32from twisted.internet.protocol import ClientCreator
33
Roger Meiercd9bc462011-01-03 20:19:07 +000034from thrift.transport import TTwisted
35from thrift.protocol import TBinaryProtocol
36
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090037
Roger Meiercd9bc462011-01-03 20:19:07 +000038@inlineCallbacks
39def main(client):
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090040 yield client.ping()
41 print('ping()')
Roger Meiercd9bc462011-01-03 20:19:07 +000042
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090043 sum = yield client.add(1, 1)
44 print(('1+1=%d' % (sum)))
Roger Meiercd9bc462011-01-03 20:19:07 +000045
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090046 work = Work()
Roger Meiercd9bc462011-01-03 20:19:07 +000047
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090048 work.op = Operation.DIVIDE
49 work.num1 = 1
50 work.num2 = 0
Roger Meiercd9bc462011-01-03 20:19:07 +000051
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090052 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 Meiercd9bc462011-01-03 20:19:07 +000058
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090059 work.op = Operation.SUBTRACT
60 work.num1 = 15
61 work.num2 = 10
Roger Meiercd9bc462011-01-03 20:19:07 +000062
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090063 diff = yield client.calculate(1, work)
64 print(('15-10=%d' % (diff)))
Roger Meiercd9bc462011-01-03 20:19:07 +000065
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090066 log = yield client.getStruct(1)
67 print(('Check log: %s' % (log.value)))
68 reactor.stop()
Roger Meiercd9bc462011-01-03 20:19:07 +000069
James E. King III3ec40312019-01-31 18:35:51 -050070
Roger Meiercd9bc462011-01-03 20:19:07 +000071if __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()