forked from M-Labs/artiq
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)
This commit is contained in:
parent
e8b9fcf0bb
commit
b5e1bd3fa2
|
@ -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
|
|
|
@ -2,12 +2,12 @@ import struct
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
import numpy
|
import numpy
|
||||||
|
import socket
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from fractions import Fraction
|
from fractions import Fraction
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from artiq.coredevice import exceptions
|
from artiq.coredevice import exceptions
|
||||||
from artiq.coredevice.comm import initialize_connection
|
|
||||||
from artiq import __version__ as software_version
|
from artiq import __version__ as software_version
|
||||||
|
|
||||||
|
|
||||||
|
@ -78,10 +78,11 @@ class CommKernel:
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
|
||||||
def open(self, **kwargs):
|
def open(self):
|
||||||
if hasattr(self, "socket"):
|
if hasattr(self, "socket"):
|
||||||
return
|
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")
|
self.socket.sendall(b"ARTIQ coredev\n")
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|
|
@ -3,8 +3,6 @@ import logging
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
from artiq.coredevice.comm import initialize_connection
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -59,10 +57,11 @@ class CommMgmt:
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
|
||||||
def open(self, **kwargs):
|
def open(self):
|
||||||
if hasattr(self, "socket"):
|
if hasattr(self, "socket"):
|
||||||
return
|
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")
|
self.socket.sendall(b"ARTIQ management\n")
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|
Loading…
Reference in New Issue