added CMD_SPLIT. sequence "0t", "-e", "-s", "-t" will produce "test" on top of the stack

This commit is contained in:
M.Scholz 2012-11-01 13:32:16 +01:00
parent 615d86fa28
commit d252822cba

View File

@ -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';