You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by bl...@apache.org on 2004/12/09 16:35:27 UTC
svn commit: r111393 - in incubator/directory/seda/branches/berin_api_proposal/src: java/org/apache/directory/seda java/org/apache/directory/seda/bufferpool java/org/apache/directory/seda/input java/org/apache/directory/seda/output test/org/apache/directory/seda/bufferpool/test test/org/apache/directory/seda/output/test test/org/apache/directory/seda/test
Author: bloritsch
Date: Thu Dec 9 07:35:24 2004
New Revision: 111393
URL: http://svn.apache.org/viewcvs?view=rev&rev=111393
Log:
Test BufferPool, using WeakReferences for bad clients who forget to clean up after themselves. Also add
the read buffer to the ReaderSource. Lastly, change WriterSink to WriterStage
Added:
incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/bufferpool/BufferPool.java
incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterStage.java
- copied, changed from r111384, incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterSink.java
incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/bufferpool/test/TestBufferPool.java
incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/output/test/TestWriterStage.java
Removed:
incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterSink.java
Modified:
incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/NetworkEvent.java
incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/SEDAServer.java
incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/ReaderSource.java
incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestNetworkEvent.java
Modified: incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/NetworkEvent.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/NetworkEvent.java?view=diff&rev=111393&p1=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/NetworkEvent.java&r1=111392&p2=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/NetworkEvent.java&r2=111393
==============================================================================
--- incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/NetworkEvent.java (original)
+++ incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/NetworkEvent.java Thu Dec 9 07:35:24 2004
@@ -19,6 +19,7 @@
import java.net.Socket;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
+import java.nio.ByteBuffer;
/**
* Created by IntelliJ IDEA. User: berin Date: Dec 2, 2004 Time: 9:44:03
@@ -28,6 +29,7 @@
{
private final Socket m_socket;
private final SocketChannel m_channel;
+ private ByteBuffer m_buffer;
public NetworkEvent(final SelectionKey key)
{
@@ -48,5 +50,15 @@
public SocketChannel channel()
{
return m_channel;
+ }
+
+ public void setBuffer( final ByteBuffer buffer )
+ {
+ m_buffer = buffer;
+ }
+
+ public ByteBuffer getBuffer()
+ {
+ return m_buffer;
}
}
Modified: incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/SEDAServer.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/SEDAServer.java?view=diff&rev=111393&p1=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/SEDAServer.java&r1=111392&p2=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/SEDAServer.java&r2=111393
==============================================================================
--- incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/SEDAServer.java (original)
+++ incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/SEDAServer.java Thu Dec 9 07:35:24 2004
@@ -18,7 +18,7 @@
import org.apache.directory.seda.input.Firewall;
import org.apache.directory.seda.input.ReaderSource;
-import org.apache.directory.seda.output.WriterSink;
+import org.apache.directory.seda.output.WriterStage;
import java.nio.channels.SocketChannel;
import java.io.IOException;
@@ -33,13 +33,13 @@
private static SEDAServer m_server;
private final Firewall m_firewall;
private final ReaderSource m_reader;
- private final WriterSink m_writer;
+ private final WriterStage m_writer;
private SEDAServer() throws IOException
{
m_firewall = new Firewall();
m_reader = new ReaderSource();
- m_writer = new WriterSink();
+ m_writer = new WriterStage();
}
public static SEDAServer getServer() throws IOException
Added: incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/bufferpool/BufferPool.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/bufferpool/BufferPool.java?view=auto&rev=111393
==============================================================================
--- (empty file)
+++ incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/bufferpool/BufferPool.java Thu Dec 9 07:35:24 2004
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.directory.seda.bufferpool;
+
+import java.nio.ByteBuffer;
+import java.util.LinkedList;
+import java.lang.ref.WeakReference;
+
+/**
+ * Created by IntelliJ IDEA. User: berin Date: Dec 9, 2004 Time: 9:45:15
+ * AM To change this template use File | Settings | File Templates.
+ */
+public class BufferPool
+{
+ private static final int KILOBYTE = 1024;
+ private static int m_usedBuffers;
+ private static final LinkedList m_reserveBuffers = new LinkedList();
+
+ public static ByteBuffer getBuffer()
+ {
+ ByteBuffer buffer = null;
+ while (null == buffer && m_reserveBuffers.size() > 0)
+ {
+ final WeakReference ref = (WeakReference)m_reserveBuffers.removeFirst();
+ buffer = (ByteBuffer)ref.get();
+ }
+
+ buffer = createBuffer();
+ m_usedBuffers++;
+
+ return buffer;
+ }
+
+ private static ByteBuffer createBuffer()
+ {
+ final ByteBuffer buffer = ByteBuffer.allocateDirect( KILOBYTE );
+ return buffer;
+ }
+
+ public static int buffersInUse()
+ {
+ return m_usedBuffers;
+ }
+
+ public static void putBuffer( final ByteBuffer buffer )
+ {
+ m_usedBuffers--;
+ assert buffer.isDirect();
+ assert KILOBYTE == buffer.capacity();
+ m_reserveBuffers.add( new WeakReference(buffer) );
+ }
+
+ public static int buffersInReserve()
+ {
+ return m_reserveBuffers.size();
+ }
+
+ public static void reset()
+ {
+ m_usedBuffers = 0;
+ m_reserveBuffers.clear();
+ }
+}
Modified: incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/ReaderSource.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/ReaderSource.java?view=diff&rev=111393&p1=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/ReaderSource.java&r1=111392&p2=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/ReaderSource.java&r2=111393
==============================================================================
--- incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/ReaderSource.java (original)
+++ incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/ReaderSource.java Thu Dec 9 07:35:24 2004
@@ -18,10 +18,12 @@
import org.d_haven.event.Source;
import org.apache.directory.seda.NetworkEvent;
+import org.apache.directory.seda.bufferpool.BufferPool;
import java.nio.channels.SocketChannel;
import java.nio.channels.Selector;
import java.nio.channels.SelectionKey;
+import java.nio.ByteBuffer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
@@ -59,8 +61,13 @@
while (it.hasNext() && keys.size() < elements)
{
final SelectionKey key = (SelectionKey)it.next();
- keys.add( new NetworkEvent(key) );
+ final NetworkEvent event = new NetworkEvent(key);
+ keys.add( event );
it.remove();
+
+ final ByteBuffer buffer = BufferPool.getBuffer();
+ event.channel().read( buffer );
+ event.setBuffer( buffer );
}
}
catch(Exception e)
Deleted: /incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterSink.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterSink.java?view=auto&rev=111392
==============================================================================
Copied: incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterStage.java (from r111384, incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterSink.java)
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterStage.java?view=diff&rev=111393&p1=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterSink.java&r1=111384&p2=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterStage.java&r2=111393
==============================================================================
--- incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterSink.java (original)
+++ incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/output/WriterStage.java Thu Dec 9 07:35:24 2004
@@ -20,6 +20,7 @@
import org.d_haven.event.Sink;
import org.d_haven.event.SinkException;
import org.d_haven.event.PreparedEnqueue;
+import org.apache.directory.seda.Stage;
import java.nio.channels.SocketChannel;
import java.nio.channels.Selector;
@@ -32,73 +33,13 @@
* Created by IntelliJ IDEA. User: berin Date: Dec 8, 2004 Time: 8:29:38
* AM To change this template use File | Settings | File Templates.
*/
-public class WriterSink implements Sink
+public class WriterStage extends Stage
{
private Selector m_selector;
- private long m_timeout;
- public WriterSink() throws IOException
+ public WriterStage() throws IOException
{
m_selector = Selector.open();
- m_timeout = 1L;
- }
-
- private Object[] getPending(final int number)
- {
- final ArrayList keys = new ArrayList();
- try
- {
- final int numPending = m_selector.select( m_timeout );
- final int elements = Math.min( numPending, number );
- keys.ensureCapacity( elements );
-
- final Iterator it = m_selector.selectedKeys().iterator();
- while (it.hasNext() && keys.size() < elements)
- {
- keys.add( (it.next() ) );
- it.remove();
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
-
- return keys.toArray();
- }
-
- public void enqueue( Object o ) throws SinkException
- {
- //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public void enqueue( Object[] objects ) throws SinkException
- {
- //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public boolean tryEnqueue( Object o )
- {
- return false; //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public PreparedEnqueue prepareEnqueue( Object[] objects )
- throws SinkException
- {
- return null; //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public int size()
- {
- try
- {
- return m_selector.select( m_timeout );
- }
- catch ( IOException e )
- {
- e.printStackTrace();
- return 0;
- }
}
public void connect( final SocketChannel client ) throws IOException
Added: incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/bufferpool/test/TestBufferPool.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/bufferpool/test/TestBufferPool.java?view=auto&rev=111393
==============================================================================
--- (empty file)
+++ incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/bufferpool/test/TestBufferPool.java Thu Dec 9 07:35:24 2004
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.directory.seda.bufferpool.test;
+
+import junit.framework.TestCase;
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.seda.bufferpool.BufferPool;
+
+/**
+ * Created by IntelliJ IDEA. User: berin Date: Dec 9, 2004 Time: 9:40:14
+ * AM To change this template use File | Settings | File Templates.
+ */
+public class TestBufferPool extends TestCase
+{
+ private static final int KB = 1024;
+
+ public TestBufferPool(final String name)
+ {
+ super(name);
+ }
+
+ public void tearDown() throws Exception
+ {
+ super.tearDown();
+
+ BufferPool.reset();
+ }
+
+ public void testGetBuffer()
+ {
+ final ByteBuffer buffer = BufferPool.getBuffer();
+
+ assertNotNull(buffer);
+ assertEquals(KB, buffer.capacity());
+ assertTrue( buffer.isDirect() );
+ assertEquals( 1, BufferPool.buffersInUse() );
+
+ BufferPool.putBuffer(buffer);
+ }
+
+ public void testPutBuffer()
+ {
+ final ByteBuffer buffer = BufferPool.getBuffer();
+ assertEquals( 1, BufferPool.buffersInUse() );
+ assertEquals( 0, BufferPool.buffersInReserve() );
+
+ BufferPool.putBuffer(buffer);
+ assertEquals( 0, BufferPool.buffersInUse() );
+ assertEquals( 1, BufferPool.buffersInReserve() );
+ }
+
+ public void testBuffersInReserve()
+ {
+ assertEquals( 0, BufferPool.buffersInReserve() );
+
+ final ByteBuffer buffer1 = BufferPool.getBuffer();
+ BufferPool.putBuffer( buffer1 );
+
+ assertEquals( 1, BufferPool.buffersInReserve() );
+
+ final ByteBuffer buffer2 = BufferPool.getBuffer();
+
+ assertEquals( 0, BufferPool.buffersInReserve() );
+
+ BufferPool.putBuffer( buffer2 );
+
+ assertEquals( 1, BufferPool.buffersInReserve() );
+ }
+}
Added: incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/output/test/TestWriterStage.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/output/test/TestWriterStage.java?view=auto&rev=111393
==============================================================================
--- (empty file)
+++ incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/output/test/TestWriterStage.java Thu Dec 9 07:35:24 2004
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.directory.seda.output.test;
+
+/**
+ * Created by IntelliJ IDEA. User: berin Date: Dec 9, 2004 Time:
+ * 10:33:51 AM To change this template use File | Settings | File
+ * Templates.
+ */
+public class TestWriterStage
+{
+}
Modified: incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestNetworkEvent.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestNetworkEvent.java?view=diff&rev=111393&p1=incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestNetworkEvent.java&r1=111392&p2=incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestNetworkEvent.java&r2=111393
==============================================================================
--- incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestNetworkEvent.java (original)
+++ incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestNetworkEvent.java Thu Dec 9 07:35:24 2004
@@ -97,4 +97,14 @@
assertEquals(m_clientChannel.socket(), event.socket() );
assertEquals(m_clientChannel, event.channel() );
}
+
+ public void testSetBuffer() throws IOException
+ {
+ final NetworkEvent event = new NetworkEvent(m_clientChannel);
+
+ final ByteBuffer buffer = ByteBuffer.allocate( 1 );
+ event.setBuffer( buffer );
+
+ assertEquals( buffer, event.getBuffer() );
+ }
}