Cleared buffer

This commit is contained in:
senft-lap 2012-11-06 14:56:19 +01:00
parent c6b43ba3cd
commit 6686c28084
2 changed files with 42 additions and 25 deletions

View File

@ -5,6 +5,7 @@ import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.Stack;
@ -14,16 +15,18 @@ class BufferedNetworkStack {
* One single element on the stack.
*/
public class Element {
public int id;
public String[] cunks;
public Element(int size) {
public Element(int size, int id) {
cunks = new String[size];
this.id = id;
System.out.println("Created new Element, size: " + size);
}
public boolean isComplete() {
for (String s : cunks) {
if (s.isEmpty()) {
if (s == null || s.isEmpty()) {
return false;
}
}
@ -31,18 +34,11 @@ class BufferedNetworkStack {
}
public String toString() {
boolean flag = false;
StringBuilder result = new StringBuilder(16);
for (String s : cunks) {
if (!s.isEmpty()) {
result.append(s);
flag = true;
}
}
// Add a flag, if the element is incomplete
if (flag) {
result.append(" (Incomplete)");
// if (!s.isEmpty()) {
result.append(s);
// }
}
return result.toString();
@ -59,11 +55,14 @@ class BufferedNetworkStack {
protected static final int BUF_SIZE = 4;
private Stack<Element> stack;
private Stack<String> stack;
private ArrayList<Element> temp;
public BufferedNetworkStack(final int port) throws IOException {
stack = new Stack<Element>();
stack = new Stack<String>();
temp = new ArrayList<Element>();
channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(port));
@ -90,7 +89,11 @@ class BufferedNetworkStack {
int size = Integer.valueOf(new Character(strReceived.charAt(1))
.toString());
String info = BUF_SIZE + "," + stack.size();
stack.push(new Element(size));
// stack.push(new Element(size));
// Pick better ID
int id = temp.size();
temp.add(id, new Element(size, id));
send(info, client);
break;
@ -103,17 +106,28 @@ class BufferedNetworkStack {
String data = strReceived.substring(3);
System.out.println("Received chunk #" + chunk_id + " of data #"
+ data_id + ": " + data);
// System.out.println("Received chunk #" + chunk_id +
// " of data #"
// + data_id + ": " + data);
stack.get(data_id).cunks[chunk_id] = data;
temp.get(data_id).cunks[chunk_id] = data;
// stack.get(data_id).cunks[chunk_id] = data;
if (temp.get(data_id).isComplete()) {
// Move from temp stack
Element e = temp.get(data_id);
// temp.remove(e);
stack.add(e.toString());
// System.out.println("Moved " + e);
}
break;
case CMD_POP:
try {
Element popped = stack.pop();
send(popped.toString(), client);
System.out.println("Pop " + popped);
// Element popped = stack.pop();
// send(popped.toString(), client);
// System.out.println("Pop " + popped);
} catch (EmptyStackException e) {
System.out.println("Received pop but stack is empty");
}
@ -137,8 +151,8 @@ class BufferedNetworkStack {
public String toString() {
StringBuilder result = new StringBuilder(128);
for (Element e : stack) {
result.append("[").append(e).append("],");
for (String s : stack) {
result.append("[").append(s).append("],");
}
result.append('\n');

View File

@ -11,11 +11,13 @@ public class BufferedNetworkStackClient {
protected static final byte CMD_POP = '2';
protected static final byte CMD_PRINT = '3';
protected static final int BUF_SIZE = 128;
private DatagramChannel channel = null;
private ByteBuffer buf;
public BufferedNetworkStackClient() {
buf = ByteBuffer.allocate(1024);
buf = ByteBuffer.allocate(BUF_SIZE);
}
public void push(String s, String host, int port) throws IOException {
@ -35,7 +37,7 @@ public class BufferedNetworkStackClient {
channel.send(buf, dest);
buf.flip();
buf.clear();
buf = ByteBuffer.allocate(BUF_SIZE);
channel.receive(buf);
byte[] receivedData = buf.array();
@ -74,6 +76,7 @@ public class BufferedNetworkStackClient {
try {
channel.send(buf, dest);
buf.clear();
} catch (IOException e) {
e.printStackTrace();
}