blob: ddce6e93898348954f6e5fe1ded64f79a18f95a1 [file] [log] [blame]
Kurt B. Kaiserb4179362002-07-26 00:06:42 +00001"""RPC Implemention, originally written for the Python Idle IDE
2
3For security reasons, GvR requested that Idle's Python execution server process
4connect to the Idle process, which listens for the connection. Since Idle has
Terry Jan Reedyc30b7b12013-03-11 17:57:08 -04005only one client per server, this was not a limitation.
Kurt B. Kaiserb4179362002-07-26 00:06:42 +00006
7 +---------------------------------+ +-------------+
Alexandre Vassalottice261952008-05-12 02:31:37 +00008 | socketserver.BaseRequestHandler | | SocketIO |
Kurt B. Kaiserb4179362002-07-26 00:06:42 +00009 +---------------------------------+ +-------------+
10 ^ | register() |
11 | | unregister()|
12 | +-------------+
13 | ^ ^
14 | | |
15 | + -------------------+ |
16 | | |
17 +-------------------------+ +-----------------+
18 | RPCHandler | | RPCClient |
19 | [attribute of RPCServer]| | |
20 +-------------------------+ +-----------------+
21
22The RPCServer handler class is expected to provide register/unregister methods.
23RPCHandler inherits the mix-in class SocketIO, which provides these methods.
24
25See the Idle run.main() docstring for further information on how this was
26accomplished in Idle.
27
28"""
29
30import sys
Kurt B. Kaisera00050f2003-05-08 20:26:55 +000031import os
Chui Tey5d2af632002-05-26 13:36:41 +000032import socket
33import select
Alexandre Vassalottice261952008-05-12 02:31:37 +000034import socketserver
Chui Tey5d2af632002-05-26 13:36:41 +000035import struct
Guido van Rossum99603b02007-07-20 00:22:32 +000036import pickle
Chui Tey5d2af632002-05-26 13:36:41 +000037import threading
Alexandre Vassalottif260e442008-05-11 19:59:59 +000038import queue
Chui Tey5d2af632002-05-26 13:36:41 +000039import traceback
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000040import copyreg
Chui Tey5d2af632002-05-26 13:36:41 +000041import types
42import marshal
Andrew Svetlovcd49d532012-03-25 11:43:02 +030043import builtins
Chui Tey5d2af632002-05-26 13:36:41 +000044
Kurt B. Kaisera00050f2003-05-08 20:26:55 +000045
Chui Tey5d2af632002-05-26 13:36:41 +000046def unpickle_code(ms):
47 co = marshal.loads(ms)
48 assert isinstance(co, types.CodeType)
49 return co
50
51def pickle_code(co):
52 assert isinstance(co, types.CodeType)
53 ms = marshal.dumps(co)
54 return unpickle_code, (ms,)
55
Kurt B. Kaiseradc63842002-08-25 14:08:07 +000056# XXX KBK 24Aug02 function pickling capability not used in Idle
57# def unpickle_function(ms):
58# return ms
Chui Tey5d2af632002-05-26 13:36:41 +000059
Kurt B. Kaiseradc63842002-08-25 14:08:07 +000060# def pickle_function(fn):
61# assert isinstance(fn, type.FunctionType)
Walter Dörwald70a6b492004-02-12 17:35:32 +000062# return repr(fn)
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000063
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000064copyreg.pickle(types.CodeType, pickle_code, unpickle_code)
65# copyreg.pickle(types.FunctionType, pickle_function, unpickle_function)
Chui Tey5d2af632002-05-26 13:36:41 +000066
67BUFSIZE = 8*1024
Kurt B. Kaiser24d7e0c2003-06-05 23:51:29 +000068LOCALHOST = '127.0.0.1'
Chui Tey5d2af632002-05-26 13:36:41 +000069
Alexandre Vassalottice261952008-05-12 02:31:37 +000070class RPCServer(socketserver.TCPServer):
Chui Tey5d2af632002-05-26 13:36:41 +000071
72 def __init__(self, addr, handlerclass=None):
73 if handlerclass is None:
74 handlerclass = RPCHandler
Alexandre Vassalottice261952008-05-12 02:31:37 +000075 socketserver.TCPServer.__init__(self, addr, handlerclass)
Chui Tey5d2af632002-05-26 13:36:41 +000076
Kurt B. Kaiserb4179362002-07-26 00:06:42 +000077 def server_bind(self):
78 "Override TCPServer method, no bind() phase for connecting entity"
79 pass
Chui Tey5d2af632002-05-26 13:36:41 +000080
Kurt B. Kaiserb4179362002-07-26 00:06:42 +000081 def server_activate(self):
82 """Override TCPServer method, connect() instead of listen()
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000083
Kurt B. Kaiserb4179362002-07-26 00:06:42 +000084 Due to the reversed connection, self.server_address is actually the
85 address of the Idle Client to which we are connecting.
Chui Tey5d2af632002-05-26 13:36:41 +000086
Kurt B. Kaiserb4179362002-07-26 00:06:42 +000087 """
88 self.socket.connect(self.server_address)
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000089
Kurt B. Kaiserb4179362002-07-26 00:06:42 +000090 def get_request(self):
91 "Override TCPServer method, return already connected socket"
92 return self.socket, self.server_address
Chui Tey5d2af632002-05-26 13:36:41 +000093
Kurt B. Kaiser003091c2003-02-17 18:57:16 +000094 def handle_error(self, request, client_address):
Kurt B. Kaisere51529d2003-03-22 19:15:58 +000095 """Override TCPServer method
96
97 Error message goes to __stderr__. No error message if exiting
98 normally or socket raised EOF. Other exceptions not handled in
99 server code will cause os._exit.
100
101 """
Kurt B. Kaiser003091c2003-02-17 18:57:16 +0000102 try:
103 raise
104 except SystemExit:
105 raise
Kurt B. Kaisere51529d2003-03-22 19:15:58 +0000106 except:
Kurt B. Kaiser05293772003-03-22 20:11:14 +0000107 erf = sys.__stderr__
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000108 print('\n' + '-'*40, file=erf)
109 print('Unhandled server exception!', file=erf)
Amaury Forgeot d'Arcbed17102008-11-29 01:48:47 +0000110 print('Thread: %s' % threading.current_thread().name, file=erf)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000111 print('Client Address: ', client_address, file=erf)
112 print('Request: ', repr(request), file=erf)
Kurt B. Kaisere51529d2003-03-22 19:15:58 +0000113 traceback.print_exc(file=erf)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000114 print('\n*** Unrecoverable, server exiting!', file=erf)
115 print('-'*40, file=erf)
Kurt B. Kaiser05293772003-03-22 20:11:14 +0000116 os._exit(0)
Kurt B. Kaiser003091c2003-02-17 18:57:16 +0000117
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000118#----------------- end class RPCServer --------------------
Kurt B. Kaiser003091c2003-02-17 18:57:16 +0000119
Chui Tey5d2af632002-05-26 13:36:41 +0000120objecttable = {}
Alexandre Vassalottif260e442008-05-11 19:59:59 +0000121request_queue = queue.Queue(0)
122response_queue = queue.Queue(0)
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000123
Chui Tey5d2af632002-05-26 13:36:41 +0000124
Kurt B. Kaiserdcba6622004-12-21 22:10:32 +0000125class SocketIO(object):
Chui Tey5d2af632002-05-26 13:36:41 +0000126
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000127 nextseq = 0
128
Chui Tey5d2af632002-05-26 13:36:41 +0000129 def __init__(self, sock, objtable=None, debugging=None):
Benjamin Petersonb03ca4b2008-06-13 02:00:47 +0000130 self.sockthread = threading.current_thread()
Chui Tey5d2af632002-05-26 13:36:41 +0000131 if debugging is not None:
132 self.debugging = debugging
133 self.sock = sock
134 if objtable is None:
135 objtable = objecttable
136 self.objtable = objtable
Chui Tey5d2af632002-05-26 13:36:41 +0000137 self.responses = {}
138 self.cvars = {}
139
140 def close(self):
141 sock = self.sock
142 self.sock = None
143 if sock is not None:
144 sock.close()
145
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000146 def exithook(self):
147 "override for specific exit action"
Roger Serwy71c9e1a2013-03-31 01:00:15 -0500148 os._exit(0)
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000149
Chui Tey5d2af632002-05-26 13:36:41 +0000150 def debug(self, *args):
151 if not self.debugging:
152 return
Amaury Forgeot d'Arcbed17102008-11-29 01:48:47 +0000153 s = self.location + " " + str(threading.current_thread().name)
Chui Tey5d2af632002-05-26 13:36:41 +0000154 for a in args:
155 s = s + " " + str(a)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000156 print(s, file=sys.__stderr__)
Chui Tey5d2af632002-05-26 13:36:41 +0000157
158 def register(self, oid, object):
159 self.objtable[oid] = object
160
161 def unregister(self, oid):
162 try:
163 del self.objtable[oid]
164 except KeyError:
165 pass
166
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000167 def localcall(self, seq, request):
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +0000168 self.debug("localcall:", request)
Chui Tey5d2af632002-05-26 13:36:41 +0000169 try:
170 how, (oid, methodname, args, kwargs) = request
171 except TypeError:
172 return ("ERROR", "Bad request format")
Guido van Rossum811c4e02006-08-22 15:45:46 +0000173 if oid not in self.objtable:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000174 return ("ERROR", "Unknown object id: %r" % (oid,))
Chui Tey5d2af632002-05-26 13:36:41 +0000175 obj = self.objtable[oid]
176 if methodname == "__methods__":
177 methods = {}
178 _getmethods(obj, methods)
179 return ("OK", methods)
180 if methodname == "__attributes__":
181 attributes = {}
182 _getattributes(obj, attributes)
183 return ("OK", attributes)
184 if not hasattr(obj, methodname):
Walter Dörwald70a6b492004-02-12 17:35:32 +0000185 return ("ERROR", "Unsupported method name: %r" % (methodname,))
Chui Tey5d2af632002-05-26 13:36:41 +0000186 method = getattr(obj, methodname)
187 try:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000188 if how == 'CALL':
189 ret = method(*args, **kwargs)
190 if isinstance(ret, RemoteObject):
191 ret = remoteref(ret)
192 return ("OK", ret)
193 elif how == 'QUEUE':
194 request_queue.put((seq, (method, args, kwargs)))
195 return("QUEUED", None)
196 else:
197 return ("ERROR", "Unsupported message type: %s" % how)
Kurt B. Kaiser003091c2003-02-17 18:57:16 +0000198 except SystemExit:
199 raise
Andrew Svetlov05bab932012-03-14 13:22:12 -0700200 except KeyboardInterrupt:
201 raise
Kurt B. Kaiser9ac783d2003-03-10 20:42:24 +0000202 except socket.error:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000203 raise
Andrew Svetlov05bab932012-03-14 13:22:12 -0700204 except Exception as ex:
205 return ("CALLEXC", ex)
Chui Tey5d2af632002-05-26 13:36:41 +0000206 except:
Kurt B. Kaisere852c192004-12-23 04:39:55 +0000207 msg = "*** Internal Error: rpc.py:SocketIO.localcall()\n\n"\
208 " Object: %s \n Method: %s \n Args: %s\n"
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000209 print(msg % (oid, method, args), file=sys.__stderr__)
Kurt B. Kaiser86bc4642003-02-27 23:04:17 +0000210 traceback.print_exc(file=sys.__stderr__)
Kurt B. Kaiser8cd0def2003-01-31 05:06:43 +0000211 return ("EXCEPTION", None)
212
Chui Tey5d2af632002-05-26 13:36:41 +0000213 def remotecall(self, oid, methodname, args, kwargs):
Kurt B. Kaiser003091c2003-02-17 18:57:16 +0000214 self.debug("remotecall:asynccall: ", oid, methodname)
Chui Tey5d2af632002-05-26 13:36:41 +0000215 seq = self.asynccall(oid, methodname, args, kwargs)
Kurt B. Kaiser0930c432002-12-06 21:45:24 +0000216 return self.asyncreturn(seq)
Chui Tey5d2af632002-05-26 13:36:41 +0000217
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000218 def remotequeue(self, oid, methodname, args, kwargs):
219 self.debug("remotequeue:asyncqueue: ", oid, methodname)
220 seq = self.asyncqueue(oid, methodname, args, kwargs)
221 return self.asyncreturn(seq)
222
Chui Tey5d2af632002-05-26 13:36:41 +0000223 def asynccall(self, oid, methodname, args, kwargs):
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000224 request = ("CALL", (oid, methodname, args, kwargs))
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000225 seq = self.newseq()
Benjamin Petersonb03ca4b2008-06-13 02:00:47 +0000226 if threading.current_thread() != self.sockthread:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000227 cvar = threading.Condition()
228 self.cvars[seq] = cvar
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000229 self.debug(("asynccall:%d:" % seq), oid, methodname, args, kwargs)
230 self.putmessage((seq, request))
Chui Tey5d2af632002-05-26 13:36:41 +0000231 return seq
232
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000233 def asyncqueue(self, oid, methodname, args, kwargs):
234 request = ("QUEUE", (oid, methodname, args, kwargs))
235 seq = self.newseq()
Benjamin Petersonb03ca4b2008-06-13 02:00:47 +0000236 if threading.current_thread() != self.sockthread:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000237 cvar = threading.Condition()
238 self.cvars[seq] = cvar
239 self.debug(("asyncqueue:%d:" % seq), oid, methodname, args, kwargs)
240 self.putmessage((seq, request))
241 return seq
242
Chui Tey5d2af632002-05-26 13:36:41 +0000243 def asyncreturn(self, seq):
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000244 self.debug("asyncreturn:%d:call getresponse(): " % seq)
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000245 response = self.getresponse(seq, wait=0.05)
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000246 self.debug(("asyncreturn:%d:response: " % seq), response)
Chui Tey5d2af632002-05-26 13:36:41 +0000247 return self.decoderesponse(response)
248
249 def decoderesponse(self, response):
250 how, what = response
251 if how == "OK":
252 return what
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000253 if how == "QUEUED":
254 return None
Chui Tey5d2af632002-05-26 13:36:41 +0000255 if how == "EXCEPTION":
Kurt B. Kaiser003091c2003-02-17 18:57:16 +0000256 self.debug("decoderesponse: EXCEPTION")
257 return None
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000258 if how == "EOF":
259 self.debug("decoderesponse: EOF")
260 self.decode_interrupthook()
261 return None
Chui Tey5d2af632002-05-26 13:36:41 +0000262 if how == "ERROR":
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +0000263 self.debug("decoderesponse: Internal ERROR:", what)
Kurt B. Kaiserad667422007-08-23 01:06:15 +0000264 raise RuntimeError(what)
Andrew Svetlov05bab932012-03-14 13:22:12 -0700265 if how == "CALLEXC":
266 self.debug("decoderesponse: Call Exception:", what)
267 raise what
Kurt B. Kaiserad667422007-08-23 01:06:15 +0000268 raise SystemError(how, what)
Chui Tey5d2af632002-05-26 13:36:41 +0000269
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000270 def decode_interrupthook(self):
271 ""
272 raise EOFError
273
Chui Tey5d2af632002-05-26 13:36:41 +0000274 def mainloop(self):
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000275 """Listen on socket until I/O not ready or EOF
276
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000277 pollresponse() will loop looking for seq number None, which
Kurt B. Kaiser94afd302003-03-12 20:52:00 +0000278 never comes, and exit on EOFError.
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000279
280 """
Chui Tey5d2af632002-05-26 13:36:41 +0000281 try:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000282 self.getresponse(myseq=None, wait=0.05)
Chui Tey5d2af632002-05-26 13:36:41 +0000283 except EOFError:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000284 self.debug("mainloop:return")
285 return
Chui Tey5d2af632002-05-26 13:36:41 +0000286
Kurt B. Kaiser94afd302003-03-12 20:52:00 +0000287 def getresponse(self, myseq, wait):
288 response = self._getresponse(myseq, wait)
Chui Tey5d2af632002-05-26 13:36:41 +0000289 if response is not None:
290 how, what = response
291 if how == "OK":
292 response = how, self._proxify(what)
293 return response
294
295 def _proxify(self, obj):
296 if isinstance(obj, RemoteProxy):
297 return RPCProxy(self, obj.oid)
Guido van Rossum13257902007-06-07 23:15:56 +0000298 if isinstance(obj, list):
Kurt B. Kaiser66aaf742007-08-09 18:00:23 +0000299 return list(map(self._proxify, obj))
Chui Tey5d2af632002-05-26 13:36:41 +0000300 # XXX Check for other types -- not currently needed
301 return obj
302
Kurt B. Kaiser94afd302003-03-12 20:52:00 +0000303 def _getresponse(self, myseq, wait):
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000304 self.debug("_getresponse:myseq:", myseq)
Benjamin Petersonb03ca4b2008-06-13 02:00:47 +0000305 if threading.current_thread() is self.sockthread:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000306 # this thread does all reading of requests or responses
Chui Tey5d2af632002-05-26 13:36:41 +0000307 while 1:
Kurt B. Kaiser94afd302003-03-12 20:52:00 +0000308 response = self.pollresponse(myseq, wait)
Chui Tey5d2af632002-05-26 13:36:41 +0000309 if response is not None:
310 return response
311 else:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000312 # wait for notification from socket handling thread
313 cvar = self.cvars[myseq]
314 cvar.acquire()
Guido van Rossum811c4e02006-08-22 15:45:46 +0000315 while myseq not in self.responses:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000316 cvar.wait()
Chui Tey5d2af632002-05-26 13:36:41 +0000317 response = self.responses[myseq]
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000318 self.debug("_getresponse:%s: thread woke up: response: %s" %
319 (myseq, response))
Chui Tey5d2af632002-05-26 13:36:41 +0000320 del self.responses[myseq]
321 del self.cvars[myseq]
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000322 cvar.release()
Kurt B. Kaiser003091c2003-02-17 18:57:16 +0000323 return response
Chui Tey5d2af632002-05-26 13:36:41 +0000324
325 def newseq(self):
326 self.nextseq = seq = self.nextseq + 2
327 return seq
328
329 def putmessage(self, message):
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000330 self.debug("putmessage:%d:" % message[0])
Chui Tey5d2af632002-05-26 13:36:41 +0000331 try:
332 s = pickle.dumps(message)
Kurt B. Kaiserd6ab77d2004-01-21 19:21:11 +0000333 except pickle.PicklingError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000334 print("Cannot pickle:", repr(message), file=sys.__stderr__)
Chui Tey5d2af632002-05-26 13:36:41 +0000335 raise
336 s = struct.pack("<i", len(s)) + s
337 while len(s) > 0:
Kurt B. Kaiser003091c2003-02-17 18:57:16 +0000338 try:
Kurt B. Kaiserd6ab77d2004-01-21 19:21:11 +0000339 r, w, x = select.select([], [self.sock], [])
340 n = self.sock.send(s[:BUFSIZE])
Kurt B. Kaiser935ea9a2005-05-10 03:44:24 +0000341 except (AttributeError, TypeError):
Terry Jan Reedyba6c0d32013-06-08 00:22:45 -0400342 raise OSError("socket no longer exists")
Kurt B. Kaiser935ea9a2005-05-10 03:44:24 +0000343 except socket.error:
344 raise
Kurt B. Kaiser003091c2003-02-17 18:57:16 +0000345 else:
346 s = s[n:]
Chui Tey5d2af632002-05-26 13:36:41 +0000347
Kurt B. Kaiser2d726df2007-08-22 21:33:27 +0000348 buff = b''
Chui Tey5d2af632002-05-26 13:36:41 +0000349 bufneed = 4
350 bufstate = 0 # meaning: 0 => reading count; 1 => reading data
351
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000352 def pollpacket(self, wait):
Chui Tey5d2af632002-05-26 13:36:41 +0000353 self._stage0()
Kurt B. Kaiser2d726df2007-08-22 21:33:27 +0000354 if len(self.buff) < self.bufneed:
Kurt B. Kaiserd6ab77d2004-01-21 19:21:11 +0000355 r, w, x = select.select([self.sock.fileno()], [], [], wait)
356 if len(r) == 0:
Chui Tey5d2af632002-05-26 13:36:41 +0000357 return None
358 try:
359 s = self.sock.recv(BUFSIZE)
360 except socket.error:
361 raise EOFError
362 if len(s) == 0:
363 raise EOFError
Kurt B. Kaiser2d726df2007-08-22 21:33:27 +0000364 self.buff += s
Chui Tey5d2af632002-05-26 13:36:41 +0000365 self._stage0()
366 return self._stage1()
367
368 def _stage0(self):
Kurt B. Kaiser2d726df2007-08-22 21:33:27 +0000369 if self.bufstate == 0 and len(self.buff) >= 4:
370 s = self.buff[:4]
371 self.buff = self.buff[4:]
Chui Tey5d2af632002-05-26 13:36:41 +0000372 self.bufneed = struct.unpack("<i", s)[0]
373 self.bufstate = 1
374
375 def _stage1(self):
Kurt B. Kaiser2d726df2007-08-22 21:33:27 +0000376 if self.bufstate == 1 and len(self.buff) >= self.bufneed:
377 packet = self.buff[:self.bufneed]
378 self.buff = self.buff[self.bufneed:]
Chui Tey5d2af632002-05-26 13:36:41 +0000379 self.bufneed = 4
380 self.bufstate = 0
381 return packet
382
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000383 def pollmessage(self, wait):
Chui Tey5d2af632002-05-26 13:36:41 +0000384 packet = self.pollpacket(wait)
385 if packet is None:
386 return None
387 try:
388 message = pickle.loads(packet)
Kurt B. Kaiserd6ab77d2004-01-21 19:21:11 +0000389 except pickle.UnpicklingError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000390 print("-----------------------", file=sys.__stderr__)
391 print("cannot unpickle packet:", repr(packet), file=sys.__stderr__)
Chui Tey5d2af632002-05-26 13:36:41 +0000392 traceback.print_stack(file=sys.__stderr__)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000393 print("-----------------------", file=sys.__stderr__)
Chui Tey5d2af632002-05-26 13:36:41 +0000394 raise
395 return message
396
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000397 def pollresponse(self, myseq, wait):
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000398 """Handle messages received on the socket.
399
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000400 Some messages received may be asynchronous 'call' or 'queue' requests,
401 and some may be responses for other threads.
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000402
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000403 'call' requests are passed to self.localcall() with the expectation of
404 immediate execution, during which time the socket is not serviced.
405
406 'queue' requests are used for tasks (which may block or hang) to be
407 processed in a different thread. These requests are fed into
408 request_queue by self.localcall(). Responses to queued requests are
409 taken from response_queue and sent across the link with the associated
410 sequence numbers. Messages in the queues are (sequence_number,
411 request/response) tuples and code using this module removing messages
412 from the request_queue is responsible for returning the correct
413 sequence number in the response_queue.
414
415 pollresponse() will loop until a response message with the myseq
416 sequence number is received, and will save other responses in
417 self.responses and notify the owning thread.
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000418
419 """
Chui Tey5d2af632002-05-26 13:36:41 +0000420 while 1:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000421 # send queued response if there is one available
422 try:
423 qmsg = response_queue.get(0)
Alexandre Vassalottif260e442008-05-11 19:59:59 +0000424 except queue.Empty:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000425 pass
426 else:
427 seq, response = qmsg
428 message = (seq, ('OK', response))
429 self.putmessage(message)
430 # poll for message on link
431 try:
432 message = self.pollmessage(wait)
433 if message is None: # socket not ready
434 return None
435 except EOFError:
436 self.handle_EOF()
Chui Tey5d2af632002-05-26 13:36:41 +0000437 return None
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000438 except AttributeError:
439 return None
Chui Tey5d2af632002-05-26 13:36:41 +0000440 seq, resq = message
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000441 how = resq[0]
Kurt B. Kaiser0a0e6c32003-01-25 03:26:35 +0000442 self.debug("pollresponse:%d:myseq:%s" % (seq, myseq))
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000443 # process or queue a request
444 if how in ("CALL", "QUEUE"):
Kurt B. Kaiserbc286132003-01-25 21:33:40 +0000445 self.debug("pollresponse:%d:localcall:call:" % seq)
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000446 response = self.localcall(seq, resq)
Kurt B. Kaiserbc286132003-01-25 21:33:40 +0000447 self.debug("pollresponse:%d:localcall:response:%s"
448 % (seq, response))
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000449 if how == "CALL":
450 self.putmessage((seq, response))
451 elif how == "QUEUE":
452 # don't acknowledge the 'queue' request!
453 pass
Chui Tey5d2af632002-05-26 13:36:41 +0000454 continue
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000455 # return if completed message transaction
Chui Tey5d2af632002-05-26 13:36:41 +0000456 elif seq == myseq:
457 return resq
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000458 # must be a response for a different thread:
Chui Tey5d2af632002-05-26 13:36:41 +0000459 else:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000460 cv = self.cvars.get(seq, None)
Kurt B. Kaiser003091c2003-02-17 18:57:16 +0000461 # response involving unknown sequence number is discarded,
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000462 # probably intended for prior incarnation of server
Chui Tey5d2af632002-05-26 13:36:41 +0000463 if cv is not None:
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000464 cv.acquire()
Kurt B. Kaiser003091c2003-02-17 18:57:16 +0000465 self.responses[seq] = resq
Chui Tey5d2af632002-05-26 13:36:41 +0000466 cv.notify()
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000467 cv.release()
Chui Tey5d2af632002-05-26 13:36:41 +0000468 continue
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +0000469
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000470 def handle_EOF(self):
471 "action taken upon link being closed by peer"
472 self.EOFhook()
473 self.debug("handle_EOF")
474 for key in self.cvars:
475 cv = self.cvars[key]
476 cv.acquire()
477 self.responses[key] = ('EOF', None)
478 cv.notify()
479 cv.release()
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000480 # call our (possibly overridden) exit function
481 self.exithook()
482
483 def EOFhook(self):
484 "Classes using rpc client/server can override to augment EOF action"
485 pass
486
Kurt B. Kaiserb4179362002-07-26 00:06:42 +0000487#----------------- end class SocketIO --------------------
Chui Tey5d2af632002-05-26 13:36:41 +0000488
Kurt B. Kaiserdcba6622004-12-21 22:10:32 +0000489class RemoteObject(object):
Chui Tey5d2af632002-05-26 13:36:41 +0000490 # Token mix-in class
491 pass
492
493def remoteref(obj):
494 oid = id(obj)
495 objecttable[oid] = obj
496 return RemoteProxy(oid)
497
Kurt B. Kaiserdcba6622004-12-21 22:10:32 +0000498class RemoteProxy(object):
Chui Tey5d2af632002-05-26 13:36:41 +0000499
500 def __init__(self, oid):
501 self.oid = oid
502
Alexandre Vassalottice261952008-05-12 02:31:37 +0000503class RPCHandler(socketserver.BaseRequestHandler, SocketIO):
Chui Tey5d2af632002-05-26 13:36:41 +0000504
Kurt B. Kaiser0930c432002-12-06 21:45:24 +0000505 debugging = False
506 location = "#S" # Server
Chui Tey5d2af632002-05-26 13:36:41 +0000507
508 def __init__(self, sock, addr, svr):
509 svr.current_handler = self ## cgt xxx
510 SocketIO.__init__(self, sock)
Alexandre Vassalottice261952008-05-12 02:31:37 +0000511 socketserver.BaseRequestHandler.__init__(self, sock, addr, svr)
Chui Tey5d2af632002-05-26 13:36:41 +0000512
Chui Tey5d2af632002-05-26 13:36:41 +0000513 def handle(self):
Alexandre Vassalottice261952008-05-12 02:31:37 +0000514 "handle() method required by socketserver"
Chui Tey5d2af632002-05-26 13:36:41 +0000515 self.mainloop()
516
517 def get_remote_proxy(self, oid):
518 return RPCProxy(self, oid)
519
520class RPCClient(SocketIO):
521
Kurt B. Kaiser0930c432002-12-06 21:45:24 +0000522 debugging = False
523 location = "#C" # Client
524
Kurt B. Kaiserb4179362002-07-26 00:06:42 +0000525 nextseq = 1 # Requests coming from the client are odd numbered
Chui Tey5d2af632002-05-26 13:36:41 +0000526
527 def __init__(self, address, family=socket.AF_INET, type=socket.SOCK_STREAM):
Kurt B. Kaiseradc63842002-08-25 14:08:07 +0000528 self.listening_sock = socket.socket(family, type)
Kurt B. Kaiseradc63842002-08-25 14:08:07 +0000529 self.listening_sock.bind(address)
530 self.listening_sock.listen(1)
Kurt B. Kaiserb4179362002-07-26 00:06:42 +0000531
532 def accept(self):
Kurt B. Kaiseradc63842002-08-25 14:08:07 +0000533 working_sock, address = self.listening_sock.accept()
Kurt B. Kaiser74d93c82002-12-23 22:51:03 +0000534 if self.debugging:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000535 print("****** Connection request from ", address, file=sys.__stderr__)
Kurt B. Kaiser24d7e0c2003-06-05 23:51:29 +0000536 if address[0] == LOCALHOST:
Kurt B. Kaiseradc63842002-08-25 14:08:07 +0000537 SocketIO.__init__(self, working_sock)
Kurt B. Kaiserb4179362002-07-26 00:06:42 +0000538 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000539 print("** Invalid host: ", address, file=sys.__stderr__)
Kurt B. Kaiserb4179362002-07-26 00:06:42 +0000540 raise socket.error
Chui Tey5d2af632002-05-26 13:36:41 +0000541
542 def get_remote_proxy(self, oid):
543 return RPCProxy(self, oid)
544
Kurt B. Kaiserdcba6622004-12-21 22:10:32 +0000545class RPCProxy(object):
Chui Tey5d2af632002-05-26 13:36:41 +0000546
547 __methods = None
548 __attributes = None
549
550 def __init__(self, sockio, oid):
551 self.sockio = sockio
552 self.oid = oid
553
554 def __getattr__(self, name):
555 if self.__methods is None:
556 self.__getmethods()
557 if self.__methods.get(name):
558 return MethodProxy(self.sockio, self.oid, name)
559 if self.__attributes is None:
560 self.__getattributes()
Guido van Rossum811c4e02006-08-22 15:45:46 +0000561 if name in self.__attributes:
Kurt B. Kaiserdcba6622004-12-21 22:10:32 +0000562 value = self.sockio.remotecall(self.oid, '__getattribute__',
563 (name,), {})
564 return value
565 else:
Kurt B. Kaiserad667422007-08-23 01:06:15 +0000566 raise AttributeError(name)
Kurt B. Kaisera00050f2003-05-08 20:26:55 +0000567
Chui Tey5d2af632002-05-26 13:36:41 +0000568 def __getattributes(self):
569 self.__attributes = self.sockio.remotecall(self.oid,
570 "__attributes__", (), {})
571
572 def __getmethods(self):
573 self.__methods = self.sockio.remotecall(self.oid,
574 "__methods__", (), {})
575
576def _getmethods(obj, methods):
577 # Helper to get a list of methods from an object
578 # Adds names to dictionary argument 'methods'
579 for name in dir(obj):
580 attr = getattr(obj, name)
Florent Xicluna5d1155c2011-10-28 14:45:05 +0200581 if callable(attr):
Chui Tey5d2af632002-05-26 13:36:41 +0000582 methods[name] = 1
Guido van Rossum13257902007-06-07 23:15:56 +0000583 if isinstance(obj, type):
Chui Tey5d2af632002-05-26 13:36:41 +0000584 for super in obj.__bases__:
585 _getmethods(super, methods)
586
587def _getattributes(obj, attributes):
588 for name in dir(obj):
589 attr = getattr(obj, name)
Florent Xicluna5d1155c2011-10-28 14:45:05 +0200590 if not callable(attr):
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +0000591 attributes[name] = 1
Chui Tey5d2af632002-05-26 13:36:41 +0000592
Kurt B. Kaiserdcba6622004-12-21 22:10:32 +0000593class MethodProxy(object):
Chui Tey5d2af632002-05-26 13:36:41 +0000594
595 def __init__(self, sockio, oid, name):
596 self.sockio = sockio
597 self.oid = oid
598 self.name = name
599
600 def __call__(self, *args, **kwargs):
601 value = self.sockio.remotecall(self.oid, self.name, args, kwargs)
602 return value
603
Chui Tey5d2af632002-05-26 13:36:41 +0000604
Kurt B. Kaiser62685d32003-09-10 02:42:18 +0000605# XXX KBK 09Sep03 We need a proper unit test for this module. Previously
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000606# existing test code was removed at Rev 1.27 (r34098).
Andrew Svetlovcd49d532012-03-25 11:43:02 +0300607
608def displayhook(value):
609 """Override standard display hook to use non-locale encoding"""
610 if value is None:
611 return
612 # Set '_' to None to avoid recursion
613 builtins._ = None
614 text = repr(value)
615 try:
616 sys.stdout.write(text)
617 except UnicodeEncodeError:
618 # let's use ascii while utf8-bmp codec doesn't present
619 encoding = 'ascii'
620 bytes = text.encode(encoding, 'backslashreplace')
621 text = bytes.decode(encoding, 'strict')
622 sys.stdout.write(text)
623 sys.stdout.write("\n")
624 builtins._ = value