ugly workaround for IPv4. any better ideas?

This commit is contained in:
M.Scholz 2012-11-21 12:23:20 +01:00
parent 15f863078b
commit 6efc846fb0
2 changed files with 30 additions and 40 deletions

View File

@ -16,6 +16,10 @@ public class Network {
System.setProperty("java.util.logging.config.file",
"logging.properties");
byte[] test = new byte[] {Byte.valueOf("127"), '.'};
String s = new String(test);
System.out.println(s);
try {
LogManager.getLogManager().readConfiguration();
}

View File

@ -31,7 +31,7 @@ public class Node {
private UDPListen udpListen;
public Node() {
System.setProperty("java.net.preferIPv4Stack" , "true"); // optional IPv4 (mac, windows)
//System.setProperty("java.net.preferIPv4Stack" , "true"); // optional IPv4 (mac, windows)
//get a port
Random r = new Random();
@ -106,7 +106,6 @@ public class Node {
buf.put(MessageType.NEW_NEIGHBOR_IPV4);
// TODO: man braucht wahrscheinlich nicht mal .toString()
// a.getHostName() und a.getPort() sollten auch gehen.
for (String part : addr.toString().substring(1).split("\\.")) {
if (!part.contains(":")) {
buf.put(Byte.valueOf(part));
@ -132,43 +131,19 @@ public class Node {
LOGGER.info("Name: " + getName() + ", Leaving...");
//loop over each neighbor i
for (int i = 0; i < neighbors.size(); i++) {
try {
if (neighbors.size() > 2) {
if (i == 0) {
putAddrInBuf(buf, neighbors.get(1));
channel.send(buf, neighbors.get(i));
putAddrInBuf(buf, neighbors.get(neighbors.size() - 1));
channel.send(buf, neighbors.get(i));
} else if (i == neighbors.size() - 1) {
putAddrInBuf(buf, neighbors.get(0));
channel.send(buf, neighbors.get(i));
putAddrInBuf(buf, neighbors.get(i - 1));
channel.send(buf, neighbors.get(i));
} else {
putAddrInBuf(buf, neighbors.get(i - 1));
channel.send(buf, neighbors.get(i));
putAddrInBuf(buf, neighbors.get(i + 1));
channel.send(buf, neighbors.get(i));
}
} else if (neighbors.size() == 2) {
putAddrInBuf(buf, neighbors.get(Math.abs(i - 1)));
for(int j = 0; j < neighbors.size(); j++){
try {
//send all neighbors j to neighbor i
putAddrInBuf(buf, neighbors.get(j));
channel.send(buf, neighbors.get(i));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
//send LEAVE to neighbor i
buf.clear();
buf.put(MessageType.LEAVE);
buf.flip();
@ -179,6 +154,8 @@ public class Node {
e.printStackTrace();
}
}
try {
if (thread != null) {
udpListen.terminate();
@ -255,9 +232,6 @@ public class Node {
break;
case MessageType.NEW_NEIGHBOR_IPV4:
LOGGER.info(name + " received new IPv4 neighbor"
+ receivedFrom.toString() + ": "
+ new String(buf.array()));
byte[] byte_addr = new byte[7];
@ -267,16 +241,28 @@ public class Node {
if (i < 6)
byte_addr[i + 1] = '.';
}
int port = buf.getInt();
//build string (--> workaround)
String address = "";
for(int i = 0; i < byte_addr.length; i++){
if(byte_addr[i] == 46) // skip the dot
continue;
address += byte_addr[i]+".";
}
address = address.substring(0, address.length()-1);
InetSocketAddress new_neighbor = new InetSocketAddress(
new String(byte_addr), port);
address, port);
// check, if we have the neighbor already.
if (!hasNeighbor(new_neighbor)) {
neighbors.add(new_neighbor);
}
LOGGER.info(name + " received new IPv4 neighbor"
+ receivedFrom.toString() + ": "
+ new String(new_neighbor.toString()));
// TODO: send PING to new neighbor and expect wait
// to