-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmi.py
executable file
·297 lines (232 loc) · 10.3 KB
/
rmi.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from concurrent.futures.thread import ThreadPoolExecutor
from threading import Thread
import message
from synchronized import synchronized
import socket
import inspect
import traceback
import dill
import logging
class Remote():
'''
Marker class
'''
def __init__(self):
super().__init__()
class RemoteObject(Remote):
stubs = dict()
def __init__(self):
super().__init__()
@staticmethod
@synchronized
def addStub(obj, stub):
RemoteObject.stubs[obj] = stub
@staticmethod
@synchronized
def deleteStub(obj):
RemoteObject.stubs.pop(obj)
@staticmethod
@synchronized
def toStub(obj):
if RemoteObject.stubs.get(obj):
return RemoteObject.stubs.get(obj)
else:
raise Exception('no such object exist')
class RMISocketFactory:
'''
An RMISocketFactory instance is used by the RMI runtime in order to obtain client and server sockets for RMI calls. An application may use the setSocketFactory method to request that the RMI runtime use its socket factory instance instead of the default implementation.
You can use the RMISocketFactory class to create a server socket that is bound to a specific address, restricting the origin of requests. For example, the following code implements a socket factory that binds server sockets to an IPv4 loopback address. This restricts RMI to processing requests only from the local host.
# https://docs.oracle.com/javase/8/docs/api/java/rmi/server/RMISocketFactory.html
'''
def create_server_socket(self, port=0):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', port))
sock.listen()
logging.info('server socket created at', sock.getsockname())
host = socket.gethostbyname(socket.gethostname())
port = sock.getsockname()[1]
endpoint = f'{host}:{port}'
return sock, endpoint
def create_client_socket(self, host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
return sock
class UnicastRemoteObject():
'''
Used for exporting a remote object and obtaining a stub that communicates to the remote object. Stubs are either generated at runtime using dynamic proxy objects, or they are generated statically at build time.
# https://docs.oracle.com/javase/8/docs/api/java/rmi/server/UnicastRemoteObject.html
'''
def __init__(self):
super().__init__()
@staticmethod
def export_object(remote_object, port=0, socket_factory=RMISocketFactory()):
'''
Exports the remote object to make it available to receive incoming calls, using a transport specified by the given socket factory.
'''
ref = UnicastServerRef(port, socket_factory, remote_object)
ref.init_server()
proxy = ref.export_object()
return proxy
class Registry:
'''
Registry is a remote interface to a simple remote object registry that provides methods for storing and retrieving remote object references bound with arbitrary string names.
'''
_protocol = 'tcp'
_clients = []
def __init__(self, port=1099):
super().__init__()
self.remote_objects = {}
def bind(self, obj, name):
if (self.remote_objects.get(name)):
raise Exception(f'{name} is already bound')
else:
self.remote_objects[name] = obj
def unbind(self, name):
if(self.remote_objects.get(name)):
self.remote_objects.pop(name)
else:
raise Exception(f'{name} is not currently bound')
def rebind(self, obj, name):
self.remote_objects[name] = obj
def list(self):
return list(self.remote_objects.keys())
def lookup(self, name) -> Remote:
if(self.remote_objects.get(name)):
obj = self.remote_objects.get(name)
return obj
else:
raise Exception(f'{name} is not currently bound')
class LocateRegistry:
'''
LocateRegistry is used to obtain a reference to a bootstrap remote object registry on server, or to create a remote object registry that accepts calls on a specific port.
Note that a getRegistry call does not actually make a connection to the remote host. It simply creates a local reference to the remote registry and will succeed even if no registry is running on the remote host. Therefore, a subsequent method invocation to a remote registry returned as a result of this method may fail.
# https://docs.oracle.com/javase/8/docs/api/java/rmi/registry/LocateRegistry.html
'''
_port = 1099
def __init__(self):
super().__init__()
@staticmethod
def create_registry(port) -> Registry:
'''
LocateRegistry is used to obtain a reference to a bootstrap remote object registry on a particular host (including the local host), or to create a remote object registry that accepts calls on a specific port.
'''
registry = Registry(port)
ref = UnicastServerRef(port, RMISocketFactory(), registry)
ref.init_server()
proxy = ref.export_object()
return proxy
@staticmethod
def get_registry(host, port) -> Registry:
'''
Returns a reference to the the remote object Registry for the local host on the specified port.
'''
ref = UnicastServerRef(port, RMISocketFactory(), Registry())
ref.set_ep(host, port)
proxy = ref.export_object()
return proxy
class UnicastServerRef:
'''
UnicastServerRef implements the remote reference layer server-side behavior for remote objects.
'''
__futures = []
def __init__(self, port: int, socket_factory: RMISocketFactory, remote_object: Remote):
super().__init__()
self.__executor = ThreadPoolExecutor(max_workers=32)
self.__ep = f'localhost:{port}'
self.server_sock = None
self.remote_object = remote_object
self.__port = port
self.socket_factory = socket_factory
def init_server(self):
self.server_sock, self.__ep = self.socket_factory.create_server_socket(self.__port)
Thread(target=self.start_server_socket).start()
def set_ep(self, host: str, port: str|int):
self.__ep = f'{host}:{port}'
self.__port = port
def start_server_socket(self):
'''
Starts server and binds to accept incoming connections. All incoming connections are handled in
a thread pool when new connection accepted.
'''
while True:
conn, addr = self.server_sock.accept()
t = self.__executor.submit(fn=self.new_conn, conn=conn, addr=addr)
# self.__futures.append(t)
# for future in as_completed(self.__futures):
# try:
# print("thread finished, result is ",future.result())
# except TimeoutError():
# print("ConnectTimeout.")
# print(len(self.__futures))
def new_conn(self, conn: socket, addr: socket._RetAddress):
'''
Handles data communication and method calls from client until message data arguments fully received
and method run processed its implementation to prepare return value. Sends back return value as binary
serialization to caller.
'''
logging.debug(conn, addr)
with conn:
logging.debug('Connected by', addr)
data = conn.recv(2048)
msg = dill.loads(data)
if isinstance(msg, message.InvokeMessage):
try:
res = self._run_(msg.method, msg.args)
conn.sendall(dill.dumps(res))
except Exception as e:
logging.exception('exception:', e)
traceback.print_exc()
return
def _run_(self, method_name, args):
'''
Finds method by its name in a remote accessible object and executes it.
:param method_name: name of the method in object
:param args: arguments to pass into method_name
:return: value from original return statement of method_name in remote object
'''
method = getattr(self.remote_object, method_name)
return method(*args)
def export_object(self):
proxy = Proxy(self.__ep, self.remote_object)
return proxy
class Proxy(Remote):
def __init__(self, ep, obj_interface, socket_factory=RMISocketFactory()):
self.ep = ep
unallowed = ['_invoke_', 'ep', '_create_method_']
self.socket_factory = socket_factory
methods = filter(lambda x: x not in unallowed ,get_method_names(obj_interface))
for m_name in methods:
method = self._create_method_(self._invoke_, m_name)
setattr(self, m_name, method)
def _invoke_(self, method, args):
host = self.ep.split(':')[0]
port = int(self.ep.split(':')[1])
with self.socket_factory.create_client_socket(host, port) as client_sock:
msg = message.InvokeMessage(method=method, args=args)
client_sock.sendall(dill.dumps(msg))
data = client_sock.recv(2048)
if data:
obj = dill.loads(data)
return obj
def __getstate__(self):
state = self.__dict__.copy()
# Remove the unpicklable entries.
return state
def _create_method_(self, to_call, m_name):
return lambda *args: to_call(m_name, args)
def get_methods(obj):
'''
Get python methods from a python runtime object.
:param obj: python object
:return: list of all methods in an object
'''
methods = [member for member in [getattr(obj, attr) for attr in dir(obj)] if inspect.ismethod(member)]
return methods
def get_method_names(obj):
'''
Get method names from a python runtime object.
:param obj: python object
:return: list of all method names in an object
'''
method_names = [attr for attr in dir(obj) if (inspect.ismethod(getattr(obj, attr)) and not attr.startswith('__'))]
return method_names