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