blob: 3324bbd55f299f6e0cd85d39cc455eba53a9d38d [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
9from shared.ttypes import SharedStruct
10
11from thrift.transport import TSocket
12from thrift.transport import TTransport
13from thrift.protocol import TBinaryProtocol
14from thrift.server import TServer
15
16class CalculatorHandler:
17 def __init__(self):
18 self.log = {}
19
20 def ping(self):
21 print 'ping()'
22
23 def add(self, n1, n2):
24 print 'add(%d,%d)' % (n1, n2)
25 return n1+n2
26
27 def calculate(self, logid, work):
28 print 'calculate(%d, %s)' % (logid, work.__str__())
29
30 if work.op == Operation.ADD:
31 val = work.num1 + work.num2
32 elif work.op == Operation.SUBTRACT:
33 val = work.num1 - work.num2
34 elif work.op == Operation.MULTIPLY:
35 val = work.num1 * work.num2
36 elif work.op == Operation.DIVIDE:
37 if work.num2 == 0:
38 x = InvalidOperation()
39 x.what = work.op
40 x.why = 'Cannot divide by 0'
41 raise x
42 val = work.num1 / work.num2
43 else:
44 x = InvalidOperation()
45 x.what = work.op
46 x.why = 'Invalid operation'
47 raise x
48
49 log = SharedStruct()
50 log.key = logid
51 log.value = '%d' % (val)
52 self.log[logid] = log
53
54 return val
55
56 def getStruct(self, key):
57 print 'getStruct(%d)' % (key)
58 return self.log[key]
59
60 def zip(self):
61 print 'zip()'
62
63handler = CalculatorHandler()
64processor = Calculator.Processor(handler)
65transport = TSocket.TServerSocket(9090)
66tfactory = TTransport.TBufferedTransportFactory()
67pfactory = TBinaryProtocol.TBinaryProtocolFactory()
68
69server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
70
71# You could do one of these for a multithreaded server
72#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
73#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
74
75print 'Starting the server...'
76server.serve()
77print 'done.'