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 os.path | |
|
ppi
2015/06/24 15:14:07
This seems not to be used.
etiennej
2015/06/29 15:41:42
Done.
| |
| 6 import socket | |
| 7 import struct | |
| 8 | |
| 9 | |
| 10 class RemoteFileConnectionException(Exception): | |
| 11 def __init__(self, *args, **kwargs): | |
| 12 Exception.__init__(self, *args, **kwargs) | |
| 13 | |
|
ppi
2015/06/24 15:14:07
Two blank lines please.
etiennej
2015/06/29 15:41:42
Done.
| |
| 14 class RemoteFileConnection(object): | |
| 15 """Client for remote_file_reader server, allowing to read files on an | |
| 16 remote device.""" | |
|
ppi
2015/06/24 15:14:07
The closing triple quote should be on the next lin
etiennej
2015/06/29 15:41:42
Done.
| |
| 17 def __init__(self, host, port): | |
| 18 self.host = host | |
| 19 self.port = port | |
| 20 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| 21 self._size_struct = struct.Struct("!i") | |
| 22 | |
| 23 def connect(self): | |
| 24 self.socket.connect((self.host, self.port)) | |
| 25 | |
| 26 def close(self): | |
| 27 self.socket.close() | |
| 28 | |
| 29 def open(self, filename): | |
| 30 self._send("O %s\n" % filename) | |
| 31 result = self._receive(1) | |
| 32 if result != 'O': | |
| 33 raise RemoteFileConnectionException("Unable to open file " + filename) | |
| 34 | |
| 35 def seek(self, pos, mode = 0): | |
|
ppi
2015/06/24 15:14:07
Remove spaces around "=".
etiennej
2015/06/29 15:41:42
Done.
| |
| 36 self._send("S %d %d\n" % (pos, mode)) | |
| 37 result = self._receive(1) | |
| 38 if result != 'O': | |
| 39 raise RemoteFileConnectionException("Unable to open file.") | |
| 40 | |
| 41 def read(self, size=0): | |
| 42 assert size > 0 | |
| 43 self._send("R %d\n" % size) | |
| 44 result = self._receive(1) | |
| 45 if result != 'O': | |
| 46 raise RemoteFileConnectionException("Unable to open file.") | |
| 47 read_size = self._size_struct.unpack(self._receive(4))[0] | |
| 48 return self._receive(read_size) | |
| 49 | |
| 50 def _send(self, data): | |
| 51 while len(data) > 0: | |
| 52 sent = self.socket.send(data) | |
| 53 if sent == 0: | |
| 54 raise RemoteFileConnectionException("socket connection broken") | |
| 55 data = data[sent:] | |
| 56 | |
| 57 def _receive(self, length): | |
| 58 result = [] | |
| 59 while length > 0: | |
| 60 chunk = self.socket.recv(length) | |
| 61 if chunk == '': | |
| 62 raise RemoteFileConnectionException("socket connection broken") | |
| 63 result.append(chunk) | |
| 64 length -= len(chunk) | |
| 65 return ''.join(result) | |
| 66 | |
|
ppi
2015/06/24 15:14:07
Remove the blank line.
etiennej
2015/06/29 15:41:42
Done.
| |
| OLD | NEW |