directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r350169 [11/16] - in /directory/network: branches/chain_refactor/src/java/org/apache/mina/common/ trunk/src/examples/org/apache/mina/examples/echoserver/ trunk/src/examples/org/apache/mina/examples/httpserver/ trunk/src/examples/org/apache/...
Date Thu, 01 Dec 2005 05:19:07 GMT
Modified: directory/network/trunk/src/java/org/apache/mina/registry/Service.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/registry/Service.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/registry/Service.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/registry/Service.java Wed Nov 30 21:17:41 2005
@@ -1,138 +1,138 @@
-/*
- *   @(#) $Id$
- *
- *   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.mina.registry;
-
-import java.io.Serializable;
-import java.net.InetSocketAddress;
-import java.net.SocketAddress;
-
-import org.apache.mina.common.TransportType;
-import org.apache.mina.transport.vmpipe.VmPipeAddress;
-
-/**
- * Represents a service that is registered to {@link ServiceRegistry}.
- * 
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public class Service implements Serializable, Cloneable
-{
-    private static final long serialVersionUID = 3258407344110383155L;
-
-    private final String name;
-
-    private final TransportType transportType;
-
-    private final SocketAddress address;
-
-    /**
-     * Creates a new instance with the specified protocol name, transport type,
-     * and port number to be bound.
-     */
-    public Service( String name, TransportType transportType, int port )
-    {
-        this( name, transportType, new InetSocketAddress( port ) );
-    }
-
-    /**
-     * Creates a new instance with the specified protocol name, transport type,
-     * and socket address to be bound.
-     */
-    public Service( String name, TransportType transportType, SocketAddress address )
-    {
-        if( name == null )
-            throw new NullPointerException( "name" );
-        if( transportType == null )
-            throw new NullPointerException( "transportType" );
-        if( address == null )
-            throw new NullPointerException( "address" );
-
-        if( transportType == TransportType.VM_PIPE &&
-            !( address instanceof VmPipeAddress ) )
-        {
-            throw new IllegalArgumentException(
-                    "VM_PIPE transport type accepts only VmPipeAddress: " + address.getClass() );
-        }
-
-        this.name = name;
-        this.transportType = transportType;
-        this.address = address;
-    }
-
-    /**
-     * Returns the name of this service (protocol).
-     */
-    public String getName()
-    {
-        return name;
-    }
-
-    /**
-     * Returns the transport type this service uses.
-     */
-    public TransportType getTransportType()
-    {
-        return transportType;
-    }
-
-    /**
-     * Returns the socket address this service is bound on.
-     */
-    public SocketAddress getAddress()
-    {
-        return address;
-    }
-
-    public int hashCode()
-    {
-        return ( ( name.hashCode() * 37 ) ^ transportType.hashCode() * 37 )
-                ^ address.hashCode();
-    }
-
-    public boolean equals( Object o )
-    {
-        if( o == null )
-            return false;
-        if( this == o )
-            return true;
-        if( !( o instanceof Service ) )
-            return false;
-
-        Service that = ( Service ) o;
-        return this.name.equals( that.name )
-                && this.transportType == that.transportType
-                && this.address.equals( that.address );
-    }
-
-    public Object clone()
-    {
-        try
-        {
-            return super.clone();
-        }
-        catch( CloneNotSupportedException e )
-        {
-            throw new InternalError();
-        }
-    }
-    
-    public String toString() {
-        return "(" + transportType + ", " + name + ", " + address + ')';
-    }
-}
+/*
+ *   @(#) $Id$
+ *
+ *   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.mina.registry;
+
+import java.io.Serializable;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+
+import org.apache.mina.common.TransportType;
+import org.apache.mina.transport.vmpipe.VmPipeAddress;
+
+/**
+ * Represents a service that is registered to {@link ServiceRegistry}.
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class Service implements Serializable, Cloneable
+{
+    private static final long serialVersionUID = 3258407344110383155L;
+
+    private final String name;
+
+    private final TransportType transportType;
+
+    private final SocketAddress address;
+
+    /**
+     * Creates a new instance with the specified protocol name, transport type,
+     * and port number to be bound.
+     */
+    public Service( String name, TransportType transportType, int port )
+    {
+        this( name, transportType, new InetSocketAddress( port ) );
+    }
+
+    /**
+     * Creates a new instance with the specified protocol name, transport type,
+     * and socket address to be bound.
+     */
+    public Service( String name, TransportType transportType, SocketAddress address )
+    {
+        if( name == null )
+            throw new NullPointerException( "name" );
+        if( transportType == null )
+            throw new NullPointerException( "transportType" );
+        if( address == null )
+            throw new NullPointerException( "address" );
+
+        if( transportType == TransportType.VM_PIPE &&
+            !( address instanceof VmPipeAddress ) )
+        {
+            throw new IllegalArgumentException(
+                    "VM_PIPE transport type accepts only VmPipeAddress: " + address.getClass() );
+        }
+
+        this.name = name;
+        this.transportType = transportType;
+        this.address = address;
+    }
+
+    /**
+     * Returns the name of this service (protocol).
+     */
+    public String getName()
+    {
+        return name;
+    }
+
+    /**
+     * Returns the transport type this service uses.
+     */
+    public TransportType getTransportType()
+    {
+        return transportType;
+    }
+
+    /**
+     * Returns the socket address this service is bound on.
+     */
+    public SocketAddress getAddress()
+    {
+        return address;
+    }
+
+    public int hashCode()
+    {
+        return ( ( name.hashCode() * 37 ) ^ transportType.hashCode() * 37 )
+                ^ address.hashCode();
+    }
+
+    public boolean equals( Object o )
+    {
+        if( o == null )
+            return false;
+        if( this == o )
+            return true;
+        if( !( o instanceof Service ) )
+            return false;
+
+        Service that = ( Service ) o;
+        return this.name.equals( that.name )
+                && this.transportType == that.transportType
+                && this.address.equals( that.address );
+    }
+
+    public Object clone()
+    {
+        try
+        {
+            return super.clone();
+        }
+        catch( CloneNotSupportedException e )
+        {
+            throw new InternalError();
+        }
+    }
+    
+    public String toString() {
+        return "(" + transportType + ", " + name + ", " + address + ')';
+    }
+}

