Commit b0164b02 authored by Michel Schudel's avatar Michel Schudel

peers logica gefixed.

parent a02a405e
...@@ -20,10 +20,7 @@ import org.springframework.web.client.RestTemplate; ...@@ -20,10 +20,7 @@ import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Component @Component
...@@ -83,16 +80,18 @@ public class Network { ...@@ -83,16 +80,18 @@ public class Network {
} }
} else { } else {
LOGGER.info("registering this node to peers {}", peers); LOGGER.info("registering this node to peers {}", peers);
Set<String> newPeers = new HashSet<>();
for (String peer : peers) { for (String peer : peers) {
ResponseEntity<Set<String>> listResponseEntity = post(peer, "/registernode", instanceInfo.getNode(), new ParameterizedTypeReference<Set<String>>() { ResponseEntity<Set<String>> listResponseEntity = post(peer, "/registernode", instanceInfo.getNode(), new ParameterizedTypeReference<Set<String>>() {
}); });
if (listResponseEntity != null) { if (listResponseEntity != null) {
Set<String> list = listResponseEntity.getBody(); Set<String> list = listResponseEntity.getBody();
LOGGER.info("remote peer {} returned peer list: {}", peer, list); LOGGER.info("remote peer {} returned peer list: {}", peer, list);
peers.addAll(list); newPeers.addAll(list);
peersRepository.savePeers(peers); peersRepository.savePeers(peers);
} }
} }
peers.addAll(newPeers);
} }
} }
...@@ -125,14 +124,16 @@ public class Network { ...@@ -125,14 +124,16 @@ public class Network {
if (!peers.contains(newPeer)) { if (!peers.contains(newPeer)) {
LOGGER.info("peer {} is not previously known to this node. Peer registration will be forwarded to known peer: {}", newPeer, peers); LOGGER.info("peer {} is not previously known to this node. Peer registration will be forwarded to known peer: {}", newPeer, peers);
peers.add(newPeer); peers.add(newPeer);
Set<String> newPeers = new HashSet<>();
for (String node : peers.stream().filter(p -> !p.equals(newPeer)).collect(Collectors.toSet())) { for (String node : peers.stream().filter(p -> !p.equals(newPeer)).collect(Collectors.toSet())) {
ResponseEntity<Set<String>> listResponseEntity = post(node, "/registernode", newPeer, new ParameterizedTypeReference<Set<String>>() { ResponseEntity<Set<String>> listResponseEntity = post(node, "/registernode", newPeer, new ParameterizedTypeReference<Set<String>>() {
}); });
if (listResponseEntity != null) { if (listResponseEntity != null) {
Set<String> list = listResponseEntity.getBody(); Set<String> list = listResponseEntity.getBody();
peers.addAll(list.stream().filter(e -> !e.equals(instanceInfo.getNode())).collect(Collectors.toSet())); newPeers.addAll(list.stream().filter(e -> !e.equals(instanceInfo.getNode())).collect(Collectors.toSet()));
} }
} }
peers.addAll(newPeers);
peersRepository.savePeers(peers); peersRepository.savePeers(peers);
LOGGER.info("adding node {} to the list of known peers.", newPeer); LOGGER.info("adding node {} to the list of known peers.", newPeer);
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment