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