| | |
| | | long lastSeenAt = System.currentTimeMillis(); |
| | | int segCursor = 0; |
| | | Integer lastCurrentStationId = null; |
| | | int lastMatchedPathIndex = -1; |
| | | boolean firstRun = true; |
| | | while (true) { |
| | | try { |
| | |
| | | break; |
| | | } |
| | | |
| | | int currentIndex = effectiveFullPath.indexOf(currentStation.getStationId()); |
| | | int currentIndex = resolveCurrentPathIndex( |
| | | effectiveFullPath, |
| | | currentStation.getStationId(), |
| | | lastMatchedPathIndex |
| | | ); |
| | | if (currentIndex < 0) { |
| | | Thread.sleep(500L); |
| | | firstRun = false; |
| | | continue; |
| | | } |
| | | lastMatchedPathIndex = currentIndex; |
| | | |
| | | int remaining = effectiveFullPath.size() - currentIndex - 1; |
| | | if (remaining <= 0) { |
| | |
| | | } |
| | | } |
| | | |
| | | private int resolveCurrentPathIndex(List<Integer> fullPathStationIds, |
| | | Integer currentStationId, |
| | | int lastMatchedPathIndex) { |
| | | if (fullPathStationIds == null || fullPathStationIds.isEmpty() || currentStationId == null) { |
| | | return -1; |
| | | } |
| | | if (lastMatchedPathIndex >= 0 |
| | | && lastMatchedPathIndex < fullPathStationIds.size() |
| | | && equalsInteger(currentStationId, fullPathStationIds.get(lastMatchedPathIndex))) { |
| | | return lastMatchedPathIndex; |
| | | } |
| | | |
| | | int nextIndex = findNextStationIndex(fullPathStationIds, currentStationId, Math.max(lastMatchedPathIndex + 1, 0)); |
| | | if (nextIndex >= 0) { |
| | | return nextIndex; |
| | | } |
| | | return findNextStationIndex(fullPathStationIds, currentStationId, 0); |
| | | } |
| | | |
| | | private int findNextStationIndex(List<Integer> path, Integer stationId, int fromIndex) { |
| | | if (path == null || path.isEmpty() || stationId == null) { |
| | | return -1; |
| | | } |
| | | int startIdx = Math.max(fromIndex, 0); |
| | | for (int i = startIdx; i < path.size(); i++) { |
| | | if (equalsInteger(stationId, path.get(i))) { |
| | | return i; |
| | | } |
| | | } |
| | | return -1; |
| | | } |
| | | |
| | | private boolean sendSegmentWithRetry(StationCommand command, |
| | | StationTaskTraceRegistry traceRegistry, |
| | | Integer traceVersion, |