directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r448090 - in /directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup: Client.java ClientSessionHandler.java Server.java ServerSessionHandler.java
Date Wed, 20 Sep 2006 06:30:29 GMT
Author: trustin
Date: Tue Sep 19 23:30:28 2006
New Revision: 448090

URL: http://svn.apache.org/viewvc?view=rev&rev=448090
Log:
Applied the best practice of adding filters

Modified:
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/Client.java
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/ClientSessionHandler.java
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/Server.java
    directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/ServerSessionHandler.java

Modified: directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/Client.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/Client.java?view=diff&rev=448090&r1=448089&r2=448090
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/Client.java
(original)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/Client.java
Tue Sep 19 23:30:28 2006
@@ -23,10 +23,14 @@
 
 import org.apache.mina.common.ConnectFuture;
 import org.apache.mina.common.IoConnector;
-import org.apache.mina.common.IoConnectorConfig;
 import org.apache.mina.common.IoSession;
 import org.apache.mina.common.RuntimeIOException;
+import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory;
+import org.apache.mina.filter.LoggingFilter;
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
 import org.apache.mina.transport.socket.nio.SocketConnector;
+import org.apache.mina.transport.socket.nio.SocketConnectorConfig;
 
 /**
  * (<strong>Entry Point</strong>) Starts SumUp client.
@@ -57,9 +61,24 @@
             values[ i ] = Integer.parseInt( args[ i ] );
         }
 
-        // Set connect timeout.
         IoConnector connector = new SocketConnector();
-        ( ( IoConnectorConfig ) connector.getDefaultConfig() ).setConnectTimeout( CONNECT_TIMEOUT
);
+        
+        // Configure the service.
+        SocketConnectorConfig cfg = new SocketConnectorConfig();
+        cfg.setConnectTimeout( CONNECT_TIMEOUT );
+        if( USE_CUSTOM_CODEC )
+        {
+            cfg.getFilterChain().addLast(
+                    "codec",
+                    new ProtocolCodecFilter( new SumUpProtocolCodecFactory( false ) ) );
+        }
+        else
+        {
+            cfg.getFilterChain().addLast(
+                    "codec",
+                    new ProtocolCodecFilter( new ObjectSerializationCodecFactory() ) );
+        }
+        cfg.getFilterChain().addLast( "logger", new LoggingFilter() );
         
         IoSession session;
         for( ;; )
@@ -68,7 +87,7 @@
             {
                 ConnectFuture future = connector.connect(
                         new InetSocketAddress( HOSTNAME, PORT ),
-                        new ClientSessionHandler( USE_CUSTOM_CODEC, values ) );
+                        new ClientSessionHandler( values ), cfg );
                 
                 future.join();
                 session = future.getSession();

Modified: directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/ClientSessionHandler.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/ClientSessionHandler.java?view=diff&rev=448090&r1=448089&r2=448090
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/ClientSessionHandler.java
(original)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/ClientSessionHandler.java
Tue Sep 19 23:30:28 2006
@@ -22,13 +22,8 @@
 import org.apache.mina.common.IoHandler;
 import org.apache.mina.common.IoHandlerAdapter;
 import org.apache.mina.common.IoSession;
-import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory;
 import org.apache.mina.example.sumup.message.AddMessage;
 import org.apache.mina.example.sumup.message.ResultMessage;
-import org.apache.mina.filter.LoggingFilter;
-import org.apache.mina.filter.codec.ProtocolCodecFactory;
-import org.apache.mina.filter.codec.ProtocolCodecFilter;
-import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
 import org.apache.mina.util.SessionLog;
 
 /**
@@ -39,37 +34,17 @@
  */
 public class ClientSessionHandler extends IoHandlerAdapter
 {
-    private final boolean useCustomCodec;
     private final int[] values;
     private boolean finished;
 
-    public ClientSessionHandler( boolean useCustomCodec, int[] values )
+    public ClientSessionHandler( int[] values )
     {
-        this.useCustomCodec = useCustomCodec;
         this.values = values;
     }
 
     public boolean isFinished()
     {
         return finished;
-    }
-
-    public void sessionCreated( IoSession session ) throws Exception
-    {
-        ProtocolCodecFactory codec;
-        if( useCustomCodec )
-        {
-            codec = new SumUpProtocolCodecFactory( false );
-        }
-        else
-        {
-            codec = new ObjectSerializationCodecFactory();
-        }
-
-        session.getFilterChain().addLast(
-                "protocolFilter", new ProtocolCodecFilter( codec ) );
-        session.getFilterChain().addLast(
-                "logger", new LoggingFilter() );
     }
 
     public void sessionOpened( IoSession session )

Modified: directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/Server.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/Server.java?view=diff&rev=448090&r1=448089&r2=448090
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/Server.java
(original)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/Server.java
Tue Sep 19 23:30:28 2006
@@ -22,7 +22,12 @@
 import java.net.InetSocketAddress;
 
 import org.apache.mina.common.IoAcceptor;
+import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory;
+import org.apache.mina.filter.LoggingFilter;
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
 import org.apache.mina.transport.socket.nio.SocketAcceptor;
+import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
 
 /**
  * (<strong>Entry Point</strong>) Starts SumUp server.
@@ -38,12 +43,28 @@
 
     public static void main( String[] args ) throws Throwable
     {
-        // Create ServiceRegistry.
         IoAcceptor acceptor = new SocketAcceptor();
+        
+        // Prepare the service configuration.
+        SocketAcceptorConfig cfg = new SocketAcceptorConfig();
+        cfg.setReuseAddress( true );
+        if( USE_CUSTOM_CODEC )
+        {
+            cfg.getFilterChain().addLast(
+                    "codec",
+                    new ProtocolCodecFilter( new SumUpProtocolCodecFactory( true ) ) );
+        }
+        else
+        {
+            cfg.getFilterChain().addLast(
+                    "codec",
+                    new ProtocolCodecFilter( new ObjectSerializationCodecFactory() ) );
+        }
+        cfg.getFilterChain().addLast( "logger", new LoggingFilter() );
 
         acceptor.bind(
                 new InetSocketAddress( SERVER_PORT ),
-                new ServerSessionHandler( USE_CUSTOM_CODEC ) );
+                new ServerSessionHandler( ), cfg );
 
         System.out.println( "Listening on port " + SERVER_PORT );
     }

Modified: directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/ServerSessionHandler.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/ServerSessionHandler.java?view=diff&rev=448090&r1=448089&r2=448090
==============================================================================
--- directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/ServerSessionHandler.java
(original)
+++ directory/trunks/mina/example/src/main/java/org/apache/mina/example/sumup/ServerSessionHandler.java
Tue Sep 19 23:30:28 2006
@@ -21,14 +21,10 @@
 
 import org.apache.mina.common.IdleStatus;
 import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoHandlerAdapter;
 import org.apache.mina.common.IoSession;
-import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory;
 import org.apache.mina.example.sumup.message.AddMessage;
 import org.apache.mina.example.sumup.message.ResultMessage;
-import org.apache.mina.filter.LoggingFilter;
-import org.apache.mina.filter.codec.ProtocolCodecFactory;
-import org.apache.mina.filter.codec.ProtocolCodecFilter;
-import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
 import org.apache.mina.util.SessionLog;
 
 /**
@@ -37,33 +33,8 @@
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$
  */
-public class ServerSessionHandler implements IoHandler
+public class ServerSessionHandler extends IoHandlerAdapter
 {
-    private final boolean useCustomCodec;
-
-    public ServerSessionHandler( boolean useCustomCodec )
-    {
-        this.useCustomCodec = useCustomCodec;
-    }
-
-    public void sessionCreated( IoSession session ) throws Exception
-    {
-        ProtocolCodecFactory codec;
-        if( useCustomCodec )
-        {
-            codec = new SumUpProtocolCodecFactory( true );
-        }
-        else
-        {
-            codec = new ObjectSerializationCodecFactory();
-        }
-
-        session.getFilterChain().addFirst(
-                "protocolFilter", new ProtocolCodecFilter( codec ) );
-        session.getFilterChain().addLast(
-                "logger", new LoggingFilter() );
-    }
-
     public void sessionOpened( IoSession session )
     {
         // set idle time to 60 seconds
@@ -73,10 +44,6 @@
         session.setAttachment( new Integer( 0 ) );
     }
 
-    public void sessionClosed( IoSession session )
-    {
-    }
-
     public void messageReceived( IoSession session, Object message )
     {
         // client only sends AddMessage. otherwise, we will have to identify
@@ -108,10 +75,6 @@
             rm.setValue( sum );
             session.write( rm );
         }
-    }
-
-    public void messageSent( IoSession session, Object message )
-    {
     }
 
     public void sessionIdle( IoSession session, IdleStatus status )



Mime
View raw message