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