directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r371307 - in /directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina: common/support/ transport/socket/nio/
Date Sun, 22 Jan 2006 14:31:04 GMT
Author: trustin
Date: Sun Jan 22 06:30:56 2006
New Revision: 371307

URL: http://svn.apache.org/viewcvs?rev=371307&view=rev
Log:
Improved attribute override mechanism using hash tables

Modified:
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramConnector.java
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramSession.java
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/SocketSession.java

Modified: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java?rev=371307&r1=371306&r2=371307&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java
(original)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java
Sun Jan 22 06:30:56 2006
@@ -20,11 +20,13 @@
 
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.IdentityHashMap;
 import java.util.Map;
 import java.util.Set;
 
 import org.apache.mina.common.CloseFuture;
 import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.IoServiceProvider;
 import org.apache.mina.common.IoSession;
 import org.apache.mina.common.RuntimeIOException;
 import org.apache.mina.common.TrafficMask;
@@ -39,8 +41,38 @@
  */
 public abstract class BaseIoSession implements IoSession
 {
-    private static final IoSessionAttributeOverrider[] OVERRIDER_DEFAULT =
-        new IoSessionAttributeOverrider[ 0 ];
+    private static final Map provider2attributeOverriders = new IdentityHashMap();
+
+    /**
+     * Registers the specified {@link IoSessionAttributeOverrider} to the specified
+     * {@link IoServiceProvider}. 
+     */
+    public static void registerAttributeOverrider( Class providerClass, IoSessionAttributeOverrider
overrider )
+    {
+        Map overriders = ( Map ) provider2attributeOverriders.get( providerClass );
+        if( overriders == null )
+        {
+            overriders = new HashMap();
+            provider2attributeOverriders.put( providerClass, overriders );
+        }
+        
+        overriders.put( overrider.getKey(), overrider );
+    }
+    
+    private static Map getAttributeOverriders( Class providerClass )
+    {
+        return ( Map ) provider2attributeOverriders.get( providerClass );
+    }
+    
+    private static IoSessionAttributeOverrider getAttributeOverrider( Class providerClass,
String key )
+    {
+        Map overriders = ( Map ) provider2attributeOverriders.get( providerClass );
+        if( overriders == null )
+        {
+            return null;
+        }
+        return ( IoSessionAttributeOverrider ) overriders.get( key );
+    }
 
     private final Map attributes = new HashMap();
     private final long creationTime;
@@ -74,8 +106,6 @@
     private long lastIdleTimeForRead;
     private long lastIdleTimeForWrite;
     
-    protected IoSessionAttributeOverrider[] attributeOverriders = OVERRIDER_DEFAULT;
-
     protected BaseIoSession()
     {
         creationTime = lastReadTime = lastWriteTime =
@@ -176,48 +206,44 @@
             return value;
         }
         
-        for( int i = attributeOverriders.length - 1; i >= 0; i -- )
+        IoSessionAttributeOverrider overrider =
+            getAttributeOverrider( getProvider().getClass(), key );
+        if( overrider == null )
         {
-            IoSessionAttributeOverrider overrider = attributeOverriders[ i ];
-            if( overrider.getKey().equals( key ) )
-            {
-                try
-                {
-                    return overrider.getValue( this );
-                }
-                catch( RuntimeException e )
-                {
-                    return e;
-                }
-                catch( Exception e )
-                {
-                    throw new RuntimeIOException( "Failed to get an attribute: " + key, e
);
-                }
-            }
+            return null;
+        }
+        
+        try
+        {
+            return overrider.getValue( this );
+        }
+        catch( RuntimeException e )
+        {
+            return e;
+        }
+        catch( Exception e )
+        {
+            throw new RuntimeIOException( "Failed to get an attribute: " + key, e );
         }
-
-        return null;
     }
 
     public Object setAttribute( String key, Object value )
     {
-        for( int i = attributeOverriders.length - 1; i >= 0; i -- )
+        IoSessionAttributeOverrider overrider =
+            getAttributeOverrider( getProvider().getClass(), key );
+        if( overrider != null )
         {
-            IoSessionAttributeOverrider overrider = attributeOverriders[ i ];
-            if( overrider.getKey().equals( key ) )
+            try
+            {
+                return overrider.setValue( this, value );
+            }
+            catch( RuntimeException e )
             {
-                try
-                {
-                    return overrider.setValue( this, value );
-                }
-                catch( RuntimeException e )
-                {
-                    return e;
-                }
-                catch( Exception e )
-                {
-                    throw new RuntimeIOException( "Failed to set an attribute: " + key, e
);
-                }
+                return e;
+            }
+            catch( Exception e )
+            {
+                throw new RuntimeIOException( "Failed to set an attribute: " + key, e );
             }
         }
 
@@ -247,17 +273,15 @@
 
     public Set getAttributeKeys()
     {
-        Set keys = new HashSet( attributes.size() + attributeOverriders.length );
+        Map overriders = getAttributeOverriders( getProvider().getClass() );
+        Set keys = new HashSet( attributes.size() + overriders.size() );
 
         synchronized( attributes )
         {
             keys.addAll( attributes.keySet() );
         }
         
-        for( int i = attributeOverriders.length - 1; i >= 0; i -- )
-        {
-            keys.add( attributeOverriders[ i ].getKey() );
-        }
+        keys.addAll( overriders.keySet() );
 
         return keys;
     }

Modified: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java?rev=371307&r1=371306&r2=371307&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java
(original)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramAcceptor.java
Sun Jan 22 06:30:56 2006
@@ -20,6 +20,7 @@
 
 import java.io.IOException;
 import java.net.SocketAddress;
+import java.net.SocketException;
 import java.nio.channels.DatagramChannel;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
@@ -35,6 +36,7 @@
 import org.apache.mina.common.IoHandler;
 import org.apache.mina.common.IoService;
 import org.apache.mina.common.IoSession;
+import org.apache.mina.common.RuntimeIOException;
 import org.apache.mina.common.IoFilter.WriteRequest;
 import org.apache.mina.common.IoServiceProvider.IoAcceptor;
 import org.apache.mina.util.ExceptionUtil;
@@ -162,7 +164,17 @@
         }
 
         RegistrationRequest req = ( RegistrationRequest ) key.attachment();
