def __init__(self, conf, frmk_id, frmk_ip, frmk_port): self._framework_id = frmk_id #conf.get("control-framework", "id") self._framework_ip = frmk_ip#conf.get("control-framework", "ip") self._framework_port = frmk_port #conf.get("control-framework", "port") self._framework_ping = True self.__keep_processing = True # instatiate the control manager self.__controlmanager = ControlManager(conf) self.__alive = False self.__pingThread = None self.__reciverControlMsgThread = None self.__event = threading.Event()
class FrameworkConn(): __conn = None __controlmanager = None MSG_CONNECT = 'control id="%s" action="connect" version="' + __version__ + '"\n' def __init__(self, conf, frmk_id, frmk_ip, frmk_port): self._framework_id = frmk_id #conf.get("control-framework", "id") self._framework_ip = frmk_ip#conf.get("control-framework", "ip") self._framework_port = frmk_port #conf.get("control-framework", "port") self._framework_ping = True self.__keep_processing = True # instatiate the control manager self.__controlmanager = ControlManager(conf) self.__alive = False self.__pingThread = None self.__reciverControlMsgThread = None self.__event = threading.Event() # connect to framework daemon # attempts == 0 means that agent try to connect forever # waittime = seconds between attempts def connect(self, attempts=0, waittime=10.0): # connection attempt counter count = 0 if self.__conn is None: logger.info("Connecting to control framework (%s:%s) ..." \ % (self._framework_ip, self._framework_port)) while attempts == 0 or count < attempts: self.__connect_to_framework() if self.__conn is not None: break else: logger.info("Can't connect to control framework, " + \ "retrying in %d seconds" % (waittime)) if self.__keep_processing: time.sleep(waittime) else: count = attempts count += 1 else: logger.info("Reusing control framework connection (%s:%s) ..." \ % (self._framework_ip, self._framework_port)) return self.__conn def close(self): logger.info("Closing control framework connection ...") if self.__reciverControlMsgThread is not None: self.__reciverControlMsgThread.join(1) if self.__pingThread is not None: self.__pingThread.join(1) if self.__conn is not None: self.__conn.close() self.__conn = None self.__keep_processing = False self.__alive = False self.__controlmanager.stopProcess() # Reset the current connection by closing and reopening it def reconnect(self, attempts=0, waittime=10.0): self.__event.set() if self.__conn is not None: self.__conn.close() self.__conn = None time.sleep(2) while self.__keep_processing: if self.connect(attempts, waittime) is not None: self.__event.clear() break def send(self, msg): while self.__keep_processing: try: self.__conn.send(msg) except socket.error, e: logger.error(e) if not self.__tryReconect: self.__alive = False self.reconnect() except AttributeError, e: # self.__conn == None if not self.__tryReconect: self.__alive = False self.reconnect() else: