directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r123313 - in incubator/directory/network/trunk/mina/src: examples/org/apache/mina/examples/echoserver examples/org/apache/mina/examples/reverser java/org/apache/mina/io/datagram java/org/apache/mina/io/socket java/org/apache/mina/util
Date Sat, 25 Dec 2004 04:54:09 GMT
Author: trustin
Date: Fri Dec 24 20:54:07 2004
New Revision: 123313

URL: http://svn.apache.org/viewcvs?view=rev&rev=123313
Log:
 * Made ByteBufferPool to be able to allocate buffers with various sizes
Modified:
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineEncoder.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramConnector.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSessionConfig.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java

Modified: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java?view=diff&rev=123313&p1=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java&r1=123312&p2=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java&r2=123313
==============================================================================
--- incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java
(original)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java
Fri Dec 24 20:54:07 2004
@@ -23,6 +23,7 @@
 import org.apache.mina.common.IdleStatus;
 import org.apache.mina.io.IoHandler;
 import org.apache.mina.io.IoSession;
+import org.apache.mina.util.ByteBufferPool;
 
 /**
  * TODO Document me.
@@ -59,18 +60,20 @@
 
     public void dataRead( IoSession session, ByteBuffer rb )
     {
-        ByteBuffer wb = ByteBuffer.allocate( rb.remaining() );
+        ByteBuffer wb = ByteBufferPool.allocate( rb.remaining() );
         wb.put( rb );
         wb.flip();
         System.out.println( Thread.currentThread().getName() + ' '
                             + session.getRemoteAddress() + ": READ ("
                             + wb.remaining() + "B)" );
-        session.write( wb, null );
+        session.write( wb, wb );
     }
 
     public void dataWritten( IoSession IoSession, Object marker )
     {
         System.out.println( Thread.currentThread().getName() + ' '
                             + IoSession.getRemoteAddress() + ": WRITTEN" );
+        
+        ByteBufferPool.release( ( ByteBuffer ) marker );
     }
 }

Modified: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineEncoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineEncoder.java?view=diff&rev=123313&p1=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineEncoder.java&r1=123312&p2=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineEncoder.java&r2=123313
==============================================================================
--- incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineEncoder.java
(original)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineEncoder.java
Fri Dec 24 20:54:07 2004
@@ -24,6 +24,7 @@
 import org.apache.mina.protocol.ProtocolEncoderOutput;
 import org.apache.mina.protocol.ProtocolSession;
 import org.apache.mina.protocol.ProtocolViolationException;
+import org.apache.mina.util.ByteBufferPool;
 
 /**
  * TODO Document me.
@@ -48,7 +49,7 @@
 
         val += "\r\n";
 
-        ByteBuffer buf = ByteBuffer.allocate( val.length() );
+        ByteBuffer buf = ByteBufferPool.allocate( val.length() );
         for( int i = 0; i < val.length(); i++ )
         {
             buf.put( ( byte ) val.charAt( i ) );

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java?view=diff&rev=123313&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java&r1=123312&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java&r2=123313
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
Fri Dec 24 20:54:07 2004
@@ -35,6 +35,7 @@
 import org.apache.mina.io.ExceptionMonitor;
 import org.apache.mina.io.IoHandler;
 import org.apache.mina.io.IoHandlerFilter;
+import org.apache.mina.util.ByteBufferPool;
 import org.apache.mina.util.DefaultExceptionMonitor;
 import org.apache.mina.util.IoHandlerFilterManager;
 import org.apache.mina.util.Queue;
@@ -244,7 +245,7 @@
     private void readSession( DatagramSession session )
     {
 
-        ByteBuffer readBuf = ByteBuffer.allocate( 1500 );
+        ByteBuffer readBuf = ByteBufferPool.allocate( 2048 );
         try
         {
             SocketAddress remoteAddress = session.getChannel()

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramConnector.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramConnector.java?view=diff&rev=123313&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramConnector.java&r1=123312&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramConnector.java&r2=123313
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramConnector.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramConnector.java
Fri Dec 24 20:54:07 2004
@@ -34,6 +34,7 @@
 import org.apache.mina.io.IoHandler;
 import org.apache.mina.io.IoHandlerFilter;
 import org.apache.mina.io.IoSession;
+import org.apache.mina.util.ByteBufferPool;
 import org.apache.mina.util.DefaultExceptionMonitor;
 import org.apache.mina.util.IoHandlerFilterManager;
 import org.apache.mina.util.Queue;
@@ -238,7 +239,7 @@
     private void readSession( DatagramSession session )
     {
 
-        ByteBuffer readBuf = ByteBuffer.allocate( 1500 );
+        ByteBuffer readBuf = ByteBufferPool.allocate( 2048 );
         try
         {
             int readBytes = session.getChannel().read( readBuf );

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java?view=diff&rev=123313&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java&r1=123312&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java&r2=123313
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
Fri Dec 24 20:54:07 2004
@@ -29,7 +29,6 @@
 import org.apache.mina.common.IdleStatus;
 import org.apache.mina.common.SessionConfig;
 import org.apache.mina.io.WriteTimeoutException;
-import org.apache.mina.util.ByteBufferPool;
 import org.apache.mina.util.Queue;
 
 /**
@@ -148,8 +147,6 @@
             try
             {
                 ch.configureBlocking( false );
-                ch.socket().setSendBufferSize( ByteBufferPool.CAPACITY );
-                ch.socket().setReceiveBufferSize( ByteBufferPool.CAPACITY );
                 session.setSelectionKey( ch.register( selector,
                                                       SelectionKey.OP_READ,
                                                       session ) );

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java?view=diff&rev=123313&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java&r1=123312&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java&r2=123313
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSession.java
Fri Dec 24 20:54:07 2004
@@ -84,7 +84,7 @@
         this.filterManager = filterManager;
         this.ch = ch;
         this.config = new SocketSessionConfig( ch );
-        this.readBuf = ( ByteBuffer ) ByteBufferPool.open().limit( 0 );
+        this.readBuf = ( ByteBuffer ) ByteBufferPool.allocate( 4096 ).limit( 0 );
         this.writeBufferQueue = new Queue();
         this.writeMarkerQueue = new Queue();
         this.handler = defaultHandler;
@@ -114,7 +114,7 @@
 
     void dispose()
     {
-        ByteBufferPool.close( readBuf );
+        ByteBufferPool.release( readBuf );
     }
 
     public IoHandler getHandler()

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSessionConfig.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSessionConfig.java?view=diff&rev=123313&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSessionConfig.java&r1=123312&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSessionConfig.java&r2=123313
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSessionConfig.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketSessionConfig.java
Fri Dec 24 20:54:07 2004
@@ -97,4 +97,24 @@
     {
         ch.socket().setTrafficClass( tc );
     }
+    
+    public int getSendBufferSize() throws SocketException
+    {
+    	return ch.socket().getSendBufferSize();
+    }
+    
+    public void setSendBufferSize( int size ) throws SocketException
+    {
+    	ch.socket().setSendBufferSize( size );
+    }
+	
+    public int getReceiveBufferSize() throws SocketException
+    {
+    	return ch.socket().getReceiveBufferSize();
+    }
+    
+    public void setReceiveBufferSize( int size ) throws SocketException
+    {
+    	ch.socket().setReceiveBufferSize( size );
+    }
 }

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java?view=diff&rev=123313&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java&r1=123312&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java&r2=123313
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java
Fri Dec 24 20:54:07 2004
@@ -28,28 +28,90 @@
  */
 public class ByteBufferPool
 {
-    public static final int CAPACITY = 8192;
-
-    private static Queue buffers = new Queue();
-
-    public static synchronized ByteBuffer open()
+    private static final int[] bufferStackSizes = new int[] {
+    		16,
+			32,
+			64,
+			128,
+			256,
+			512,
+			1024,
+			2048,
+			3072,
+			4096,
+			6144,
+			8192,
+    };
+    
+    private static final Stack[] bufferStacks = new Stack[] {
+    		new Stack(),
+    		new Stack(),
+    		new Stack(),
+    		new Stack(),
+    		new Stack(),
+    		new Stack(),
+    		new Stack(),
+    		new Stack(),
+    		new Stack(),
+    		new Stack(),
+    		new Stack(),
+    		new Stack(),
+    };
+    
+    public static ByteBuffer allocate(int size)
     {
-        ByteBuffer buf = ( ByteBuffer ) buffers.pop();
-
-        if( buf == null )
-        {
-            buf = ByteBuffer.allocateDirect( CAPACITY );
-        }
-        else
-        {
-            buf.clear();
-        }
-
-        return buf;
+    	int idx = getBufferStackIndex(size);
+    	Stack stack = bufferStacks[idx];
+    	
+    	ByteBuffer buf;
+		synchronized (stack)
+		{
+			buf = (ByteBuffer) stack.pop();
+    		if (buf == null)
+    		{
+    			buf = ByteBuffer.allocate( bufferStackSizes[ idx ] );
+    		}
+		}
+		
+		buf.clear();
+		return buf;
     }
 
-    public static synchronized void close( ByteBuffer buf )
+    public static void release( ByteBuffer buf )
     {
-        buffers.push( buf );
+    	Stack stack = bufferStacks[ getBufferStackIndex( buf.capacity() ) ];
+    	synchronized (stack)
+		{
+    		stack.push( buf );
+		}
+    }
+    
+    private static int getBufferStackIndex(int size) {
+    	if (size <= 16)
+    		return 0;
+    	if (size <= 32)
+    		return 1;
+    	if (size <= 64)
+    		return 2;
+    	if (size <= 128)
+    		return 3;
+    	if (size <= 256)
+    		return 4;
+    	if (size <= 512)
+    		return 5;
+    	if (size <= 1024)
+    		return 6;
+    	if (size <= 2048)
+    		return 7;
+    	if (size <= 3072)
+    		return 8;
+    	if (size <= 4096)
+    		return 9;
+    	if (size <= 6144)
+    		return 10;
+    	if (size <= 8192)
+    		return 11;
+    	
+    	throw new IllegalArgumentException("Buffer size cannot exceed 8192: " + size);
     }
 }

Mime
View raw message