@Override public Optional<Message> generate(Connection connection) { if (connection.getState().equals(ConnectionState.NOT_CONNECTED)) { ConnectMessage connectMessage = generateConnectMessage(); connection.setState(ConnectionState.PENDING_CONNECTION); connection.setClientId(connectMessage.getId()); return Optional.of(connectMessage); } else if (connection.getState().equals(ConnectionState.PENDING_CONNECTION)) { // Connect message is still being processed return Optional.empty(); } else { PublishMessage publishMessage = generatePublishMessage(); return Optional.of(publishMessage); } }
private ConnectMessage generateConnectMessage() { ConnectMessage connectMessage = (ConnectMessage) MessageFactory.getMessageInstance(MessageType.CONNECT); connectMessage.setId(Math.abs(new Random().nextInt())); return connectMessage; }