| 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 |  | 
| Roger Meier | 1d66d06 | 2012-10-26 21:46:18 +0000 | [diff] [blame] | 22 | import sys, glob | 
|  | 23 | sys.path.append('gen-py') | 
| Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 24 | sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0]) | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 25 |  | 
|  | 26 | from tutorial import Calculator | 
|  | 27 | from tutorial.ttypes import * | 
|  | 28 |  | 
|  | 29 | from shared.ttypes import SharedStruct | 
|  | 30 |  | 
|  | 31 | from thrift.transport import TSocket | 
|  | 32 | from thrift.transport import TTransport | 
|  | 33 | from thrift.protocol import TBinaryProtocol | 
|  | 34 | from thrift.server import TServer | 
|  | 35 |  | 
|  | 36 | class CalculatorHandler: | 
|  | 37 | def __init__(self): | 
|  | 38 | self.log = {} | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 39 |  | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 40 | def ping(self): | 
| Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 41 | print('ping()') | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 42 |  | 
|  | 43 | def add(self, n1, n2): | 
| Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 44 | print('add(%d,%d)' % (n1, n2)) | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 45 | return n1+n2 | 
|  | 46 |  | 
|  | 47 | def calculate(self, logid, work): | 
| Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 48 | print('calculate(%d, %r)' % (logid, work)) | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 49 |  | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 50 | 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() | 
| Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 59 | x.whatOp = work.op | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 60 | x.why = 'Cannot divide by 0' | 
|  | 61 | raise x | 
|  | 62 | val = work.num1 / work.num2 | 
|  | 63 | else: | 
|  | 64 | x = InvalidOperation() | 
| Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 65 | x.whatOp = work.op | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 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): | 
| Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 77 | print('getStruct(%d)' % (key)) | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 78 | return self.log[key] | 
|  | 79 |  | 
|  | 80 | def zip(self): | 
| Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 81 | print('zip()') | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 82 |  | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 83 | handler = CalculatorHandler() | 
|  | 84 | processor = Calculator.Processor(handler) | 
| Christopher Piro | 860b8c9 | 2012-05-02 19:33:47 +0000 | [diff] [blame] | 85 | transport = TSocket.TServerSocket(port=9090) | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 86 | tfactory = TTransport.TBufferedTransportFactory() | 
|  | 87 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() | 
|  | 88 |  | 
|  | 89 | server = 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 |  | 
| Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 95 | print('Starting the server...') | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 96 | server.serve() | 
| Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 97 | print('done.') |