| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import cStringIO | 6 import cStringIO |
| 7 import os | 7 import os |
| 8 import pickle | 8 import pickle |
| 9 import socket | 9 import socket |
| 10 import sys | 10 import sys |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 listening_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | 27 listening_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 28 listening_socket.bind(('', port)) | 28 listening_socket.bind(('', port)) |
| 29 listening_socket.listen(1) | 29 listening_socket.listen(1) |
| 30 self._socket, _ = listening_socket.accept() | 30 self._socket, _ = listening_socket.accept() |
| 31 | 31 |
| 32 while self.Connected(): | 32 while self.Connected(): |
| 33 self._HandleRPC() | 33 self._HandleRPC() |
| 34 | 34 |
| 35 def StopSocketServer(self): | 35 def StopSocketServer(self): |
| 36 if self._socket: | 36 if self._socket: |
| 37 self._socket.shutdown(socket.SHUT_RDWR) | 37 try: |
| 38 self._socket.close() | 38 self._socket.shutdown(socket.SHUT_RDWR) |
| 39 self._socket.close() |
| 40 except socket.error: |
| 41 pass |
| 39 self._socket = None | 42 self._socket = None |
| 40 | 43 |
| 41 def Connected(self): | 44 def Connected(self): |
| 42 return self._socket | 45 return self._socket |
| 43 | 46 |
| 44 def CreateTarget(self, target_class): | 47 def CreateTarget(self, target_class): |
| 45 """Creates an instance of the specified class to serve as the RPC target. | 48 """Creates an instance of the specified class to serve as the RPC target. |
| 46 | 49 |
| 47 RPC calls can be made on the target. | 50 RPC calls can be made on the target. |
| 48 """ | 51 """ |
| (...skipping 20 matching lines...) Expand all Loading... |
| 69 sys.stderr = stderr = cStringIO.StringIO() | 72 sys.stderr = stderr = cStringIO.StringIO() |
| 70 | 73 |
| 71 # Make requested method call. | 74 # Make requested method call. |
| 72 result = None | 75 result = None |
| 73 exception = None | 76 exception = None |
| 74 try: | 77 try: |
| 75 if getattr(self, request[0], None): | 78 if getattr(self, request[0], None): |
| 76 result = getattr(self, request[0])(*request[1], **request[2]) | 79 result = getattr(self, request[0])(*request[1], **request[2]) |
| 77 else: | 80 else: |
| 78 result = getattr(self.target, request[0])(*request[1], **request[2]) | 81 result = getattr(self.target, request[0])(*request[1], **request[2]) |
| 79 except BaseException as e: | 82 except BaseException, e: |
| 80 exception = (e.__class__.__name__, str(e)) | 83 exception = (e.__class__.__name__, str(e)) |
| 81 | 84 |
| 82 # Put output back to the way it was before. | 85 # Put output back to the way it was before. |
| 83 sys.stdout = old_stdout | 86 sys.stdout = old_stdout |
| 84 sys.stderr = old_stderr | 87 sys.stderr = old_stderr |
| 85 | 88 |
| 86 # Package up and send the result of the method call. | 89 # Package up and send the result of the method call. |
| 87 response = pickle.dumps((result, stdout.getvalue(), stderr.getvalue(), | 90 response = pickle.dumps((result, stdout.getvalue(), stderr.getvalue(), |
| 88 exception)) | 91 exception)) |
| 89 if self._socket.send(response) != len(response): | 92 if self._socket.send(response) != len(response): |
| 90 self.StopSocketServer() | 93 self.StopSocketServer() |
| OLD | NEW |