-        DatagramSession s = new DatagramSession( this, ch, req.handler );
+        DatagramSession s;
+        
+        try
+        {
+            s = new DatagramSession( this, ch, req.handler );
+        }
+        catch( SocketException e )
+        {
+            throw new RuntimeIOException( "Failed to create a session.", e );
+        }
+
         s.setRemoteAddress( remoteAddress );
         s.setSelectionKey( key );
         
@@ -292,14 +304,14 @@
             it.remove();
 
             DatagramChannel ch = ( DatagramChannel ) key.channel();
-
             RegistrationRequest req = ( RegistrationRequest ) key.attachment();
-            DatagramSession session =
-                new DatagramSession( this, ch, req.handler );
-            session.setSelectionKey( key );
             
             try
             {
+                DatagramSession session =
+                    new DatagramSession( this, ch, req.handler );
+                session.setSelectionKey( key );
+
                 ( ( DatagramFilterChain ) session.getFilterChain() ).sessionCreated( session
);
 
                 if( key.isReadable() )

Modified: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramConnector.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramConnector.java?rev=371307&r1=371306&r2=371307&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramConnector.java
(original)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramConnector.java
Sun Jan 22 06:30:56 2006
@@ -485,12 +485,12 @@
             if( req == null )
                 break;
 
-            DatagramSession session =
-                new DatagramSession( this, req.channel, req.handler );
-
             boolean success = false;
             try
             {
+                DatagramSession session =
+                    new DatagramSession( this, req.channel, req.handler );
+
                 req.filterChainBuilder.buildFilterChain( session.getFilterChain() );
                 ( ( DatagramFilterChain ) session.getFilterChain() ).sessionCreated( session
);
 

Modified: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramSession.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramSession.java?rev=371307&r1=371306&r2=371307&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramSession.java
(original)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/DatagramSession.java
Sun Jan 22 06:30:56 2006
@@ -32,6 +32,7 @@
 import org.apache.mina.common.IoSession;
 import org.apache.mina.common.IoFilter.WriteRequest;
 import org.apache.mina.common.support.BaseIoSession;
+import org.apache.mina.common.support.IoSessionAttributeOverrider;
 import org.apache.mina.util.Queue;
 
 /**
@@ -42,6 +43,15 @@
  */
 class DatagramSession extends BaseIoSession
 {
+    static
+    {
+        BaseIoSession.registerAttributeOverrider( DatagramServiceProvider.class, new ReceiveBufferSizeOverrider()
);
+        BaseIoSession.registerAttributeOverrider( DatagramServiceProvider.class, new SendBufferSizeOverrider()
);
+        BaseIoSession.registerAttributeOverrider( DatagramServiceProvider.class, new BroadcastOverrider()
);
+        BaseIoSession.registerAttributeOverrider( DatagramServiceProvider.class, new ReuseAddressOverrider()
);
+        BaseIoSession.registerAttributeOverrider( DatagramServiceProvider.class, new TrafficClassOverrider()
);
+    }
+
     private final DatagramSessionManager managerDelegate;
     private final DatagramFilterChain filterChain;
     private final DatagramChannel ch;
@@ -50,12 +60,14 @@
     private final IoAddress localAddress;
     private IoAddress remoteAddress;
     private SelectionKey key;
+    private int readBufferSize;
 
     /**
      * Creates a new instance.
+     * @throws SocketException 
      */
     DatagramSession( DatagramSessionManager managerDelegate,
-                         DatagramChannel ch, IoHandler defaultHandler )
+                     DatagramChannel ch, IoHandler defaultHandler ) throws SocketException
     {
         this.managerDelegate = managerDelegate;
         this.filterChain = new DatagramFilterChain( this );
@@ -69,6 +81,7 @@
             this.remoteAddress = SocketAddresses.compose( "datagram", remoteAddress );
         }
         this.localAddress = SocketAddresses.compose( "datagram", ch.socket().getLocalSocketAddress()
);
+        this.readBufferSize = ch.socket().getReceiveBufferSize();
     }
     
     DatagramSessionManager getManagerDelegate()
@@ -168,33 +181,116 @@
     {
         managerDelegate.updateTrafficMask( this );
     }
-
-    public int getReceiveBufferSize() throws SocketException {
-        return ch.socket().getReceiveBufferSize();
-    }
-
-    public void setReceiveBufferSize( int receiveBufferSize ) throws SocketException
+    
+    int getReadBufferSize()
     {
-        ch.socket().setReceiveBufferSize( receiveBufferSize );
+        return readBufferSize;
     }
 
-    public boolean getBroadcast() throws SocketException
+    private static class ReceiveBufferSizeOverrider implements IoSessionAttributeOverrider
     {
-        return ch.socket().getBroadcast();
-    }
+        public String getKey()
+        {
+            return Constants.RECEIVE_BUFFER_SIZE;
+        }
 
-    public void setBroadcast( boolean broadcast ) throws SocketException
+        public Object getValue( IoSession session ) throws Exception
+        {
+            return new Integer( ( ( DatagramSession ) session ).getChannel().socket().getReceiveBufferSize()
);
+        }
+
+        public Object setValue( IoSession session, Object value ) throws Exception
+        {
+            Object oldValue = getValue( session );
+            Integer receiveBufferSize = ( Integer ) value;
+            ( ( DatagramSession ) session ).getChannel().socket().setReceiveBufferSize( receiveBufferSize.intValue()
);
+            ( ( DatagramSession ) session ).readBufferSize = receiveBufferSize.intValue();
+            return oldValue;
+        }
+    }
+    
+    private static class SendBufferSizeOverrider implements IoSessionAttributeOverrider
     {
-        ch.socket().setBroadcast( broadcast );
+        public String getKey()
+        {
+            return Constants.SEND_BUFFER_SIZE;
+        }
+
+        public Object getValue( IoSession session ) throws Exception
+        {
+            return new Integer( ( ( DatagramSession ) session ).getChannel().socket().getSendBufferSize()
);
+        }
+
+        public Object setValue( IoSession session, Object value ) throws Exception
+        {
+            Object oldValue = getValue( session );
+            Integer sendBufferSize = ( Integer ) value;
+            ( ( DatagramSession ) session ).getChannel().socket().setSendBufferSize( sendBufferSize.intValue()
);
+            return oldValue;
+        }
     }
 
