Commit 0799c239 authored by Schudel, MJ (Michel)'s avatar Schudel, MJ (Michel)

finding the correct ip adress is hard. simplify the code for now with an...

finding the correct ip adress is hard. simplify the code for now with an optional configuration property, and add something to the readme about this.
parent b0a05f49
......@@ -70,8 +70,6 @@ Er is ook een swagger UI die je kan gebruiken: http://localhost:8080/swagger-ui.
`Ctrl+C` stopt het proces.
#### Snelle turnaround
Tijdens het ontwikkelen van je blockchain backend kan je beter de frontend laten draaien en alleen je backend opnieuw starten.
......@@ -233,6 +231,10 @@ De Blockchain bestaat in alleen in-memory. Het is handig om de blockchain naar d
Een Blockchain op 1 node heeft natuurlijk weinig zin. Daarom gaan we nu de blockchain aansluiten op een peer-to-peer netwerk. In eerste instantie met meerdere nodes op je eigen laptop, maar het zou natuurlijk leuk zijn als we ook verbinding kunnen maken met nodes op machines van je eigen collega's!
*Om jouw node bereikbaar te laten zijn voor anderen: check eerst of het door de applicatie gevonden ip adres (te vinden in de output onder: "node name of this node is") inderdaad het voor anderen bereikbare dns adres is dat door de lokale router is uitgereikt (kijken met bv `ipconfig` in Windows). Is dit niet het geval, zet dan in de `application.properties` de property `node.ipAdress` op de juiste waarde.*
### Aansluiting op het netwerk
De `Network` class bevat al alle functionaliteit om te communiceren met het peer-to-peer network.
......
......@@ -3,13 +3,13 @@ package nl.craftsmen.blockchain.craftscoinnode.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.net.*;
import java.util.Enumeration;
import java.util.List;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Component
public class InstanceInfo {
......@@ -19,33 +19,25 @@ public class InstanceInfo {
private ServerProperties serverProperties;
private int port;
private String node;
private String thisIpAdress;
@Autowired
public InstanceInfo(ServerProperties serverProperties) {
public InstanceInfo(ServerProperties serverProperties, @Value("${node.ipAdress:#{null}}") String thisIpAdress) {
this.serverProperties = serverProperties;
this.thisIpAdress = thisIpAdress;
}
@PostConstruct
public void init() {
String host = "";
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
List<InterfaceAddress> addresses = networkInterface.getInterfaceAddresses();
for (InterfaceAddress interfaceAddress: addresses) {
InetAddress address = interfaceAddress.getAddress();
String hostAddress1 = address.getHostAddress();
if (address.isSiteLocalAddress() && interfaceAddress.getNetworkPrefixLength() <= 16) {
host = hostAddress1;
break;
}
}
if (this.thisIpAdress == null) {
LOGGER.info("property 'thisIpAddress' not set in application.properties, determining ip address...");
this.thisIpAdress = InetAddress.getLocalHost().getHostAddress();
}
this.port = serverProperties.getPort();
this.node = host + ":" + this.port;
this.node = this.thisIpAdress + ":" + this.port;
LOGGER.info("node name of this node is {}", node);
} catch (SocketException e) {
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
......
server.port=8080
bootstrap.peer.host=localhost
bootstrap.peer.port=8080
miningWalletId=me
\ No newline at end of file
miningWalletId=me
#set the property below to the correct, reachable dns address if the application doesn't find out this node's ip address correctly.
#node.ipAdress=x.x.x.x
\ No newline at end of file
......@@ -12,16 +12,25 @@ public class InstanceInfoTest {
private InstanceInfo instanceInfo;
private ServerProperties serverProperties;
@Before
public void setup() {
ServerProperties serverProperties = mock(ServerProperties.class);
serverProperties = mock(ServerProperties.class);
when(serverProperties.getPort()).thenReturn(8080);
instanceInfo = new InstanceInfo(serverProperties);
}
@Test
public void test() {
public void testWithAutomaticIpDiscovery() {
instanceInfo = new InstanceInfo(serverProperties, null);
instanceInfo.init();
assertThat(instanceInfo.getPort()).isEqualTo(8080);
}
@Test
public void testWithConfiguredIp() {
instanceInfo = new InstanceInfo(serverProperties, "192.168.0.170");
instanceInfo.init();
assertThat(instanceInfo.getPort()).isEqualTo(8080);
assertThat(instanceInfo.getNode()).isEqualTo("192.168.0.170:8080");
}
}
\ No newline at end of file
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