Skip to content

Commit

Permalink
Remove idsWaiting
Browse files Browse the repository at this point in the history
  • Loading branch information
lolzballs committed May 24, 2015
1 parent d7f72ff commit e8a5ebb
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions Common/src/tk/jackyliao123/proxy/TunnelChannelWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
public class TunnelChannelWrapper extends ChannelWrapper {
private final HashMap<Integer, ArrayDeque<ByteBuffer>> dataBuffers;
private final ArrayDeque<Integer> ids;
private final ArrayDeque<Integer> idsWaiting;

public TunnelChannelWrapper(SocketChannel channel, SelectionKey key) {
super(channel, key);
this.dataBuffers = new HashMap<Integer, ArrayDeque<ByteBuffer>>();
this.ids = new ArrayDeque<Integer>();
this.idsWaiting = new ArrayDeque<Integer>();
}

@Override
Expand Down Expand Up @@ -46,6 +44,7 @@ public void pushWriteBuffer(int connectionId, ByteBuffer data) {

@Override
public ByteBuffer getWriteBuffer() {
System.out.println(ids);
if (ids.isEmpty()) {
removeInterest(SelectionKey.OP_WRITE);
System.out.println("get buffer null");
Expand All @@ -54,36 +53,26 @@ public ByteBuffer getWriteBuffer() {

int client = ids.pop();

ids.push(client);
idsWaiting.push(client);

ArrayDeque<ByteBuffer> clientBuffers = dataBuffers.get(client);
if (clientBuffers.isEmpty()) {
removeInterest(SelectionKey.OP_WRITE);
System.out.println("get buffer null");
System.out.println("Empty");
dataBuffers.remove(client);
return null;
}

System.out.println("Push onto stack");
ids.push(client);

super.pushWriteBuffer(clientBuffers.pop());

System.out.println(ids + ", " + idsWaiting);
return super.getWriteBuffer();
}

@Override
public ByteBuffer popWriteBuffer() throws IOException {
System.out.println(idsWaiting);

int client = idsWaiting.pop();
System.out.println("Pop " + client);
ArrayDeque<ByteBuffer> buffers = dataBuffers.get(client);

if (buffers.isEmpty()) {
ids.remove(client);
dataBuffers.remove(client);
}

ByteBuffer b = writeBuffers.removeFirst();
if (dataBuffers.isEmpty()) {
System.out.println("Nothing left, removing opwrite");
removeInterest(SelectionKey.OP_WRITE);
}
return b;
Expand Down

0 comments on commit e8a5ebb

Please sign in to comment.