1*5e3eaea3SApple OSS Distributionsfrom __future__ import absolute_import 2*5e3eaea3SApple OSS Distributions 3*5e3eaea3SApple OSS Distributionsfrom builtins import bytes 4*5e3eaea3SApple OSS Distributionsfrom builtins import object 5*5e3eaea3SApple OSS Distributions 6*5e3eaea3SApple OSS Distributionsimport logging 7*5e3eaea3SApple OSS Distributionsimport socket 8*5e3eaea3SApple OSS Distributionsimport select 9*5e3eaea3SApple OSS Distributions 10*5e3eaea3SApple OSS Distributionsclass Interface(object): 11*5e3eaea3SApple OSS Distributions """Basic communication interface.""" 12*5e3eaea3SApple OSS Distributions def __init__(self, host_cfg, portnum): 13*5e3eaea3SApple OSS Distributions super(Interface, self).__init__() 14*5e3eaea3SApple OSS Distributions self.host_cfg = host_cfg 15*5e3eaea3SApple OSS Distributions self.portnum = portnum 16*5e3eaea3SApple OSS Distributions self.pkt_size = 8192 17*5e3eaea3SApple OSS Distributions self.socket = None 18*5e3eaea3SApple OSS Distributions self.isblocking = True 19*5e3eaea3SApple OSS Distributions logging.debug("created %s" % str(self)) 20*5e3eaea3SApple OSS Distributions 21*5e3eaea3SApple OSS Distributions def connect(self): 22*5e3eaea3SApple OSS Distributions self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 23*5e3eaea3SApple OSS Distributions self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 24*5e3eaea3SApple OSS Distributions self.socket.bind((self.host_cfg, self.portnum)) 25*5e3eaea3SApple OSS Distributions logging.debug("Initializing network interface for communication host: %s:%d", self.host_cfg, self.portnum) 26*5e3eaea3SApple OSS Distributions self.socket.listen(5) 27*5e3eaea3SApple OSS Distributions num_retries = 3 28*5e3eaea3SApple OSS Distributions while num_retries > 0: 29*5e3eaea3SApple OSS Distributions ra,wa,ea = select.select([self.socket], [], [], 30) 30*5e3eaea3SApple OSS Distributions if not ra: 31*5e3eaea3SApple OSS Distributions num_retries -= 1 32*5e3eaea3SApple OSS Distributions logging.warning("timeout: select returned empty list. retrying..") 33*5e3eaea3SApple OSS Distributions continue 34*5e3eaea3SApple OSS Distributions self.connection, addr = self.socket.accept() 35*5e3eaea3SApple OSS Distributions logging.info("Connected to client from %s" % str(addr)) 36*5e3eaea3SApple OSS Distributions return True 37*5e3eaea3SApple OSS Distributions logging.error("Failed to connect. Exiting after multiple attempts.") 38*5e3eaea3SApple OSS Distributions return False 39*5e3eaea3SApple OSS Distributions 40*5e3eaea3SApple OSS Distributions def read(self): 41*5e3eaea3SApple OSS Distributions if self.isblocking: 42*5e3eaea3SApple OSS Distributions #BUG TODO make this unblocking soon 43*5e3eaea3SApple OSS Distributions #logging.warn("blocking read bug") 44*5e3eaea3SApple OSS Distributions self.connection.settimeout(15) 45*5e3eaea3SApple OSS Distributions self.isblocking = False 46*5e3eaea3SApple OSS Distributions r_bytes = bytes() 47*5e3eaea3SApple OSS Distributions try: 48*5e3eaea3SApple OSS Distributions r_bytes = self.connection.recv(self.pkt_size) 49*5e3eaea3SApple OSS Distributions except Exception as e: 50*5e3eaea3SApple OSS Distributions #logging.debug("Found exception in recv. %s " % (str(e))) 51*5e3eaea3SApple OSS Distributions pass 52*5e3eaea3SApple OSS Distributions 53*5e3eaea3SApple OSS Distributions return r_bytes 54*5e3eaea3SApple OSS Distributions 55*5e3eaea3SApple OSS Distributions def write(self, str): 56*5e3eaea3SApple OSS Distributions if not self.isblocking: 57*5e3eaea3SApple OSS Distributions self.connection.setblocking(1) 58*5e3eaea3SApple OSS Distributions self.isblocking = True 59*5e3eaea3SApple OSS Distributions return self.connection.send(str.encode()) 60*5e3eaea3SApple OSS Distributions 61*5e3eaea3SApple OSS Distributions def close(self): 62*5e3eaea3SApple OSS Distributions if self.connection: 63*5e3eaea3SApple OSS Distributions logging.debug('closing connection.') 64*5e3eaea3SApple OSS Distributions self.connection.close() 65*5e3eaea3SApple OSS Distributions return self.socket 66*5e3eaea3SApple OSS Distributions 67*5e3eaea3SApple OSS Distributions def __str__(self): 68*5e3eaea3SApple OSS Distributions return "interface: %s %d" % (self.host_cfg, self.portnum) 69