blob: d2343edd3d37dd7d57f8f96ffa18deba7844d349 [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
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090022import glob
23import sys
Roger Meier1d66d062012-10-26 21:46:18 +000024sys.path.append('gen-py')
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090025sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0])
Mark Slee07a3aab2007-03-07 05:45:10 +000026
27from tutorial import Calculator
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090028from tutorial.ttypes import InvalidOperation, Operation
Mark Slee07a3aab2007-03-07 05:45:10 +000029
30from shared.ttypes import SharedStruct
31
32from thrift.transport import TSocket
33from thrift.transport import TTransport
34from thrift.protocol import TBinaryProtocol
35from thrift.server import TServer
36
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090037
Mark Slee07a3aab2007-03-07 05:45:10 +000038class CalculatorHandler:
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090039 def __init__(self):
40 self.log = {}
David Reiss0c90f6f2008-02-06 22:18:40 +000041
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090042 def ping(self):
43 print('ping()')
Mark Slee07a3aab2007-03-07 05:45:10 +000044
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090045 def add(self, n1, n2):
46 print('add(%d,%d)' % (n1, n2))
47 return n1 + n2
Mark Slee07a3aab2007-03-07 05:45:10 +000048
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090049 def calculate(self, logid, work):
50 print('calculate(%d, %r)' % (logid, work))
David Reiss0c90f6f2008-02-06 22:18:40 +000051
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090052 if work.op == Operation.ADD:
53 val = work.num1 + work.num2
54 elif work.op == Operation.SUBTRACT:
55 val = work.num1 - work.num2
56 elif work.op == Operation.MULTIPLY:
57 val = work.num1 * work.num2
58 elif work.op == Operation.DIVIDE:
59 if work.num2 == 0:
Kengo Seki46554d02019-12-27 07:56:12 +090060 raise InvalidOperation(work.op, 'Cannot divide by 0')
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090061 val = work.num1 / work.num2
62 else:
Kengo Seki46554d02019-12-27 07:56:12 +090063 raise InvalidOperation(work.op, 'Invalid operation')
Mark Slee07a3aab2007-03-07 05:45:10 +000064
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090065 log = SharedStruct()
66 log.key = logid
67 log.value = '%d' % (val)
68 self.log[logid] = log
Mark Slee07a3aab2007-03-07 05:45:10 +000069
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090070 return val
Mark Slee07a3aab2007-03-07 05:45:10 +000071
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090072 def getStruct(self, key):
73 print('getStruct(%d)' % (key))
74 return self.log[key]
Mark Slee07a3aab2007-03-07 05:45:10 +000075
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090076 def zip(self):
77 print('zip()')
David Reiss0c90f6f2008-02-06 22:18:40 +000078
James E. King, III0ad20bd2017-09-30 15:44:16 -070079
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090080if __name__ == '__main__':
81 handler = CalculatorHandler()
82 processor = Calculator.Processor(handler)
walter-weinmannfaa2a3e2017-09-18 20:32:10 +020083 transport = TSocket.TServerSocket(host='127.0.0.1', port=9090)
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090084 tfactory = TTransport.TBufferedTransportFactory()
85 pfactory = TBinaryProtocol.TBinaryProtocolFactory()
Mark Slee07a3aab2007-03-07 05:45:10 +000086
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090087 server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
Mark Slee07a3aab2007-03-07 05:45:10 +000088
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090089 # You could do one of these for a multithreaded server
Nobuaki Sukegawad479e232016-02-28 11:28:19 +090090 # server = TServer.TThreadedServer(
91 # processor, transport, tfactory, pfactory)
92 # server = TServer.TThreadPoolServer(
93 # processor, transport, tfactory, pfactory)
Mark Slee07a3aab2007-03-07 05:45:10 +000094
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090095 print('Starting the server...')
96 server.serve()
97 print('done.')