directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From blorit...@apache.org
Subject svn commit: r111319 - in incubator/directory/seda/branches/berin_api_proposal/src: java/org/apache/directory/seda java/org/apache/directory/seda/input test/org/apache/directory/seda/input/test test/org/apache/directory/seda/test
Date Wed, 08 Dec 2004 22:11:19 GMT
Author: bloritsch
Date: Wed Dec  8 14:11:18 2004
New Revision: 111319

URL: http://svn.apache.org/viewcvs?view=rev&rev=111319
Log:
Reader object almost done (need to actually read info)
Added:
   incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/input/test/TestReaderSource.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/ProtocolRouter.java
   incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/Firewall.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
   incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestProtocolRouter.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=111319&p1=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/NetworkEvent.java&r1=111318&p2=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/NetworkEvent.java&r2=111319
==============================================================================
--- 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
Wed Dec  8 14:11:18 2004
@@ -16,7 +16,9 @@
  */
 package org.apache.directory.seda;
 
-import java.net.ServerSocket;
+import java.net.Socket;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.SocketChannel;
 
 /**
  * Created by IntelliJ IDEA. User: berin Date: Dec 2, 2004 Time: 9:44:03
@@ -24,15 +26,27 @@
  */
 public class NetworkEvent
 {
-    private final ServerSocket m_socket;
+    private final Socket m_socket;
+    private final SocketChannel m_channel;
 
-    public NetworkEvent(final ServerSocket socket)
+    public NetworkEvent(final SelectionKey key)
     {
-        m_socket = socket;
+        this( (SocketChannel)key.channel() );
     }
 
-    public ServerSocket getSocket()
+    public NetworkEvent( final SocketChannel channel )
+    {
+        m_channel = channel;
+        m_socket = m_channel.socket();
+    }
+
+    public Socket socket()
     {
         return m_socket;
+    }
+
+    public SocketChannel channel()
+    {
+        return m_channel;
     }
 }

Modified: incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/ProtocolRouter.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/ProtocolRouter.java?view=diff&rev=111319&p1=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/ProtocolRouter.java&r1=111318&p2=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/ProtocolRouter.java&r2=111319
==============================================================================
--- incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/ProtocolRouter.java
(original)
+++ incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/ProtocolRouter.java
Wed Dec  8 14:11:18 2004
@@ -29,7 +29,7 @@
         if ( event instanceof NetworkEvent )
         {
             final NetworkEvent netEvent = (NetworkEvent) event;
-            final int port = netEvent.getSocket().getLocalPort();
+            final int port = netEvent.socket().getLocalPort();
 
             Pipe pipe = getNamedPipe( Integer.toString( port ) );
 

Modified: incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/Firewall.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/Firewall.java?view=diff&rev=111319&p1=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/Firewall.java&r1=111318&p2=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/Firewall.java&r2=111319
==============================================================================
--- incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/Firewall.java
(original)
+++ incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/Firewall.java
Wed Dec  8 14:11:18 2004
@@ -23,10 +23,6 @@
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
 import java.io.IOException;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.net.Socket;
-import java.net.ServerSocket;
 import java.net.SocketAddress;
 import java.util.HashSet;
 import java.util.Set;

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=111319&p1=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/ReaderSource.java&r1=111318&p2=incubator/directory/seda/branches/berin_api_proposal/src/java/org/apache/directory/seda/input/ReaderSource.java&r2=111319
==============================================================================
--- 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
Wed Dec  8 14:11:18 2004
@@ -17,6 +17,7 @@
 package org.apache.directory.seda.input;
 
 import org.d_haven.event.Source;
+import org.apache.directory.seda.NetworkEvent;
 
 import java.nio.channels.SocketChannel;
 import java.nio.channels.Selector;
@@ -57,7 +58,8 @@
             final Iterator it = m_selector.selectedKeys().iterator();
             while (it.hasNext() && keys.size() < elements)
             {
-                keys.add( (it.next() ) );
+                final SelectionKey key = (SelectionKey)it.next();
+                keys.add( new NetworkEvent(key) );
                 it.remove();
             }
         }

