[cooja] Get rid of all those redundant IP address to String functions

Commonly use the String representation functions of IPUtils.
This commit is contained in:
Enrico Joerns 2014-08-29 12:43:48 +02:00
parent 1babb9475d
commit f93b58b9fb
4 changed files with 12 additions and 76 deletions

View file

@ -44,6 +44,7 @@ import org.contikios.cooja.MoteInterface;
import org.contikios.cooja.mote.memory.MemoryInterface;
import org.contikios.cooja.mote.memory.MemoryInterface.SegmentMonitor;
import org.contikios.cooja.mote.memory.VarMemory;
import org.contikios.cooja.util.IPUtils;
/**
* Read-only interface to IPv4 or IPv6 address.
@ -93,36 +94,14 @@ public class IPAddress extends MoteInterface {
*/
public String getIPString() {
if (isVersion4()) {
String ipString = "";
byte[] ip = moteMem.getByteArray("uip_hostaddr", 4);
for (int i = 0; i < 3; i++) {
ipString += (0xFF & ip[i]) + ".";
}
ipString += (0xFF & ip[3]);
return ipString;
return IPUtils.getIPv4AddressString(ip);
} else if (isVersion6()) {
String ipString = getUncompressedIPv6Address();
return compressIPv6Address(ipString);
return IPUtils.getCompressedIPv6AddressString(getIPv6Address());
}
return null;
}
public static String compressIPv6Address(String ipString) {
if (ipString.contains(":0000:0000:0000:0000:")) {
ipString = ipString.replace(":0000:0000:0000:0000:", "::");
} else if (ipString.contains(":0000:0000:0000:")) {
ipString = ipString.replace(":0000:0000:0000:", "::");
} else if (ipString.contains(":0000:0000:")) {
ipString = ipString.replace(":0000:0000:", "::");
} else if (ipString.contains(":0000:")) {
ipString = ipString.replace(":0000:", "::");
}
while (ipString.contains(":0")) {
ipString = ipString.replaceAll(":0", ":");
}
return ipString;
}
public byte[] getIPv6Address() {
byte[] ip = null;
@ -169,23 +148,6 @@ public class IPAddress extends MoteInterface {
return ip;
}
public static String getUncompressedIPv6AddressString(byte[] ip) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 14; i += 2) {
sb.append(String.format("%02x%02x:", 0xFF & ip[i + 0], 0xFF & ip[i + 1]));
}
sb.append(String.format("%02x%02x", 0xFF & ip[14], 0xFF & ip[15]));
return sb.toString();
}
public String getUncompressedIPv6Address() {
byte[] ip = getIPv6Address();
if (ip == null) {
return "";
}
return getUncompressedIPv6AddressString(ip);
}
/**
* @return True if mote has an IPv4 address
*/

View file

@ -104,6 +104,7 @@ import org.contikios.cooja.mote.memory.MemoryInterface.SegmentMonitor;
import org.contikios.cooja.mote.memory.VarMemory;
import org.contikios.cooja.motes.AbstractEmulatedMote;
import org.contikios.cooja.util.ArrayQueue;
import org.contikios.cooja.util.IPUtils;
import org.contikios.cooja.util.StringUtils;
/**
@ -1696,7 +1697,7 @@ public class BufferListener extends VisPlugin {
} else {
mem = ba.mem;
}
return IPAddress.compressIPv6Address(StringUtils.toHex(mem, 2).replaceAll(" ", ":"));
return IPUtils.getCompressedIPv6AddressString(mem);
}
}
@ -1708,15 +1709,7 @@ public class BufferListener extends VisPlugin {
if (ba.mem.length < 4) {
return "[must monitor at least 4 bytes]";
}
StringBuilder sb = new StringBuilder();
sb.append(0xff&ba.mem[0]);
sb.append(".");
sb.append(0xff&ba.mem[1]);
sb.append(".");
sb.append(0xff&ba.mem[2]);
sb.append(".");
sb.append(0xff&ba.mem[3]);
return sb.toString();
return IPUtils.getIPv4AddressString(ba.mem);
}
}

View file

@ -1,6 +1,6 @@
package org.contikios.cooja.plugins.analyzers;
import org.contikios.cooja.util.StringUtils;
import org.contikios.cooja.util.IPUtils;
public class IPHCPacketAnalyzer extends PacketAnalyzer {
@ -466,9 +466,9 @@ public class IPHCPacketAnalyzer extends PacketAnalyzer {
.append(", FL = ").append(flowLabel)
.append("<br>");
verbose.append("From ");
printAddress(verbose, srcAddress);
IPUtils.getUncompressedIPv6AddressString(verbose, srcAddress);
verbose.append(" to ");
printAddress(verbose, destAddress);
IPUtils.getUncompressedIPv6AddressString(verbose, destAddress);
if (error != null) {
verbose.append(" ").append(error);
}
@ -496,13 +496,4 @@ public class IPHCPacketAnalyzer extends PacketAnalyzer {
}
}
public static void printAddress(StringBuilder out, byte[] address) {
for (int i = 0; i < 16; i += 2) {
out.append(StringUtils.toHex((byte) (address[i] & 0xff))
+ StringUtils.toHex((byte) (address[i + 1] & 0xff)));
if (i < 14) {
out.append(":");
}
}
}
}

View file

@ -1,6 +1,6 @@
package org.contikios.cooja.plugins.analyzers;
import org.contikios.cooja.util.StringUtils;
import org.contikios.cooja.util.IPUtils;
public class IPv6PacketAnalyzer extends PacketAnalyzer {
@ -60,23 +60,13 @@ public class IPv6PacketAnalyzer extends PacketAnalyzer {
.append("</b> TC = ").append(trafficClass)
.append(" FL: ").append(flowLabel).append("<br>");
verbose.append("From ");
printAddress(verbose, srcAddress);
IPUtils.getUncompressedIPv6AddressString(verbose, srcAddress);
verbose.append(" to ");
printAddress(verbose, destAddress);
IPUtils.getUncompressedIPv6AddressString(verbose, destAddress);
packet.lastDispatch = (byte) (proto & 0xff);
packet.level = APPLICATION_LEVEL;
return ANALYSIS_OK_CONTINUE;
}
public static void printAddress(StringBuilder out, byte[] address) {
for (int i = 0; i < 16; i += 2) {
out.append(StringUtils.toHex((byte) (address[i] & 0xff))
+ StringUtils.toHex((byte) (address[i + 1] & 0xff)));
if (i < 14) {
out.append(":");
}
}
}
}