Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import socket | |
| 6 import struct | |
| 7 | |
| 8 | |
| 9 class RemoteFileConnectionException(Exception): | |
| 10 def __init__(self, *args, **kwargs): | |
| 11 Exception.__init__(self, *args, **kwargs) | |
| 12 | |
| 13 | |
| 14 class RemoteFileConnection(object): | |
| 15 """Client for remote_file_reader server, allowing to read files on an | |
| 16 remote device. | |
| 17 """ | |
| 18 def __init__(self, host, port): | |
| 19 self.host = host | |
| 20 self.port = port | |
| 21 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| 22 self._size_struct = struct.Struct("!i") | |
|
ppi
2015/07/09 13:12:39
is "size_struct" the only private member? Should a
etiennej
2015/07/15 07:13:20
Done.
| |
| 23 | |
| 24 def connect(self): | |
| 25 self.socket.connect((self.host, self.port)) | |
| 26 | |
| 27 def close(self): | |
|
ppi
2015/07/09 13:12:39
if this is a pair for "connect", let's call it "di
qsr
2015/07/09 13:54:18
It has to be called close (and is in fact a pair f
qsr
2015/07/09 14:06:16
Ok, I was mis-remembering, we do not ever close th
etiennej
2015/07/15 07:13:20
Done.
| |
| 28 self.socket.close() | |
| 29 | |
| 30 def open(self, filename): | |
|
ppi
2015/07/09 13:12:39
When the file will be closed on the remote side? I
qsr
2015/07/09 13:54:18
The file will be closed when you disconnect, or wh
etiennej
2015/07/15 07:13:20
Acknowledged.
| |
| 31 self._send("O %s\n" % filename) | |
| 32 result = self._receive(1) | |
| 33 if result != 'O': | |
| 34 raise RemoteFileConnectionException("Unable to open file " + filename) | |
| 35 | |
| 36 def seek(self, pos, mode=0): | |
| 37 self._send("S %d %d\n" % (pos, mode)) | |
| 38 result = self._receive(1) | |
| 39 if result != 'O': | |
| 40 raise RemoteFileConnectionException("Unable to open file.") | |
|
ppi
2015/07/09 13:12:38
Update the exception message here and below.
etiennej
2015/07/15 07:13:20
Done.
| |
| 41 | |
| 42 def read(self, size=0): | |
| 43 assert size > 0 | |
| 44 self._send("R %d\n" % size) | |
| 45 result = self._receive(1) | |
| 46 if result != 'O': | |
| 47 raise RemoteFileConnectionException("Unable to open file.") | |
| 48 read_size = self._size_struct.unpack(self._receive(4))[0] | |
| 49 return self._receive(read_size) | |
| 50 | |
| 51 def _send(self, data): | |
| 52 while len(data) > 0: | |
| 53 sent = self.socket.send(data) | |
| 54 if sent == 0: | |
| 55 raise RemoteFileConnectionException("socket connection broken") | |
|
ppi
2015/07/09 13:12:39
Capitalize and end with a period to match the othe
etiennej
2015/07/15 07:13:20
Done.
| |
| 56 data = data[sent:] | |
| 57 | |
| 58 def _receive(self, length): | |
| 59 result = [] | |
| 60 while length > 0: | |
| 61 chunk = self.socket.recv(length) | |
| 62 if chunk == '': | |
| 63 raise RemoteFileConnectionException("socket connection broken") | |
| 64 result.append(chunk) | |
| 65 length -= len(chunk) | |
| 66 return ''.join(result) | |
| OLD | NEW |