| 30 |
ashish |
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 |
|
|
|
22 |
import sys
|
|
|
23 |
sys.path.append('../gen-py')
|
|
|
24 |
|
|
|
25 |
from tutorial import Calculator
|
|
|
26 |
from tutorial.ttypes import *
|
|
|
27 |
|
|
|
28 |
from shared.ttypes import SharedStruct
|
|
|
29 |
|
|
|
30 |
from thrift.transport import TSocket
|
|
|
31 |
from thrift.transport import TTransport
|
|
|
32 |
from thrift.protocol import TBinaryProtocol
|
|
|
33 |
from thrift.server import TServer
|
|
|
34 |
|
|
|
35 |
class CalculatorHandler:
|
|
|
36 |
def __init__(self):
|
|
|
37 |
self.log = {}
|
|
|
38 |
|
|
|
39 |
def ping(self):
|
|
|
40 |
print 'ping()'
|
|
|
41 |
|
|
|
42 |
def add(self, n1, n2):
|
|
|
43 |
print 'add(%d,%d)' % (n1, n2)
|
|
|
44 |
return n1+n2
|
|
|
45 |
|
|
|
46 |
def calculate(self, logid, work):
|
|
|
47 |
print 'calculate(%d, %r)' % (logid, work)
|
|
|
48 |
|
|
|
49 |
if work.op == Operation.ADD:
|
|
|
50 |
val = work.num1 + work.num2
|
|
|
51 |
elif work.op == Operation.SUBTRACT:
|
|
|
52 |
val = work.num1 - work.num2
|
|
|
53 |
elif work.op == Operation.MULTIPLY:
|
|
|
54 |
val = work.num1 * work.num2
|
|
|
55 |
elif work.op == Operation.DIVIDE:
|
|
|
56 |
if work.num2 == 0:
|
|
|
57 |
x = InvalidOperation()
|
|
|
58 |
x.what = work.op
|
|
|
59 |
x.why = 'Cannot divide by 0'
|
|
|
60 |
raise x
|
|
|
61 |
val = work.num1 / work.num2
|
|
|
62 |
else:
|
|
|
63 |
x = InvalidOperation()
|
|
|
64 |
x.what = work.op
|
|
|
65 |
x.why = 'Invalid operation'
|
|
|
66 |
raise x
|
|
|
67 |
|
|
|
68 |
log = SharedStruct()
|
|
|
69 |
log.key = logid
|
|
|
70 |
log.value = '%d' % (val)
|
|
|
71 |
self.log[logid] = log
|
|
|
72 |
|
|
|
73 |
return val
|
|
|
74 |
|
|
|
75 |
def getStruct(self, key):
|
|
|
76 |
print 'getStruct(%d)' % (key)
|
|
|
77 |
return self.log[key]
|
|
|
78 |
|
|
|
79 |
def zip(self):
|
|
|
80 |
print 'zip()'
|
|
|
81 |
|
|
|
82 |
handler = CalculatorHandler()
|
|
|
83 |
processor = Calculator.Processor(handler)
|
|
|
84 |
transport = TSocket.TServerSocket(9090)
|
|
|
85 |
tfactory = TTransport.TBufferedTransportFactory()
|
|
|
86 |
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
|
|
|
87 |
|
|
|
88 |
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
|
|
|
89 |
|
|
|
90 |
# You could do one of these for a multithreaded server
|
|
|
91 |
#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
|
|
|
92 |
#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
|
|
|
93 |
|
|
|
94 |
print 'Starting the server...'
|
|
|
95 |
server.serve()
|
|
|
96 |
print 'done.'
|