Modified: directory/network/trunk/src/java/org/apache/mina/registry/ServiceRegistry.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/registry/ServiceRegistry.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/registry/ServiceRegistry.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/registry/ServiceRegistry.java Wed Nov 30 21:17:41 2005
@@ -1,78 +1,84 @@
-/*
- *   @(#) $Id$
- *
- *   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.mina.registry;
-
-import java.io.IOException;
-import java.util.Set;
-
-import org.apache.mina.common.IoAcceptor;
-import org.apache.mina.common.IoHandler;
-import org.apache.mina.common.TransportType;
-
-/**
- * Interface for the internet service registry. The registry is used by MINA
- * to associate services with ports and transport protocols.
- * 
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public interface ServiceRegistry
-{
-    /**
-     * Binds the specified I/O handler to the specified service.
-     */
-    void bind( Service service, IoHandler ioHandler ) throws IOException;
-
-    /**
-     * Unbinds the specified service (and its aggregated I/O handler or
-     * protocol provider). 
-     */
-    void unbind( Service service );
-    
-    /**
-     * Unbinds all services (and their aggregated I/O handlers or
-     * protocol providers). 
-     */
-    void unbindAll();
-
-    /**
-     * Returns {@link Set} of all services bound in this registry.
-     */
-    Set getAllServices();
-    
-    /**
-     * Returns {@link Set} of services bound in this registry with the
-     * specified service(or protocol) name.
-     */
-    Set getServices(String name);
-
-    /**
-     * Returns {@link Set} of services bound in this registry with the
-     * specified transport type.
-     */
-    Set getServices(TransportType transportType);
-    
-    /**
-     * Returns {@link Set} of services bound in this registry with the
-     * specified port number.
-     */
-    Set getServices(int port);
-
-    IoAcceptor getAcceptor( TransportType transportType );
-}
+/*
+ *   @(#) $Id$
+ *
+ *   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.mina.registry;
+
+import java.io.IOException;
+import java.util.Set;
+
+import org.apache.mina.common.IoAcceptor;
+import org.apache.mina.common.IoFilterChainBuilder;
+import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.TransportType;
+
+/**
+ * Interface for the internet service registry. The registry is used by MINA
+ * to associate services with ports and transport protocols.
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface ServiceRegistry
+{
+    /**
+     * Binds the specified I/O handler to the specified service.
+     */
+    void bind( Service service, IoHandler ioHandler ) throws IOException;
+
+    /**
+     * Binds the specified I/O handler to the specified service.
+     */
+    void bind( Service service, IoHandler ioHandler, IoFilterChainBuilder filterChainBuilder ) throws IOException;
+
+    /**
+     * Unbinds the specified service (and its aggregated I/O handler or
+     * protocol provider). 
+     */
+    void unbind( Service service );
+    
+    /**
+     * Unbinds all services (and their aggregated I/O handlers or
+     * protocol providers). 
+     */
+    void unbindAll();
+
+    /**
+     * Returns {@link Set} of all services bound in this registry.
+     */
+    Set getAllServices();
+    
+    /**
+     * Returns {@link Set} of services bound in this registry with the
+     * specified service(or protocol) name.
+     */
+    Set getServices(String name);
+
+    /**
+     * Returns {@link Set} of services bound in this registry with the
+     * specified transport type.
+     */
+    Set getServices(TransportType transportType);
+    
+    /**
+     * Returns {@link Set} of services bound in this registry with the
+     * specified port number.
+     */
+    Set getServices(int port);
+
+    IoAcceptor getAcceptor( TransportType transportType );
+}

Modified: directory/network/trunk/src/java/org/apache/mina/registry/SimpleServiceRegistry.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/registry/SimpleServiceRegistry.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/registry/SimpleServiceRegistry.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/registry/SimpleServiceRegistry.java Wed Nov 30 21:17:41 2005
@@ -11,6 +11,8 @@
 import java.util.Set;
 
 import org.apache.mina.common.IoAcceptor;
+import org.apache.mina.common.IoFilterChain;
+import org.apache.mina.common.IoFilterChainBuilder;
 import org.apache.mina.common.IoHandler;
 import org.apache.mina.common.TransportType;
 import org.apache.mina.filter.ThreadPoolFilter;
@@ -44,11 +46,21 @@
     {
     }
 
-    public void bind( Service service, IoHandler ioHandler ) throws IOException
+    public void bind( Service service, IoHandler handler ) throws IOException
+    {
+        bind( service, handler, null );
+    }
+    
+    public void bind( Service service, IoHandler handler, IoFilterChainBuilder filterChainBuilder ) throws IOException
     {
         IoAcceptor acceptor = findAcceptor( service.getTransportType() );
-        acceptor.bind( service.getAddress(), ioHandler );
-        startThreadPools();
+        if( filterChainBuilder == null )
+        {
+            filterChainBuilder = IoFilterChainBuilder.NOOP;
+        }
+        acceptor.bind(
+                service.getAddress(), handler,
+                new IoFilterChainBuilderWrapper( filterChainBuilder ) );
         services.add( service );
     }
 
@@ -66,7 +78,6 @@
         }
         
         services.remove( service );
-        stopThreadPools();
     }
     
     public synchronized void unbindAll()
@@ -160,42 +171,26 @@
             return vmPipeAcceptor;
 
     }
-
-    private void startThreadPools()
-    {
-        // Start thread pools only when the first service is bound.
-        // If any services are bound, it means that thread pools are started
-        // already.
-        if( !services.isEmpty() )
-            return;
-
-        try
-        {
-            socketIoAcceptor.getFilterChain().addFirst( "threadPool", threadPoolFilter );
-            datagramIoAcceptor.getFilterChain().addFirst( "threadPool", threadPoolFilter );
-            vmPipeAcceptor.getFilterChain().addFirst( "threadPool", threadPoolFilter );
-        }
-        catch( Exception e )
-        {
-            throw ( InternalError ) new InternalError( "Unexpected exception." ).initCause( e );
-        }
-    }
-
-    private void stopThreadPools()
+    
+    private class IoFilterChainBuilderWrapper implements IoFilterChainBuilder
     {
-        // Stop thread pools only when all services are unbound.
-        if( !services.isEmpty() )
-            return;
-
-        try
+        private final IoFilterChainBuilder originalBuilder;
+        
+        private IoFilterChainBuilderWrapper( IoFilterChainBuilder originalBuilder )
         {
-            socketIoAcceptor.getFilterChain().remove( "threadPool" );
-            datagramIoAcceptor.getFilterChain().remove( "threadPool" );
-            vmPipeAcceptor.getFilterChain().remove( "threadPool" );
+            this.originalBuilder = originalBuilder;
         }
-        catch( Exception e )
+        
+        public void buildFilterChain( IoFilterChain chain ) throws Exception
         {
-            throw ( InternalError ) new InternalError( "Unexpected exception." ).initCause( e );
+            try
+            {
+                originalBuilder.buildFilterChain( chain );
+            }
+            finally
+            {
+                chain.addFirst( "threadPool", threadPoolFilter );
+            }
         }
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java Wed Nov 30 21:17:41 2005
@@ -1,40 +1,40 @@
-/*
- *   @(#) $Id$
- *
- *   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.mina.transport.socket.nio;
-
-import org.apache.mina.common.IoAcceptor;
-import org.apache.mina.common.support.DelegatedIoAcceptor;
-import org.apache.mina.transport.socket.nio.support.DatagramAcceptorDelegate;
-
-/**
- * {@link IoAcceptor} for datagram transport (UDP/IP).
- * 
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public class DatagramAcceptor extends DelegatedIoAcceptor
-{
-    /**
-     * Creates a new instance.
-     */
-    public DatagramAcceptor()
-    {
-        super( new DatagramAcceptorDelegate() );
-    }
-}
+/*
+ *   @(#) $Id$
+ *
+ *   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.mina.transport.socket.nio;
+
+import org.apache.mina.common.IoAcceptor;
+import org.apache.mina.common.support.DelegatedIoAcceptor;
+import org.apache.mina.transport.socket.nio.support.DatagramAcceptorDelegate;
+
+/**
+ * {@link IoAcceptor} for datagram transport (UDP/IP).
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class DatagramAcceptor extends DelegatedIoAcceptor
+{
+    /**
+     * Creates a new instance.
+     */
+    public DatagramAcceptor()
+    {
+        init( new DatagramAcceptorDelegate( this ) );
+    }
+}

