Every number used in the protocoll is an Integer now
This commit is contained in:
parent
f669af74f7
commit
9ccbff21c2
@ -44,20 +44,22 @@ class BufferedNetworkStack {
|
||||
|
||||
private DatagramChannel channel;
|
||||
|
||||
protected static final int BUF_SIZE = 4;
|
||||
protected static final int BUF_SIZE = 10;
|
||||
|
||||
private Stack<String> stack;
|
||||
|
||||
private ArrayList<Element> temp;
|
||||
|
||||
public BufferedNetworkStack(final int port) throws IOException {
|
||||
|
||||
|
||||
stack = new Stack<String>();
|
||||
temp = new ArrayList<Element>();
|
||||
|
||||
channel = DatagramChannel.open();
|
||||
channel.socket().bind(new InetSocketAddress(port));
|
||||
|
||||
StringBuilder received;
|
||||
|
||||
ByteBuffer buf = ByteBuffer.allocate(BUF_SIZE);
|
||||
buf.clear();
|
||||
|
||||
@ -68,47 +70,47 @@ class BufferedNetworkStack {
|
||||
// make buffer readable
|
||||
buf.flip();
|
||||
|
||||
byte[] receivedData = buf.array();
|
||||
byte messageType = receivedData[0];
|
||||
String strReceived = new String(receivedData).toString().trim();
|
||||
|
||||
// System.out.println(messageType);
|
||||
// System.out.println(strReceived);
|
||||
byte messageType = buf.get();
|
||||
|
||||
switch (messageType) {
|
||||
case MessageType.CMD_PUSH:
|
||||
int size = Integer.valueOf(strReceived.substring(1));
|
||||
String info = BUF_SIZE + "," + stack.size();
|
||||
int size = buf.getInt();
|
||||
|
||||
// TODO: Pick better ID
|
||||
int id = stack.size();
|
||||
int id = temp.size();
|
||||
temp.add(id, new Element(size, id));
|
||||
|
||||
send(info, client);
|
||||
buf.flip();
|
||||
buf.clear();
|
||||
|
||||
buf.putInt(BUF_SIZE);
|
||||
buf.putInt(id);
|
||||
buf.flip();
|
||||
channel.send(buf, client);
|
||||
break;
|
||||
|
||||
case MessageType.CMD_DATA:
|
||||
int session_id = Integer.valueOf(strReceived.charAt(1)); //TODO: chartAT(' ') wirft exception bei LEerzeichen
|
||||
int chunk_id = Integer.valueOf(strReceived.charAt(2));
|
||||
int sessionId = buf.getInt();
|
||||
int chunkId = buf.getInt();
|
||||
|
||||
String data = strReceived.substring(3);
|
||||
received = new StringBuilder();
|
||||
while (buf.hasRemaining()) {
|
||||
received.append((char) buf.get());
|
||||
}
|
||||
|
||||
// System.out.println("Received chunk #" + chunk_id +
|
||||
// System.out.println("Received chunk #" + chunkId +
|
||||
// " of data #"
|
||||
// + session_id + ": " + data);
|
||||
// + sessionId + ": " + received);
|
||||
|
||||
temp.get(session_id).chunks[chunk_id] = data;
|
||||
temp.get(sessionId).chunks[chunkId] = received.toString();
|
||||
|
||||
if (temp.get(session_id).isComplete()) {
|
||||
if (temp.get(sessionId).isComplete()) {
|
||||
// Move from temp stack
|
||||
Element e = temp.get(session_id);
|
||||
Element e = temp.get(sessionId);
|
||||
|
||||
// TODO: Element auch wirklich löschen aber dann ändern sich
|
||||
// die IDs
|
||||
// temp.remove(e);
|
||||
stack.add(e.toString());
|
||||
|
||||
System.out.println("Moved " + e + " from temp to stack");
|
||||
}
|
||||
break;
|
||||
|
||||
@ -117,6 +119,7 @@ class BufferedNetworkStack {
|
||||
String popped = stack.pop();
|
||||
send(popped, client);
|
||||
System.out.println("Pop " + popped);
|
||||
|
||||
} catch (EmptyStackException e) {
|
||||
System.out.println("Received pop but stack is empty");
|
||||
}
|
||||
@ -127,8 +130,15 @@ class BufferedNetworkStack {
|
||||
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: " + "["
|
||||
+ strReceived + "]");
|
||||
+ received.toString() + "]");
|
||||
break;
|
||||
|
||||
}
|
||||
@ -155,7 +165,6 @@ class BufferedNetworkStack {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
@ -28,8 +28,7 @@ public class BufferedNetworkStackClient {
|
||||
buf.clear();
|
||||
buf.put(MessageType.CMD_PUSH);
|
||||
|
||||
byte[] length = String.valueOf(s.length()).getBytes();
|
||||
buf.put(length);
|
||||
buf.putInt(s.length());
|
||||
|
||||
buf.flip();
|
||||
channel.send(buf, dest);
|
||||
@ -47,16 +46,11 @@ public class BufferedNetworkStackClient {
|
||||
}
|
||||
channel.configureBlocking(true);
|
||||
|
||||
byte[] receivedData = buf.array();
|
||||
|
||||
String infoPacket = new String(receivedData).trim();
|
||||
buf.flip();
|
||||
|
||||
// 0 = buffer_size, 1 = id
|
||||
String[] info = infoPacket.split(",");
|
||||
int remoteBufSize = Integer.valueOf(info[0]);
|
||||
int sessionId = Integer.valueOf(info[1]);
|
||||
int remoteBufSize = buf.getInt();
|
||||
int sessionId = buf.getInt();
|
||||
|
||||
//System.out.println('4');
|
||||
sendChunks(s, dest, remoteBufSize, sessionId);
|
||||
}
|
||||
|
||||
@ -64,24 +58,30 @@ public class BufferedNetworkStackClient {
|
||||
int sessionId) {
|
||||
|
||||
// The amount of actual data we can store in a packet
|
||||
int lenData = bufSize - 3;
|
||||
int lenData = bufSize - 9;
|
||||
|
||||
// Number of chunks needed to send 's'. We use bufSize-2 because the
|
||||
// first 2 bytes of each packet are metadata (session_id, chunk_id).
|
||||
// Number of chunks needed to send 's'. We use bufSize-9 because the
|
||||
// first 9 bytes of each packet are metadata (message_type, session_id,
|
||||
// chunk_id).
|
||||
int numChunks = (int) Math.ceil((s.length() * 1.0 / lenData));
|
||||
|
||||
for (int chunkId = 0; chunkId < numChunks; chunkId++) {
|
||||
buf.flip();
|
||||
buf.clear();
|
||||
|
||||
buf.put(MessageType.CMD_DATA);
|
||||
buf.put((byte) (sessionId));
|
||||
buf.put((byte) (chunkId));
|
||||
buf.putInt(sessionId);
|
||||
buf.putInt(chunkId);
|
||||
|
||||
int chunkStart = chunkId * lenData;
|
||||
|
||||
String str = sessionId + chunkId + s.substring(chunkStart, chunkStart + lenData);
|
||||
|
||||
buf.put(str.getBytes());
|
||||
try {
|
||||
buf.put(s.substring(chunkStart, chunkStart + lenData)
|
||||
.getBytes());
|
||||
} catch (StringIndexOutOfBoundsException e) {
|
||||
// chunkStart + lenData is not "in the string" anymore
|
||||
buf.put(s.substring(chunkStart).getBytes());
|
||||
}
|
||||
buf.flip();
|
||||
|
||||
try {
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package buffered;
|
||||
|
||||
public class MessageType {
|
||||
protected static final byte CMD_PUSH = '0';
|
||||
protected static final byte CMD_DATA = '1';
|
||||
protected static final byte CMD_POP = '2';
|
||||
protected static final byte CMD_PRINT = '3';
|
||||
protected static final byte CMD_PUSH = 0;
|
||||
protected static final byte CMD_DATA = 1;
|
||||
protected static final byte CMD_POP = 2;
|
||||
protected static final byte CMD_PRINT = 3;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user