use of com.digitalpetri.strictmachine.dsl.FsmBuilder in project milo by eclipse.
the class SessionFsmFactory method configureActiveState.
private static void configureActiveState(FsmBuilder<State, Event> fb, OpcUaClient client) {
/* Transitions */
fb.when(State.Active).on(Event.CloseSession.class).transitionTo(State.Closing);
fb.when(State.Active).on(e -> e.getClass() == Event.KeepAliveFailure.class || e.getClass() == Event.ServiceFault.class).transitionTo(State.CreatingWait);
/* External Transition Actions */
fb.onTransitionTo(State.Active).from(State.Initializing).via(Event.InitializeSuccess.class).execute(ctx -> {
Event.InitializeSuccess event = (Event.InitializeSuccess) ctx.event();
// reset the wait time
KEY_WAIT_TIME.remove(ctx);
long keepAliveInterval = client.getConfig().getKeepAliveInterval().longValue();
KEY_KEEP_ALIVE_FAILURE_COUNT.set(ctx, 0L);
ScheduledFuture<?> scheduledFuture = client.getConfig().getScheduledExecutor().scheduleWithFixedDelay(() -> ctx.fireEvent(new Event.KeepAlive(event.session)), keepAliveInterval, keepAliveInterval, TimeUnit.MILLISECONDS);
KEY_KEEP_ALIVE_SCHEDULED_FUTURE.set(ctx, scheduledFuture);
KEY_SESSION.set(ctx, event.session);
SessionFuture sessionFuture = KEY_SESSION_FUTURE.get(ctx);
client.getConfig().getExecutor().execute(() -> sessionFuture.future.complete(event.session));
});
fb.onTransitionTo(State.Active).from(State.Initializing).via(Event.InitializeSuccess.class).execute(FsmContext::processShelvedEvents);
fb.onTransitionFrom(State.Active).to(s -> s == State.Closing || s == State.CreatingWait).viaAny().execute(ctx -> {
ScheduledFuture<?> scheduledFuture = KEY_KEEP_ALIVE_SCHEDULED_FUTURE.remove(ctx);
if (scheduledFuture != null) {
scheduledFuture.cancel(false);
}
});
// onSessionActive() callbacks
fb.onTransitionTo(State.Active).from(s -> s != State.Active).viaAny().execute(ctx -> {
OpcUaSession session = KEY_SESSION.get(ctx);
SessionFsm.SessionActivityListeners sessionActivityListeners = KEY_SESSION_ACTIVITY_LISTENERS.get(ctx);
sessionActivityListeners.sessionActivityListeners.forEach(listener -> listener.onSessionActive(session));
});
// onSessionInactive() callbacks
fb.onTransitionFrom(State.Active).to(s -> s != State.Active).viaAny().execute(ctx -> {
OpcUaSession session = KEY_SESSION.get(ctx);
SessionFsm.SessionActivityListeners sessionActivityListeners = KEY_SESSION_ACTIVITY_LISTENERS.get(ctx);
sessionActivityListeners.sessionActivityListeners.forEach(listener -> listener.onSessionInactive(session));
});
/* Internal Transition Actions */
fb.onInternalTransition(State.Active).via(Event.KeepAlive.class).execute(ctx -> {
Event.KeepAlive event = (Event.KeepAlive) ctx.event();
sendKeepAlive(client, event.session).whenComplete((response, ex) -> {
if (response != null) {
DataValue[] results = response.getResults();
if (results != null && results.length > 0) {
Object value = results[0].getValue().getValue();
if (value instanceof Integer) {
ServerState state = ServerState.from((Integer) value);
LOGGER.debug("[{}] ServerState: {}", ctx.getInstanceId(), state);
}
}
KEY_KEEP_ALIVE_FAILURE_COUNT.set(ctx, 0L);
} else {
Long keepAliveFailureCount = KEY_KEEP_ALIVE_FAILURE_COUNT.get(ctx);
if (keepAliveFailureCount == null) {
keepAliveFailureCount = 1L;
} else {
keepAliveFailureCount += 1L;
}
KEY_KEEP_ALIVE_FAILURE_COUNT.set(ctx, keepAliveFailureCount);
long keepAliveFailuresAllowed = client.getConfig().getKeepAliveFailuresAllowed().longValue();
if (keepAliveFailureCount > keepAliveFailuresAllowed) {
LOGGER.warn("[{}] Keep Alive failureCount={} exceeds failuresAllowed={}", ctx.getInstanceId(), keepAliveFailureCount, keepAliveFailuresAllowed);
ctx.fireEvent(new Event.KeepAliveFailure());
} else {
LOGGER.debug("[{}] Keep Alive failureCount={}", ctx.getInstanceId(), keepAliveFailureCount, ex);
}
}
});
});
fb.onInternalTransition(State.Active).via(Event.GetSession.class).execute(SessionFsmFactory::handleGetSessionEvent);
fb.onInternalTransition(State.Active).via(Event.OpenSession.class).execute(SessionFsmFactory::handleOpenSessionEvent);
}
use of com.digitalpetri.strictmachine.dsl.FsmBuilder in project milo by eclipse.
the class SessionFsmFactory method configureClosingState.
private static void configureClosingState(FsmBuilder<State, Event> fb, OpcUaClient client) {
/* Transitions */
fb.when(State.Closing).on(Event.CloseSessionSuccess.class).transitionTo(State.Inactive);
/* External Transition Actions */
fb.onTransitionTo(State.Closing).from(State.Active).via(Event.CloseSession.class).execute(ctx -> {
SessionFsm.CloseFuture closeFuture = new SessionFsm.CloseFuture();
KEY_CLOSE_FUTURE.set(ctx, closeFuture);
Event.CloseSession closeSession = (Event.CloseSession) ctx.event();
complete(closeSession.future).with(closeFuture.future);
OpcUaSession session = KEY_SESSION.get(ctx);
closeSession(ctx, client, session).whenComplete((u, ex) -> {
if (u != null) {
LOGGER.debug("[{}] Session closed: {}", ctx.getInstanceId(), session);
} else {
LOGGER.debug("[{}] CloseSession failed: {}", ctx.getInstanceId(), ex.getMessage(), ex);
}
ctx.fireEvent(new Event.CloseSessionSuccess());
});
});
fb.onTransitionFrom(State.Closing).to(State.Inactive).via(Event.CloseSessionSuccess.class).execute(ctx -> {
SessionFsm.CloseFuture closeFuture = KEY_CLOSE_FUTURE.get(ctx);
if (closeFuture != null) {
client.getConfig().getExecutor().execute(() -> closeFuture.future.complete(Unit.VALUE));
}
});
/* Internal Transition Actions */
fb.onInternalTransition(State.Closing).via(Event.CloseSession.class).execute(ctx -> {
Event.CloseSession event = (Event.CloseSession) ctx.event();
SessionFsm.CloseFuture closeFuture = KEY_CLOSE_FUTURE.get(ctx);
if (closeFuture != null) {
complete(event.future).with(closeFuture.future);
}
});
fb.onInternalTransition(State.Closing).via(e -> e.getClass() != Event.CloseSession.class).execute(ctx -> ctx.shelveEvent(ctx.event()));
}
Aggregations