Modified: directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramConnector.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramConnector.java Wed Nov 30 21:17:41 2005
@@ -1,40 +1,40 @@
-/*
- *   @(#) $Id$
- *
- *   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.mina.transport.socket.nio;
-
-import org.apache.mina.common.IoConnector;
-import org.apache.mina.common.support.DelegatedIoConnector;
-import org.apache.mina.transport.socket.nio.support.DatagramConnectorDelegate;
-
-/**
- * {@link IoConnector} for datagram transport (UDP/IP).
- * 
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public class DatagramConnector extends DelegatedIoConnector
-{
-    /**
-     * Creates a new instance.
-     */
-    public DatagramConnector()
-    {
-        super( new DatagramConnectorDelegate() );
-    }
-}
+/*
+ *   @(#) $Id$
+ *
+ *   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.mina.transport.socket.nio;
+
+import org.apache.mina.common.IoConnector;
+import org.apache.mina.common.support.DelegatedIoConnector;
+import org.apache.mina.transport.socket.nio.support.DatagramConnectorDelegate;
+
+/**
+ * {@link IoConnector} for datagram transport (UDP/IP).
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class DatagramConnector extends DelegatedIoConnector
+{
+    /**
+     * Creates a new instance.
+     */
+    public DatagramConnector()
+    {
+        init( new DatagramConnectorDelegate( this ) );
+    }
+}

Modified: directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramSession.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramSession.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramSession.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/DatagramSession.java Wed Nov 30 21:17:41 2005
@@ -1,37 +1,37 @@
-/*
- *   @(#) $Id$
- *
- *   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.mina.transport.socket.nio;
-
-import java.net.SocketException;
-
-import org.apache.mina.common.IoSession;
-
-/**
- * An {@link IoSession} for datagram transport (UDP/IP).
- * 
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public interface DatagramSession extends IoSession
-{
-    boolean getReuseAddress() throws SocketException;
-    void setReuseAddress( boolean on ) throws SocketException;
-    int getTrafficClass() throws SocketException;
-    void setTrafficClass( int tc ) throws SocketException;
+/*
+ *   @(#) $Id$
+ *
+ *   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.mina.transport.socket.nio;
+
+import java.net.SocketException;
+
+import org.apache.mina.common.IoSession;
+
+/**
+ * An {@link IoSession} for datagram transport (UDP/IP).
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface DatagramSession extends IoSession
+{
+    boolean getReuseAddress() throws SocketException;
+    void setReuseAddress( boolean on ) throws SocketException;
+    int getTrafficClass() throws SocketException;
+    void setTrafficClass( int tc ) throws SocketException;
 }

Modified: directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java Wed Nov 30 21:17:41 2005
@@ -1,83 +1,83 @@
-/*
- *   @(#) $Id$
- *
- *   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.mina.transport.socket.nio;
-
-import org.apache.mina.common.IoAcceptor;
-import org.apache.mina.common.support.DelegatedIoAcceptor;
-import org.apache.mina.transport.socket.nio.support.SocketAcceptorDelegate;
-
-/**
- * {@link IoAcceptor} for socket transport (TCP/IP).
- * 
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public class SocketAcceptor extends DelegatedIoAcceptor implements SocketSessionManager
-{
-    /**
-     * Creates a new instance.
-     */
-    public SocketAcceptor()
-    {
-        super( new SocketAcceptorDelegate() );
-    }
-
-    public int getReceiveBufferSize()
-    {
-        return ( ( SocketAcceptorDelegate ) delegate ).getReceiveBufferSize();
-    }
-
-    /**
-     * @param receiveBufferSize <tt>-1</tt> to use the default value.
-     */
-    public void setReceiveBufferSize( int receiveBufferSize )
-    {
-        ( ( SocketAcceptorDelegate ) delegate ).setReceiveBufferSize( receiveBufferSize );
-    }
-
-    public boolean isReuseAddress()
-    {
-        return ( ( SocketAcceptorDelegate ) delegate ).isReuseAddress();
-    }
-
-    public void setReuseAddress( boolean reuseAddress )
-    {
-        ( ( SocketAcceptorDelegate ) delegate ).setReuseAddress( reuseAddress );
-    }
-
-    public int getBacklog()
-    {
-        return ( ( SocketAcceptorDelegate ) delegate ).getBacklog();
-    }
-
-    public void setBacklog( int backlog )
-    {
-        ( ( SocketAcceptorDelegate ) delegate ).setBacklog( backlog );
-    }
-
-    public int getProcessors()
-    {
-        return ( ( SocketAcceptorDelegate ) delegate ).getProcessors();
-    }
-
-    public void setProcessors( int nProcessor )
-    {
-        ( ( SocketAcceptorDelegate ) delegate ).setProcessors( nProcessor );
-    }
-}
+/*
+ *   @(#) $Id$
+ *
+ *   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.mina.transport.socket.nio;
+
+import org.apache.mina.common.IoAcceptor;
+import org.apache.mina.common.support.DelegatedIoAcceptor;
+import org.apache.mina.transport.socket.nio.support.SocketAcceptorDelegate;
+
+/**
+ * {@link IoAcceptor} for socket transport (TCP/IP).
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class SocketAcceptor extends DelegatedIoAcceptor implements SocketSessionManager
+{
+    /**
+     * Creates a new instance.
+     */
+    public SocketAcceptor()
+    {
+        init( new SocketAcceptorDelegate( this ) );
+    }
+
+    public int getReceiveBufferSize()
+    {
+        return ( ( SocketAcceptorDelegate ) delegate ).getReceiveBufferSize();
+    }
+
+    /**
+     * @param receiveBufferSize <tt>-1</tt> to use the default value.
+     */
+    public void setReceiveBufferSize( int receiveBufferSize )
+    {
+        ( ( SocketAcceptorDelegate ) delegate ).setReceiveBufferSize( receiveBufferSize );
+    }
+
+    public boolean isReuseAddress()
+    {
+        return ( ( SocketAcceptorDelegate ) delegate ).isReuseAddress();
+    }
+
+    public void setReuseAddress( boolean reuseAddress )
+    {
+        ( ( SocketAcceptorDelegate ) delegate ).setReuseAddress( reuseAddress );
+    }
+
+    public int getBacklog()
+    {
+        return ( ( SocketAcceptorDelegate ) delegate ).getBacklog();
+    }
+
+    public void setBacklog( int backlog )
+    {
+        ( ( SocketAcceptorDelegate ) delegate ).setBacklog( backlog );
+    }
+
+    public int getProcessors()
+    {
+        return ( ( SocketAcceptorDelegate ) delegate ).getProcessors();
+    }
+
+    public void setProcessors( int nProcessor )
+    {
+        ( ( SocketAcceptorDelegate ) delegate ).setProcessors( nProcessor );
+    }
+}

