From b5e1bd3fa2169fe88d4d0cdd4eedeb95fa41fa7f Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 23 Dec 2019 19:53:49 +0800 Subject: [PATCH] coredevice: simplify/cleanup network connection code This removes: * host-side keepalive, which turns out not to be required * custom connection timeout (the default is OK) * SSH tunneling support (doesn't seem to be actually used anywhere) --- artiq/coredevice/comm.py | 37 --------------------------------- artiq/coredevice/comm_kernel.py | 7 ++++--- artiq/coredevice/comm_mgmt.py | 7 +++---- 3 files changed, 7 insertions(+), 44 deletions(-) delete mode 100644 artiq/coredevice/comm.py diff --git a/artiq/coredevice/comm.py b/artiq/coredevice/comm.py deleted file mode 100644 index 19a104e54..000000000 --- a/artiq/coredevice/comm.py +++ /dev/null @@ -1,37 +0,0 @@ -import sys -import socket -import logging - - -logger = logging.getLogger(__name__) - - -def set_keepalive(sock, after_idle, interval, max_fails): - if sys.platform.startswith("linux"): - sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) - sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, after_idle) - sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, interval) - sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, max_fails) - elif sys.platform.startswith("win") or sys.platform.startswith("cygwin"): - # setting max_fails is not supported, typically ends up being 5 or 10 - # depending on Windows version - sock.ioctl(socket.SIO_KEEPALIVE_VALS, - (1, after_idle*1000, interval*1000)) - else: - logger.warning("TCP keepalive not supported on platform '%s', ignored", - sys.platform) - - -def initialize_connection(host, port, ssh_transport=None): - if ssh_transport is None: - sock = socket.create_connection((host, port), 5.0) - sock.settimeout(None) - set_keepalive(sock, 3, 2, 3) - logger.debug("connected to %s:%d", host, port) - else: - sock = ssh_transport.open_channel("direct-tcpip", (host, port), - ("localhost", 9999), timeout=5.0) - ssh_transport.set_keepalive(2) - logger.debug("connected to %s:%d via SSH transport to %s:%d", - host, port, *ssh_transport.getpeername()) - return sock diff --git a/artiq/coredevice/comm_kernel.py b/artiq/coredevice/comm_kernel.py index 268d672cb..a4782318c 100644 --- a/artiq/coredevice/comm_kernel.py +++ b/artiq/coredevice/comm_kernel.py @@ -2,12 +2,12 @@ import struct import logging import traceback import numpy +import socket from enum import Enum from fractions import Fraction from collections import namedtuple from artiq.coredevice import exceptions -from artiq.coredevice.comm import initialize_connection from artiq import __version__ as software_version @@ -78,10 +78,11 @@ class CommKernel: self.host = host self.port = port - def open(self, **kwargs): + def open(self): if hasattr(self, "socket"): return - self.socket = initialize_connection(self.host, self.port, **kwargs) + self.socket = socket.create_connection((self.host, self.port)) + logger.debug("connected to %s:%d", self.host, self.port) self.socket.sendall(b"ARTIQ coredev\n") def close(self): diff --git a/artiq/coredevice/comm_mgmt.py b/artiq/coredevice/comm_mgmt.py index 5b078b8da..6468499e0 100644 --- a/artiq/coredevice/comm_mgmt.py +++ b/artiq/coredevice/comm_mgmt.py @@ -3,8 +3,6 @@ import logging import socket import struct -from artiq.coredevice.comm import initialize_connection - logger = logging.getLogger(__name__) @@ -59,10 +57,11 @@ class CommMgmt: self.host = host self.port = port - def open(self, **kwargs): + def open(self): if hasattr(self, "socket"): return - self.socket = initialize_connection(self.host, self.port, **kwargs) + self.socket = socket.create_connection((self.host, self.port)) + logger.debug("connected to %s:%d", self.host, self.port) self.socket.sendall(b"ARTIQ management\n") def close(self):