Merged "clever" ID creation
This commit is contained in:
parent
2ccc3cf772
commit
809f97de30
@ -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;
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user