You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by kk...@apache.org on 2011/11/10 06:53:21 UTC
svn commit: r1200168 - in
/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net:
NioBlockingSelector.java NioChannel.java NioEndpoint.java
NioSelectorPool.java SecureNioChannel.java
Author: kkolinko
Date: Thu Nov 10 05:53:21 2011
New Revision: 1200168
URL: http://svn.apache.org/viewvc?rev=1200168&view=rev
Log:
Merging revision 1187781 from tomcat/trunk:
Whitespace removal from /java/org/apache/tomcat
I won't merge all of it (as it spans bcel etc.), but only our code.
Part 5.
Modified:
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioBlockingSelector.java
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioChannel.java
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioSelectorPool.java
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java
Modified: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioBlockingSelector.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioBlockingSelector.java?rev=1200168&r1=1200167&r2=1200168&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioBlockingSelector.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioBlockingSelector.java Thu Nov 10 05:53:21 2011
@@ -38,18 +38,18 @@ import org.apache.tomcat.util.MutableInt
import org.apache.tomcat.util.net.NioEndpoint.KeyAttachment;
public class NioBlockingSelector {
-
+
private static final Log log = LogFactory.getLog(NioBlockingSelector.class);
-
+
private static int threadCounter = 0;
-
+
protected Selector sharedSelector;
-
+
protected BlockPoller poller;
public NioBlockingSelector() {
-
+
}
-
+
public void open(Selector selector) {
sharedSelector = selector;
poller = new BlockPoller();
@@ -58,7 +58,7 @@ public class NioBlockingSelector {
poller.setName("NioBlockingSelector.BlockPoller-"+(++threadCounter));
poller.start();
}
-
+
public void close() {
if (poller!=null) {
poller.disable();
@@ -120,7 +120,7 @@ public class NioBlockingSelector {
if (writeTimeout > 0 && (keycount == 0))
timedout = (System.currentTimeMillis() - time) >= writeTimeout;
} //while
- if (timedout)
+ if (timedout)
throw new SocketTimeoutException();
} finally {
poller.remove(att,SelectionKey.OP_WRITE);
@@ -197,7 +197,7 @@ public class NioBlockingSelector {
return read;
}
-
+
protected static class BlockPoller extends Thread {
protected volatile boolean run = true;
protected Selector selector = null;
@@ -227,7 +227,7 @@ public class NioBlockingSelector {
if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch());
}
}
-
+
public void add(final KeyAttachment key, final int ops, final KeyReference ref) {
Runnable r = new Runnable() {
@Override
@@ -257,7 +257,7 @@ public class NioBlockingSelector {
events.offer(r);
wakeup();
}
-
+
public void remove(final KeyAttachment key, final int ops) {
Runnable r = new Runnable() {
@Override
@@ -318,7 +318,7 @@ public class NioBlockingSelector {
int keyCount = 0;
try {
int i = wakeupCounter.get();
- if (i>0)
+ if (i>0)
keyCount = selector.selectNow();
else {
wakeupCounter.set(-1);
@@ -380,16 +380,16 @@ public class NioBlockingSelector {
if (log.isDebugEnabled())log.debug("",ignore);
}
}
-
+
public void countDown(CountDownLatch latch) {
if ( latch == null ) return;
latch.countDown();
}
}
-
+
public static class KeyReference {
SelectionKey key = null;
-
+
@Override
public void finalize() {
if (key!=null && key.isValid()) {
Modified: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioChannel.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioChannel.java?rev=1200168&r1=1200167&r2=1200168&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioChannel.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioChannel.java Thu Nov 10 05:53:21 2011
@@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -30,11 +30,11 @@ import org.apache.tomcat.util.net.NioEnd
import org.apache.tomcat.util.net.SecureNioChannel.ApplicationBufferHandler;
/**
- *
+ *
* Base class for a SocketChannel wrapper used by the endpoint.
* This way, logic for a SSL socket channel remains the same as for
* a non SSL, making sure we don't need to code for any exception cases.
- *
+ *
* @author Filip Hanik
* @version 1.0
*/
@@ -47,7 +47,7 @@ public class NioChannel implements ByteC
protected ApplicationBufferHandler bufHandler;
protected Poller poller;
-
+
protected boolean sendFile = false;
public NioChannel(SocketChannel channel, ApplicationBufferHandler bufHandler) throws IOException {
@@ -60,7 +60,7 @@ public class NioChannel implements ByteC
bufHandler.getWriteBuffer().clear();
this.sendFile = false;
}
-
+
public int getBufferSize() {
if ( bufHandler == null ) return 0;
int size = 0;
@@ -70,7 +70,7 @@ public class NioChannel implements ByteC
}
/**
- * returns true if the network buffer has
+ * returns true if the network buffer has
* been flushed out and is empty
* @return boolean
*/
@@ -198,11 +198,11 @@ public class NioChannel implements ByteC
public String toString() {
return super.toString()+":"+this.sc.toString();
}
-
+
public int getOutboundRemaining() {
return 0;
}
-
+
/**
* Return true if the buffer wrote data
* @throws IOException
@@ -210,14 +210,14 @@ public class NioChannel implements ByteC
public boolean flushOutbound() throws IOException {
return false;
}
-
+
public boolean isSendFile() {
return sendFile;
}
-
+
public void setSendFile(boolean s) {
this.sendFile = s;
}
-
+
}
Modified: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java?rev=1200168&r1=1200167&r2=1200168&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Thu Nov 10 05:53:21 2011
@@ -80,48 +80,48 @@ public class NioEndpoint extends Abstrac
public static final int OP_REGISTER = 0x100; //register interest op
public static final int OP_CALLBACK = 0x200; //callback interest op
-
+
// ----------------------------------------------------------------- Fields
protected NioSelectorPool selectorPool = new NioSelectorPool();
-
+
/**
* Server socket "pointer".
*/
protected ServerSocketChannel serverSock = null;
-
+
/**
* use send file
*/
protected boolean useSendfile = true;
-
+
/**
* The size of the OOM parachute.
*/
protected int oomParachute = 1024*1024;
/**
- * The oom parachute, when an OOM error happens,
- * will release the data, giving the JVM instantly
+ * The oom parachute, when an OOM error happens,
+ * will release the data, giving the JVM instantly
* a chunk of data to be able to recover with.
*/
protected byte[] oomParachuteData = null;
-
+
/**
* Make sure this string has already been allocated
*/
- protected static final String oomParachuteMsg =
+ protected static final String oomParachuteMsg =
"SEVERE:Memory usage is low, parachute is non existent, your system may start failing.";
-
+
/**
* Keep track of OOM warning messages.
*/
long lastParachuteCheck = System.currentTimeMillis();
-
+
/**
- *
+ *
*/
protected volatile CountDownLatch stopLatch = null;
-
+
/**
* Cache for SocketProcessor objects
*/
@@ -142,7 +142,7 @@ public class NioEndpoint extends Abstrac
}
else return false;
}
-
+
@Override
public SocketProcessor poll() {
SocketProcessor result = super.poll();
@@ -151,7 +151,7 @@ public class NioEndpoint extends Abstrac
}
return result;
}
-
+
@Override
public void clear() {
super.clear();
@@ -197,7 +197,7 @@ public class NioEndpoint extends Abstrac
}
};
-
+
/**
* Cache for poller events
*/
@@ -258,7 +258,7 @@ public class NioEndpoint extends Abstrac
}
else return false;
}
-
+
@Override
public NioChannel poll() {
NioChannel result = super.poll();
@@ -268,7 +268,7 @@ public class NioEndpoint extends Abstrac
}
return result;
}
-
+
@Override
public void clear() {
super.clear();
@@ -290,7 +290,7 @@ public class NioEndpoint extends Abstrac
try {
if (name.startsWith(selectorPoolName)) {
return IntrospectionUtils.setProperty(selectorPool, name.substring(selectorPoolName.length()), value);
- } else {
+ } else {
return super.setProperty(name, value);
}
}catch ( Exception x ) {
@@ -401,7 +401,7 @@ public class NioEndpoint extends Abstrac
protected SSLContext sslContext = null;
public SSLContext getSSLContext() { return sslContext;}
public void setSSLContext(SSLContext c) { sslContext = c;}
-
+
// --------------------------------------------------------- OOM Parachute Methods
protected void checkParachute() {
@@ -416,22 +416,22 @@ public class NioEndpoint extends Abstrac
lastParachuteCheck = System.currentTimeMillis();
}
}
-
+
protected boolean reclaimParachute(boolean force) {
if ( oomParachuteData != null ) return true;
- if ( oomParachute > 0 && ( force || (Runtime.getRuntime().freeMemory() > (oomParachute*2))) )
+ if ( oomParachute > 0 && ( force || (Runtime.getRuntime().freeMemory() > (oomParachute*2))) )
oomParachuteData = new byte[oomParachute];
return oomParachuteData != null;
}
-
+
protected void releaseCaches() {
this.keyCache.clear();
this.nioChannels.clear();
this.processorCache.clear();
if ( handler != null ) handler.recycle();
-
+
}
-
+
// --------------------------------------------------------- Public Methods
/**
* Number of keepalive sockets.
@@ -461,7 +461,7 @@ public class NioEndpoint extends Abstrac
serverSock = ServerSocketChannel.open();
socketProperties.setProperties(serverSock.socket());
InetSocketAddress addr = (getAddress()!=null?new InetSocketAddress(getAddress(),getPort()):new InetSocketAddress(getPort()));
- serverSock.socket().bind(addr,getBacklog());
+ serverSock.socket().bind(addr,getBacklog());
serverSock.configureBlocking(true); //mimic APR behavior
serverSock.socket().setSoTimeout(getSocketProperties().getSoTimeout());
@@ -490,11 +490,11 @@ public class NioEndpoint extends Abstrac
sslUtil.configureSessionContext(sessionContext);
}
}
-
+
if (oomParachute>0) reclaimParachute(true);
selectorPool.open();
}
-
+
public KeyManager[] wrap(KeyManager[] managers) {
if (managers==null) return null;
KeyManager[] result = new KeyManager[managers.length];
@@ -518,14 +518,14 @@ public class NioEndpoint extends Abstrac
if (!running) {
running = true;
paused = false;
-
+
// Create worker collection
if ( getExecutor() == null ) {
createExecutor();
}
initializeConnectionLatch();
-
+
// Start poller threads
pollers = new Poller[getPollerThreadCount()];
for (int i=0; i<pollers.length; i++) {
@@ -574,7 +574,7 @@ public class NioEndpoint extends Abstrac
nioChannels.clear();
processorCache.clear();
shutdownExecutor();
-
+
}
@@ -660,7 +660,7 @@ public class NioEndpoint extends Abstrac
channel = new NioChannel(socket, bufhandler);
}
- } else {
+ } else {
channel.setIOChannel(socket);
if ( channel instanceof SecureNioChannel ) {
SSLEngine engine = createSSLEngine();
@@ -696,7 +696,7 @@ public class NioEndpoint extends Abstrac
engine.setUseClientMode(false);
if ( getCiphersArray().length > 0 ) engine.setEnabledCipherSuites(getCiphersArray());
if ( getSslEnabledProtocolsArray().length > 0 ) engine.setEnabledProtocols(getSslEnabledProtocolsArray());
-
+
return engine;
}
@@ -754,7 +754,7 @@ public class NioEndpoint extends Abstrac
// Loop until we receive a shutdown command
while (running) {
-
+
// Loop if endpoint is paused
while (paused && running) {
try {
@@ -770,7 +770,7 @@ public class NioEndpoint extends Abstrac
try {
//if we have reached max connections, wait
countUpOrAwaitConnection();
-
+
SocketChannel socket = null;
try {
// Accept the next incoming connection from the server
@@ -836,28 +836,28 @@ public class NioEndpoint extends Abstrac
// ----------------------------------------------------- Poller Inner Classes
/**
- *
+ *
* PollerEvent, cacheable object for poller events to avoid GC
*/
public static class PollerEvent implements Runnable {
-
+
protected NioChannel socket;
protected int interestOps;
protected KeyAttachment key;
public PollerEvent(NioChannel ch, KeyAttachment k, int intOps) {
reset(ch, k, intOps);
}
-
+
public void reset(NioChannel ch, KeyAttachment k, int intOps) {
socket = ch;
interestOps = intOps;
key = k;
}
-
+
public void reset() {
reset(null, null, 0);
}
-
+
@Override
public void run() {
if ( interestOps == OP_REGISTER ) {
@@ -899,13 +899,13 @@ public class NioEndpoint extends Abstrac
}
}//end if
}//run
-
+
@Override
public String toString() {
return super.toString()+"[intOps="+this.interestOps+"]";
}
}
-
+
/**
* Poller class.
*/
@@ -913,12 +913,12 @@ public class NioEndpoint extends Abstrac
protected Selector selector;
protected ConcurrentLinkedQueue<Runnable> events = new ConcurrentLinkedQueue<Runnable>();
-
+
protected volatile boolean close = false;
protected long nextExpiration = 0;//optimize expiration handling
-
+
protected AtomicLong wakeupCounter = new AtomicLong(0l);
-
+
protected volatile int keyCount = 0;
public Poller() throws IOException {
@@ -929,9 +929,9 @@ public class NioEndpoint extends Abstrac
this.selector = Selector.open();
}
}
-
+
public int getKeyCount() { return keyCount; }
-
+
public Selector getSelector() { return selector;}
/**
@@ -945,12 +945,12 @@ public class NioEndpoint extends Abstrac
events.clear();
selector.wakeup();
}
-
+
public void addEvent(Runnable event) {
events.offer(event);
if ( wakeupCounter.incrementAndGet() == 0 ) selector.wakeup();
}
-
+
public void cometInterest(NioChannel socket) {
KeyAttachment att = (KeyAttachment)socket.getAttachment(false);
add(socket,att.getCometOps());
@@ -959,7 +959,7 @@ public class NioEndpoint extends Abstrac
selector.wakeup();
}
}
-
+
/**
* Add specified socket and associated pool to the poller. The socket will
* be added to a temporary array, and polled first after a maximum amount
@@ -971,7 +971,7 @@ public class NioEndpoint extends Abstrac
public void add(final NioChannel socket) {
add(socket,SelectionKey.OP_READ);
}
-
+
public void add(final NioChannel socket, final int interestOps) {
PollerEvent r = eventCache.poll();
if ( r==null) r = new PollerEvent(socket,null,interestOps);
@@ -981,7 +981,7 @@ public class NioEndpoint extends Abstrac
/**
* Processes events in the event queue of the Poller.
- *
+ *
* @return <code>true</code> if some events were processed,
* <code>false</code> if queue was empty
*/
@@ -1004,7 +1004,7 @@ public class NioEndpoint extends Abstrac
return result;
}
-
+
public void register(final NioChannel socket)
{
socket.setPoller(this);
@@ -1032,7 +1032,7 @@ public class NioEndpoint extends Abstrac
}
} else {
processSocket(ka.getChannel(), status, false); //don't dispatch if the lines below are cancelling the key
- }
+ }
}
key.attach(null);
if (ka!=null) handler.release(ka);
@@ -1068,7 +1068,7 @@ public class NioEndpoint extends Abstrac
}
if (ka!=null) {
ka.reset();
- countDownConnection();
+ countDownConnection();
}
} catch (Throwable e) {
ExceptionUtils.handleThrowable(e);
@@ -1113,8 +1113,8 @@ public class NioEndpoint extends Abstrac
}
if (close) {
timeout(0, false);
- selector.close();
- break;
+ selector.close();
+ break;
}
} catch ( NullPointerException x ) {
//sun bug 5076772 on windows JDK 1.5
@@ -1176,7 +1176,7 @@ public class NioEndpoint extends Abstrac
stopLatch.countDown();
}
-
+
protected boolean processKey(SelectionKey sk, KeyAttachment attachment) {
boolean result = true;
try {
@@ -1220,7 +1220,7 @@ public class NioEndpoint extends Abstrac
result = false;
}
}
- }
+ }
} else {
//invalid key
cancelledKey(sk, SocketStatus.ERROR,false);
@@ -1233,7 +1233,7 @@ public class NioEndpoint extends Abstrac
}
return result;
}
-
+
public boolean processSendfile(SelectionKey sk, KeyAttachment attachment, boolean reg, boolean event) {
NioChannel sc = null;
try {
@@ -1250,7 +1250,7 @@ public class NioEndpoint extends Abstrac
sc = attachment.getChannel();
sc.setSendFile(true);
WritableByteChannel wc = ((sc instanceof SecureNioChannel)?sc:sc.getIOChannel());
-
+
if (sc.getOutboundRemaining()>0) {
if (sc.flushOutbound()) {
attachment.access();
@@ -1325,9 +1325,9 @@ public class NioEndpoint extends Abstrac
//this is a must, so that we don't have multiple threads messing with the socket
reg(sk,attachment,sk.interestOps()& (~readyOps));
}
-
+
protected void reg(SelectionKey sk, KeyAttachment attachment, int intops) {
- sk.interestOps(intops);
+ sk.interestOps(intops);
attachment.interestOps(intops);
attachment.setCometOps(intops);
}
@@ -1365,11 +1365,11 @@ public class NioEndpoint extends Abstrac
long timeout = ka.getTimeout();
boolean isTimedout = timeout > 0 && delta > timeout;
if ( close ) {
- key.interestOps(0);
+ key.interestOps(0);
ka.interestOps(0); //avoid duplicate stop calls
processKey(key,ka);
} else if (isTimedout) {
- key.interestOps(0);
+ key.interestOps(0);
ka.interestOps(0); //avoid duplicate timeout calls
cancelledKey(key, SocketStatus.TIMEOUT,true);
} else if (timeout > -1) {
@@ -1399,13 +1399,13 @@ public class NioEndpoint extends Abstrac
}
}
-// ----------------------------------------------------- Key Attachment Class
+// ----------------------------------------------------- Key Attachment Class
public static class KeyAttachment extends SocketWrapper<NioChannel> {
-
+
public KeyAttachment(NioChannel channel) {
super(channel);
}
-
+
public void reset(Poller poller, NioChannel channel, long soTimeout) {
this.socket = channel;
this.poller = poller;
@@ -1439,11 +1439,11 @@ public class NioEndpoint extends Abstrac
keepAliveLeft = 100;
async = false;
}
-
+
public void reset() {
reset(null,null,-1);
}
-
+
public Poller getPoller() { return poller;}
public void setPoller(Poller poller){this.poller = poller;}
public void setComet(boolean comet) { this.comet = comet; }
@@ -1466,8 +1466,8 @@ public class NioEndpoint extends Abstrac
}
public void resetReadLatch() { readLatch = resetLatch(readLatch); }
public void resetWriteLatch() { writeLatch = resetLatch(writeLatch); }
-
- protected CountDownLatch startLatch(CountDownLatch latch, int cnt) {
+
+ protected CountDownLatch startLatch(CountDownLatch latch, int cnt) {
if ( latch == null || latch.getCount() == 0 ) {
return new CountDownLatch(cnt);
}
@@ -1475,34 +1475,34 @@ public class NioEndpoint extends Abstrac
}
public void startReadLatch(int cnt) { readLatch = startLatch(readLatch,cnt);}
public void startWriteLatch(int cnt) { writeLatch = startLatch(writeLatch,cnt);}
-
+
protected void awaitLatch(CountDownLatch latch, long timeout, TimeUnit unit) throws InterruptedException {
if ( latch == null ) throw new IllegalStateException("Latch cannot be null");
latch.await(timeout,unit);
}
public void awaitReadLatch(long timeout, TimeUnit unit) throws InterruptedException { awaitLatch(readLatch,timeout,unit);}
public void awaitWriteLatch(long timeout, TimeUnit unit) throws InterruptedException { awaitLatch(writeLatch,timeout,unit);}
-
+
public long getLastRegistered() { return lastRegistered; }
public void setLastRegistered(long reg) { lastRegistered = reg; }
-
+
public void setSendfileData(SendfileData sf) { this.sendfileData = sf;}
public SendfileData getSendfileData() { return this.sendfileData;}
-
+
protected boolean comet = false;
protected int cometOps = SelectionKey.OP_READ;
protected boolean cometNotify = false;
protected CountDownLatch readLatch = null;
protected CountDownLatch writeLatch = null;
protected SendfileData sendfileData = null;
-
+
}
// ------------------------------------------------ Application Buffer Handler
public static class NioBufferHandler implements ApplicationBufferHandler {
protected ByteBuffer readbuf = null;
protected ByteBuffer writebuf = null;
-
+
public NioBufferHandler(int readsize, int writesize, boolean direct) {
if ( direct ) {
readbuf = ByteBuffer.allocateDirect(readsize);
@@ -1512,7 +1512,7 @@ public class NioEndpoint extends Abstrac
writebuf = ByteBuffer.allocate(writesize);
}
}
-
+
@Override
public ByteBuffer expand(ByteBuffer buffer, int remaining) {return buffer;}
@Override
@@ -1547,17 +1547,17 @@ public class NioEndpoint extends Abstrac
protected class SocketProcessor implements Runnable {
protected NioChannel socket = null;
- protected SocketStatus status = null;
+ protected SocketStatus status = null;
public SocketProcessor(NioChannel socket, SocketStatus status) {
reset(socket,status);
}
-
+
public void reset(NioChannel socket, SocketStatus status) {
this.socket = socket;
this.status = status;
}
-
+
@Override
public void run() {
boolean launch = false;
@@ -1566,7 +1566,7 @@ public class NioEndpoint extends Abstrac
try {
key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
int handshake = -1;
-
+
try {
if (key!=null) handshake = socket.handshake(key.isReadable(), key.isWritable());
}catch ( IOException x ) {
@@ -1587,7 +1587,7 @@ public class NioEndpoint extends Abstrac
(KeyAttachment) key.attachment(),
status);
}
-
+
if (state == SocketState.CLOSED) {
// Close socket and pool
try {
Modified: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioSelectorPool.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioSelectorPool.java?rev=1200168&r1=1200167&r2=1200168&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioSelectorPool.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/NioSelectorPool.java Thu Nov 10 05:53:21 2011
@@ -39,19 +39,19 @@ import org.apache.tomcat.util.MutableInt
*/
public class NioSelectorPool {
-
+
public NioSelectorPool() {
}
-
+
private static final Log log = LogFactory.getLog(NioSelectorPool.class);
protected static final boolean SHARED =
Boolean.valueOf(System.getProperty("org.apache.tomcat.util.net.NioSelectorShared", "true")).booleanValue();
-
+
protected NioBlockingSelector blockingSelector;
-
+
protected volatile Selector SHARED_SELECTOR;
-
+
protected int maxSelectors = 200;
protected long sharedSelectorTimeout = 30000;
protected int maxSpareSelectors = -1;
@@ -168,8 +168,8 @@ public class NioSelectorPool {
public int write(ByteBuffer buf, NioChannel socket, Selector selector, long writeTimeout) throws IOException {
return write(buf,socket,selector,writeTimeout,true,null);
}
-
- public int write(ByteBuffer buf, NioChannel socket, Selector selector,
+
+ public int write(ByteBuffer buf, NioChannel socket, Selector selector,
long writeTimeout, boolean block,MutableInteger lastWrite) throws IOException {
if ( SHARED && block ) {
return blockingSelector.write(buf,socket,writeTimeout,lastWrite);
@@ -186,7 +186,7 @@ public class NioSelectorPool {
cnt = socket.write(buf); //write the data
if (lastWrite!=null) lastWrite.set(cnt);
if (cnt == -1) throw new EOFException();
-
+
written += cnt;
if (cnt > 0) {
time = System.currentTimeMillis(); //reset our timeout timer
@@ -229,7 +229,7 @@ public class NioSelectorPool {
return read(buf,socket,selector,readTimeout,true);
}
/**
- * Performs a read using the bytebuffer for data to be read and a selector to register for events should
+ * Performs a read using the bytebuffer for data to be read and a selector to register for events should
* you have the block=true.
* If the <code>selector</code> parameter is null, then it will perform a busy read that could
* take up a lot of CPU cycles.
@@ -261,7 +261,7 @@ public class NioSelectorPool {
read += cnt;
if (cnt > 0) continue; //read some more
if (cnt==0 && (read>0 || (!block) ) ) break; //we are done reading
- }
+ }
if ( selector != null ) {//perform a blocking read
//register OP_WRITE to the selector
if (key==null) key = socket.getIOChannel().register(selector, SelectionKey.OP_READ);
Modified: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java?rev=1200168&r1=1200167&r2=1200168&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java Thu Nov 10 05:53:21 2011
@@ -32,28 +32,28 @@ import javax.net.ssl.SSLEngineResult.Sta
import org.apache.tomcat.util.MutableInteger;
/**
- *
+ *
* Implementation of a secure socket channel
* @author Filip Hanik
* @version 1.0
*/
public class SecureNioChannel extends NioChannel {
-
+
protected ByteBuffer netInBuffer;
protected ByteBuffer netOutBuffer;
-
+
protected SSLEngine sslEngine;
-
+
protected boolean handshakeComplete = false;
protected HandshakeStatus handshakeStatus; //gets set by handshake
-
+
protected boolean closed = false;
protected boolean closing = false;
-
+
protected NioSelectorPool pool;
-
- public SecureNioChannel(SocketChannel channel, SSLEngine engine,
+
+ public SecureNioChannel(SocketChannel channel, SSLEngine engine,
ApplicationBufferHandler bufHandler, NioSelectorPool pool) throws IOException {
super(channel,bufHandler);
this.sslEngine = engine;
@@ -62,17 +62,17 @@ public class SecureNioChannel extends Ni
//allocate network buffers - TODO, add in optional direct non-direct buffers
if ( netInBuffer == null ) netInBuffer = ByteBuffer.allocateDirect(netBufSize);
if ( netOutBuffer == null ) netOutBuffer = ByteBuffer.allocateDirect(netBufSize);
-
+
//selector pool for blocking operations
this.pool = pool;
-
+
//ensure that the application has a large enough read/write buffers
//by doing this, we should not encounter any buffer overflow errors
bufHandler.expand(bufHandler.getReadBuffer(), appBufSize);
bufHandler.expand(bufHandler.getWriteBuffer(), appBufSize);
reset();
}
-
+
public void reset(SSLEngine engine) throws IOException {
this.sslEngine = engine;
reset();
@@ -91,7 +91,7 @@ public class SecureNioChannel extends Ni
sslEngine.beginHandshake();
handshakeStatus = sslEngine.getHandshakeStatus();
}
-
+
@Override
public int getBufferSize() {
int size = super.getBufferSize();
@@ -100,12 +100,12 @@ public class SecureNioChannel extends Ni
return size;
}
-
-//===========================================================================================
+
+//===========================================================================================
// NIO SSL METHODS
//===========================================================================================
/**
- * returns true if the network buffer has
+ * returns true if the network buffer has
* been flushed out and is empty
* @return boolean
*/
@@ -118,7 +118,7 @@ public class SecureNioChannel extends Ni
}
return !netOutBuffer.hasRemaining();
}
-
+
/**
* Flushes the buffer to the network, non blocking
* @param buf ByteBuffer
@@ -134,7 +134,7 @@ public class SecureNioChannel extends Ni
return true;
}
}
-
+
/**
* Performs SSL handshake, non blocking, but performs NEED_TASK on the same thread.<br>
* Hence, you should never call this method using your Acceptor thread, as you would slow down
@@ -149,11 +149,11 @@ public class SecureNioChannel extends Ni
@Override
public int handshake(boolean read, boolean write) throws IOException {
if ( handshakeComplete ) return 0; //we have done our initial handshake
-
+
if (!flush(netOutBuffer)) return SelectionKey.OP_WRITE; //we still have data to write
-
+
SSLEngineResult handshake = null;
-
+
while (!handshakeComplete) {
switch ( handshakeStatus ) {
case NOT_HANDSHAKING: {
@@ -164,13 +164,13 @@ public class SecureNioChannel extends Ni
//we are complete if we have delivered the last package
handshakeComplete = !netOutBuffer.hasRemaining();
//return 0 if we are complete, otherwise we still have data to write
- return handshakeComplete?0:SelectionKey.OP_WRITE;
+ return handshakeComplete?0:SelectionKey.OP_WRITE;
}
case NEED_WRAP: {
//perform the wrap function
handshake = handshakeWrap(write);
if ( handshake.getStatus() == Status.OK ){
- if (handshakeStatus == HandshakeStatus.NEED_TASK)
+ if (handshakeStatus == HandshakeStatus.NEED_TASK)
handshakeStatus = tasks();
} else {
//wrap should always work with our buffers
@@ -180,7 +180,7 @@ public class SecureNioChannel extends Ni
//should actually return OP_READ if we have NEED_UNWRAP
return SelectionKey.OP_WRITE;
}
- //fall down to NEED_UNWRAP on the same call, will result in a
+ //fall down to NEED_UNWRAP on the same call, will result in a
//BUFFER_UNDERFLOW if it needs data
}
//$FALL-THROUGH$
@@ -188,7 +188,7 @@ public class SecureNioChannel extends Ni
//perform the unwrap function
handshake = handshakeUnwrap(read);
if ( handshake.getStatus() == Status.OK ) {
- if (handshakeStatus == HandshakeStatus.NEED_TASK)
+ if (handshakeStatus == HandshakeStatus.NEED_TASK)
handshakeStatus = tasks();
} else if ( handshake.getStatus() == Status.BUFFER_UNDERFLOW ){
//read more data, reregister for OP_READ
@@ -204,15 +204,15 @@ public class SecureNioChannel extends Ni
}
default: throw new IllegalStateException("Invalid handshake status:"+handshakeStatus);
}//switch
- }//while
- //return 0 if we are complete, otherwise reregister for any activity that
+ }//while
+ //return 0 if we are complete, otherwise reregister for any activity that
//would cause this method to be called again.
return handshakeComplete?0:(SelectionKey.OP_WRITE|SelectionKey.OP_READ);
}
-
+
/**
* Force a blocking handshake to take place for this key.
- * This requires that both network and application buffers have been emptied out prior to this call taking place, or a
+ * This requires that both network and application buffers have been emptied out prior to this call taking place, or a
* IOException will be thrown.
* @param timeout - timeout in milliseconds for each socket operation
* @throws IOException - if an IO exception occurs or if application or network buffers contain data
@@ -268,9 +268,9 @@ public class SecureNioChannel extends Ni
if (selector!=null) try {selector.close();} catch (Exception ignore) {}
}
}
-
-
-
+
+
+
/**
* Executes all the tasks needed on the same thread.
* @return HandshakeStatus
@@ -303,7 +303,7 @@ public class SecureNioChannel extends Ni
if ( doWrite ) flush(netOutBuffer);
return result;
}
-
+
/**
* Perform handshake unwrap
* @param doread boolean
@@ -311,7 +311,7 @@ public class SecureNioChannel extends Ni
* @throws IOException
*/
protected SSLEngineResult handshakeUnwrap(boolean doread) throws IOException {
-
+
if (netInBuffer.position() == netInBuffer.limit()) {
//clear the buffer if we have emptied it out on data
netInBuffer.clear();
@@ -320,7 +320,7 @@ public class SecureNioChannel extends Ni
//if we have data to read, read it
int read = sc.read(netInBuffer);
if (read == -1) throw new IOException("EOF encountered during handshake.");
- }
+ }
SSLEngineResult result;
boolean cont = false;
//loop while we can perform pure SSLEngine data
@@ -344,7 +344,7 @@ public class SecureNioChannel extends Ni
}while ( cont );
return result;
}
-
+
/**
* Sends a SSL close message, will not physically close the connection here.<br>
* To close the connection, you could do something like
@@ -423,7 +423,7 @@ public class SecureNioChannel extends Ni
int netread = sc.read(netInBuffer);
//did we reach EOF? if so send EOF up one layer.
if (netread == -1) return -1;
-
+
//the data read
int read = 0;
//the SSL engine result
@@ -435,7 +435,7 @@ public class SecureNioChannel extends Ni
unwrap = sslEngine.unwrap(netInBuffer, dst);
//compact the buffer
netInBuffer.compact();
-
+
if ( unwrap.getStatus()==Status.OK || unwrap.getStatus()==Status.BUFFER_UNDERFLOW ) {
//we did receive some data, add it to our total
read += unwrap.bytesProduced();
@@ -444,7 +444,7 @@ public class SecureNioChannel extends Ni
//if we need more network data, then bail out for now.
if ( unwrap.getStatus() == Status.BUFFER_UNDERFLOW ) break;
}else if ( unwrap.getStatus()==Status.BUFFER_OVERFLOW && read>0 ) {
- //buffer overflow can happen, if we have read data, then
+ //buffer overflow can happen, if we have read data, then
//empty out the dst buffer before we do another read
break;
}else {
@@ -499,7 +499,7 @@ public class SecureNioChannel extends Ni
if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) tasks();
} else {
throw new IOException("Unable to wrap data, invalid engine state: " +result.getStatus());
- }
+ }
//force a flush
flush(netOutBuffer);
@@ -507,12 +507,12 @@ public class SecureNioChannel extends Ni
return written;
}
}
-
+
@Override
public int getOutboundRemaining() {
return netOutBuffer.remaining();
}
-
+
@Override
public boolean flushOutbound() throws IOException {
int remaining = netOutBuffer.remaining();
@@ -521,7 +521,7 @@ public class SecureNioChannel extends Ni
return remaining2 < remaining;
}
-
+
/**
* Callback interface to be able to expand buffers
* when buffer overflow exceptions happen
@@ -558,7 +558,7 @@ public class SecureNioChannel extends Ni
public void setBufHandler(ApplicationBufferHandler bufHandler) {
this.bufHandler = bufHandler;
}
-
+
@Override
public SocketChannel getIOChannel() {
return sc;
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org