- /*
- * We are using tri-state return here to indicate one of three conditions:
- * - if a matching entry is found, return an Optional containing it
- * - if a matching entry is not found, but it makes sense to keep looking at other queues, return null
- * - if a conflicting entry is encountered, indicating we should ignore this request, return an empty Optional
- */
- @SuppressFBWarnings(value = "NP_OPTIONAL_RETURN_NULL",
- justification = "Returning null Optional is documented in the API contract.")
- private static Optional<TransmittedConnectionEntry> findMatchingEntry(final Queue<? extends ConnectionEntry> queue,
- final ResponseEnvelope<?> envelope) {
- // Try to find the request in a queue. Responses may legally come back in a different order, hence we need
- // to use an iterator
- final Iterator<? extends ConnectionEntry> it = queue.iterator();
- while (it.hasNext()) {
- final ConnectionEntry e = it.next();
- final Request<?, ?> request = e.getRequest();
- final Response<?, ?> response = envelope.getMessage();
-
- // First check for matching target, or move to next entry
- if (!request.getTarget().equals(response.getTarget())) {
- continue;
- }
-
- // Sanity-check logical sequence, ignore any out-of-order messages
- if (request.getSequence() != response.getSequence()) {
- LOG.debug("Expecting sequence {}, ignoring response {}", request.getSequence(), envelope);
- return Optional.empty();
- }
-
- // Check if the entry has (ever) been transmitted
- if (!(e instanceof TransmittedConnectionEntry)) {
- return Optional.empty();
- }
-
- final TransmittedConnectionEntry te = (TransmittedConnectionEntry) e;
-
- // Now check session match
- if (envelope.getSessionId() != te.getSessionId()) {
- LOG.debug("Expecting session {}, ignoring response {}", te.getSessionId(), envelope);
- return Optional.empty();
- }
- if (envelope.getTxSequence() != te.getTxSequence()) {
- LOG.warn("Expecting txSequence {}, ignoring response {}", te.getTxSequence(), envelope);
- return Optional.empty();
- }
-
- LOG.debug("Completing request {} with {}", request, envelope);
- it.remove();
- return Optional.of(te);
- }
-
- return null;