Added: incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/input/test/TestReaderSource.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/input/test/TestReaderSource.java?view=auto&rev=111319
==============================================================================
--- (empty file)
+++ incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/input/test/TestReaderSource.java
Wed Dec  8 14:11:18 2004
@@ -0,0 +1,128 @@
+/*
+ *   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.input.test;
+
+import junit.framework.TestCase;
+
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.io.IOException;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.SocketChannel;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.seda.input.ConnectSource;
+import org.apache.directory.seda.input.ReaderSource;
+import org.apache.directory.seda.NetworkEvent;
+
+/**
+ * Created by IntelliJ IDEA. User: berin Date: Dec 3, 2004 Time: 9:12:50
+ * AM To change this template use File | Settings | File Templates.
+ */
+public class TestReaderSource extends TestCase
+{
+    private static final int PORT = 6666;
+    private SocketChannel m_clientChannel;
+    private ReaderSource m_source;
+    private ServerSocketChannel m_serverChannel;
+    private SocketChannel m_channel;
+
+    public TestReaderSource(final String name)
+    {
+        super(name);
+    }
+
+    public void setUp() throws Exception
+    {
+        super.setUp();
+
+        m_serverChannel = ServerSocketChannel.open();
+        m_serverChannel.socket().bind( new InetSocketAddress(PORT) );
+
+        m_channel = SocketChannel.open();
+        m_channel.configureBlocking( false );
+        m_channel.connect(  new InetSocketAddress(InetAddress.getLocalHost(), PORT) );
+
+        m_clientChannel = m_serverChannel.accept();
+        m_channel.finishConnect();
+
+        m_source = new ReaderSource();
+        m_source.connect( m_clientChannel );
+    }
+
+    public void tearDown() throws Exception
+    {
+        super.tearDown();
+
+        m_source.disconnect(m_clientChannel);
+        m_source.close();
+
+        m_clientChannel.close();
+        m_serverChannel.close();
+        m_channel.close();
+    }
+
+    public void testDequeue() throws IOException
+    {
+        m_channel.write( ByteBuffer.wrap( "test".getBytes() ) );
+        final Object key;
+
+        key = m_source.dequeue();
+
+        assertNotNull( key );
+        assertTrue( key instanceof NetworkEvent );
+
+        assertEquals( m_clientChannel, ((NetworkEvent)key).channel() );
+    }
+
+    public void testDequeue_many() throws IOException
+    {
+        m_channel.write( ByteBuffer.wrap( "test".getBytes() ) );
+
+        final Object[] key = m_source.dequeue(1);
+        assertNotNull( key );
+        assertEquals( 1, key.length );
+        assertTrue( key[0] instanceof NetworkEvent );
+
+        assertEquals( m_clientChannel, ((NetworkEvent)key[0]).channel() );
+    }
+
+    public void testDequeue_all() throws IOException
+    {
+        m_channel.write( ByteBuffer.wrap( "test".getBytes() ) );
+
+        final Object[] key = m_source.dequeueAll();
+        assertNotNull( key );
+        assertEquals( 1, key.length );
+        assertTrue( key[0] instanceof NetworkEvent );
+
+        assertEquals( m_clientChannel, ((NetworkEvent)key[0]).channel() );
+    }
+
+    public void testSize_setTimeout() throws IOException
+    {
+        m_channel.write( ByteBuffer.wrap( "test".getBytes() ) );
+
+        m_source.setTimeout( 10L );
+        assertEquals( 1, m_source.size() );
+
+        m_source.dequeueAll();
+    }
+}

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=111319&p1=incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestNetworkEvent.java&r1=111318&p2=incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestNetworkEvent.java&r2=111319
==============================================================================
--- 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
Wed Dec  8 14:11:18 2004
@@ -19,9 +19,18 @@
 import junit.framework.TestCase;
 
 import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.io.IOException;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.Selector;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.ByteBuffer;
 
 import org.apache.directory.seda.NetworkEvent;
