Merge branch 'master' of mojotrollz.eu:college

Conflicts:
	ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStack.java
	ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java
This commit is contained in:
Ulf Gebhardt 2012-11-06 15:05:45 +01:00
commit 1938738e24
2 changed files with 41 additions and 23 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,7 +34,6 @@ class BufferedNetworkStack {
}
public String toString() {
boolean flag = false;
StringBuilder result = new StringBuilder(16);
for (String s : cunks) {
if (s != null && !s.isEmpty()) {
@ -39,11 +41,6 @@ class BufferedNetworkStack {
flag = true;
}
}
// Add a flag, if the element is incomplete
if (flag) {
result.append(" (Incomplete)");
}
return result.toString();
@ -59,11 +56,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 +90,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 +107,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 +152,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');
@ -161,4 +176,4 @@ class BufferedNetworkStack {
e.printStackTrace();
}
}
}
}

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, Exception{
@ -35,7 +37,7 @@ public class BufferedNetworkStackClient {
channel.send(buf, dest);
buf.flip();
buf.clear();
buf = ByteBuffer.allocate(BUF_SIZE);
channel.configureBlocking(false);
if(channel.receive(buf) == null){
@ -79,6 +81,7 @@ public class BufferedNetworkStackClient {
try {
channel.send(buf, dest);
buf.clear();
} catch (IOException e) {
e.printStackTrace();
}
@ -114,4 +117,4 @@ public class BufferedNetworkStackClient {
InetSocketAddress dest = new InetSocketAddress(host, port);
channel.send(ByteBuffer.wrap(new byte[] { CMD_PRINT }), dest);
}
}
}