Created a ListenThread
This commit is contained in:
parent
aa48947b78
commit
bc6b9d1563
@ -14,13 +14,17 @@ import common.MessageType;
|
||||
class BufferedNetworkStack {
|
||||
|
||||
private DatagramChannel channel;
|
||||
private ByteBuffer buf;
|
||||
|
||||
protected static final int BUF_SIZE = 10;
|
||||
protected static final int BUF_SIZE = 1024;
|
||||
|
||||
private Stack<String> stack;
|
||||
|
||||
private ArrayList<Element> temp;
|
||||
|
||||
private ListenThread listenThread;
|
||||
|
||||
public BufferedNetworkStack(final int port) throws IOException {
|
||||
|
||||
stack = new Stack<String>();
|
||||
@ -29,95 +33,10 @@ class BufferedNetworkStack {
|
||||
channel = DatagramChannel.open();
|
||||
channel.socket().bind(new InetSocketAddress(port));
|
||||
|
||||
StringBuilder received;
|
||||
buf = ByteBuffer.allocate(BUF_SIZE);
|
||||
|
||||
ByteBuffer buf = ByteBuffer.allocate(BUF_SIZE);
|
||||
buf.clear();
|
||||
|
||||
while (true) {
|
||||
// write stuff from channel to buffer
|
||||
SocketAddress client = channel.receive(buf);
|
||||
|
||||
// make buffer readable
|
||||
buf.flip();
|
||||
|
||||
byte messageType = buf.get();
|
||||
|
||||
switch (messageType) {
|
||||
case MessageType.CMD_PUSH:
|
||||
int size = buf.getInt();
|
||||
|
||||
int id = temp.size();
|
||||
temp.add(id, new Element(size, id));
|
||||
|
||||
buf.flip();
|
||||
buf.clear();
|
||||
|
||||
buf.putInt(BUF_SIZE);
|
||||
buf.putInt(id);
|
||||
buf.flip();
|
||||
channel.send(buf, client);
|
||||
break;
|
||||
|
||||
case MessageType.CMD_DATA:
|
||||
int sessionId = buf.getInt();
|
||||
int chunkId = buf.getInt();
|
||||
|
||||
received = new StringBuilder();
|
||||
while (buf.hasRemaining()) {
|
||||
received.append((char) buf.get());
|
||||
}
|
||||
|
||||
// System.out.println("Received chunk #" + chunkId +
|
||||
// " of data #"
|
||||
// + sessionId + ": " + received);
|
||||
|
||||
for (Element e : temp) {
|
||||
if (e.id == sessionId) {
|
||||
e.chunks[chunkId] = received.toString();
|
||||
|
||||
if (e.isComplete()) {
|
||||
temp.remove(e);
|
||||
stack.add(e.toString());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case MessageType.CMD_POP:
|
||||
try {
|
||||
String popped = stack.pop();
|
||||
send(popped, client);
|
||||
System.out.println("Pop " + popped);
|
||||
|
||||
} catch (EmptyStackException e) {
|
||||
System.out.println("Received pop but stack is empty");
|
||||
}
|
||||
break;
|
||||
|
||||
case MessageType.CMD_PRINT:
|
||||
System.out.println(toString());
|
||||
break;
|
||||
|
||||
default:
|
||||
received = new StringBuilder();
|
||||
while (buf.hasRemaining()) {
|
||||
int b = buf.get();
|
||||
received.append((char) b);
|
||||
System.out.println("Received " + (char) b);
|
||||
}
|
||||
|
||||
System.out.println("Received unknown command: " + "["
|
||||
+ received.toString() + "]");
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
// clear buffer and make it ready to write
|
||||
buf = ByteBuffer.allocate(BUF_SIZE);
|
||||
// buf.clear();
|
||||
}
|
||||
listenThread = new ListenThread();
|
||||
listenThread.start();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
@ -131,13 +50,125 @@ class BufferedNetworkStack {
|
||||
}
|
||||
|
||||
private void send(String text, SocketAddress client) {
|
||||
public void stop() {
|
||||
listenThread.setStopping();
|
||||
|
||||
try {
|
||||
channel.send(ByteBuffer.wrap(text.getBytes()), client);
|
||||
channel.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private class ListenThread extends Thread {
|
||||
private boolean running = false;
|
||||
|
||||
public void run() {
|
||||
running = true;
|
||||
StringBuilder received;
|
||||
SocketAddress client = null;
|
||||
|
||||
while (running) {
|
||||
// write stuff from channel to buffer
|
||||
try {
|
||||
client = channel.receive(buf);
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
// make buffer readable
|
||||
buf.flip();
|
||||
|
||||
byte messageType = buf.get();
|
||||
|
||||
switch (messageType) {
|
||||
case MessageType.CMD_PUSH:
|
||||
int size = buf.getInt();
|
||||
|
||||
int id = temp.size();
|
||||
temp.add(id, new Element(size, id));
|
||||
|
||||
buf.flip();
|
||||
buf.clear();
|
||||
|
||||
buf.putInt(BUF_SIZE);
|
||||
buf.putInt(id);
|
||||
buf.flip();
|
||||
try {
|
||||
channel.send(buf, client);
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
break;
|
||||
|
||||
case MessageType.CMD_DATA:
|
||||
int sessionId = buf.getInt();
|
||||
int chunkId = buf.getInt();
|
||||
|
||||
received = new StringBuilder();
|
||||
while (buf.hasRemaining()) {
|
||||
received.append((char) buf.get());
|
||||
}
|
||||
|
||||
// System.out.println("Received chunk #" + chunkId +
|
||||
// " of data #"
|
||||
// + sessionId + ": " + received);
|
||||
|
||||
for (Element e : temp) {
|
||||
if (e.id == sessionId) {
|
||||
e.chunks[chunkId] = received.toString();
|
||||
|
||||
if (e.isComplete()) {
|
||||
temp.remove(e);
|
||||
stack.add(e.toString());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case MessageType.CMD_POP:
|
||||
try {
|
||||
String popped = stack.pop();
|
||||
send(popped, client);
|
||||
System.out.println("Pop " + popped);
|
||||
|
||||
} catch (EmptyStackException e) {
|
||||
System.out.println("Received pop but stack is empty");
|
||||
}
|
||||
break;
|
||||
|
||||
case MessageType.CMD_PRINT:
|
||||
System.out.println(toString());
|
||||
break;
|
||||
|
||||
default:
|
||||
received = new StringBuilder();
|
||||
while (buf.hasRemaining()) {
|
||||
int b = buf.get();
|
||||
received.append((char) b);
|
||||
System.out.println("Received " + (char) b);
|
||||
}
|
||||
|
||||
System.out.println("Received unknown command: " + "["
|
||||
+ received.toString() + "]");
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
// clear buffer and make it ready to write
|
||||
buf = ByteBuffer.allocate(BUF_SIZE);
|
||||
// buf.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void setStopping() {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
try {
|
||||
new BufferedNetworkStack(9999);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user