From 79f7ea0d55c01f2980d18bd640b7e58b165ce46f Mon Sep 17 00:00:00 2001 From: Benjamin Cheng Date: Wed, 27 May 2015 16:20:18 -0400 Subject: [PATCH] Huge changes to make multiplexing work --- .../proxy/TunnelChannelWrapper.java | 47 ++++++------------- .../proxy/event/EventProcessor.java | 2 +- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/Common/src/tk/jackyliao123/proxy/TunnelChannelWrapper.java b/Common/src/tk/jackyliao123/proxy/TunnelChannelWrapper.java index fb17af0..c51b81c 100644 --- a/Common/src/tk/jackyliao123/proxy/TunnelChannelWrapper.java +++ b/Common/src/tk/jackyliao123/proxy/TunnelChannelWrapper.java @@ -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> dataBuffers; - private final ArrayDeque ids; public TunnelChannelWrapper(SocketChannel channel, SelectionKey key) { super(channel, key); this.dataBuffers = new HashMap>(); - this.ids = new ArrayDeque(); } @Override @@ -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>> iterator = dataBuffers.entrySet().iterator(); + while(iterator.hasNext()) { + Map.Entry> entry = iterator.next(); + ArrayDeque clientBuffers = entry.getValue(); + ByteBuffer buffer = clientBuffers.removeFirst(); + if(clientBuffers.isEmpty()){ + iterator.remove(); + } + super.pushWriteBuffer(buffer); + } } - int client = ids.pop(); - - ArrayDeque 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; diff --git a/Common/src/tk/jackyliao123/proxy/event/EventProcessor.java b/Common/src/tk/jackyliao123/proxy/event/EventProcessor.java index 30ccdc3..517fe58 100644 --- a/Common/src/tk/jackyliao123/proxy/event/EventProcessor.java +++ b/Common/src/tk/jackyliao123/proxy/event/EventProcessor.java @@ -27,6 +27,7 @@ public Selector getSelector() { public void process(long timeout) throws IOException { selector.select(timeout); + Iterator keys = selector.selectedKeys().iterator(); while (keys.hasNext()) { SelectionKey key = keys.next(); @@ -85,7 +86,6 @@ public void process(long timeout) throws IOException { } } } - } if (!key.isValid()) {