Skip to content

Commit 2c62879

Browse files
committed
Merge branch 'origin/review/idFinder-base' into develop
2 parents abf2193 + 6bad1d3 commit 2c62879

File tree

2 files changed

+89
-73
lines changed

2 files changed

+89
-73
lines changed

webdictionary/src/main/java/com/eis0/kademlianetwork/informationdeliverymanager/IdFinderHandler.java

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,17 @@
3838
* @author Edoardo Raimondi (added the check cicle to verify if the target node is 'alive')
3939
*/
4040
public class IdFinderHandler {
41-
private static final int ATTEMPTS = 7;
41+
private static int maxAttempts = 10;
4242
private static final String ID_TO_FIND_NULL = "The idToFind cannot be null";
4343
private static final String SEARCHER_NULL = "The searcher cannot be null";
4444
private static final String NODES_LIST_NULL = "The nodeList cannot be null";
4545
private static final String NODES_LIST_EMPTY = "The nodeList cannot be empty";
46+
private static final String INSUFFICIENT_ATTEMPTS = "The attemptsNumber must be higher then zero";
4647

4748
/**
4849
* Searches for a specific Id, which needs to be closer to a given resource Id.
49-
* The list of possible nodes is formed by the closest nodes, and it always contains the local node
50-
* (if it doesn't, the node is automatically added)
50+
* The list of possible nodes is formed by the closest nodes, and it always contains the local
51+
* node (if it doesn't, the node is automatically added)
5152
*
5253
* @param idToFind The ID to find in the network
5354
* @param searcher The Peer which is searching for the ID
@@ -59,8 +60,13 @@ public static void searchId(@NonNull KademliaId idToFind, @NonNull SMSPeer searc
5960
//Obtain the local RoutingTable
6061
SMSKademliaRoutingTable table = KademliaJoinableNetwork.getInstance().getLocalRoutingTable();
6162
//Create a list containing the closest nodes (five is more than enough)
62-
List<SMSKademliaNode> nodeList = table.findClosest(idToFind, ATTEMPTS);
63-
63+
List<SMSKademliaNode> nodeList = table.findClosest(idToFind, maxAttempts);
64+
//Local node, inserted in the list if not already present
65+
SMSKademliaNode local = KademliaJoinableNetwork.getInstance().getLocalNode();
66+
if (!nodeList.contains(local)) {
67+
//inserted in the last position, must be the last node to be checked
68+
nodeList.add(local);
69+
}
6470
searchIdList(idToFind, searcher, nodeList);
6571
}
6672

@@ -72,16 +78,10 @@ public static void searchId(@NonNull KademliaId idToFind, @NonNull SMSPeer searc
7278
* @param searcher The peer which is searching for the ID
7379
* @param nodeList
7480
*/
75-
public static void searchIdList(@NonNull KademliaId idToFind, @NonNull SMSPeer searcher, @NonNull List<SMSKademliaNode> nodeList) {
81+
public static void searchIdList(@NonNull KademliaId idToFind, @NonNull SMSPeer searcher,
82+
@NonNull List<SMSKademliaNode> nodeList) {
7683
if (nodeList.isEmpty()) throw new IllegalArgumentException(NODES_LIST_EMPTY);
7784
if (nodeList == null) throw new NullPointerException(NODES_LIST_NULL);
78-
79-
//Local node, inserted in the list if not already present
80-
SMSKademliaNode local = KademliaJoinableNetwork.getInstance().getLocalNode();
81-
if (!nodeList.contains(local)) {
82-
//inserted in the last position, must be the last node to be checked
83-
nodeList.add(local);
84-
}
8585
//Get the node with the node ID closest to the idToFind
8686
//The first node in the nodeList is the one with the node Id closest to the idToFind
8787
SMSKademliaNode closestNode = nodeList.get(0);
@@ -113,7 +113,8 @@ private static void sendResult(KademliaId idToFind, SMSPeer targetPeer) {
113113
.buildMessage();
114114
if (targetPeer.equals(KademliaJoinableNetwork.getInstance().getLocalNode().getPeer())) {
115115
//If I'm searching the id for myself, then I have a pending request I can directly fulfill
116-
KademliaJoinableNetwork.getInstance().getRequestsHandler().completeFindIdRequest(idToFind, targetPeer);
116+
KademliaJoinableNetwork.getInstance().getRequestsHandler().
117+
completeFindIdRequest(idToFind, targetPeer);
117118
return;
118119
}
119120
SMSManager.getInstance().sendMessage(searchResult);
@@ -151,7 +152,8 @@ private static void keepLooking(KademliaId idToFind, SMSPeer searcherNode, SMSPe
151152
* anymore, it is removed from the RoutingTable, and the research is started
152153
* again
153154
*/
154-
private static void retryIfDead(KademliaId idToFind, SMSPeer searcherNode, SMSPeer nodeToCheck, List<SMSKademliaNode> nodeList) {
155+
private static void retryIfDead(KademliaId idToFind, SMSPeer searcherNode, SMSPeer nodeToCheck,
156+
List<SMSKademliaNode> nodeList) {
155157
if (!KademliaJoinableNetwork.getInstance().isAlive(nodeToCheck)) {
156158
//The first node is the one tested, if I'm here it failed.
157159
//I remove it and restart the process
@@ -160,4 +162,21 @@ private static void retryIfDead(KademliaId idToFind, SMSPeer searcherNode, SMSPe
160162
searchIdList(idToFind, searcherNode, nodeList);
161163
}
162164
}
165+
166+
/**
167+
* The number of maximum attempts for the research of the node in the RoutingTable is by default
168+
* set to 10; the reason for the existence of this value is to assure that, if the node can't
169+
* reach a large number of nodes, it can still insert the resource inside of the Network; the
170+
* resource will be then relocated during the refresh, IF the unresponsive node returns to live.
171+
* If it doesn't, then the node chosen as closest one remains the closest one.
172+
* It must also be considered the time that each attempts takes (10s)
173+
* Warning: when the application is closed the number of attempts is reset
174+
*
175+
* @param attemptsNumber The maximum number of attempts before saving the resource in the local
176+
* dictionary
177+
*/
178+
public static void setAttemptsNumber(int attemptsNumber) {
179+
if (attemptsNumber < 1) throw new IllegalArgumentException(INSUFFICIENT_ATTEMPTS);
180+
maxAttempts = attemptsNumber;
181+
}
163182
}

webdictionary/src/test/java/com/eis0/kademlianetwork/IdFinderHandlerTest.java

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@
2929
@RunWith(PowerMockRunner.class)
3030
@PrepareForTest({KademliaNetwork.class, SMSManager.class, KademliaJoinableNetwork.class})
3131
public class IdFinderHandlerTest {
32-
//SEARCHER
33-
private SMSPeer SEARCHER_PEER;
34-
private KademliaId SEARCHER_ID;
35-
private SMSKademliaNode SEARCHER;
32+
//searcher
33+
private SMSPeer searcherPeer;
34+
private KademliaId searcherId;
35+
private SMSKademliaNode searcher;
3636
//NODE1
37-
private SMSPeer VALID_PEER1;
38-
private KademliaId VALID_NODE1_ID;
39-
private SMSKademliaNode VALID_NODE1;
37+
private SMSPeer validPeer1;
38+
private KademliaId validNodeId1;
39+
private SMSKademliaNode validNode1;
4040
//NODE2
41-
private SMSPeer VALID_PEER2;
42-
private KademliaId VALID_NODE2_ID;
43-
private SMSKademliaNode VALID_NODE2;
41+
private SMSPeer validPeer2;
42+
private KademliaId validNodeId2;
43+
private SMSKademliaNode validNode2;
4444

4545
private KademliaJoinableNetwork networkMock;
4646
private SMSManager smsManagerMock;
@@ -51,19 +51,19 @@ public class IdFinderHandlerTest {
5151
@Before
5252
public void setup() {
5353
//Searcher
54-
SEARCHER_PEER = new SMSPeer("+393423541601");
55-
SEARCHER_ID = new KademliaId(SEARCHER_PEER);
56-
SEARCHER = new SMSKademliaNode(SEARCHER_PEER);
54+
searcherPeer = new SMSPeer("+393423541601");
55+
searcherId = new KademliaId(searcherPeer);
56+
searcher = new SMSKademliaNode(searcherPeer);
5757
//Node1
58-
VALID_PEER1 = new SMSPeer("+393335552121");
59-
VALID_NODE1_ID = new KademliaId(VALID_PEER1);
60-
VALID_NODE1 = new SMSKademliaNode(VALID_PEER1);
58+
validPeer1 = new SMSPeer("+393335552121");
59+
validNodeId1 = new KademliaId(validPeer1);
60+
validNode1 = new SMSKademliaNode(validPeer1);
6161
//Node2
62-
VALID_PEER2 = new SMSPeer("+393423541602");
63-
VALID_NODE2_ID = new KademliaId(VALID_PEER2);
64-
VALID_NODE2 = new SMSKademliaNode(VALID_PEER2);
62+
validPeer2 = new SMSPeer("+393423541602");
63+
validNodeId2 = new KademliaId(validPeer2);
64+
validNode2 = new SMSKademliaNode(validPeer2);
6565
//RoutingTable
66-
routingTable = new SMSKademliaRoutingTable(SEARCHER, new DefaultConfiguration());
66+
routingTable = new SMSKademliaRoutingTable(searcher, new DefaultConfiguration());
6767

6868
//MOCK
6969
networkMock = mock(KademliaJoinableNetwork.class);
@@ -75,48 +75,45 @@ public void setup() {
7575
PowerMockito.mockStatic(KademliaJoinableNetwork.class);
7676
PowerMockito.when(KademliaJoinableNetwork.getInstance()).thenReturn(networkMock);
7777

78-
when(networkMock.getLocalNode()).thenReturn(SEARCHER);
78+
when(networkMock.getLocalNode()).thenReturn(searcher);
7979
when(networkMock.getLocalRoutingTable()).thenReturn(routingTable);
8080

81-
when(networkMock.isAlive(SEARCHER_PEER)).thenReturn(true);
82-
when(networkMock.isAlive(VALID_PEER1)).thenReturn(true);
83-
when(networkMock.isAlive(VALID_PEER2)).thenReturn(false);
84-
81+
when(networkMock.isAlive(searcherPeer)).thenReturn(true);
82+
when(networkMock.isAlive(validPeer1)).thenReturn(true);
83+
when(networkMock.isAlive(validPeer2)).thenReturn(false);
8584

8685
when(networkMock.getRequestsHandler()).thenReturn(requestsHandler);
8786

88-
//when(networkMock.isNodeInNetwork(VALID_NODE2)).thenReturn(true);
89-
9087
}
9188

9289
@Test(expected = NullPointerException.class)
9390
public void nullId_throws() {
94-
IdFinderHandler.searchId(null, SEARCHER_PEER);
91+
IdFinderHandler.searchId(null, searcherPeer);
9592
}
9693

9794
@Test(expected = NullPointerException.class)
9895
public void nullSearcher_throws() {
99-
IdFinderHandler.searchId(VALID_NODE1_ID, null);
96+
IdFinderHandler.searchId(validNodeId1, null);
10097
}
10198

10299
/**
103100
* This test starts a searchId operation, that should end with the calling Request being closed,
104-
* but the Request doesn't exist; as it is written, the code simply ignore the situation after
105-
* having started the research
101+
* but the Request doesn't exist; in the way it is written, the code should simply ignore the
102+
* situation after having started the research (no Exceptions)
106103
*/
107104
@Test
108-
public void nullRequest_throws() {
109-
IdFinderHandler.searchId(VALID_NODE1_ID, SEARCHER_PEER);
105+
public void closingUnexistingRequest_noExcpetionThrow() {
106+
IdFinderHandler.searchId(validNodeId1, searcherPeer);
110107
}
111108

112109
/**
113110
* In case the searched Id corresponds to my Id, there is no research made by SMS messages
114111
*/
115112
@Test
116113
public void findMyId() {
117-
IdFinderHandler.searchId(SEARCHER_ID, SEARCHER_PEER);
118-
String expectedTextMessage = RequestTypes.Ping.ordinal() + " " + SEARCHER_ID + " / / /";
119-
SMSMessage expectedMessage = new SMSMessage(SEARCHER_PEER, expectedTextMessage);
114+
IdFinderHandler.searchId(searcherId, searcherPeer);
115+
String expectedTextMessage = RequestTypes.Ping.ordinal() + " " + searcherId + " / / /";
116+
SMSMessage expectedMessage = new SMSMessage(searcherPeer, expectedTextMessage);
120117
verify(smsManagerMock, times(0)).sendMessage(expectedMessage);
121118
}
122119

@@ -127,8 +124,8 @@ public void findMyId() {
127124
*/
128125
@Test
129126
public void findNotMyId_inserted() {
130-
routingTable.insert(VALID_NODE1);
131-
IdFinderHandler.searchId(VALID_NODE1_ID, SEARCHER_PEER);
127+
routingTable.insert(validNode1);
128+
IdFinderHandler.searchId(validNodeId1, searcherPeer);
132129
verify(smsManagerMock, times(1)).sendMessage(any(SMSMessage.class));
133130
}
134131

@@ -139,8 +136,8 @@ public void findNotMyId_inserted() {
139136
*/
140137
@Test
141138
public void findNotMyId_inserted_DEAD() {
142-
routingTable.insert(VALID_NODE2);
143-
IdFinderHandler.searchId(VALID_NODE2_ID, SEARCHER_PEER);
139+
routingTable.insert(validNode2);
140+
IdFinderHandler.searchId(validNodeId2, searcherPeer);
144141
verify(smsManagerMock, times(1)).sendMessage(any(SMSMessage.class));
145142
}
146143

@@ -150,16 +147,16 @@ public void findNotMyId_inserted_DEAD() {
150147
*/
151148
@Test
152149
public void findNotMyId_notInserted() {
153-
IdFinderHandler.searchId(VALID_NODE1_ID, SEARCHER_PEER);
150+
IdFinderHandler.searchId(validNodeId1, searcherPeer);
154151
verify(smsManagerMock, times(0)).sendMessage(any(SMSMessage.class));
155152
}
156153

157154

158155
@Test
159156
public void findIdInTable_notSearchedByMe() {
160-
IdFinderHandler.searchId(SEARCHER_ID, VALID_PEER1);
161-
String expectedTextMessage = RequestTypes.FindIdSearchResult.ordinal() + " " + SEARCHER + " / / /";
162-
SMSMessage expectedMessage = new SMSMessage(VALID_PEER1, expectedTextMessage);
157+
IdFinderHandler.searchId(searcherId, validPeer1);
158+
String expectedTextMessage = RequestTypes.FindIdSearchResult.ordinal() + " " + searcher + " / / /";
159+
SMSMessage expectedMessage = new SMSMessage(validPeer1, expectedTextMessage);
163160
verify(smsManagerMock, times(1)).sendMessage(expectedMessage);
164161
}
165162

@@ -168,20 +165,20 @@ public void findIdInTable_notSearchedByMe() {
168165
*/
169166
@Test
170167
public void findIdInTable_notSearchedByMe_DEAD() {
171-
IdFinderHandler.searchId(SEARCHER_ID, VALID_PEER2);
172-
String expectedTextMessage = RequestTypes.FindIdSearchResult.ordinal() + " " + SEARCHER + " / / /";
173-
SMSMessage expectedMessage = new SMSMessage(VALID_PEER2, expectedTextMessage);
168+
IdFinderHandler.searchId(searcherId, validPeer2);
169+
String expectedTextMessage = RequestTypes.FindIdSearchResult.ordinal() + " " + searcher + " / / /";
170+
SMSMessage expectedMessage = new SMSMessage(validPeer2, expectedTextMessage);
174171
verify(smsManagerMock, times(1)).sendMessage(expectedMessage);
175172
}
176173

177174
@Test
178175
public void forwardRequest() {
179-
routingTable.insert(VALID_NODE1);
180-
IdFinderHandler.searchId(VALID_NODE1_ID, SEARCHER_PEER);
176+
routingTable.insert(validNode1);
177+
IdFinderHandler.searchId(validNodeId1, searcherPeer);
181178
String expectedTextMessage =
182179
RequestTypes.FindId.ordinal() + " " +
183-
VALID_NODE1_ID + " " + SEARCHER_PEER + " / /";
184-
SMSMessage expectedMessage = new SMSMessage(VALID_PEER1, expectedTextMessage);
180+
validNodeId1 + " " + searcherPeer + " / /";
181+
SMSMessage expectedMessage = new SMSMessage(validPeer1, expectedTextMessage);
185182
verify(smsManagerMock, times(1)).sendMessage(expectedMessage);
186183
}
187184

@@ -196,9 +193,9 @@ public void forwardRequest() {
196193
@Test
197194
public void noLocalNode_localNodeSearched() {
198195
List<SMSKademliaNode> nodes = routingTable.getAllNodes();
199-
nodes.add(VALID_NODE1);
200-
nodes.remove(SEARCHER);
201-
IdFinderHandler.searchIdList(SEARCHER_ID, SEARCHER_PEER, nodes);
196+
nodes.add(validNode1);
197+
nodes.remove(searcher);
198+
IdFinderHandler.searchIdList(searcherId, searcherPeer, nodes);
202199
verify(smsManagerMock, times(1)).sendMessage(any(SMSMessage.class));
203200
}
204201

@@ -208,10 +205,10 @@ public void noLocalNode_localNodeSearched() {
208205
@Test
209206
public void noLocalNode_notLocalNodeSearched() {
210207
List<SMSKademliaNode> nodes = routingTable.getAllNodes();
211-
nodes.add(VALID_NODE1);
212-
nodes.remove(SEARCHER);
213-
IdFinderHandler.searchIdList(VALID_NODE1_ID, SEARCHER_PEER, nodes);
214-
//A message is sent to the VALID_NODE1
208+
nodes.add(validNode1);
209+
nodes.remove(searcher);
210+
IdFinderHandler.searchIdList(validNodeId1, searcherPeer, nodes);
211+
//A message is sent to the validNode1
215212
verify(smsManagerMock, times(1)).sendMessage(any(SMSMessage.class));
216213
}
217214
}

0 commit comments

Comments
 (0)