def __scheduler_event(self, event): if self.__state == self.State.NONE: self.__data_handler.send_pre_working(self._connector) self.__state = self.State.PRE_WORKING self.__pre_working_start = time.time() logger.debug( "InnerWorker %d current state:NONE change state to PRE_WORKING" % (self._worker_id)) elif self.__state == self.State.PRE_WORKING: if (time.time() - self.__pre_working_start) > 5: self.__state = self.State.CLOSED logger.error( "InnerWorker %d stay in PRO_WORKING state over 5 seconds,close it" % (self._worker_id)) elif self.__state == self.State.CLOSED: # close all paired outer worker try: close_event = forward_event.CloseConEvent(self._worker_id) self.__north_interface_channel(close_event) self._connector.close() self._connector_change_callback(self._connector, self.handler_event) except Exception, e: logger.error(e.message) self.__state = self.State.DONE logger.debug( "InnerWorker %d current state:CLOSED change state to DONE" % (self._worker_id))
def __scheduler_event(self, event): if not isinstance(event,forward_event.SchedulerEvent): return if self.__state == self.State.NONE: self.__connector = connector.ConnectorClient(self.__inner_ip, self.__inner_port) self.__connector.connect() if self.__connector.con_state == connector.CON_STATE.CON_CONNECTED: self.__state = self.State.WORKING self.__connecte_success() elif self.__connector.con_state == connector.CON_STATE.CON_FAILED: self.__state = self.State.CLOSED elif self.__connector.con_state == connector.CON_STATE.CON_CONNECTING: self.__connector_change_callback(self.__connector, self.handler_event) self.__state = self.State.CONNECTING logger.debug("InnerWorker %d connect to %s:%d current state:%s"%(self.__id,self.__inner_ip,self.__inner_port,str(self.__state))) elif self.__state == self.State.WORKING: if self.__connector.con_state != connector.CON_STATE.CON_CONNECTED: self.__state = self.State.CLOSED logger.debug("InnerWorker %d current state:WORKING change state to DONE due connector state error:%s"%(self.__id,str(self.__connector.con_state)) ) elif self.__state == self.State.CLOSED: #send closed message to outer #self.__data_handler.inner_closed(self.__id,self.__inner_ip,self.__inner_port,outer_connector) close_event = forward_event.CloseConEvent(self.__id) self.__north_interface_channel(close_event) self.__connector.close() self.__connector_change_callback(self.__connector, self.handler_event) self.__state = self.State.DONE logger.debug("InnerWorker %d current state:CLOSED change state to DONE"%(self.__id))
def __scheduler_event(self, event): if self.__state == self.State.NONE: self.__data_handler.create_connection( self._worker_id, self.__inner_ip, self.__inner_port, self.__sourth_interface_channel) self.__state = self.State.CONNECTING_TO_INNER self.__connecting_begin_time = time.time() logger.debug( "OuterWorker %d current state:NONE create inner connecttion to %s:%d ,change state to CONNECTING_TO_INNER" % (self._worker_id, self.__inner_ip, self.__inner_port)) elif self.__state == self.State.CONNECTING_TO_INNER: if (time.time() - self.__connecting_begin_time) >= 5: #connecting can not over 5 seconds self.__state = self.State.CLOSED logger.error( "OuterWorker %d stay in CONNECTING_TO_INNER state over 5 seconds,change state to CLOSED" % (self._worker_id)) elif self.__state == self.State.WORKING: if self._connector.con_state != connector.CON_STATE.CON_CONNECTED: self.__state = self.State.CLOSED logger.debug( "OuterWorker %d current state:WORKING change state to CLOSED due connector state error:%s" % (self._worker_id, str(self._connector.con_state))) elif self.__state == self.State.CLOSED: # send closed msg to remote close_event = forward_event.CloseConEvent(self._worker_id) self.__sourth_interface_channel(close_event) self._connector.close() self._connector_change_callback(self._connector, self.handler_event) self.__state = self.State.DONE logger.debug( "OuterWorker %d current state:CLOSED change state to DONE" % (self._worker_id))
def __handle_data(self): datas = self.__data_handler.get_forward_datas(self.__ring_buffer) for data in datas: if data.data_type in (forward_data.DATA_TYPE.CONNECTION_SUCCESS, forward_data.DATA_TYPE.TRANS_DATA): trans_event = forward_event.TransDataEvent(data.id, data) self.__north_interface_channel(trans_event) elif data.data_type == forward_data.DATA_TYPE.CLOSE_CONNECTION: close_event = forward_event.CloseConEvent(data.id) self.__north_interface_channel(close_event) elif data.data_type == forward_data.DATA_TYPE.HEART_BEAT: self.send_heart_beat_reply() logger.info("InnerWorker %d send heartbeat reply " % (self._worker_id))
def __handle_data(self): datas = self.__data_handler.get_forward_datas(self.__ring_buffer) for data in datas: if data.data_type in (forward_data.DATA_TYPE.NEW_CONNECTION, forward_data.DATA_TYPE.TRANS_DATA): trans_event = forward_event.TransDataEvent(data.id, data) self.__sourth_interface_channel(trans_event) elif data.data_type == forward_data.DATA_TYPE.PRE_WORKING: self.__data_handler.send_preworking_reply(self.__connector) elif data.data_type == forward_data.DATA_TYPE.CLOSE_CONNECTION: close_event = forward_event.CloseConEvent(data.id) self.__sourth_interface_channel(close_event) elif data.data_type == forward_data.DATA_TYPE.HEART_BEAT: logger.debug('OuterWorker recv heartbeat reply') self.__last_hear_beat_reply_time = time.time()
def __scheduler_event(self, event): if not isinstance(event, forward_event.SchedulerEvent): return if self.__state == self.State.NONE: #print 'outer_worker:connect to %s %d'%(self.__outer_ip,self.__outer_port) self.__connector = connector.ConnectorClient( self.__outer_ip, self.__outer_port) self.__connector.connect() if self.__connector.con_state == connector.CON_STATE.CON_CONNECTED: self.__connector_change_callback(self.__connector, self.handler_event) self.__state = self.State.WORKING elif self.__connector.con_state == connector.CON_STATE.CON_FAILED: #do nothing pass elif self.__connector.con_state == connector.CON_STATE.CON_CONNECTING: self.__connector_change_callback(self.__connector, self.handler_event) self.__state = self.State.CONNECTING elif self.__state == self.State.WORKING: if self.__connector.con_state != connector.CON_STATE.CON_CONNECTED: self.__state = self.State.DISCONNECTED logger.debug( "OuterWorker current state:WORKING change state to DONE due connector state error:%s" % (str(self.__connector.con_state))) return else: current_time = time.time() if current_time - self.__last_heart_beat_time >= 30: self.send_heart_beat() self.__last_heart_beat_time = current_time logger.debug("OuterWorker send hearbeat") elif self.__state == self.State.DISCONNECTED: close_event = forward_event.CloseConEvent(0) self.__sourth_interface_channel(close_event) self.__state = self.State.NONE logger.debug( "OuterWorker current state:DISCONNECTED close all inner_worker,change state to NONE" ) self.__handle_data()
def close_inner_worker(self, worker_id): if not self.__inner_workers.has_key(worker_id): raise Exception('can not find this worker:' + str(worker_id)) close_event = forward_event.CloseConEvent(worker_id) self.__inner_workers[worker_id].handler_event(close_event)