-    public int getSendBufferSize() throws SocketException
+    private static class BroadcastOverrider implements IoSessionAttributeOverrider
     {
-        return ch.socket().getSendBufferSize();
+        public String getKey()
+        {
+            return Constants.BROADCAST;
+        }
+
+        public Object getValue( IoSession session ) throws Exception
+        {
+            return ( ( DatagramSession ) session ).getChannel().socket().getBroadcast()?
Boolean.TRUE : Boolean.FALSE;
+        }
+
+        public Object setValue( IoSession session, Object value ) throws Exception
+        {
+            Object oldValue = getValue( session );
+            ( ( DatagramSession ) session ).getChannel().socket().setBroadcast(
+                    ( ( Boolean ) value ).equals( Boolean.TRUE ) );
+            return oldValue;
+        }
     }
+    
+    private static class ReuseAddressOverrider implements IoSessionAttributeOverrider
+    {
+        public String getKey()
+        {
+            return Constants.REUSE_ADDRESS;
+        }
+
+        public Object getValue( IoSession session ) throws Exception
+        {
+            return ( ( DatagramSession ) session ).getChannel().socket().getReuseAddress()?
Boolean.TRUE : Boolean.FALSE;
+        }
 
-    public void setSendBufferSize( int sendBufferSize ) throws SocketException
+        public Object setValue( IoSession session, Object value ) throws Exception
+        {
+            Object oldValue = getValue( session );
+            ( ( DatagramSession ) session ).getChannel().socket().setReuseAddress(
+                    ( ( Boolean ) value ).equals( Boolean.TRUE ) );
+            return oldValue;
+        }
+        
+    }
+    
+    private static class TrafficClassOverrider implements IoSessionAttributeOverrider
     {
-        ch.socket().setSendBufferSize( sendBufferSize );
+        public String getKey()
+        {
+            return Constants.TRAFFIC_CLASS;
+        }
+
+        public Object getValue( IoSession session ) throws Exception
+        {
+            return new Integer( ( ( DatagramSession ) session ).getChannel().socket().getTrafficClass()
);
+        }
+
+        public Object setValue( IoSession session, Object value ) throws Exception
+        {
+            Object oldValue = getValue( session );
+            Integer trafficClass = ( Integer ) value;
+            ( ( DatagramSession ) session ).getChannel().socket().setTrafficClass( trafficClass.intValue()
);
+            return oldValue;
+        }
     }
 }

