def writestate(self, state): ''' Write the current state to a state file in the session dir. ''' try: f = open(os.path.join(self.sessiondir, "state"), "w") f.write("%d %s\n" % (state, coreapi.state_name(state))) f.close() except Exception, e: self.warn("Error writing state file: %s" % e)
def run_state_hooks(self, state): if state not in self._state_hooks: return for hook in self._state_hooks[state]: try: hook(state) except Exception, e: self.warn("ERROR: exception occured when running %s state " "hook: %s: %s" % (coreapi.state_name(state), hook, e))
def write_state(self, state): """ Write the current state to a state file in the session dir. :param int state: state to write to file :return: nothing """ try: state_file = open(self._state_file, "w") state_file.write("%d %s\n" % (state, coreapi.state_name(state))) state_file.close() except IOError: logger.exception("error writing state file: %s", state)
def run_state_hooks(self, state): """ Run state hooks. :param int state: state to run hooks for :return: nothing """ for hook in self._state_hooks.get(state, []): try: hook(state) except: message = "exception occured when running %s state hook: %s" % (coreapi.state_name(state), hook) logger.exception(message) self.exception(ExceptionLevels.ERROR, "Session.run_state_hooks", None, message)
def run_state_hooks(self, state): """ Run state hooks. :param int state: state to run hooks for :return: nothing """ for hook in self._state_hooks.get(state, []): try: hook(state) except: message = "exception occured when running %s state hook: %s" % ( coreapi.state_name(state), hook) logger.exception(message) self.exception(ExceptionLevels.ERROR, "Session.run_state_hooks", None, message)
def check_runtime(self): """ Check if we have entered the runtime state, that all nodes have been started and the emulation is running. Start the event loop once we have entered runtime (time=0). """ # this is called from instantiate() after receiving an event message # for the instantiation state, and from the broker when distributed # nodes have been started logger.info("session(%s) checking if not in runtime state, current state: %s", self.session_id, coreapi.state_name(self.state)) if self.state == EventTypes.RUNTIME_STATE.value: logger.info("valid runtime state found, returning") return # check to verify that all nodes and networks are running if not self.broker.instantiation_complete(): return # start event loop and set to runtime self.event_loop.run() self.set_state(EventTypes.RUNTIME_STATE, send_event=True)
def setstate(self, state, info = False, sendevent = False, returnevent = False): ''' Set the session state. When info is true, log the state change event using the session handler's info method. When sendevent is true, generate a CORE API Event Message and send to the connected entity. ''' if state == self._state: return [] self._time = time.time() self._state = state self.run_state_hooks(state) replies = [] if self.isconnected() and info: statename = coreapi.state_name(state) with self._handlerslock: for handler in self._handlers: handler.info("SESSION %s STATE %d: %s at %s" % \ (self.sessionid, state, statename, time.ctime())) self.writestate(state) self.runhook(state) if sendevent: tlvdata = "" tlvdata += coreapi.CoreEventTlv.pack(coreapi.CORE_TLV_EVENT_TYPE, state) msg = coreapi.CoreEventMessage.pack(0, tlvdata) # send Event Message to connected handlers (e.g. GUI) if self.isconnected(): try: if returnevent: replies.append(msg) else: self.broadcastraw(None, msg) except Exception, e: self.warn("Error sending Event Message: %s" % e) # also inform slave servers tmp = self.broker.handlerawmsg(msg)
def check_runtime(self): """ Check if we have entered the runtime state, that all nodes have been started and the emulation is running. Start the event loop once we have entered runtime (time=0). """ # this is called from instantiate() after receiving an event message # for the instantiation state, and from the broker when distributed # nodes have been started logger.info( "session(%s) checking if not in runtime state, current state: %s", self.session_id, coreapi.state_name(self.state)) if self.state == EventTypes.RUNTIME_STATE.value: logger.info("valid runtime state found, returning") return # check to verify that all nodes and networks are running if not self.broker.instantiation_complete(): return # start event loop and set to runtime self.event_loop.run() self.set_state(EventTypes.RUNTIME_STATE, send_event=True)