Added temporary stack, fixed sessionID and chunkID encoding

This commit is contained in:
senft-lap 2012-11-06 16:12:22 +01:00
parent 709cad9919
commit 565303573f
2 changed files with 22 additions and 24 deletions

View File

@ -72,49 +72,47 @@ class BufferedNetworkStack {
byte messageType = receivedData[0];
String strReceived = new String(receivedData).toString().trim();
System.out.println(messageType);
System.out.println(strReceived);
// System.out.println(messageType);
// System.out.println(strReceived);
switch (messageType) {
case CMD_PUSH:
int size = Integer.valueOf(new Character(strReceived.charAt(1))
.toString());
case MessageType.CMD_PUSH:
int size = Integer.valueOf(strReceived.substring(1));
String info = BUF_SIZE + "," + stack.size();
// stack.push(new Element(size));
// Pick better ID
// TODO: Pick better ID
int id = temp.size();
temp.add(id, new Element(size, id));
send(info, client);
break;
case CMD_DATA:
int data_id = Integer.valueOf(new Character(strReceived
.charAt(1)).toString());
int chunk_id = Integer.valueOf(new Character(strReceived
.charAt(2)).toString());
case MessageType.CMD_DATA:
int session_id = Integer.valueOf(strReceived.charAt(1));
int chunk_id = Integer.valueOf(strReceived.charAt(2));
String data = strReceived.substring(3);
// System.out.println("Received chunk #" + chunk_id +
// " of data #"
// + data_id + ": " + data);
// + session_id + ": " + data);
temp.get(data_id).cunks[chunk_id] = data;
temp.get(session_id).chunks[chunk_id] = data;
// stack.get(data_id).cunks[chunk_id] = data;
if (temp.get(data_id).isComplete()) {
if (temp.get(session_id).isComplete()) {
// Move from temp stack
Element e = temp.get(data_id);
Element e = temp.get(session_id);
// TODO: Element auch wirklich löschen aber dann ändern sich
// die IDs
// temp.remove(e);
stack.add(e.toString());
// System.out.println("Moved " + e);
System.out.println("Moved " + e + " from temp to stack");
}
break;
case CMD_POP:
case MessageType.CMD_POP:
try {
// Element popped = stack.pop();
// send(popped.toString(), client);
@ -136,7 +134,8 @@ class BufferedNetworkStack {
}
// clear buffer and make it ready to write
buf.clear();
buf = ByteBuffer.allocate(BUF_SIZE);
// buf.clear();
}
}

View File

@ -56,13 +56,12 @@ public class BufferedNetworkStackClient {
// first 2 bytes of each packet are metadata (session_id, chunk_id).
int numChunks = (int) Math.ceil((s.length() * 1.0 / lenData));
// System.out.println(numChunks);
for (int chunkId = 0; chunkId < numChunks; chunkId++) {
buf.flip();
buf.clear();
buf.put(CMD_DATA);
buf.put(String.valueOf(sessionId).getBytes());
buf.put(String.valueOf(chunkId).getBytes());
buf.put(MessageType.CMD_DATA);
buf.put((byte) (sessionId));
buf.put((byte) (chunkId));
int chunkStart = chunkId * lenData;