included java nio (channel and buffer system)

This commit is contained in:
M.Scholz 2012-11-01 12:56:08 +01:00
parent 434e4323ea
commit 615d86fa28

View File

@ -1,11 +1,19 @@
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.
*
*
@ -19,47 +27,49 @@ import java.util.Stack;
*/
class NetworkStack {
private DatagramSocket serverSocket;
//private DatagramSocket serverSocket;
private DatagramChannel channel;
protected static final byte CMD_PUSH = '0';
protected static final byte CMD_POP = '1';
protected static final byte CMD_PRINT = '2';
private Stack<String> stack;
private byte[] inBuffer = new byte[1024];
private byte[] outBuffer = new byte[1024];
public NetworkStack(final int port) throws SocketException {
serverSocket = new DatagramSocket(port);
public NetworkStack(final int port) throws IOException {
stack = new Stack<String>();
DatagramPacket receivePacket;
channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(port));
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.clear();
while (true) {
receivePacket = new DatagramPacket(inBuffer, inBuffer.length);
SocketAddress client = channel.receive(buf); // write stuff from channel to buffer
try {
serverSocket.receive(receivePacket);
} catch (IOException e) {
e.printStackTrace();
}
buf.flip(); // make buffer readable
byte[] receivedData = buf.array();
byte[] receivedData = receivePacket.getData();
byte messageType = receivedData[0];
String received = new String(receivePacket.getData()).trim();
String received = new String(receivedData.toString().trim());
switch (messageType) {
case CMD_PUSH:
String value = new String(receivedData).substring(1).trim();
stack.push(value);
System.out.println("Push " + value);
break;
case CMD_POP:
try {
String popped = stack.pop() + '\n';
send(popped, receivePacket.getAddress(),
receivePacket.getPort());
send(popped, client);
System.out.println("Pop " + popped);
} catch (EmptyStackException e) {
System.out.println("Received pop but stack is empty");
@ -74,36 +84,34 @@ class NetworkStack {
}
result.append('\n');
send(result.toString(), receivePacket.getAddress(),
receivePacket.getPort());
send(result.toString(), client);
break;
default:
System.out.println("Received unknown command: " + "["
+ received + "]");
break;
}
inBuffer = new byte[1024];
buf.clear(); //clear buffer and make it ready to write
}
}
private void send(String text, InetAddress addr, int port) {
outBuffer = text.getBytes();
DatagramPacket sendPacket = new DatagramPacket(outBuffer,
outBuffer.length, addr, port);
private void send(String text, SocketAddress client) {
try {
serverSocket.send(sendPacket);
channel.send(ByteBuffer.wrap(text.getBytes()), client);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[]) {
try {
new NetworkStack(9999);
} catch (SocketException e) {
new NetworkStack(1234);
} catch (IOException e) {
e.printStackTrace();
}
}