88import samba .domain .messages .requests .FindContent ;
99import samba .network .history .HistoryNetwork ;
1010
11+ import java .util .Collections ;
1112import java .util .HashSet ;
1213import java .util .List ;
1314import java .util .Optional ;
@@ -36,18 +37,31 @@ public class RecursiveLookupTaskFindContent {
3637 private Optional <FindContentResult > content = Optional .empty ();
3738 private final int timeout ;
3839 private final Set <NodeRecord > interestedNodes = new HashSet <>();
40+ private final Set <NodeRecord > excludedNodes = new HashSet <>();
41+ private Optional <NodeRecord > respondingNode ;
3942
4043 public RecursiveLookupTaskFindContent (
4144 final HistoryNetwork historyNetwork ,
4245 final Bytes contentKey ,
4346 final Bytes homeNodeId ,
4447 final Set <NodeRecord > foundNodes ,
4548 final int timeout ) {
49+ this (historyNetwork , contentKey , homeNodeId , foundNodes , Set .of (), timeout );
50+ }
51+
52+ public RecursiveLookupTaskFindContent (
53+ final HistoryNetwork historyNetwork ,
54+ final Bytes contentKey ,
55+ final Bytes homeNodeId ,
56+ final Set <NodeRecord > foundNodes ,
57+ final Set <NodeRecord > excludedNodes ,
58+ final int timeout ) {
4659 this .historyNetwork = historyNetwork ;
4760 this .contentKey = contentKey ;
4861 this .queriedNodeIds .add (homeNodeId );
4962 this .foundNodes .addAll (foundNodes );
5063 this .timeout = timeout ;
64+ this .excludedNodes .addAll (excludedNodes );
5165 }
5266
5367 public CompletableFuture <Optional <FindContentResult >> execute () {
@@ -61,6 +75,7 @@ private synchronized void sendRequests() {
6175 return ;
6276 }
6377 if (content .isPresent ()) {
78+ LOG .error ("No nodes left to query" );
6479 future .complete (content );
6580 return ;
6681 }
@@ -72,7 +87,7 @@ private synchronized void sendRequests() {
7287 .collect (Collectors .toList ());
7388
7489 if (nodesToQuery .isEmpty ()) {
75- future .completeExceptionally ( new RuntimeException ( "No nodes left to query." ) );
90+ future .complete ( content );
7691 return ;
7792 }
7893
@@ -99,14 +114,17 @@ private void queryPeer(final NodeRecord peer) {
99114 LOG .debug ("Node {} returned empty result" , peer .getNodeId ());
100115 } else {
101116 FindContentResult contentResult = result .get ();
102- if (contentResult .getContent () != null ) {
117+ if (contentResult .getContent () != null && !excludedNodes .contains (peer )) {
118+ this .respondingNode = Optional .of (peer );
103119 content = Optional .of (contentResult );
104120 future .complete (content );
105121 return ;
106122 }
107123 // Add nodes to foundNodes and continue searching
108124 foundNodes .addAll (
109- contentResult .getEnrs ().stream ()
125+ Optional .ofNullable (contentResult .getEnrs ())
126+ .orElseGet (Collections ::emptyList )
127+ .stream ()
110128 .map (historyNetwork ::nodeRecordFromEnr )
111129 .flatMap (Optional ::stream )
112130 .filter (node -> !queriedNodeIds .contains (node .getNodeId ()))
@@ -133,4 +151,8 @@ private void queryPeer(final NodeRecord peer) {
133151 public Set <NodeRecord > getInterestedNodes () {
134152 return interestedNodes ;
135153 }
154+
155+ public Optional <NodeRecord > getRespondingNode () {
156+ return respondingNode ;
157+ }
136158}
0 commit comments