/// <summary> /// Adds the specified event to the state machine's event queue. /// </summary> /// <param name="nsfEvent">The event to queue.</param> /// <param name="isPriorityEvent">Flag indicating if the event should be queued to the back of the queue (false) or the front of the queue (true).</param> /// <param name="logEventQueued">Flag indicating if an event queued trace should be added to the trace log.</param> private void queueEvent(NSFEvent nsfEvent, bool isPriorityEvent, bool logEventQueued) { if (!isTopStateMachine()) { TopStateMachine.queueEvent(nsfEvent, isPriorityEvent, logEventQueued); return; } lock (stateMachineMutex) { // Do not allow events to be queued if terminating or terminated, // except for run to completion event, which may be queued if terminating to allow proper semantics to continue until terminated. if ((TerminationStatus == NSFEventHandlerTerminationStatus.EventHandlerTerminated) || ((TerminationStatus == NSFEventHandlerTerminationStatus.EventHandlerTerminating) && (nsfEvent != runToCompletionEvent))) { return; } // Handle special case of terminate event by setting status and queuing a single terminate event. // Terminate event must be the last event queued to guarantee safe deletion when it is handled. if (nsfEvent == terminateEvent) { if (TerminationStatus == NSFEventHandlerTerminationStatus.EventHandlerReady) { TerminationStatus = NSFEventHandlerTerminationStatus.EventHandlerTerminating; } } // Event limit detection looks for too many events queued for the state machine. // If more than the specified number of events are queued, the state machine will remove // its queued events, call the event limit actions, and stop after executing any events queued // by the limit actions. if ((EventLimitDetectionEnabled) && (QueuedEvents == EventLimit)) { EventThread.removeEventsFor(this); QueuedEvents = 0; EventLimitActions.execute(new NSFStateMachineContext(this, null, null, null, nsfEvent)); // Stop the state machine so that no more event processing occurs until started again stopStateMachine(); NSFTraceLog.PrimaryTraceLog.addTrace(NSFTraceTags.ErrorTag, NSFTraceTags.SourceTag, Name, NSFTraceTags.MessageTag, "EventLimit"); return; } nsfEvent.Destination = this; EventThread.queueEvent(nsfEvent, isPriorityEvent, logEventQueued); ++QueuedEvents; } }
/// <summary> /// Handles an event. /// </summary> /// <param name="nsfEvent">The event to handle.</param> /// <returns>Status indicating if the event was handled or not.</returns> /// <remarks> /// This method is for use only by the North State Framework's internal logic. /// It processes the event using UML defined behavior, including run to completion. /// </remarks> public NSFEventStatus handleEvent(NSFEvent nsfEvent) { lock (stateMachineMutex) { --QueuedEvents; // This should only happen if events are queued without using the queueEvent method if (QueuedEvents < 0) { QueuedEvents = 0; } } // Handle status changing events if ((nsfEvent == startEvent)) { RunStatus = NSFEventHandlerRunStatus.EventHandlerStarted; } else if (nsfEvent == stopEvent) { RunStatus = NSFEventHandlerRunStatus.EventHandlerStopped; } else if (nsfEvent == terminateEvent) { TerminationStatus = NSFEventHandlerTerminationStatus.EventHandlerTerminated; EventThread.removeEventHandler(this); return(NSFEventStatus.NSFEventHandled); } else if (nsfEvent == resetEvent) { reset(); } // Don't process events if stopped if (RunStatus == NSFEventHandlerRunStatus.EventHandlerStopped) { return(NSFEventStatus.NSFEventUnhandled); } // If not already active, enter state machine at the root if (!active) { enter(new NSFStateMachineContext(this, this, null, null, startEvent), false); } // Process the event NSFEventStatus eventStatus = NSFEventStatus.NSFEventUnhandled; try { eventStatus = processEvent(nsfEvent); if (eventStatus == NSFEventStatus.NSFEventHandled) { runToCompletion(); } // Consecutive loop detection looks for too many events without the state machine pausing. // If more than the specified number of transitions occur without a pause, the state machine will remove // its queued events, call the consecutive loop limit actions, and stop after executing any events queued // by the actions. if (ConsecutiveLoopDetectionEnabled) { ++consecutiveLoopCount; if (consecutiveLoopCount == ConsecutiveLoopLimit) { lock (stateMachineMutex) { EventThread.removeEventsFor(this); QueuedEvents = 0; } ConsecutiveLoopLimitActions.execute(new NSFStateMachineContext(this, null, null, null, nsfEvent)); // Stop the state machine so that no more event processing occurs until started again stopStateMachine(); // Reset consecutive loop count in case state machine is started again consecutiveLoopCount = 0; NSFTraceLog.PrimaryTraceLog.addTrace(NSFTraceTags.ErrorTag, NSFTraceTags.SourceTag, Name, NSFTraceTags.MessageTag, "ConsecutiveLoopLimit"); } else if (QueuedEvents == 0) { // If no events are queued for this state machine, then it has paused, indicating it's not in an infinite loop. consecutiveLoopCount = 0; } } } catch (Exception exception) { handleException(new Exception(nsfEvent.Name + " event handling exception", exception)); } return(eventStatus); }