Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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') |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 25 | sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0]) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +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 |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 29 | |
| 30 | from shared.ttypes import SharedStruct |
| 31 | |
| 32 | from thrift.transport import TSocket |
| 33 | from thrift.transport import TTransport |
| 34 | from thrift.protocol import TBinaryProtocol |
| 35 | from thrift.server import TServer |
| 36 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 37 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 38 | class CalculatorHandler: |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 39 | def __init__(self): |
| 40 | self.log = {} |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 41 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 42 | def ping(self): |
| 43 | print('ping()') |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 44 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 45 | def add(self, n1, n2): |
| 46 | print('add(%d,%d)' % (n1, n2)) |
| 47 | return n1 + n2 |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 48 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 49 | def calculate(self, logid, work): |
| 50 | print('calculate(%d, %r)' % (logid, work)) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 51 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 52 | 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: |
| 60 | x = InvalidOperation() |
| 61 | x.whatOp = work.op |
| 62 | x.why = 'Cannot divide by 0' |
| 63 | raise x |
| 64 | val = work.num1 / work.num2 |
| 65 | else: |
| 66 | x = InvalidOperation() |
| 67 | x.whatOp = work.op |
| 68 | x.why = 'Invalid operation' |
| 69 | raise x |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 70 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 71 | log = SharedStruct() |
| 72 | log.key = logid |
| 73 | log.value = '%d' % (val) |
| 74 | self.log[logid] = log |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 75 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 76 | return val |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 77 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 78 | def getStruct(self, key): |
| 79 | print('getStruct(%d)' % (key)) |
| 80 | return self.log[key] |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 81 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 82 | def zip(self): |
| 83 | print('zip()') |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 84 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 85 | if __name__ == '__main__': |
| 86 | handler = CalculatorHandler() |
| 87 | processor = Calculator.Processor(handler) |
| 88 | transport = TSocket.TServerSocket(port=9090) |
| 89 | tfactory = TTransport.TBufferedTransportFactory() |
| 90 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 91 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 92 | server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 93 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 94 | # You could do one of these for a multithreaded server |
Nobuaki Sukegawa | d479e23 | 2016-02-28 11:28:19 +0900 | [diff] [blame^] | 95 | # server = TServer.TThreadedServer( |
| 96 | # processor, transport, tfactory, pfactory) |
| 97 | # server = TServer.TThreadPoolServer( |
| 98 | # processor, transport, tfactory, pfactory) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 99 | |
Nobuaki Sukegawa | 7e286b0 | 2016-01-11 11:25:09 +0900 | [diff] [blame] | 100 | print('Starting the server...') |
| 101 | server.serve() |
| 102 | print('done.') |