fixed string.split(), fixed indices

This commit is contained in:
M.Scholz 2012-11-21 10:30:58 +01:00
parent bd184a6cc2
commit 43c34c7bec

View File

@ -9,6 +9,7 @@ import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;
import common.MessageType;
@ -30,11 +31,27 @@ public class Node {
private UDPListen udpListen;
public Node() {
<<<<<<< HEAD
// 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();
boolean goodPort = false;
int port = 0;
while(!goodPort){
port = r.nextInt(65000);
if(port > 1024){
goodPort = true;
}
}
>>>>>>> fixed string.split(), fixed indices
try {
channel = DatagramChannel.open();
channel.socket().bind(null);
channel.socket().bind(new InetSocketAddress("localhost", port));
channel.configureBlocking(false);
buf = ByteBuffer.allocate(BUF_SIZE);
@ -94,13 +111,13 @@ 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().split(".")) {
for (String part : addr.toString().substring(1).split("\\.")) {
if (!part.contains(":")) {
buf.put(Byte.valueOf(part));
} else {
// Last part (contains port)
String[] lastPart = part.split(":");
String[] lastPart = part.split("\\:");
buf.put(Byte.valueOf(lastPart[0]));
buf.putInt(Integer.valueOf(lastPart[1]));
}
@ -116,6 +133,8 @@ public class Node {
* Circularly link all neighbors, remove itself form all neighbors and exit.
*/
public void leave() {
System.out.println(neighbors);
LOGGER.info("Name: " + getName() + ", Leaving...");
for (int i = 0; i < neighbors.size(); i++) {
@ -244,13 +263,13 @@ public class Node {
LOGGER.info(name + " received new IPv6 neighbor"
+ receivedFrom.toString() + ": "
+ new String(buf.array()));
byte[] byte_addr = new byte[7];
// Read 4 Bytes and 1 Int
for (int i = 0; i < 8; i = i + 2) {
for (int i = 0; i < 7; i = i + 2) {
byte_addr[i] = buf.get();
if (i < 7)
if (i < 6)
byte_addr[i + 1] = '.';
}