+import org.apache.directory.seda.input.ReaderSource;
 
 /**
  * Created by IntelliJ IDEA. User: berin Date: Dec 2, 2004 Time: 9:47:26
@@ -30,7 +39,11 @@
 public class TestNetworkEvent extends TestCase
 {
     private static final int PORT = 6666;
-    private ServerSocket m_socket;
+    private SocketChannel m_channel;
+    private Selector m_selector;
+    private SelectionKey m_key;
+    private ServerSocketChannel m_serverChannel;
+    private SocketChannel m_clientChannel;
 
     public TestNetworkEvent(final String name)
     {
@@ -41,21 +54,50 @@
     {
         super.setUp();
 
-        m_socket = new ServerSocket(PORT);
+        m_serverChannel = ServerSocketChannel.open();
+        m_serverChannel.socket().bind( new InetSocketAddress(PORT) );
+
+        m_channel = SocketChannel.open();
+        m_channel.configureBlocking( false );
+        m_channel.connect(  new InetSocketAddress(InetAddress.getLocalHost(), PORT) );
+
+        m_clientChannel = m_serverChannel.accept();
+        m_clientChannel.configureBlocking( false );
+        m_channel.finishConnect();
+
+        m_selector = Selector.open();
+        m_key = m_clientChannel.register( m_selector, SelectionKey.OP_READ );
     }
 
     public void tearDown() throws Exception
     {
         super.tearDown();
 
-        m_socket.close();
+        m_key.cancel();
+        m_selector.selectNow();
+        m_clientChannel.close();
+        m_serverChannel.close();
+        m_channel.close();
+        m_selector.close();
+    }
+
+    public void testCreate_key() throws IOException
+    {
+        m_channel.write( ByteBuffer.wrap("test".getBytes()));
+        m_selector.select();
+        final NetworkEvent event = new NetworkEvent((SelectionKey)m_selector.selectedKeys().iterator().next());
+
+        assertNotNull(event);
+        assertEquals(m_clientChannel.socket(), event.socket() );
+        assertEquals(m_clientChannel, event.channel() );
     }
 
-    public void testCreate() throws IOException
+    public void testCreate_channel() throws IOException
     {
-        final NetworkEvent event = new NetworkEvent(m_socket);
+        final NetworkEvent event = new NetworkEvent(m_clientChannel);
 
         assertNotNull(event);
-        assertEquals(m_socket, event.getSocket() );
+        assertEquals(m_clientChannel.socket(), event.socket() );
+        assertEquals(m_clientChannel, event.channel() );
     }
 }

Modified: incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestProtocolRouter.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestProtocolRouter.java?view=diff&rev=111319&p1=incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestProtocolRouter.java&r1=111318&p2=incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestProtocolRouter.java&r2=111319
==============================================================================
--- incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestProtocolRouter.java
(original)
+++ incubator/directory/seda/branches/berin_api_proposal/src/test/org/apache/directory/seda/test/TestProtocolRouter.java
Wed Dec  8 14:11:18 2004
@@ -24,8 +24,12 @@
 import org.d_haven.event.command.SwitchedEnqueuePredicate;
 
 import java.net.ServerSocket;
+import java.net.InetSocketAddress;
+import java.net.InetAddress;
 import java.io.IOException;
 import java.util.concurrent.CyclicBarrier;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.ServerSocketChannel;
 
 /**
  * Created by IntelliJ IDEA. User: berin Date: Dec 2, 2004 Time: 9:35:36
@@ -34,7 +38,9 @@
 public class TestProtocolRouter extends TestCase
 {
     private static final int PORT = 6666;
-    private ServerSocket m_socket;
+    private SocketChannel m_channel;
+    private ServerSocketChannel m_serverChannel;
+    private SocketChannel m_clientChannel;
 
     public TestProtocolRouter(final String name)
     {
@@ -45,14 +51,23 @@
     {
         super.setUp();
 
-        m_socket = new ServerSocket(PORT);
+        m_serverChannel = ServerSocketChannel.open();
+        m_serverChannel.socket().bind( new InetSocketAddress(PORT) );
+
+        m_channel = SocketChannel.open();
+        m_channel.configureBlocking( false );
+        m_channel.connect(  new InetSocketAddress(InetAddress.getLocalHost(), PORT) );
+
+        m_clientChannel = m_serverChannel.accept();
     }
 
     public void tearDown() throws Exception
     {
         super.tearDown();
 
-        m_socket.close();
+        m_serverChannel.close();
+        m_channel.close();
+        m_clientChannel.close();
     }
 
     public void testCreateRouter()
@@ -68,7 +83,7 @@
 
         protocolRouter.addPipe("6666", port6666);
 
-        final NetworkEvent event6666 = new NetworkEvent(m_socket);
+        final NetworkEvent event6666 = new NetworkEvent(m_clientChannel);
 
         protocolRouter.handleEvent( event6666 );
 
@@ -96,7 +111,7 @@
 
         protocolRouter.addPipe("80", port80);
 
-        final NetworkEvent event6666 = new NetworkEvent(m_socket);
+        final NetworkEvent event6666 = new NetworkEvent(m_clientChannel);
 
         protocolRouter.handleEvent( event6666 );
 
@@ -116,7 +131,7 @@
 
         protocolRouter.addPipe("6666", port6666);
 
-        final NetworkEvent event6666 = new NetworkEvent(m_socket);
+        final NetworkEvent event6666 = new NetworkEvent(m_clientChannel);
         final CyclicBarrier barrier = new CyclicBarrier(2);
 
         final Thread runner = new Thread(new Runnable() {

Mime
View raw message