Merged "clever" ID creation

This commit is contained in:
senft-lap 2012-11-13 16:30:08 +01:00
parent 2ccc3cf772
commit 809f97de30
2 changed files with 21 additions and 4 deletions

View File

@ -9,7 +9,7 @@ import common.MessageType;
import common.Util;
public class BufferedNetworkStackClient {
protected static final long TIMEOUT = 3000;
protected static final long TIMEOUT = 5000;
protected static final int BUF_SIZE = 2048;

View File

@ -17,6 +17,8 @@ public class BufferedNetworkStack {
private DatagramChannel channel;
private ByteBuffer buf;
// Size of reading buffer (in bytes)
// At least 10, because we have 9 bytes of metadata in every packet
protected static final int BUF_SIZE = 1024;
private Stack<String> stack;
@ -83,11 +85,11 @@ public class BufferedNetworkStack {
case MessageType.CMD_PUSH:
int size = buf.getInt();
int id = temp.size();
int id = generateID();
int numChunks = Util.numChunks(BUF_SIZE, size);
temp.add(id, new Element(numChunks, id));
temp.add(new Element(numChunks, id));
buf.flip();
buf.clear();
@ -155,7 +157,7 @@ public class BufferedNetworkStack {
default:
received = new StringBuilder();
while (buf.hasRemaining()) {
int b = buf.get();
byte b = buf.get();
received.append((char) b);
System.out.println("Received " + (char) b);
}
@ -177,6 +179,21 @@ public class BufferedNetworkStack {
}
}
private int generateID() {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
boolean isUsed = false;
for (Element e : temp) {
if (i == e.id) {
isUsed = true;
}
}
if (!isUsed) {
return i;
}
}
return -1;
}
public static void main(String args[]) {
try {
new BufferedNetworkStack(9999);