Rev 30 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/env python## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.#import syssys.path.append('../gen-py')from tutorial import Calculatorfrom tutorial.ttypes import *from shared.ttypes import SharedStructfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.server import TServerclass CalculatorHandler:def __init__(self):self.log = {}def ping(self):print 'ping()'def add(self, n1, n2):print 'add(%d,%d)' % (n1, n2)return n1+n2def calculate(self, logid, work):print 'calculate(%d, %r)' % (logid, work)if work.op == Operation.ADD:val = work.num1 + work.num2elif work.op == Operation.SUBTRACT:val = work.num1 - work.num2elif work.op == Operation.MULTIPLY:val = work.num1 * work.num2elif work.op == Operation.DIVIDE:if work.num2 == 0:x = InvalidOperation()x.what = work.opx.why = 'Cannot divide by 0'raise xval = work.num1 / work.num2else:x = InvalidOperation()x.what = work.opx.why = 'Invalid operation'raise xlog = SharedStruct()log.key = logidlog.value = '%d' % (val)self.log[logid] = logreturn valdef getStruct(self, key):print 'getStruct(%d)' % (key)return self.log[key]def zip(self):print 'zip()'handler = CalculatorHandler()processor = Calculator.Processor(handler)transport = TSocket.TServerSocket(9090)tfactory = TTransport.TBufferedTransportFactory()pfactory = TBinaryProtocol.TBinaryProtocolFactory()server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)# You could do one of these for a multithreaded server#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)print 'Starting the server...'server.serve()print 'done.'