blob: 9dcecd97c5d5876880ab99e23f63fb3a9679ad8e [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#!/usr/bin/env python
2
3import sys
4sys.path.append('../gen-py')
5
6from tutorial import Calculator
7from tutorial.ttypes import *
8
Mark Slee76791962007-03-14 02:47:35 +00009from thrift import Thrift
Mark Slee07a3aab2007-03-07 05:45:10 +000010from thrift.transport import TSocket
11from thrift.transport import TTransport
12from thrift.protocol import TBinaryProtocol
13
Mark Slee07a3aab2007-03-07 05:45:10 +000014try:
Mark Slee07a3aab2007-03-07 05:45:10 +000015
Mark Slee76791962007-03-14 02:47:35 +000016 # Make socket
17 transport = TSocket.TSocket('localhost', 9090)
Mark Slee07a3aab2007-03-07 05:45:10 +000018
Mark Slee76791962007-03-14 02:47:35 +000019 # Buffering is critical. Raw sockets are very slow
20 transport = TTransport.TBufferedTransport(transport)
Mark Slee07a3aab2007-03-07 05:45:10 +000021
Mark Slee76791962007-03-14 02:47:35 +000022 # Wrap in a protocol
23 protocol = TBinaryProtocol.TBinaryProtocol(transport)
Mark Slee07a3aab2007-03-07 05:45:10 +000024
Mark Slee76791962007-03-14 02:47:35 +000025 # Create a client to use the protocol encoder
26 client = Calculator.Client(protocol)
Mark Slee07a3aab2007-03-07 05:45:10 +000027
Mark Slee76791962007-03-14 02:47:35 +000028 # Connect!
29 transport.open()
30
31 client.ping()
32 print 'ping()'
33
34 sum = client.add(1,1)
35 print '1+1=%d' % (sum)
36
37 work = Work()
38
39 work.op = Operation.DIVIDE
40 work.num1 = 1
41 work.num2 = 0
42
43 try:
44 quotient = client.calculate(1, work)
45 print 'Whoa? You know how to divide by zero?'
46 except InvalidOperation, io:
47 print 'InvalidOperation: %s' % (io.__str__())
48
49 work.op = Operation.SUBTRACT
50 work.num1 = 15
51 work.num2 = 10
52
53 diff = client.calculate(1, work)
54 print '15-10=%d' % (diff)
55
56 log = client.getStruct(1)
57 print 'Check log: %s' % (log.value)
58
59 # Close!
60 transport.close()
61
62except Thrift.TException, tx:
63 print '%s' % (tx.message)