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 State<BaseClientState, BaseClientData> clientConnected(final ClientConnected clientConnected, final BaseClientData data) { return ifEventUpToDate(clientConnected, () -> { LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId()); startMessageMappingProcessor(data.getConnection().getMappingContext().orElse(null)); allocateResourcesOnConnection(clientConnected); data.getSessionSender().ifPresent(origin -> origin.tell(new Status.Success(CONNECTED), getSelf())); return goTo(CONNECTED).using(data.resetSession() .setConnectionStatus(ConnectionStatus.OPEN) .setConnectionStatusDetails("Connected at " + Instant.now())); }); }
private State<BaseClientState, BaseClientData> clientConnected(final ClientConnected clientConnected, final BaseClientData data) { return ifEventUpToDate(clientConnected, () -> { LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId()); startMessageMappingProcessor(data.getConnection().getMappingContext().orElse(null)); allocateResourcesOnConnection(clientConnected); data.getSessionSender().ifPresent(origin -> origin.tell(new Status.Success(CONNECTED), getSelf())); return goTo(CONNECTED).using(data.resetSession() .setConnectionStatus(ConnectionStatus.OPEN) .setConnectionStatusDetails("Connected at " + Instant.now())); }); }
private State<BaseClientState, BaseClientData> clientDisconnected(final ClientDisconnected event, final BaseClientData data) { return ifEventUpToDate(event, () -> { LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId()); stopMessageMappingProcessorActor(); cleanupResourcesForConnection(); data.getSessionSender().ifPresent(sender -> sender.tell(new Status.Success(DISCONNECTED), getSelf())); return goTo(DISCONNECTED).using(data.resetSession() .setConnectionStatus(ConnectionStatus.CLOSED) .setConnectionStatusDetails("Disconnected at " + Instant.now())); }); }
private State<BaseClientState, BaseClientData> clientDisconnected(final ClientDisconnected event, final BaseClientData data) { return ifEventUpToDate(event, () -> { LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId()); stopMessageMappingProcessorActor(); cleanupResourcesForConnection(); data.getSessionSender().ifPresent(sender -> sender.tell(new Status.Success(DISCONNECTED), getSelf())); return goTo(DISCONNECTED).using(data.resetSession() .setConnectionStatus(ConnectionStatus.CLOSED) .setConnectionStatusDetails("Disconnected at " + Instant.now())); }); }
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> connectionTimedOut(final Object event, final BaseClientData data) { data.getSessionSender().ifPresent(sender -> { final DittoRuntimeException error = ConnectionFailedException.newBuilder(connectionId()) .dittoHeaders(data.getSessionHeaders()) .build(); sender.tell(new Status.Failure(error), getSelf()); }); cleanupResourcesForConnection(); cleanupFurtherResourcesOnConnectionTimeout(stateName()); return goTo(UNKNOWN).using(data.resetSession() .setConnectionStatus(ConnectionStatus.FAILED) .setConnectionStatusDetails("Connection timed out at " + Instant.now() + " while " + stateName())); }
private FSM.State<BaseClientState, BaseClientData> connectionTimedOut(final Object event, final BaseClientData data) { data.getSessionSender().ifPresent(sender -> { final DittoRuntimeException error = ConnectionFailedException.newBuilder(connectionId()) .dittoHeaders(data.getSessionHeaders()) .build(); sender.tell(new Status.Failure(error), getSelf()); }); cleanupResourcesForConnection(); cleanupFurtherResourcesOnConnectionTimeout(stateName()); return goTo(UNKNOWN).using(data.resetSession() .setConnectionStatus(ConnectionStatus.FAILED) .setConnectionStatusDetails("Connection timed out at " + Instant.now() + " while " + stateName())); }
return goTo(TESTING) .using(setSession(data, sender, testConnection.getDittoHeaders()) .setConnection(connection)
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()) ); }); }
return goTo(TESTING) .using(setSession(data, sender, testConnection.getDittoHeaders()) .setConnection(connection)
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 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 }); }