From d252822cba323c53ee2e8f0ec58cd217bc85e1c5 Mon Sep 17 00:00:00 2001 From: "M.Scholz" Date: Thu, 1 Nov 2012 13:32:16 +0100 Subject: [PATCH] added CMD_SPLIT. sequence "0t", "-e", "-s", "-t" will produce "test" on top of the stack --- GIT/P2P/uebungen/2/p2p_ex2/src/NetworkStack.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/GIT/P2P/uebungen/2/p2p_ex2/src/NetworkStack.java b/GIT/P2P/uebungen/2/p2p_ex2/src/NetworkStack.java index 10664db2..15c6740b 100644 --- a/GIT/P2P/uebungen/2/p2p_ex2/src/NetworkStack.java +++ b/GIT/P2P/uebungen/2/p2p_ex2/src/NetworkStack.java @@ -1,19 +1,13 @@ package u1; import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; -import java.net.SocketException; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; import java.util.EmptyStackException; import java.util.Stack; -import javax.sound.sampled.ReverbType; - /* Truncated messages (happens after 1024 bytes) are not handled as such by the server. * * @@ -32,6 +26,7 @@ class NetworkStack { private DatagramChannel channel; protected static final byte CMD_PUSH = '0'; + protected static final byte CMD_SPLIT = '-'; protected static final byte CMD_POP = '1'; protected static final byte CMD_PRINT = '2'; @@ -44,7 +39,7 @@ class NetworkStack { channel = DatagramChannel.open(); channel.socket().bind(new InetSocketAddress(port)); - ByteBuffer buf = ByteBuffer.allocate(1024); + ByteBuffer buf = ByteBuffer.allocate(2); buf.clear(); @@ -66,6 +61,13 @@ class NetworkStack { System.out.println("Push " + value); break; + case CMD_SPLIT: + String old = stack.pop(); + String valueNew = old + new String(receivedData).substring(1).trim(); + stack.push(valueNew); + System.out.println("Pop previous message: " + old + " and Push " + valueNew); + break; + case CMD_POP: try { String popped = stack.pop() + '\n';