blob: 916e91570d788f965b6061e045cdcf085ce2d241 [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
Mark Slee07a3aab2007-03-07 05:45:10 +000022import sys
23sys.path.append('../gen-py')
24
25from tutorial import Calculator
26from tutorial.ttypes import *
27
Mark Slee76791962007-03-14 02:47:35 +000028from thrift import Thrift
Mark Slee07a3aab2007-03-07 05:45:10 +000029from thrift.transport import TSocket
30from thrift.transport import TTransport
31from thrift.protocol import TBinaryProtocol
32
Mark Slee07a3aab2007-03-07 05:45:10 +000033try:
Mark Slee07a3aab2007-03-07 05:45:10 +000034
Mark Slee76791962007-03-14 02:47:35 +000035 # Make socket
36 transport = TSocket.TSocket('localhost', 9090)
Mark Slee07a3aab2007-03-07 05:45:10 +000037
Mark Slee76791962007-03-14 02:47:35 +000038 # Buffering is critical. Raw sockets are very slow
39 transport = TTransport.TBufferedTransport(transport)
Mark Slee07a3aab2007-03-07 05:45:10 +000040
Mark Slee76791962007-03-14 02:47:35 +000041 # Wrap in a protocol
42 protocol = TBinaryProtocol.TBinaryProtocol(transport)
Mark Slee07a3aab2007-03-07 05:45:10 +000043
Mark Slee76791962007-03-14 02:47:35 +000044 # Create a client to use the protocol encoder
45 client = Calculator.Client(protocol)
Mark Slee07a3aab2007-03-07 05:45:10 +000046
Mark Slee76791962007-03-14 02:47:35 +000047 # Connect!
48 transport.open()
49
50 client.ping()
51 print 'ping()'
52
53 sum = client.add(1,1)
54 print '1+1=%d' % (sum)
55
56 work = Work()
David Reiss0c90f6f2008-02-06 22:18:40 +000057
Mark Slee76791962007-03-14 02:47:35 +000058 work.op = Operation.DIVIDE
59 work.num1 = 1
60 work.num2 = 0
61
62 try:
63 quotient = client.calculate(1, work)
64 print 'Whoa? You know how to divide by zero?'
65 except InvalidOperation, io:
David Reiss8bb593e2009-01-24 19:37:41 +000066 print 'InvalidOperation: %r' % io
David Reiss0c90f6f2008-02-06 22:18:40 +000067
Mark Slee76791962007-03-14 02:47:35 +000068 work.op = Operation.SUBTRACT
69 work.num1 = 15
70 work.num2 = 10
David Reiss0c90f6f2008-02-06 22:18:40 +000071
Mark Slee76791962007-03-14 02:47:35 +000072 diff = client.calculate(1, work)
73 print '15-10=%d' % (diff)
74
75 log = client.getStruct(1)
76 print 'Check log: %s' % (log.value)
77
78 # Close!
79 transport.close()
80
81except Thrift.TException, tx:
82 print '%s' % (tx.message)