Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 1 | #!/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 Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 22 | import glob |
| 23 | import sys |
Roger Meier | 1d66d06 | 2012-10-26 21:46:18 +0000 | [diff] [blame] | 24 | sys.path.append('gen-py.twisted') |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 25 | sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0]) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 26 | |
| 27 | from tutorial import Calculator |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 28 | from tutorial.ttypes import InvalidOperation, Operation |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 29 | |
| 30 | from shared.ttypes import SharedStruct |
| 31 | |
| 32 | from zope.interface import implements |
| 33 | from twisted.internet import reactor |
| 34 | |
| 35 | from thrift.transport import TTwisted |
| 36 | from thrift.protocol import TBinaryProtocol |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 37 | |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 38 | |
| 39 | class CalculatorHandler: |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 40 | implements(Calculator.Iface) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 41 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 42 | def __init__(self): |
| 43 | self.log = {} |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 44 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 45 | def ping(self): |
| 46 | print('ping()') |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 47 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 48 | def add(self, n1, n2): |
| 49 | print('add(%d,%d)' % (n1, n2)) |
| 50 | return n1 + n2 |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 51 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 52 | def calculate(self, logid, work): |
| 53 | print('calculate(%d, %r)' % (logid, work)) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 54 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 55 | 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: |
| 63 | x = InvalidOperation() |
| 64 | x.whatOp = work.op |
| 65 | x.why = 'Cannot divide by 0' |
| 66 | raise x |
| 67 | val = work.num1 / work.num2 |
| 68 | else: |
| 69 | x = InvalidOperation() |
| 70 | x.whatOp = work.op |
| 71 | x.why = 'Invalid operation' |
| 72 | raise x |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 73 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 74 | log = SharedStruct() |
| 75 | log.key = logid |
| 76 | log.value = '%d' % (val) |
| 77 | self.log[logid] = log |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 78 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 79 | return val |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 80 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 81 | def getStruct(self, key): |
| 82 | print('getStruct(%d)' % (key)) |
| 83 | return self.log[key] |
| 84 | |
| 85 | def zip(self): |
| 86 | print('zip()') |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 87 | |
James E. King III | 3ec4031 | 2019-01-31 18:35:51 -0500 | [diff] [blame^] | 88 | |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 89 | if __name__ == '__main__': |
| 90 | handler = CalculatorHandler() |
| 91 | processor = Calculator.Processor(handler) |
| 92 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 93 | server = reactor.listenTCP( |
| 94 | 9090, |
| 95 | TTwisted.ThriftServerFactory(processor, pfactory), |
| 96 | interface="127.0.0.1") |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 97 | reactor.run() |