Modified: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/SocketSession.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/SocketSession.java?rev=371307&r1=371306&r2=371307&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/SocketSession.java
(original)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/transport/socket/nio/SocketSession.java
Sun Jan 22 06:30:56 2006
@@ -43,17 +43,17 @@
  */
 class SocketSession extends BaseIoSession
 {
-    private static final IoSessionAttributeOverrider[] OVERRIDERS =
+    static
     {
-        new KeepAliveOverrider(),
-        new OOBInlineOverrider(),
-        new ReuseAddressOverrider(),
-        new LingerOverrider(),
-        new NoDelayOverrider(),
-        new TrafficClassOverrider(),
-        new SendBufferSizeOverrider(),
-        new ReceiveBufferSizeOverrider(),
-    };
+        BaseIoSession.registerAttributeOverrider( SocketServiceProvider.class, new ReceiveBufferSizeOverrider()
);
+        BaseIoSession.registerAttributeOverrider( SocketServiceProvider.class, new SendBufferSizeOverrider()
);
+        BaseIoSession.registerAttributeOverrider( SocketServiceProvider.class, new ReuseAddressOverrider()
);
+        BaseIoSession.registerAttributeOverrider( SocketServiceProvider.class, new TrafficClassOverrider()
);
+        BaseIoSession.registerAttributeOverrider( SocketServiceProvider.class, new KeepAliveOverrider()
);
+        BaseIoSession.registerAttributeOverrider( SocketServiceProvider.class, new OOBInlineOverrider()
);
+        BaseIoSession.registerAttributeOverrider( SocketServiceProvider.class, new LingerOverrider()
);
+        BaseIoSession.registerAttributeOverrider( SocketServiceProvider.class, new NoDelayOverrider()
);
+    }
     
     private final SocketIoProcessor ioProcessor;
     private final SocketFilterChain filterChain;
@@ -83,7 +83,6 @@
         this.remoteAddress = SocketAddresses.compose( "socket", ch.socket().getRemoteSocketAddress()
);
         this.localAddress = SocketAddresses.compose( "socket", ch.socket().getLocalSocketAddress()
);
         this.readBufferSize = ch.socket().getReceiveBufferSize();
-        this.attributeOverriders = OVERRIDERS;
     }
     
     SocketIoProcessor getIoProcessor()



Mime
View raw message