directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Directory Wiki] Update of "MinaTutorial" by sishen
Date Tue, 31 Oct 2006 09:39:14 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Directory Wiki" for change notification.

The following page has been changed by sishen:
http://wiki.apache.org/directory/MinaTutorial

------------------------------------------------------------------------------
          session.close();
      }
  
-     public void dataRead( IoSession session, Object message )
+     public void messageReceived( IoSession session, Object message )
      {
          if (!(message instanceof ByteBuffer))
              return;
@@ -100, +100 @@

  You’ve just implemented echo protocol with MINA.  Now you need to bind your handler to
a server port:
  
  {{{
+ import java.net.InetSocketAddress;
+ import org.apache.mina.transport.socket.nio.SocketAcceptor;
+ import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
- package org.apache.mina.examples.echoserver;
- 
- import org.apache.mina.common.*;
- import org.apache.mina.io.*;
- import org.apache.mina.io.filter.*;
- import org.apache.mina.registry.*;
  
  public class Main
  {
@@ -115, +112 @@

  
      public static void main( String[] args ) throws Exception
      {
-         ServiceRegistry registry = new SimpleServiceRegistry();
+         SocketAcceptor acceptor = new SocketAcceptor();
+         SocketAcceptorConfig defaultConfig = new SocketAcceptorConfig();
+         defaultConfig.setReuseAddress(true);
-         
+      
+         // Bind
+         acceptor.bind(new InetSocketAddress(PORT), new EchoProtocolHandler(), defaultConfig);
- // Bind
-         Service service = new Service( "echo",
- TransportType.SOCKET, PORT );
-         registry.bind( service, new EchoProtocolHandler() );
  
          System.out.println( "Listening on port " + PORT );
      }
@@ -141, +138 @@

  Our echo protocol handler doesn’t log any I/O events.  We could log them by adding a filter
that logs them.  MINA, fortunately, provides IoLoggingFilter that provides that functionality.
 Let’s add a logging filter to ServiceRegistry.
  
  {{{
- private static void addLogger( ServiceRegistry registry )
+ ....
+ DefaultIoFilterChainBuilder chain = config.getFilterChain();
+ addLogger(chain);
+ ....
+ 
+ private static void addLogger( DefaultIoFilterChainBuilder chain ) throws Exception
  {
+     chain.addLast( "logger", new LoggingFilter() );
-     IoAcceptor acceptor =
- registry.getIoAcceptor( TransportType.SOCKET );
-     acceptor.getFilterChain().addLast( "logger",
-  new IoLoggingFilter() );
      System.out.println( "Logging ON" );
  }
  }}}
@@ -154, +153 @@

  What about SSL?  MINA also provides an SSL filter that works in Java 5 or above.
  
  {{{
- private static void addSSLSupport( ServiceRegistry registry )
+ ....
+ DefaultIoFilterChainBuilder chain = config.getFilterChain();
+ addLogger(chain);
+ ....
+ 
+ private static void addSSLSupport( DefaultIoFilterChainBuilder chain )
-         throws Exception
+     throws Exception
  {
      SSLFilter sslFilter =
          new SSLFilter( BogusSSLContextFactory.getInstance( true ) );
-     IoAcceptor acceptor =
-  registry.getIoAcceptor( TransportType.SOCKET );
-     acceptor.getFilterChain().addLast( "sslFilter", sslFilter );
+     chain.addLast( "sslFilter", sslFilter );
      System.out.println( "SSL ON" );
  }
  }}}

Mime
View raw message