Skip to content

Commit

Permalink
Huge changes to make multiplexing work
Browse files Browse the repository at this point in the history
  • Loading branch information
lolzballs committed May 27, 2015
1 parent e8a5ebb commit 79f7ea0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
47 changes: 15 additions & 32 deletions Common/src/tk/jackyliao123/proxy/TunnelChannelWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import java.nio.channels.SocketChannel;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class TunnelChannelWrapper extends ChannelWrapper {
private final HashMap<Integer, ArrayDeque<ByteBuffer>> dataBuffers;
private final ArrayDeque<Integer> ids;

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

@Override
Expand All @@ -29,50 +29,33 @@ public void pushWriteBuffer(int connectionId, ByteBuffer data) {
dataBuffers.put(connectionId, buffers);
}

buffers.push(data);

if (ids.contains(connectionId)) {
return;
}
ids.push(connectionId);

System.out.println(ids + "!");
buffers.addLast(data);

addInterest(SelectionKey.OP_WRITE);
System.out.println("Interest added");
}

@Override
public ByteBuffer getWriteBuffer() {
System.out.println(ids);
if (ids.isEmpty()) {
removeInterest(SelectionKey.OP_WRITE);
System.out.println("get buffer null");
return null;
if(writeBuffers.isEmpty()) {
Iterator<Map.Entry<Integer, ArrayDeque<ByteBuffer>>> iterator = dataBuffers.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<Integer, ArrayDeque<ByteBuffer>> entry = iterator.next();
ArrayDeque<ByteBuffer> clientBuffers = entry.getValue();
ByteBuffer buffer = clientBuffers.removeFirst();
if(clientBuffers.isEmpty()){
iterator.remove();
}
super.pushWriteBuffer(buffer);
}
}

int client = ids.pop();

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

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

super.pushWriteBuffer(clientBuffers.pop());

return super.getWriteBuffer();
}

@Override
public ByteBuffer popWriteBuffer() throws IOException {
ByteBuffer b = writeBuffers.removeFirst();
if (dataBuffers.isEmpty()) {
System.out.println("Nothing left, removing opwrite");
if (writeBuffers.isEmpty() && dataBuffers.isEmpty()) {
removeInterest(SelectionKey.OP_WRITE);
}
return b;
Expand Down
2 changes: 1 addition & 1 deletion Common/src/tk/jackyliao123/proxy/event/EventProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public Selector getSelector() {
public void process(long timeout) throws IOException {
selector.select(timeout);


Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey key = keys.next();
Expand Down Expand Up @@ -85,7 +86,6 @@ public void process(long timeout) throws IOException {
}
}
}

}

if (!key.isValid()) {
Expand Down

0 comments on commit 79f7ea0

Please sign in to comment.