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(); } }
private CompletionStage<Status.Status> testMessageMappingProcessor(@Nullable final MappingContext mappingContext) { try { // this one throws DittoRuntimeExceptions when the mapper could not be configured MessageMappingProcessor.of(connectionId(), mappingContext, getContext().getSystem(), log); return CompletableFuture.completedFuture(new Status.Success("mapping")); } catch (final DittoRuntimeException dre) { log.info("Got DittoRuntimeException during initialization of MessageMappingProcessor: {} {} - desc: {}", dre.getClass().getSimpleName(), dre.getMessage(), dre.getDescription().orElse("")); getSender().tell(dre, getSelf()); return CompletableFuture.completedFuture(new Status.Failure(dre)); } }
private CompletionStage<Status.Status> testMessageMappingProcessor(@Nullable final MappingContext mappingContext) { try { // this one throws DittoRuntimeExceptions when the mapper could not be configured MessageMappingProcessor.of(connectionId(), mappingContext, getContext().getSystem(), log); return CompletableFuture.completedFuture(new Status.Success("mapping")); } catch (final DittoRuntimeException dre) { log.info("Got DittoRuntimeException during initialization of MessageMappingProcessor: {} {} - desc: {}", dre.getClass().getSimpleName(), dre.getMessage(), dre.getDescription().orElse("")); getSender().tell(dre, getSelf()); return CompletableFuture.completedFuture(new Status.Failure(dre)); } }
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 FSM.State<BaseClientState, BaseClientData> closeConnection(final CloseConnection closeConnection, final BaseClientData data) { final ActorRef sender = getSender(); doDisconnectClient(data.getConnection(), sender); return goTo(DISCONNECTING).using(setSession(data, sender, closeConnection.getDittoHeaders()) .setDesiredConnectionStatus(ConnectionStatus.CLOSED) .setConnectionStatusDetails("closing or deleting connection at " + Instant.now())); }
private FSM.State<BaseClientState, BaseClientData> closeConnection(final CloseConnection closeConnection, final BaseClientData data) { final ActorRef sender = getSender(); doDisconnectClient(data.getConnection(), sender); return goTo(DISCONNECTING).using(setSession(data, sender, closeConnection.getDittoHeaders()) .setDesiredConnectionStatus(ConnectionStatus.CLOSED) .setConnectionStatusDetails("closing or deleting connection at " + Instant.now())); }
private void handleOutboundSignal(final OutboundSignal signal) { enhanceLogUtil(signal.getSource()); if (messageMappingProcessorActor != null) { messageMappingProcessorActor.tell(signal, getSender()); } else { log.info("Cannot handle <{}> signal as there is no MessageMappingProcessor available.", signal.getSource().getType()); } }
private void handleOutboundSignal(final OutboundSignal signal) { enhanceLogUtil(signal.getSource()); if (messageMappingProcessorActor != null) { messageMappingProcessorActor.tell(signal, getSender()); } else { log.info("Cannot handle <{}> signal as there is no MessageMappingProcessor available.", signal.getSource().getType()); } }
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(); }
/** * Creates the handler for messages in testing state. * Overwrite and extend by additional matchers. * * @return an FSM function builder */ protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inTestingState() { return matchEvent(Status.Status.class, (e, d) -> Objects.equals(getSender(), getSelf()), (status, data) -> { final Status.Status answerToPublish = getStatusToReport(status); data.getSessionSender().ifPresent(sender -> sender.tell(answerToPublish, getSelf())); return stop(); }) .eventEquals(StateTimeout(), BaseClientData.class, (stats, data) -> { log.info("test timed out."); data.getSessionSender().ifPresent(sender -> { final DittoRuntimeException error = ConnectionFailedException.newBuilder(connectionId()) .description(String.format("Failed to open requested connection within <%d> seconds!", TEST_CONNECTION_TIMEOUT)) .dittoHeaders(data.getSessionHeaders()) .build(); sender.tell(new Status.Failure(error), getSelf()); }); return stop(); }); }
/** * Creates the handler for messages in testing state. * Overwrite and extend by additional matchers. * * @return an FSM function builder */ protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inTestingState() { return matchEvent(Status.Status.class, (e, d) -> Objects.equals(getSender(), getSelf()), (status, data) -> { final Status.Status answerToPublish = getStatusToReport(status); data.getSessionSender().ifPresent(sender -> sender.tell(answerToPublish, getSelf())); return stop(); }) .eventEquals(StateTimeout(), BaseClientData.class, (stats, data) -> { log.info("test timed out."); data.getSessionSender().ifPresent(sender -> { final DittoRuntimeException error = ConnectionFailedException.newBuilder(connectionId()) .description(String.format("Failed to open requested connection within <%d> seconds!", TEST_CONNECTION_TIMEOUT)) .dittoHeaders(data.getSessionHeaders()) .build(); sender.tell(new Status.Failure(error), getSelf()); }); return stop(); }); }
final ActorRef sender = getSender(); final Connection connection = testConnection.getConnection();
final ActorRef sender = getSender(); final Connection connection = testConnection.getConnection();
private State<BaseClientState, BaseClientData> connectionFailure(final ConnectionFailure event, final BaseClientData data) { return ifEventUpToDate(event, () -> { LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId()); cleanupResourcesForConnection(); data.getSessionSender().ifPresent(sender -> sender.tell(getStatusToReport(event.getFailure()), getSelf())); return goTo(UNKNOWN).using(data.resetSession() .setConnectionStatus(ConnectionStatus.FAILED) .setConnectionStatusDetails(event.getFailureDescription()) .setSessionSender(getSender()) ); }); }
private State<BaseClientState, BaseClientData> connectionFailure(final ConnectionFailure event, final BaseClientData data) { return ifEventUpToDate(event, () -> { LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId()); cleanupResourcesForConnection(); data.getSessionSender().ifPresent(sender -> sender.tell(getStatusToReport(event.getFailure()), getSelf())); return goTo(UNKNOWN).using(data.resetSession() .setConnectionStatus(ConnectionStatus.FAILED) .setConnectionStatusDetails(event.getFailureDescription()) .setSessionSender(getSender()) ); }); }
private FSM.State<BaseClientState, BaseClientData> openConnection(final OpenConnection openConnection, final BaseClientData data) { final ActorRef sender = getSender(); final Connection connection = data.getConnection(); final DittoHeaders dittoHeaders = openConnection.getDittoHeaders(); if (canConnectViaSocket(connection)) { doConnectClient(connection, sender); return goTo(CONNECTING).using(setSession(data, sender, dittoHeaders)); } else { cleanupResourcesForConnection(); final DittoRuntimeException error = newConnectionFailedException(data.getConnection(), dittoHeaders); sender.tell(new Status.Failure(error), getSelf()); return goTo(UNKNOWN).using(data.resetSession()); } }
private FSM.State<BaseClientState, BaseClientData> openConnection(final OpenConnection openConnection, final BaseClientData data) { final ActorRef sender = getSender(); final Connection connection = data.getConnection(); final DittoHeaders dittoHeaders = openConnection.getDittoHeaders(); if (canConnectViaSocket(connection)) { doConnectClient(connection, sender); return goTo(CONNECTING).using(setSession(data, sender, dittoHeaders)); } else { cleanupResourcesForConnection(); final DittoRuntimeException error = newConnectionFailedException(data.getConnection(), dittoHeaders); sender.tell(new Status.Failure(error), getSelf()); return goTo(UNKNOWN).using(data.resetSession()); } }
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(); }