blob: c3e64db5c3327525185818c6a6b97c105fc123b3 [file] [log] [blame]
Roger Meiercd9bc462011-01-03 20:19:07 +00001#!/usr/bin/env python
2
3#
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.twisted')
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090025sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0])
Roger Meiercd9bc462011-01-03 20:19:07 +000026
27from tutorial import Calculator
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090028from tutorial.ttypes import InvalidOperation, Operation
Roger Meiercd9bc462011-01-03 20:19:07 +000029
30from shared.ttypes import SharedStruct
31
32from zope.interface import implements
33from twisted.internet import reactor
34
35from thrift.transport import TTwisted
36from thrift.protocol import TBinaryProtocol
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090037
Roger Meiercd9bc462011-01-03 20:19:07 +000038
39class CalculatorHandler:
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090040 implements(Calculator.Iface)
Roger Meiercd9bc462011-01-03 20:19:07 +000041
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090042 def __init__(self):
43 self.log = {}
Roger Meiercd9bc462011-01-03 20:19:07 +000044
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090045 def ping(self):
46 print('ping()')
Roger Meiercd9bc462011-01-03 20:19:07 +000047
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090048 def add(self, n1, n2):
49 print('add(%d,%d)' % (n1, n2))
50 return n1 + n2
Roger Meiercd9bc462011-01-03 20:19:07 +000051
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090052 def calculate(self, logid, work):
53 print('calculate(%d, %r)' % (logid, work))
Roger Meiercd9bc462011-01-03 20:19:07 +000054
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090055 if work.op == Operation.ADD:
56 val = work.num1 + work.num2
57 elif work.op == Operation.SUBTRACT:
58 val = work.num1 - work.num2
59 elif work.op == Operation.MULTIPLY:
60 val = work.num1 * work.num2
61 elif work.op == Operation.DIVIDE:
62 if work.num2 == 0:
Kengo Seki46554d02019-12-27 07:56:12 +090063 raise InvalidOperation(work.op, 'Cannot divide by 0')
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090064 val = work.num1 / work.num2
65 else:
Kengo Seki46554d02019-12-27 07:56:12 +090066 raise InvalidOperation(work.op, 'Invalid operation')
Roger Meiercd9bc462011-01-03 20:19:07 +000067
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090068 log = SharedStruct()
69 log.key = logid
70 log.value = '%d' % (val)
71 self.log[logid] = log
Roger Meiercd9bc462011-01-03 20:19:07 +000072
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090073 return val
Roger Meiercd9bc462011-01-03 20:19:07 +000074
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090075 def getStruct(self, key):
76 print('getStruct(%d)' % (key))
77 return self.log[key]
78
79 def zip(self):
80 print('zip()')
Roger Meiercd9bc462011-01-03 20:19:07 +000081
James E. King III3ec40312019-01-31 18:35:51 -050082
Roger Meiercd9bc462011-01-03 20:19:07 +000083if __name__ == '__main__':
84 handler = CalculatorHandler()
85 processor = Calculator.Processor(handler)
86 pfactory = TBinaryProtocol.TBinaryProtocolFactory()
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090087 server = reactor.listenTCP(
88 9090,
89 TTwisted.ThriftServerFactory(processor, pfactory),
90 interface="127.0.0.1")
Roger Meiercd9bc462011-01-03 20:19:07 +000091 reactor.run()