blob: 63f993bc6df794ed25b0fc232538abcbade7360d [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
28from shared.ttypes import SharedStruct
29
30from thrift.transport import TSocket
31from thrift.transport import TTransport
32from thrift.protocol import TBinaryProtocol
33from thrift.server import TServer
34
35class CalculatorHandler:
36 def __init__(self):
37 self.log = {}
David Reiss0c90f6f2008-02-06 22:18:40 +000038
Mark Slee07a3aab2007-03-07 05:45:10 +000039 def ping(self):
40 print 'ping()'
41
42 def add(self, n1, n2):
43 print 'add(%d,%d)' % (n1, n2)
44 return n1+n2
45
46 def calculate(self, logid, work):
David Reiss8bb593e2009-01-24 19:37:41 +000047 print 'calculate(%d, %r)' % (logid, work)
David Reiss0c90f6f2008-02-06 22:18:40 +000048
Mark Slee07a3aab2007-03-07 05:45:10 +000049 if work.op == Operation.ADD:
50 val = work.num1 + work.num2
51 elif work.op == Operation.SUBTRACT:
52 val = work.num1 - work.num2
53 elif work.op == Operation.MULTIPLY:
54 val = work.num1 * work.num2
55 elif work.op == Operation.DIVIDE:
56 if work.num2 == 0:
57 x = InvalidOperation()
58 x.what = work.op
59 x.why = 'Cannot divide by 0'
60 raise x
61 val = work.num1 / work.num2
62 else:
63 x = InvalidOperation()
64 x.what = work.op
65 x.why = 'Invalid operation'
66 raise x
67
68 log = SharedStruct()
69 log.key = logid
70 log.value = '%d' % (val)
71 self.log[logid] = log
72
73 return val
74
75 def getStruct(self, key):
76 print 'getStruct(%d)' % (key)
77 return self.log[key]
78
79 def zip(self):
80 print 'zip()'
David Reiss0c90f6f2008-02-06 22:18:40 +000081
Mark Slee07a3aab2007-03-07 05:45:10 +000082handler = CalculatorHandler()
83processor = Calculator.Processor(handler)
84transport = TSocket.TServerSocket(9090)
85tfactory = TTransport.TBufferedTransportFactory()
86pfactory = TBinaryProtocol.TBinaryProtocolFactory()
87
88server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
89
90# You could do one of these for a multithreaded server
91#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
92#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
93
94print 'Starting the server...'
95server.serve()
96print 'done.'