Modified: directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketConnector.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketConnector.java Wed Nov 30 21:17:41 2005
@@ -1,51 +1,51 @@
-/*
- *   @(#) $Id$
- *
- *   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.mina.transport.socket.nio;
-
-import org.apache.mina.common.IoConnector;
-import org.apache.mina.common.support.DelegatedIoConnector;
-import org.apache.mina.transport.socket.nio.support.SocketConnectorDelegate;
-
-/**
- * {@link IoConnector} for socket transport (TCP/IP).
- * 
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public class SocketConnector extends DelegatedIoConnector implements SocketSessionManager
-{
-    /**
-     * Creates a new instance.
-     */
-    public SocketConnector()
-    {
-        super( new SocketConnectorDelegate() );
-    }
-
-
-    public int getProcessors()
-    {
-        return ( ( SocketConnectorDelegate ) delegate ).getProcessors();
-    }
-
-    public void setProcessors( int nProcessor )
-    {
-        ( ( SocketConnectorDelegate ) delegate ).setProcessors( nProcessor );
-    }
+/*
+ *   @(#) $Id$
+ *
+ *   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.mina.transport.socket.nio;
+
+import org.apache.mina.common.IoConnector;
+import org.apache.mina.common.support.DelegatedIoConnector;
+import org.apache.mina.transport.socket.nio.support.SocketConnectorDelegate;
+
+/**
+ * {@link IoConnector} for socket transport (TCP/IP).
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class SocketConnector extends DelegatedIoConnector implements SocketSessionManager
+{
+    /**
+     * Creates a new instance.
+     */
+    public SocketConnector()
+    {
+        init( new SocketConnectorDelegate( this ) );
+    }
+
+
+    public int getProcessors()
+    {
+        return ( ( SocketConnectorDelegate ) delegate ).getProcessors();
+    }
+
+    public void setProcessors( int nProcessor )
+    {
+        ( ( SocketConnectorDelegate ) delegate ).setProcessors( nProcessor );
+    }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketSession.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketSession.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketSession.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/SocketSession.java Wed Nov 30 21:17:41 2005
@@ -1,51 +1,51 @@
-/*
- *   @(#) $Id$
- *
- *   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.mina.transport.socket.nio;
-
-import java.net.SocketException;
-
-import org.apache.mina.common.IoSession;
-
-/**
- * An {@link IoSession} for socket transport (TCP/IP).
- * 
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public interface SocketSession extends IoSession
-{
-    boolean getKeepAlive() throws SocketException;
-    void setKeepAlive( boolean on ) throws SocketException;
-    boolean getOOBInline() throws SocketException;
-    void setOOBInline( boolean on ) throws SocketException;
-    boolean getReuseAddress() throws SocketException;
-    void setReuseAddress( boolean on ) throws SocketException;
-    int getSoLinger() throws SocketException;
-    void setSoLinger( boolean on, int linger ) throws SocketException;
-    boolean getTcpNoDelay() throws SocketException;
-    void setTcpNoDelay( boolean on ) throws SocketException;
-    int getTrafficClass() throws SocketException;
-    void setTrafficClass( int tc ) throws SocketException;
-    int getSendBufferSize() throws SocketException;
-    void setSendBufferSize( int size ) throws SocketException;
-    int getReceiveBufferSize() throws SocketException;
-    void setReceiveBufferSize( int size ) throws SocketException;
-    int getSessionReceiveBufferSize();
-    void setSessionReceiveBufferSize( int size );
-}
+/*
+ *   @(#) $Id$
+ *
+ *   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.mina.transport.socket.nio;
+
+import java.net.SocketException;
+
+import org.apache.mina.common.IoSession;
+
+/**
+ * An {@link IoSession} for socket transport (TCP/IP).
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface SocketSession extends IoSession
+{
+    boolean getKeepAlive() throws SocketException;
+    void setKeepAlive( boolean on ) throws SocketException;
+    boolean getOOBInline() throws SocketException;
+    void setOOBInline( boolean on ) throws SocketException;
+    boolean getReuseAddress() throws SocketException;
+    void setReuseAddress( boolean on ) throws SocketException;
+    int getSoLinger() throws SocketException;
+    void setSoLinger( boolean on, int linger ) throws SocketException;
+    boolean getTcpNoDelay() throws SocketException;
+    void setTcpNoDelay( boolean on ) throws SocketException;
+    int getTrafficClass() throws SocketException;
+    void setTrafficClass( int tc ) throws SocketException;
+    int getSendBufferSize() throws SocketException;
+    void setSendBufferSize( int size ) throws SocketException;
+    int getReceiveBufferSize() throws SocketException;
+    void setReceiveBufferSize( int size ) throws SocketException;
+    int getSessionReceiveBufferSize();
+    void setSessionReceiveBufferSize( int size );
+}

Modified: directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java Wed Nov 30 21:17:41 2005
@@ -1,617 +1,611 @@
-/*
- *   @(#) $Id$
- *
- *   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.mina.transport.socket.nio.support;
-
-import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.net.SocketAddress;
-import java.nio.channels.DatagramChannel;
-import java.nio.channels.SelectionKey;
-import java.nio.channels.Selector;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.mina.common.ByteBuffer;
-import org.apache.mina.common.IoAcceptor;
-import org.apache.mina.common.IoFilterChain;
-import org.apache.mina.common.IoHandler;
-import org.apache.mina.common.IoSession;
-import org.apache.mina.common.IoFilter.WriteRequest;
-import org.apache.mina.common.support.BaseIoAcceptor;
-import org.apache.mina.common.support.IoSessionManagerFilterChain;
-import org.apache.mina.util.ExceptionUtil;
-import org.apache.mina.util.Queue;
-
-/**
- * {@link IoAcceptor} for datagram transport (UDP/IP).
- * 
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public class DatagramAcceptorDelegate extends BaseIoAcceptor implements IoAcceptor, DatagramSessionManager
-{
-    private static volatile int nextId = 0;
-
-    private final IoSessionManagerFilterChain filters =
-        new DatagramSessionManagerFilterChain( this );
-
-    private final int id = nextId ++ ;
-
-    private Selector selector;
-
-    private final Map channels = new HashMap();
-
-    private final Queue registerQueue = new Queue();
-
-    private final Queue cancelQueue = new Queue();
-
-    private final Queue flushingSessions = new Queue();
-
-    private Worker worker;
-
-    /**
-     * Creates a new instance.
-     */
-    public DatagramAcceptorDelegate()
-    {
-    }
-
-    public void bind( SocketAddress address, IoHandler handler )
-            throws IOException
-    {
-        if( address == null )
-            throw new NullPointerException( "address" );
-        if( handler == null )
-            throw new NullPointerException( "handler" );
-
-        if( !( address instanceof InetSocketAddress ) )
-            throw new IllegalArgumentException( "Unexpected address type: "
-                                                + address.getClass() );
-        if( ( ( InetSocketAddress ) address ).getPort() == 0 )
-            throw new IllegalArgumentException( "Unsupported port number: 0" );
-        
-        RegistrationRequest request = new RegistrationRequest( address, handler );
-        synchronized( this )
-        {
-            synchronized( registerQueue )
-            {
-                registerQueue.push( request );
-            }
-            startupWorker();
-        }
-        selector.wakeup();
-        
-        synchronized( request )
-        {
-            while( !request.done )
-            {
-                try
-                {
-                    request.wait();
-                }
-                catch( InterruptedException e )
-                {
-                }
-            }
-        }
-        
-        if( request.exception != null )
-        {
-            ExceptionUtil.throwException( request.exception );
-        }
-    }
-
-    public void unbind( SocketAddress address )
-    {
-        // FIXME: Make this work properly when disconnectClients is true.
-        if( address == null )
-            throw new NullPointerException( "address" );
-
-        CancellationRequest request = new CancellationRequest( address );
-        synchronized( this )
-        {
-            try
-            {
-                startupWorker();
-            }
-            catch( IOException e )
-            {
-                // IOException is thrown only when Worker thread is not
-                // running and failed to open a selector.  We simply throw
-                // IllegalArgumentException here because we can simply
-                // conclude that nothing is bound to the selector.
-                throw new IllegalArgumentException( "Address not bound: " + address );
-            }
-
-            synchronized( cancelQueue )
-            {
-                cancelQueue.push( request );
-            }
-        }
-        selector.wakeup();
-        
-        synchronized( request )
-        {
-            while( !request.done )
-            {
-                try
-                {
-                    request.wait();
-                }
-                catch( InterruptedException e )
-                {
-                }
-            }
-        }
-        
-        if( request.exception != null )
-        {
-            request.exception.fillInStackTrace();
-            throw request.exception;
-        }
-    }
-    
-    public IoSession newSession( SocketAddress remoteAddress, SocketAddress localAddress )
-    {
-        if( remoteAddress == null )
-        {
-            throw new NullPointerException( "remoteAddress" );
-        }
-        if( localAddress == null )
-        {
-            throw new NullPointerException( "localAddress" );
-        }
-        
-        Selector selector = this.selector;
-        DatagramChannel ch = ( DatagramChannel ) channels.get( localAddress );
-        if( selector == null || ch == null )
-        {
-            throw new IllegalArgumentException( "Unknown localAddress: " + localAddress );
-        }
-            
-        SelectionKey key = ch.keyFor( selector );
-        if( key == null )
-        {
-            throw new IllegalArgumentException( "Unknown lodalAddress: " + localAddress );
-        }
-
-        RegistrationRequest req = ( RegistrationRequest ) key.attachment();
-        DatagramSessionImpl s = new DatagramSessionImpl( filters, ch, req.handler );
-        s.setRemoteAddress( remoteAddress );
-        s.setSelectionKey( key );
-        
-        try
-        {
-            s.getManagerFilterChain().sessionCreated( s );
-        }
-        catch( Throwable t )
-        {
-            exceptionMonitor.exceptionCaught( this, t );
-        }
-        
-        return s;
-    }
-
-    private synchronized void startupWorker() throws IOException
-    {
-        if( worker == null )
-        {
-            selector = Selector.open();
-            worker = new Worker();
-            worker.start();
-        }
-    }
-
-    public void flushSession( DatagramSessionImpl session )
-    {
-        scheduleFlush( session );
-        selector.wakeup();
-    }
-
-    public void closeSession( DatagramSessionImpl session )
-    {
-    }
-
-    private void scheduleFlush( DatagramSessionImpl session )
-    {
-        synchronized( flushingSessions )
-        {
-            flushingSessions.push( session );
-        }
-    }
-
-    private class Worker extends Thread
-    {
-        public Worker()
-        {
-            super( "DatagramAcceptor-" + id );
-        }
-
-        public void run()
-        {
-            for( ;; )
-            {
-                try
-                {
-                    int nKeys = selector.select();
-
-                    registerNew();
-
-                    if( nKeys > 0 )
-                    {
-                        processReadySessions( selector.selectedKeys() );
-                    }
-
-                    flushSessions();
-                    cancelKeys();
-
-                    if( selector.keys().isEmpty() )
-                    {
-                        synchronized( DatagramAcceptorDelegate.this )
-                        {
-                            if( selector.keys().isEmpty() &&
-                                registerQueue.isEmpty() &&
-                                cancelQueue.isEmpty() )
-                            {
-                                worker = null;
-                                try
-                                {
-                                    selector.close();
-                                }
-                                catch( IOException e )
-                                {
-                                    exceptionMonitor.exceptionCaught( DatagramAcceptorDelegate.this, e );
-                                }
-                                finally
-                                {
-                                    selector = null;
-                                }
-                                break;
-                            }
-                        }
-                    }
-                }
-                catch( IOException e )
-                {
-                    exceptionMonitor.exceptionCaught( DatagramAcceptorDelegate.this,
-                            e );
-
-                    try
-                    {
-                        Thread.sleep( 1000 );
-                    }
-                    catch( InterruptedException e1 )
-                    {
-                    }
-                }
-            }
-        }
-    }
-
-    private void processReadySessions( Set keys )
-    {
-        Iterator it = keys.iterator();
-        while( it.hasNext() )
-        {
-            SelectionKey key = ( SelectionKey ) it.next();
-            it.remove();
-
-            DatagramChannel ch = ( DatagramChannel ) key.channel();
-
-            RegistrationRequest req = ( RegistrationRequest ) key.attachment();
-            DatagramSessionImpl session = new DatagramSessionImpl(
-                    filters, ch, req.handler );
-            session.setSelectionKey( key );
-            
-            try
-            {
-                session.getManagerFilterChain().sessionCreated( session );
-
-                if( key.isReadable() )
-                {
-                    readSession( session );
-                }
-
-                if( key.isWritable() )
-                {
-                    scheduleFlush( session );
-                }
-            }
-            catch( Throwable t )
-            {
-                exceptionMonitor.exceptionCaught( this, t );
-            }
-        }
-    }
-
-    private void readSession( DatagramSessionImpl session )
-    {
-
-        ByteBuffer readBuf = ByteBuffer.allocate( 2048 );
-        try
-        {
-            SocketAddress remoteAddress = session.getChannel().receive(
-                    readBuf.buf() );
-            if( remoteAddress != null )
-            {
-                readBuf.flip();
-                session.setRemoteAddress( remoteAddress );
-
-                ByteBuffer newBuf = ByteBuffer.allocate( readBuf.limit() );
-                newBuf.put( readBuf );
-                newBuf.flip();
-
-                session.increaseReadBytes( newBuf.remaining() );
-                filters.messageReceived( session, newBuf );
-            }
-        }
-        catch( IOException e )
-        {
-            filters.exceptionCaught( session, e );
-        }
-        finally
-        {
-            readBuf.release();
-        }
-    }
-
-    private void flushSessions()
-    {
-        if( flushingSessions.size() == 0 )
-            return;
-
-        for( ;; )
-        {
-            DatagramSessionImpl session;
-
-            synchronized( flushingSessions )
-            {
-                session = ( DatagramSessionImpl ) flushingSessions.pop();
-            }
-
-            if( session == null )
-                break;
-
-            try
-            {
-                flush( session );
-            }
-            catch( IOException e )
-            {
-                session.getManagerFilterChain().exceptionCaught( session, e );
-            }
-        }
-    }
-
-    private void flush( DatagramSessionImpl session ) throws IOException
-    {
-        DatagramChannel ch = session.getChannel();
-
-        Queue writeRequestQueue = session.getWriteRequestQueue();
-
-        WriteRequest req;
-        for( ;; )
-        {
-            synchronized( writeRequestQueue )
-            {
-                req = ( WriteRequest ) writeRequestQueue.first();
-            }
-
-            if( req == null )
-                break;
-
-            ByteBuffer buf = ( ByteBuffer ) req.getMessage();
-            if( buf.remaining() == 0 )
-            {
-                // pop and fire event
-                synchronized( writeRequestQueue )
-                {
-                    writeRequestQueue.pop();
-                }
-
-                req.getFuture().setWritten( true );
-                session.increaseWrittenWriteRequests();
-                session.getManagerFilterChain().messageSent( session, buf );
-                continue;
-            }
-
-            SelectionKey key = session.getSelectionKey();
-            if( key == null )
-            {
-                scheduleFlush( session );
-                break;
-            }
-            if( !key.isValid() )
-            {
-                continue;
-            }
-
-            int pos = buf.position();
-            int writtenBytes = ch
-                    .send( buf.buf(), session.getRemoteAddress() );
-
-            if( writtenBytes == 0 )
-            {
-                // Kernel buffer is full
-                key.interestOps( key.interestOps() | SelectionKey.OP_WRITE );
-            }
-            else if( writtenBytes > 0 )
-            {
-                key.interestOps( key.interestOps()
-                                 & ( ~SelectionKey.OP_WRITE ) );
-
-                // pop and fire event
-                synchronized( writeRequestQueue )
-                {
-                    writeRequestQueue.pop();
-                }
-
-                session.increaseWrittenBytes( writtenBytes );
-                req.getFuture().setWritten( true );
-                session.increaseWrittenWriteRequests();
-                session.getManagerFilterChain().messageSent( session, buf.position( pos ) );
-            }
-        }
-    }
-
-    private void registerNew()
-    {
-        if( registerQueue.isEmpty() )
-            return;
-
-        for( ;; )
-        {
-            RegistrationRequest req;
-            synchronized( registerQueue )
-            {
-                req = ( RegistrationRequest ) registerQueue.pop();
-            }
-
-            if( req == null )
-                break;
-
-            DatagramChannel ch = null;
-            try
-            {
-                ch = DatagramChannel.open();
-                ch.configureBlocking( false );
-                ch.socket().bind( req.address );
-                ch.register( selector, SelectionKey.OP_READ, req );
-                channels.put( req.address, ch );
-            }
-            catch( Throwable t )
-            {
-                req.exception = t;
-            }
-            finally
-            {
-                synchronized( req )
-                {
-                    req.done = true;
-                    req.notify();
-                }
-
-                if( ch != null && req.exception != null )
-                {
-                    try
-                    {
-                        ch.close();
-                    }
-                    catch( Throwable e )
-                    {
-                        exceptionMonitor.exceptionCaught( this, e );
-                    }
-                }
-            }
-        }
-    }
-
-    private void cancelKeys()
-    {
-        if( cancelQueue.isEmpty() )
-            return;
-
-        for( ;; )
-        {
-            CancellationRequest request;
-            synchronized( cancelQueue )
-            {
-                request = ( CancellationRequest ) cancelQueue.pop();
-            }
-            
-            if( request == null )
-            {
-                break;
-            }
-
-            DatagramChannel ch = ( DatagramChannel ) channels.remove( request.address );
-            // close the channel
-            try
-            {
-                if( ch == null )
-                {
-                    request.exception = new IllegalArgumentException(
-                            "Address not bound: " + request.address );
-                }
-                else
-                {
-                    SelectionKey key = ch.keyFor( selector );
-                    key.cancel();
-                    selector.wakeup(); // wake up again to trigger thread death
-                    ch.close();
-                }
-            }
-            catch( Throwable t )
-            {
-                exceptionMonitor.exceptionCaught( this, t );
-            }
-            finally
-            {
-                synchronized( request )
-                {
-                    request.done = true;
-                    request.notify();
-                }
-            }
-        }
-    }
-    
-    public void updateTrafficMask( DatagramSessionImpl session )
-    {
-        // There's no point in changing the traffic mask for sessions originating
-        // from this acceptor since new sessions are created every time data is
-        // received.
-    }
-
-    public IoFilterChain getFilterChain()
-    {
-        return filters;
-    }
-
-    private static class RegistrationRequest
-    {
-        private final SocketAddress address;
-        
-        private final IoHandler handler;
-        
-        private Throwable exception; 
-        
-        private boolean done;
-        
-        private RegistrationRequest( SocketAddress address, IoHandler handler )
-        {
-            this.address = address;
-            this.handler = handler;
-        }
-    }
-
-    private static class CancellationRequest
-    {
-        private final SocketAddress address;
-        private boolean done;
-        private RuntimeException exception;
-        
-        private CancellationRequest( SocketAddress address )
-        {
-            this.address = address;
-        }
-    }
-}
+/*
+ *   @(#) $Id$
+ *
+ *   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.mina.transport.socket.nio.support;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.nio.channels.DatagramChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.IoAcceptor;
+import org.apache.mina.common.IoFilterChainBuilder;
+import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.IoFilter.WriteRequest;
+import org.apache.mina.common.support.BaseIoAcceptor;
+import org.apache.mina.util.ExceptionUtil;
+import org.apache.mina.util.Queue;
+
+/**
+ * {@link IoAcceptor} for datagram transport (UDP/IP).
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class DatagramAcceptorDelegate extends BaseIoAcceptor implements IoAcceptor, DatagramSessionManager
+{
+    private static volatile int nextId = 0;
+
+    private final IoAcceptor wrapper;
+    private final int id = nextId ++ ;
+    private Selector selector;
+    private final Map channels = new HashMap();
+    private final Queue registerQueue = new Queue();
+    private final Queue cancelQueue = new Queue();
+    private final Queue flushingSessions = new Queue();
+    private Worker worker;
+
+    /**
+     * Creates a new instance.
+     */
+    public DatagramAcceptorDelegate( IoAcceptor wrapper )
+    {
+        this.wrapper = wrapper;
+    }
+
+    public void bind( SocketAddress address, IoHandler handler, IoFilterChainBuilder filterChainBuilder )
+            throws IOException
+    {
+        if( address == null )
+            throw new NullPointerException( "address" );
+        if( handler == null )
+            throw new NullPointerException( "handler" );
+
+        if( !( address instanceof InetSocketAddress ) )
+            throw new IllegalArgumentException( "Unexpected address type: "
+                                                + address.getClass() );
+        if( ( ( InetSocketAddress ) address ).getPort() == 0 )
+            throw new IllegalArgumentException( "Unsupported port number: 0" );
+        
+        if( filterChainBuilder == null )
+        {
+            filterChainBuilder = IoFilterChainBuilder.NOOP;
+        }
+
+        RegistrationRequest request = new RegistrationRequest( address, handler, filterChainBuilder );
+        synchronized( this )
+        {
+            synchronized( registerQueue )
+            {
+                registerQueue.push( request );
+            }
+            startupWorker();
+        }
+        selector.wakeup();
+        
+        synchronized( request )
+        {
+            while( !request.done )
+            {
+                try
+                {
+                    request.wait();
+                }
+                catch( InterruptedException e )
+                {
+                }
+            }
+        }
+        
+        if( request.exception != null )
+        {
+            ExceptionUtil.throwException( request.exception );
+        }
+    }
+
+    public void unbind( SocketAddress address )
+    {
+        // FIXME: Make this work properly when disconnectClients is true.
+        if( address == null )
+            throw new NullPointerException( "address" );
+
+        CancellationRequest request = new CancellationRequest( address );
+        synchronized( this )
+        {
+            try
+            {
+                startupWorker();
+            }
+            catch( IOException e )
+            {
+                // IOException is thrown only when Worker thread is not
+                // running and failed to open a selector.  We simply throw
+                // IllegalArgumentException here because we can simply
+                // conclude that nothing is bound to the selector.
+                throw new IllegalArgumentException( "Address not bound: " + address );
+            }
+
+            synchronized( cancelQueue )
+            {
+                cancelQueue.push( request );
+            }
+        }
+        selector.wakeup();
+        
+        synchronized( request )
+        {
+            while( !request.done )
+            {
+                try
+                {
+                    request.wait();
+                }
+                catch( InterruptedException e )
+                {
+                }
+            }
+        }
+        
+        if( request.exception != null )
+        {
+            request.exception.fillInStackTrace();
+            throw request.exception;
+        }
+    }
+    
+    public IoSession newSession( SocketAddress remoteAddress, SocketAddress localAddress )
+    {
+        if( remoteAddress == null )
+        {
+            throw new NullPointerException( "remoteAddress" );
+        }
+        if( localAddress == null )
+        {
+            throw new NullPointerException( "localAddress" );
+        }
+        
+        Selector selector = this.selector;
+        DatagramChannel ch = ( DatagramChannel ) channels.get( localAddress );
+        if( selector == null || ch == null )
+        {
+            throw new IllegalArgumentException( "Unknown localAddress: " + localAddress );
+        }
+            
+        SelectionKey key = ch.keyFor( selector );
+        if( key == null )
+        {
+            throw new IllegalArgumentException( "Unknown localAddress: " + localAddress );
+        }
+
+        RegistrationRequest req = ( RegistrationRequest ) key.attachment();
+        DatagramSessionImpl s = new DatagramSessionImpl( wrapper, this, ch, req.handler );
+        s.setRemoteAddress( remoteAddress );
+        s.setSelectionKey( key );
+        
+        try
+        {
+            this.filterChainBuilder.buildFilterChain( s.getFilterChain() );
+            req.filterChainBuilder.buildFilterChain( s.getFilterChain() );
+            ( ( DatagramFilterChain ) s.getFilterChain() ).sessionCreated( s );
+        }
+        catch( Throwable t )
+        {
+            exceptionMonitor.exceptionCaught( this, t );
+        }
+        
+        return s;
+    }
+
+    private synchronized void startupWorker() throws IOException
+    {
+        if( worker == null )
+        {
+            selector = Selector.open();
+            worker = new Worker();
+            worker.start();
+        }
+    }
+
+    public void flushSession( DatagramSessionImpl session )
+    {
+        scheduleFlush( session );
+        selector.wakeup();
+    }
+
+    public void closeSession( DatagramSessionImpl session )
+    {
+    }
+
+    private void scheduleFlush( DatagramSessionImpl session )
+    {
+        synchronized( flushingSessions )
+        {
+            flushingSessions.push( session );
+        }
+    }
+
+    private class Worker extends Thread
+    {
+        public Worker()
+        {
+            super( "DatagramAcceptor-" + id );
+        }
+
+        public void run()
+        {
+            for( ;; )
+            {
+                try
+                {
+                    int nKeys = selector.select();
+
+                    registerNew();
+
+                    if( nKeys > 0 )
+                    {
+                        processReadySessions( selector.selectedKeys() );
+                    }
+
+                    flushSessions();
+                    cancelKeys();
+
+                    if( selector.keys().isEmpty() )
+                    {
+                        synchronized( DatagramAcceptorDelegate.this )
+                        {
+                            if( selector.keys().isEmpty() &&
+                                registerQueue.isEmpty() &&
+                                cancelQueue.isEmpty() )
+                            {
+                                worker = null;
+                                try
+                                {
+                                    selector.close();
+                                }
+                                catch( IOException e )
+                                {
+                                    exceptionMonitor.exceptionCaught( DatagramAcceptorDelegate.this, e );
+                                }
+                                finally
+                                {
+                                    selector = null;
+                                }
+                                break;
+                            }
+                        }
+                    }
+                }
+                catch( IOException e )
+                {
+                    exceptionMonitor.exceptionCaught( DatagramAcceptorDelegate.this,
+                            e );
+
+                    try
+                    {
+                        Thread.sleep( 1000 );
+                    }
+                    catch( InterruptedException e1 )
+                    {
+                    }
+                }
+            }
+        }
+    }
+
+    private void processReadySessions( Set keys )
+    {
+        Iterator it = keys.iterator();
+        while( it.hasNext() )
+        {
+            SelectionKey key = ( SelectionKey ) it.next();
+            it.remove();
+
+            DatagramChannel ch = ( DatagramChannel ) key.channel();
+
+            RegistrationRequest req = ( RegistrationRequest ) key.attachment();
+            DatagramSessionImpl session =
+                new DatagramSessionImpl( wrapper, this, ch, req.handler );
+            session.setSelectionKey( key );
+            
+            try
+            {
+                ( ( DatagramFilterChain ) session.getFilterChain() ).sessionCreated( session );
+
+                if( key.isReadable() )
+                {
+                    readSession( session );
+                }
+
+                if( key.isWritable() )
+                {
+                    scheduleFlush( session );
+                }
+            }
+            catch( Throwable t )
+            {
+                exceptionMonitor.exceptionCaught( this, t );
+            }
+        }
+    }
+
+    private void readSession( DatagramSessionImpl session )
+    {
+
+        ByteBuffer readBuf = ByteBuffer.allocate( 2048 );
+        try
+        {
+            SocketAddress remoteAddress = session.getChannel().receive(
+                    readBuf.buf() );
+            if( remoteAddress != null )
+            {
+                readBuf.flip();
+                session.setRemoteAddress( remoteAddress );
+
+                ByteBuffer newBuf = ByteBuffer.allocate( readBuf.limit() );
+                newBuf.put( readBuf );
+                newBuf.flip();
+
+                session.increaseReadBytes( newBuf.remaining() );
+                ( ( DatagramFilterChain ) session.getFilterChain() ).messageReceived( session, newBuf );
+            }
+        }
+        catch( IOException e )
+        {
+            ( ( DatagramFilterChain ) session.getFilterChain() ).exceptionCaught( session, e );
+        }
+        finally
+        {
+            readBuf.release();
+        }
+    }
+
+    private void flushSessions()
+    {
+        if( flushingSessions.size() == 0 )
+            return;
+
+        for( ;; )
+        {
+            DatagramSessionImpl session;
+
+            synchronized( flushingSessions )
+            {
+                session = ( DatagramSessionImpl ) flushingSessions.pop();
+            }
+
+            if( session == null )
+                break;
+
+            try
+            {
+                flush( session );
+            }
+            catch( IOException e )
+            {
+                ( ( DatagramFilterChain ) session.getFilterChain() ).exceptionCaught( session, e );
+            }
+        }
+    }
+
+    private void flush( DatagramSessionImpl session ) throws IOException
+    {
+        DatagramChannel ch = session.getChannel();
+
+        Queue writeRequestQueue = session.getWriteRequestQueue();
+
+        WriteRequest req;
+        for( ;; )
+        {
+            synchronized( writeRequestQueue )
+            {
+                req = ( WriteRequest ) writeRequestQueue.first();
+            }
+
+            if( req == null )
+                break;
+
+            ByteBuffer buf = ( ByteBuffer ) req.getMessage();
+            if( buf.remaining() == 0 )
+            {
+                // pop and fire event
+                synchronized( writeRequestQueue )
+                {
+                    writeRequestQueue.pop();
+                }
+
+                req.getFuture().setWritten( true );
+                session.increaseWrittenWriteRequests();
+                ( ( DatagramFilterChain ) session.getFilterChain() ).messageSent( session, buf );
+                continue;
+            }
+
+            SelectionKey key = session.getSelectionKey();
+            if( key == null )
+            {
+                scheduleFlush( session );
+                break;
+            }
+            if( !key.isValid() )
+            {
+                continue;
+            }
+
+            int pos = buf.position();
+            int writtenBytes = ch
+                    .send( buf.buf(), session.getRemoteAddress() );
+
+            if( writtenBytes == 0 )
+            {
+                // Kernel buffer is full
+                key.interestOps( key.interestOps() | SelectionKey.OP_WRITE );
+            }
+            else if( writtenBytes > 0 )
+            {
+                key.interestOps( key.interestOps()
+                                 & ( ~SelectionKey.OP_WRITE ) );
+
+                // pop and fire event
+                synchronized( writeRequestQueue )
+                {
+                    writeRequestQueue.pop();
+                }
+
+                session.increaseWrittenBytes( writtenBytes );
+                req.getFuture().setWritten( true );
+                session.increaseWrittenWriteRequests();
+                ( ( DatagramFilterChain ) session.getFilterChain() ).messageSent( session, buf.position( pos ) );
+            }
+        }
+    }
+
+    private void registerNew()
+    {
+        if( registerQueue.isEmpty() )
+            return;
+
+        for( ;; )
+        {
+            RegistrationRequest req;
+            synchronized( registerQueue )
+            {
+                req = ( RegistrationRequest ) registerQueue.pop();
+            }
+
+            if( req == null )
+                break;
+
+            DatagramChannel ch = null;
+            try
+            {
+                ch = DatagramChannel.open();
+                ch.configureBlocking( false );
+                ch.socket().bind( req.address );
+                ch.register( selector, SelectionKey.OP_READ, req );
+                channels.put( req.address, ch );
+            }
+            catch( Throwable t )
+            {
+                req.exception = t;
+            }
+            finally
+            {
+                synchronized( req )
+                {
+                    req.done = true;
+                    req.notify();
+                }
+
+                if( ch != null && req.exception != null )
+                {
+                    try
+                    {
+                        ch.close();
+                    }
+                    catch( Throwable e )
+                    {
+                        exceptionMonitor.exceptionCaught( this, e );
+                    }
+                }
+            }
+        }
+    }
+
+    private void cancelKeys()
+    {
+        if( cancelQueue.isEmpty() )
+            return;
+
+        for( ;; )
+        {
+            CancellationRequest request;
+            synchronized( cancelQueue )
+            {
+                request = ( CancellationRequest ) cancelQueue.pop();
+            }
+            
+            if( request == null )
+            {
+                break;
+            }
+
+            DatagramChannel ch = ( DatagramChannel ) channels.remove( request.address );
+            // close the channel
+            try
+            {
+                if( ch == null )
+                {
+                    request.exception = new IllegalArgumentException(
+                            "Address not bound: " + request.address );
+                }
+                else
+                {
+                    SelectionKey key = ch.keyFor( selector );
+                    key.cancel();
+                    selector.wakeup(); // wake up again to trigger thread death
+                    ch.close();
+                }
+            }
+            catch( Throwable t )
+            {
+                exceptionMonitor.exceptionCaught( this, t );
+            }
+            finally
+            {
+                synchronized( request )
+                {
+                    request.done = true;
+                    request.notify();
+                }
+            }
+        }
+    }
+    
+    public void updateTrafficMask( DatagramSessionImpl session )
+    {
+        // There's no point in changing the traffic mask for sessions originating
+        // from this acceptor since new sessions are created every time data is
+        // received.
+    }
+
+    private static class RegistrationRequest
+    {
+        private final SocketAddress address;
+        private final IoHandler handler;
+        private final IoFilterChainBuilder filterChainBuilder;
+
+        private Throwable exception; 
+        private boolean done;
+        
+        private RegistrationRequest( SocketAddress address, IoHandler handler, IoFilterChainBuilder filterChainBuilder )
+        {
+            this.address = address;
+            this.handler = handler;
+            this.filterChainBuilder = filterChainBuilder;
+        }
+    }
+
+    private static class CancellationRequest
+    {
+        private final SocketAddress address;
+        private boolean done;
+        private RuntimeException exception;
+        
+        private CancellationRequest( SocketAddress address )
+        {
+            this.address = address;
+        }
+    }
+}



Mime
View raw message