Skip to content

Commit

Permalink
Merge 5943eaa into 714eada
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Sep 24, 2019
2 parents 714eada + 5943eaa commit 8a0d3e0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
12 changes: 7 additions & 5 deletions adb_shell/adb_device.py
Expand Up @@ -39,6 +39,8 @@ class AdbDevice(object):
banner : str, None
The hostname of the machine where the Python interpreter is currently running; if
it is not provided, it will be determined via ``socket.gethostname()``
default_timeout_s : float, None
Default timeout in seconds for TCP packets, or ``None``; see :class:`~adb_shell.tcp_handle.TcpHandle`
Attributes
----------
Expand All @@ -53,7 +55,7 @@ class AdbDevice(object):
"""

def __init__(self, serial, banner=None):
def __init__(self, serial, banner=None, default_timeout_s=None):
if banner and isinstance(banner, str):
self._banner = banner
else:
Expand All @@ -66,7 +68,7 @@ def __init__(self, serial, banner=None):

self._serial = serial

self._handle = TcpHandle(self._serial)
self._handle = TcpHandle(self._serial, default_timeout_s)

@property
def available(self):
Expand Down Expand Up @@ -109,8 +111,8 @@ def connect(self, rsa_keys=None, timeout_s=constants.DEFAULT_TIMEOUT_S, auth_tim
rsa_keys : list, None
A list of signers of type :class:`~adb_shell.auth.sign_cryptography.CryptographySigner`,
:class:`~adb_shell.auth.sign_pycryptodome.PycryptodomeAuthSigner`, or :class:`adb_shell.auth.sign_pythonrsa.PythonRSASigner`
timeout_s : int
Timeout in seconds for TCP packets
timeout_s : float, None
Timeout in seconds for TCP packets, or ``None``
auth_timeout_s : int
TODO
total_timeout_s : int
Expand All @@ -124,7 +126,7 @@ def connect(self, rsa_keys=None, timeout_s=constants.DEFAULT_TIMEOUT_S, auth_tim
"""
# 1. Use the handle to establish a socket connection
self._handle.close()
self._handle.connect(auth_timeout_s)
self._handle.connect(timeout_s)

# 2. Send a ``b'CNXN'`` message
msg = AdbMessage(constants.CNXN, constants.VERSION, constants.MAX_ADB_DATA, b'host::%s\0' % self._banner_bytes)
Expand Down
5 changes: 1 addition & 4 deletions adb_shell/constants.py
Expand Up @@ -54,11 +54,8 @@
#: The size of an ADB message
MESSAGE_SIZE = struct.calcsize(MESSAGE_FORMAT)

# Default timeout (in ms) for :meth:`adb_shell.adb_device.AdbDevice.shell`
DEFAULT_TIMEOUT_MS = 9000

#: Default timeout (in s) for :meth:`adb_shell.tcp_handle.TcpHandle.bulk_read` and :meth:`adb_shell.tcp_handle.TcpHandle.bulk_write`
DEFAULT_TIMEOUT_S = DEFAULT_TIMEOUT_MS / 1000.
DEFAULT_TIMEOUT_S = 1.

#: Default authentication timeout (in s) for :meth:`adb_shell.tcp_handle.TcpHandle.connect`
DEFAULT_AUTH_TIMEOUT_S = 10.
Expand Down
16 changes: 11 additions & 5 deletions adb_shell/tcp_handle.py
Expand Up @@ -14,7 +14,6 @@
import select
import socket

from . import constants
from .exceptions import TcpTimeoutException


Expand All @@ -25,11 +24,15 @@ class TcpHandle(object):
----------
serial : str, bytes, bytearray
Android device serial of the form "host" or "host:port". (Host may be an IP address or a host name.)
default_timeout_s : float, None
Default timeout in seconds for TCP packets, or ``None``
Attributes
----------
_connection : socket.socket, None
A socket connection to the device
_default_timeout_s : float, None
Default timeout in seconds for TCP packets, or ``None``
host : str
The address of the device
port : str
Expand All @@ -38,7 +41,7 @@ class TcpHandle(object):
``<host>:<port>``
"""
def __init__(self, serial):
def __init__(self, serial, default_timeout_s=None):
if ':' in serial:
self.host, port = serial.split(':')
self.port = int(port)
Expand All @@ -47,6 +50,9 @@ def __init__(self, serial):
self.port = 5555

self.serial = '{}:{}'.format(self.host, self.port)

self._default_timeout_s = default_timeout_s

self._connection = None

@property
Expand Down Expand Up @@ -79,7 +85,7 @@ def connect(self, timeout_s=None):
TODO
"""
timeout = constants.DEFAULT_AUTH_TIMEOUT_S if timeout_s is None else timeout_s
timeout = self._default_timeout_s if timeout_s is None else timeout_s
self._connection = socket.create_connection((self.host, self.port), timeout=timeout)
if timeout:
# Put the socket in non-blocking mode
Expand Down Expand Up @@ -107,7 +113,7 @@ def bulk_read(self, numbytes, timeout_s=None):
Reading timed out.
"""
timeout = constants.DEFAULT_TIMEOUT_S if timeout_s is None else timeout_s
timeout = self._default_timeout_s if timeout_s is None else timeout_s
readable, _, _ = select.select([self._connection], [], [], timeout)
if readable:
return self._connection.recv(numbytes)
Expand Down Expand Up @@ -136,7 +142,7 @@ def bulk_write(self, data, timeout_s=None):
Sending data timed out. No data was sent.
"""
timeout = constants.DEFAULT_TIMEOUT_S if timeout_s is None else timeout_s
timeout = self._default_timeout_s if timeout_s is None else timeout_s
_, writeable, _ = select.select([], [self._connection], [], timeout)
if writeable:
return self._connection.send(data)
Expand Down
13 changes: 11 additions & 2 deletions tests/test_tcp_handle.py
Expand Up @@ -6,7 +6,7 @@
from . import patchers


class TestAdbHandle(unittest.TestCase):
class TestTcpHandle(unittest.TestCase):
def setUp(self):
"""Create a ``TcpHandle`` and connect to a TCP service.
Expand All @@ -19,6 +19,15 @@ def tearDown(self):
"""Close the socket connection."""
self.handle.close()

def test_connect_with_timeout(self):
"""TODO
"""
self.handle.close()
with patchers.patch_create_connection:
self.handle.connect(timeout_s=1)
self.assertTrue(True)

def test_bulk_read(self):
"""TODO
Expand Down Expand Up @@ -46,7 +55,7 @@ def test_bulk_write(self):
self.handle.bulk_write(b'FAIL')


class TestAdbHandle2(TestAdbHandle):
class TestTcpHandle2(TestTcpHandle):
def setUp(self):
"""Create a ``TcpHandle`` and connect to a TCP service.
Expand Down

0 comments on commit 8a0d3e0

Please sign in to comment.