-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpc.py
162 lines (146 loc) · 4.69 KB
/
rpc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import logging, inspect, sys, traceback, pickle
import messages_pb2 as msg
import guid as Guid
# MethodInfo={
# "NewChunk": (msg.NewChunk_Request, msg.NewChunk_Response),
# "DeleteChunk": (msg.DeleteChunk_Request, msg.DeleteChunk_Response),
# "NewVolume": (msg.NewVolume_Request, msg.NewVolume_Response),
# "AssembleVolume": (msg.AssembleVolume_Request, msg.AssembleVolume_Response),
# "DisassembleVolume":(msg.DisassembleVolume_Request, msg.DisassembleVolume_Response),
# "RepairVolume": (msg.RepairVolume_Request, msg.RepairVolume_Response),
# "ReadVolume": (msg.ReadVolume_Request, msg.ReadVolume_Response),
# "WriteVolume": (msg.WriteVolume_Request, msg.WriteVolume_Response),
# "MoveVolume": (msg.MoveVolume_Request, msg.MoveVolume_Response),
# "ChMod": (msg.ChMod_Request, msg.ChMod_Response),
# "DeleteVolume": (msg.DeleteVolume_Request, msg.DeleteVolume_Response),
# "CreateLinK": (msg.CreateLink_Request, msg.CreateLink_Response),
#}
class ServiceTerminated:
pass
def BuildMethodInfo(theServer):
ret={}
for func in dir(theServer):
if func.startswith('_') or func.endswith('_'):
continue
target=getattr(theServer, func)
if not inspect.ismethod(target):
continue
req=getattr(msg, func+'_Request', None) or getattr(msg, func, type(None))
res=getattr(msg, func+'_Response', type(None))
ret[func]=(req, res)
return ret
def sendRpc(s, guid, token, name, body):
'''Guid token messageName bodySize\n'''
line="%s %u %s %u\n" % (Guid.toStr(guid), token, name, len(body))
s.sendall(line)
out = s.sendall(body)
def recvRpc(s):
fd=s.makefile()
parts=fd.readline().split()
if len(parts)==0:
raise ServiceTerminated()
guid=Guid.fromStr(parts[0])
token=int(parts[1])
name=parts[2]
size=int(parts[3])
body=fd.read(size)
fd.close()
if len(body)!=size:
raise IOError('Invalid rpc message')
return guid,token,name,body
class RpcService:
def __init__(self, Server, MethodInfo=None):
self.theServer=Server
self.peerguid=None
self.cursocket=None
Server.service=self
self.methodInfo=MethodInfo or BuildMethodInfo(Server)
logging.info(self.methodInfo.keys())
def peerGuid(self):
return self.peerguid
def currentSocket(self):
return self.cursocket
def doService(self, socket):
guid,token,name,body=recvRpc(socket)
self.peerguid=guid
self.cursocket=socket
try:
MI=self.methodInfo[name]
argument=MI[0].FromString(body)
method=getattr(self.theServer, name)
rettype=MI[1]
ret=method(argument)
assert type(ret)==rettype
if ret==None: return
body=ret.SerializeToString()
except:
name='Exception'
exception = sys.exc_info()[1]
body=pickle.dumps(exception)
logging.error("exception ", exc_info=1)
sendRpc(socket, guid, token, name, body)
def handler(self, socket, address):
guid=None
try:
while True:
self.doService(socket)
except ServiceTerminated:
pass
finally:
if guid==None: return
if hasattr(self.theServer, '_onConnectionClose_'):
self.theServer._onConnectionClose_()
class RpcStub:
def __init__(self, guid, socket=None, Interface=None, MethodInfo=None):
if Interface==None and MethodInfo==None:
raise ValueError("At least provide an Interface or a MethodInfo")
self.socket=socket
self.guid=guid
self.token=0
if isinstance(MethodInfo, dict): #if it's MethodInfo
self.methodInfo=MethodInfo
else:
self.methodInfo=BuildMethodInfo(Interface)
logging.info(self.methodInfo.keys())
def callMethod(self, name, argument, socket=None):
MI=self.methodInfo[name]
assert type(argument)==MI[0]
body=argument.SerializeToString()
socket = socket or self.socket
sendRpc(socket, self.guid, self.token, name, body)
if MI[1]==type(None): return
guid,token,name_,body_ = recvRpc(socket)
assert guid==self.guid
assert token==self.token
if name_=='Exception':
exception=pickle.loads(body_)
#print type(exception)
raise exception
assert name_==name
self.token=token+1
ret=MI[1].FromString(body_)
return ret
###################### Test ####################################
def test_BuildMethodInfo():
print(MethodInfo)
class mds:
def NewChunk(): pass
def DeleteChunk(): pass
def NewVolume(): pass
def AssembleVolume(): pass
def DisassembleVolume():pass
def RepairVolume(): pass
def ReadVolume(): pass
def WriteVolume(): pass
def MoveVolume(): pass
def ChMod(): pass
def DeleteVolume(): pass
def CreateLink(): pass
mi=BuildMethodInfo(mds)
print(mi)
def test_RpcService():
pass
if __name__=="__main__":
logging.basicConfig(level=logging.INFO)
test_BuildMethodInfo()
test_RpcService()