blob: c4559ff3d9dd6ff763e5206d09381f97c060295e [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#!/usr/bin/env python
2
David Reissea2cba82009-03-30 21:35:00 +00003#
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')
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090024sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0])
Mark Slee07a3aab2007-03-07 05:45:10 +000025
26from tutorial import Calculator
27from tutorial.ttypes import *
28
Mark Slee76791962007-03-14 02:47:35 +000029from thrift import Thrift
Mark Slee07a3aab2007-03-07 05:45:10 +000030from thrift.transport import TSocket
31from thrift.transport import TTransport
32from thrift.protocol import TBinaryProtocol
33
Mark Slee07a3aab2007-03-07 05:45:10 +000034try:
Mark Slee07a3aab2007-03-07 05:45:10 +000035
Mark Slee76791962007-03-14 02:47:35 +000036 # Make socket
37 transport = TSocket.TSocket('localhost', 9090)
Mark Slee07a3aab2007-03-07 05:45:10 +000038
Mark Slee76791962007-03-14 02:47:35 +000039 # Buffering is critical. Raw sockets are very slow
40 transport = TTransport.TBufferedTransport(transport)
Mark Slee07a3aab2007-03-07 05:45:10 +000041
Mark Slee76791962007-03-14 02:47:35 +000042 # Wrap in a protocol
43 protocol = TBinaryProtocol.TBinaryProtocol(transport)
Mark Slee07a3aab2007-03-07 05:45:10 +000044
Mark Slee76791962007-03-14 02:47:35 +000045 # Create a client to use the protocol encoder
46 client = Calculator.Client(protocol)
Mark Slee07a3aab2007-03-07 05:45:10 +000047
Mark Slee76791962007-03-14 02:47:35 +000048 # Connect!
49 transport.open()
50
51 client.ping()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090052 print('ping()')
Mark Slee76791962007-03-14 02:47:35 +000053
54 sum = client.add(1,1)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090055 print(('1+1=%d' % (sum)))
Mark Slee76791962007-03-14 02:47:35 +000056
57 work = Work()
David Reiss0c90f6f2008-02-06 22:18:40 +000058
Mark Slee76791962007-03-14 02:47:35 +000059 work.op = Operation.DIVIDE
60 work.num1 = 1
61 work.num2 = 0
62
63 try:
64 quotient = client.calculate(1, work)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090065 print('Whoa? You know how to divide by zero?')
66 except InvalidOperation as e:
67 print(('InvalidOperation: %r' % e))
David Reiss0c90f6f2008-02-06 22:18:40 +000068
Mark Slee76791962007-03-14 02:47:35 +000069 work.op = Operation.SUBTRACT
70 work.num1 = 15
71 work.num2 = 10
David Reiss0c90f6f2008-02-06 22:18:40 +000072
Mark Slee76791962007-03-14 02:47:35 +000073 diff = client.calculate(1, work)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090074 print(('15-10=%d' % (diff)))
Mark Slee76791962007-03-14 02:47:35 +000075
76 log = client.getStruct(1)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090077 print(('Check log: %s' % (log.value)))
Mark Slee76791962007-03-14 02:47:35 +000078
79 # Close!
80 transport.close()
81
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090082except Thrift.TException as tx:
83 print(('%s' % (tx.message)))