Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 1 | """RPC Client module.""" |
| 2 | |
| 3 | import sys |
| 4 | import socket |
| 5 | import pickle |
| 6 | import __builtin__ |
| 7 | import os |
| 8 | |
| 9 | |
| 10 | # Default verbosity (0 = silent, 1 = print connections, 2 = print requests too) |
| 11 | VERBOSE = 1 |
| 12 | |
| 13 | |
| 14 | class Client: |
| 15 | |
| 16 | """RPC Client class. No need to derive a class -- it's fully generic.""" |
| 17 | |
| 18 | def __init__(self, address, verbose = VERBOSE): |
| 19 | if type(address) == type(0): |
| 20 | address = ('', address) |
| 21 | self._address = address |
| 22 | self._verbose = verbose |
| 23 | if self._verbose: print "Connecting to %s ..." % repr(address) |
| 24 | self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 25 | self._socket.connect(address) |
| 26 | if self._verbose: print "Connected." |
| 27 | self._lastid = 0 # Last id for which a reply has been received |
| 28 | self._nextid = 1 # Id of next request |
| 29 | self._replies = {} # Unprocessed replies |
| 30 | self._rf = self._socket.makefile('r') |
| 31 | self._wf = self._socket.makefile('w') |
| 32 | self._methods = self._call('.methods') |
| 33 | |
| 34 | def __del__(self): |
| 35 | self._close() |
| 36 | |
| 37 | def _close(self): |
| 38 | if self._rf: self._rf.close() |
| 39 | self._rf = None |
| 40 | if self._wf: self._wf.close() |
| 41 | self._wf = None |
| 42 | if self._socket: self._socket.close() |
| 43 | self._socket = None |
| 44 | |
| 45 | def __getattr__(self, name): |
| 46 | if name in self._methods: |
| 47 | method = _stub(self, name) |
| 48 | setattr(self, name, method) # XXX circular reference |
| 49 | return method |
| 50 | raise AttributeError, name |
| 51 | |
| 52 | def _setverbose(self, verbose): |
| 53 | self._verbose = verbose |
| 54 | |
| 55 | def _call(self, name, *args): |
| 56 | return self._vcall(name, args) |
| 57 | |
| 58 | def _vcall(self, name, args): |
| 59 | return self._recv(self._vsend(name, args)) |
| 60 | |
| 61 | def _send(self, name, *args): |
| 62 | return self._vsend(name, args) |
| 63 | |
| 64 | def _send_noreply(self, name, *args): |
| 65 | return self._vsend(name, args, 0) |
| 66 | |
| 67 | def _vsend_noreply(self, name, args): |
| 68 | return self._vsend(name, args, 0) |
| 69 | |
| 70 | def _vsend(self, name, args, wantreply = 1): |
| 71 | id = self._nextid |
| 72 | self._nextid = id+1 |
| 73 | if not wantreply: id = -id |
| 74 | request = (name, args, id) |
| 75 | if self._verbose > 1: print "sending request: %s" % repr(request) |
| 76 | wp = pickle.Pickler(self._wf) |
| 77 | wp.dump(request) |
| 78 | return id |
| 79 | |
| 80 | def _recv(self, id): |
| 81 | exception, value, rid = self._vrecv(id) |
| 82 | if rid != id: |
| 83 | raise RuntimeError, "request/reply id mismatch: %d/%d" % (id, rid) |
| 84 | if exception is None: |
| 85 | return value |
| 86 | x = exception |
| 87 | if hasattr(__builtin__, exception): |
| 88 | x = getattr(__builtin__, exception) |
| 89 | elif exception in ('posix.error', 'mac.error'): |
| 90 | x = os.error |
| 91 | if x == exception: |
| 92 | exception = x |
| 93 | raise exception, value |
| 94 | |
| 95 | def _vrecv(self, id): |
| 96 | self._flush() |
| 97 | if self._replies.has_key(id): |
| 98 | if self._verbose > 1: print "retrieving previous reply, id = %d" % id |
| 99 | reply = self._replies[id] |
| 100 | del self._replies[id] |
| 101 | return reply |
| 102 | aid = abs(id) |
| 103 | while 1: |
| 104 | if self._verbose > 1: print "waiting for reply, id = %d" % id |
| 105 | rp = pickle.Unpickler(self._rf) |
| 106 | reply = rp.load() |
| 107 | del rp |
| 108 | if self._verbose > 1: print "got reply: %s" % repr(reply) |
| 109 | rid = reply[2] |
| 110 | arid = abs(rid) |
| 111 | if arid == aid: |
| 112 | if self._verbose > 1: print "got it" |
| 113 | return reply |
| 114 | self._replies[rid] = reply |
| 115 | if arid > aid: |
| 116 | if self._verbose > 1: print "got higher id, assume all ok" |
| 117 | return (None, None, id) |
| 118 | |
| 119 | def _flush(self): |
| 120 | self._wf.flush() |
| 121 | |
| 122 | |
Guido van Rossum | 45babef | 1995-06-21 01:00:17 +0000 | [diff] [blame] | 123 | from security import Security |
| 124 | |
| 125 | |
| 126 | class SecureClient(Client, Security): |
| 127 | |
| 128 | def __init__(self, *args): |
| 129 | import string |
| 130 | apply(Client.__init__, (self,) + args) |
| 131 | Security.__init__(self) |
| 132 | line = self._rf.readline() |
| 133 | challenge = string.atoi(string.strip(firstline)) |
| 134 | response = self._encode_challenge(challenge) |
| 135 | line = `long(response)` |
| 136 | if line[-1] in 'Ll': line = line[:-1] |
| 137 | self._wf.write(line + '\n') |
| 138 | self._wf.flush() |
| 139 | |
Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 140 | class _stub: |
| 141 | |
| 142 | """Helper class for Client -- each instance serves as a method of the client.""" |
| 143 | |
| 144 | def __init__(self, client, name): |
| 145 | self._client = client |
| 146 | self._name = name |
| 147 | |
| 148 | def __call__(self, *args): |
| 149 | return self._client._vcall(self._name, args) |
| 150 | |