private State<BaseClientState, BaseClientData> ifEventUpToDate(final Object event, final Supplier<State<BaseClientState, BaseClientData>> thenExecute) { final BaseClientState state = stateName(); final ActorRef sender = getSender(); if (isEventUpToDate(event, state, sender)) { return thenExecute.get(); } else { log.warning("Received stale event <{}> at state <{}>", event, state); return stay(); } }
private State<BaseClientState, BaseClientData> ifEventUpToDate(final Object event, final Supplier<State<BaseClientState, BaseClientData>> thenExecute) { final BaseClientState state = stateName(); final ActorRef sender = getSender(); if (isEventUpToDate(event, state, sender)) { return thenExecute.get(); } else { log.warning("Received stale event <{}> at state <{}>", event, state); return stay(); } }
/** * Creates the handler for messages common to all states. * <p> * Overwrite and extend by additional matchers. * </p> * * @return an FSM function builder */ protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inAnyState() { return matchEvent(RetrieveConnectionMetrics.class, BaseClientData.class, this::retrieveConnectionMetrics) .event(OutboundSignal.WithExternalMessage.class, BaseClientData.class, (outboundSignal, data) -> { handleExternalMessage(outboundSignal); return stay(); }) .event(OutboundSignal.class, BaseClientData.class, (signal, data) -> { handleOutboundSignal(signal); return stay(); }); }
/** * Creates the handler for messages common to all states. * <p> * Overwrite and extend by additional matchers. * </p> * * @return an FSM function builder */ protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inAnyState() { return matchEvent(RetrieveConnectionMetrics.class, BaseClientData.class, this::retrieveConnectionMetrics) .event(OutboundSignal.WithExternalMessage.class, BaseClientData.class, (outboundSignal, data) -> { handleExternalMessage(outboundSignal); return stay(); }) .event(OutboundSignal.class, BaseClientData.class, (signal, data) -> { handleOutboundSignal(signal); return stay(); }); }
private State<BaseClientState, BaseClientData> onUnknownEvent(final Object event, final BaseClientData state) { log.warning("received unknown/unsupported message {} in state {} - status: {} - sender: {}", event, stateName(), state.getConnectionStatus() + ": " + state.getConnectionStatusDetails().orElse(""), getSender()); final ActorRef sender = getSender(); if (!Objects.equals(sender, getSelf()) && !Objects.equals(sender, getContext().system().deadLetters())) { sender.tell(unhandledExceptionForSignalInState(event, stateName()), getSelf()); } return stay(); }
private State<BaseClientState, BaseClientData> onUnknownEvent(final Object event, final BaseClientData state) { Object message = event; if (event instanceof Failure) { message = ((Failure) event).cause(); } if (event instanceof Status.Failure) { message = ((Status.Failure) event).cause(); } if (message instanceof Throwable) { log.error((Throwable) message, "received Exception {} in state {} - status: {} - sender: {}", message, stateName(), state.getConnectionStatus() + ": " + state.getConnectionStatusDetails().orElse(""), getSender()); } else { log.warning("received unknown/unsupported message {} in state {} - status: {} - sender: {}", message, stateName(), state.getConnectionStatus() + ": " + state.getConnectionStatusDetails().orElse(""), getSender()); } final ActorRef sender = getSender(); if (!Objects.equals(sender, getSelf()) && !Objects.equals(sender, getContext().system().deadLetters())) { sender.tell(unhandledExceptionForSignalInState(event, stateName()), getSelf()); } return stay(); }
private FSM.State<BaseClientState, BaseClientData> retrieveConnectionMetrics( final RetrieveConnectionMetrics command, final BaseClientData data) { final ActorRef sender = getSender(); final ActorRef self = getSelf(); final DittoHeaders dittoHeaders = command.getDittoHeaders().toBuilder() .source(org.eclipse.ditto.services.utils.config.ConfigUtil.instanceIdentifier()) .build(); final CompletionStage<List<SourceMetrics>> sourceMetricsFuture = getCurrentSourcesMetrics(); final CompletionStage<List<TargetMetrics>> targetMetricsFuture = getCurrentTargetsMetrics(); final CompletionStage<ConnectionMetrics> metricsFuture = sourceMetricsFuture.thenCompose(sourceMetrics -> targetMetricsFuture.thenApply(targetMetrics -> ConnectivityModelFactory.newConnectionMetrics( getCurrentConnectionStatus(), getCurrentConnectionStatusDetails().orElse(null), getInConnectionStatusSince(), stateName().name(), sourceMetrics, targetMetrics) ) ); metricsFuture.thenAccept(connectionMetrics -> { final Object response = RetrieveConnectionMetricsResponse.of(connectionId(), connectionMetrics, dittoHeaders); sender.tell(response, self); }); return stay(); }
private FSM.State<BaseClientState, BaseClientData> retrieveConnectionMetrics( final RetrieveConnectionMetrics command, final BaseClientData data) { final ActorRef sender = getSender(); final ActorRef self = getSelf(); final DittoHeaders dittoHeaders = command.getDittoHeaders().toBuilder() .source(org.eclipse.ditto.services.utils.config.ConfigUtil.instanceIdentifier()) .build(); final CompletionStage<List<SourceMetrics>> sourceMetricsFuture = getCurrentSourcesMetrics(); final CompletionStage<List<TargetMetrics>> targetMetricsFuture = getCurrentTargetsMetrics(); final CompletionStage<ConnectionMetrics> metricsFuture = sourceMetricsFuture.thenCompose(sourceMetrics -> targetMetricsFuture.thenApply(targetMetrics -> ConnectivityModelFactory.newConnectionMetrics( getCurrentConnectionStatus(), getCurrentConnectionStatusDetails().orElse(null), getInConnectionStatusSince(), stateName().name(), sourceMetrics, targetMetrics) ) ); metricsFuture.thenAccept(connectionMetrics -> { final Object response = RetrieveConnectionMetricsResponse.of(connectionId(), connectionMetrics, dittoHeaders); sender.tell(response, self); }); return stay(); }
private FSMStateFunctionBuilder<BaseClientState, BaseClientData> inUnknownState() { return matchEvent(OpenConnection.class, BaseClientData.class, this::openConnection) .event(CloseConnection.class, BaseClientData.class, this::closeConnection) .event(TestConnection.class, BaseClientData.class, this::testConnection) .eventEquals(StateTimeout(), BaseClientData.class, (state, data) -> { if (ConnectionStatus.OPEN == data.getDesiredConnectionStatus()) { log.info("Did not receive connect command within init-timeout, connecting"); final OpenConnection openConnection = OpenConnection.of(connectionId(), DittoHeaders.empty()); getSelf().tell(openConnection, getSelf()); } else if (ConnectionStatus.CLOSED == data.getDesiredConnectionStatus()) { log.info( "Did not receive connect command within init-timeout, desired state is closed, going to disconnected state."); return goTo(DISCONNECTED); } else { log.info( "Did not receive connect command within init-timeout, desired state is {}, do nothing.", data.getDesiredConnectionStatus()); } return stay(); // handle self-told commands later }); }
private FSMStateFunctionBuilder<BaseClientState, BaseClientData> inUnknownState() { return matchEvent(OpenConnection.class, BaseClientData.class, this::openConnection) .event(CloseConnection.class, BaseClientData.class, this::closeConnection) .event(TestConnection.class, BaseClientData.class, this::testConnection) .eventEquals(StateTimeout(), BaseClientData.class, (state, data) -> { if (ConnectionStatus.OPEN == data.getDesiredConnectionStatus()) { log.info("Did not receive connect command within init-timeout, connecting"); final OpenConnection openConnection = OpenConnection.of(connectionId(), DittoHeaders.empty()); getSelf().tell(openConnection, getSelf()); } else if (ConnectionStatus.CLOSED == data.getDesiredConnectionStatus()) { log.info( "Did not receive connect command within init-timeout, desired state is closed, going to disconnected state."); return goTo(DISCONNECTED); } else { log.info( "Did not receive connect command within init-timeout, desired state is {}, do nothing.", data.getDesiredConnectionStatus()); } return stay(); // handle self-told commands later }); }