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