diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/src/client/BufferedNetworkStackClient.java b/ws2012/P2P/uebungen/2/p2p_ex2/src/client/BufferedNetworkStackClient.java index aa042a0d..840d3b5a 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/src/client/BufferedNetworkStackClient.java +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/client/BufferedNetworkStackClient.java @@ -20,18 +20,20 @@ public class BufferedNetworkStackClient { buf = ByteBuffer.allocate(BUF_SIZE); } - public void push(String s, String host, int port) throws IOException, + public void push(String text, String host, int port) throws IOException, TimeoutException { if (channel == null) { channel = DatagramChannel.open(); } + String trimmed = text.trim(); + InetSocketAddress dest = new InetSocketAddress(host, port); buf = ByteBuffer.allocate(BUF_SIZE); buf.put(MessageType.CMD_PUSH); - buf.putInt(s.length()); + buf.putInt(trimmed.length()); buf.flip(); channel.send(buf, dest); @@ -54,7 +56,7 @@ public class BufferedNetworkStackClient { int remoteBufSize = buf.getInt(); int sessionId = buf.getInt(); - sendChunks(s, dest, remoteBufSize, sessionId); + sendChunks(trimmed, dest, remoteBufSize, sessionId); } private void sendChunks(String s, InetSocketAddress dest, int bufSize, diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/src/common/Util.java b/ws2012/P2P/uebungen/2/p2p_ex2/src/common/Util.java index 8cf100a4..ecde9f41 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/src/common/Util.java +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/common/Util.java @@ -12,10 +12,8 @@ public class Util { * @return The number of chunks needed */ public static int numChunks(int bufSize, int length) { - /* - * We have to do bufSize - 9 here, because we have 9 bytes of meta data - * in every packet. - */ + // We have to do bufSize - 9 here, because we have 9 bytes of meta data + // in every packet return (int) Math.ceil(length * 1.0 / actualData(bufSize)); } diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/src/tests/BufferedNetworkStackTests.java b/ws2012/P2P/uebungen/2/p2p_ex2/src/tests/BufferedNetworkStackTests.java index 9810ae89..c30b0ef0 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/src/tests/BufferedNetworkStackTests.java +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/tests/BufferedNetworkStackTests.java @@ -44,7 +44,7 @@ public class BufferedNetworkStackTests { List values = new ArrayList(); for (int i = 0; i < 100; i++) { - String uuid = UUID.randomUUID().toString(); + String uuid = UUID.randomUUID().toString().trim(); values.add(uuid); try { @@ -76,7 +76,37 @@ public class BufferedNetworkStackTests { for (int j = 0; j < numOfIds; j++) { uuidBuilder.append(UUID.randomUUID().toString()); } - String uuid = uuidBuilder.toString(); + String uuid = uuidBuilder.toString().trim(); + + values.add(uuid); + try { + client.push(uuid, HOST, PORT); + } catch (IOException e) { + fail("Couldn't send to server. " + e); + } catch (TimeoutException e) { + fail("Connection timed out. " + e); + } + } + + for (int i = values.size() - 1; i >= 0; i--) { + try { + assertEquals(values.get(i), client.pop(HOST, PORT)); + } catch (IOException | TimeoutException e) { + fail("Couldn't send to server" + e); + } + } + } + + @Test + public void testRandomLongLengthWithSpaces() { + List values = new ArrayList(); + + for (int i = 0; i < 100; i++) { + StringBuilder uuidBuilder = new StringBuilder(4 * 128); + for (int j = 0; j < 4; j++) { + uuidBuilder.append(UUID.randomUUID().toString()).append(" "); + } + String uuid = uuidBuilder.toString().trim(); values.add(uuid); try {