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 "MinaTutorialInChinese" by fff
Date Mon, 05 Feb 2007 04:24:23 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 fff:
http://wiki.apache.org/directory/MinaTutorialInChinese

The comment on the change is:
code{ProtocolSession -> IoSession; method data* (in handler)-> message*;}

------------------------------------------------------------------------------
    * sessionClosed: 当IO连接被关闭时被调用。
    * sessionIdle: 当在远程实体和用户程序之间没有数据传输的时候被调用。
    * exceptionCaught: 当IoAcceptor 或者你的IoHandler.中出现异常时被调用。
-   * dataRead: 当从远程实体读取数据时被调用。
-   * dataWritten: 当你想远程实体发出请求时被调用
+   * messageReceived: 当接收到新的协议消息时被调用。可以在这里实现你的控制流程。
+   * messageSent: 当用户请求的消息通过 IoSession#write(Object) 确实发送后被调用。
  
  下面我们看看如何实现echo协议的IoHandler。
  
@@ -64, +64 @@

  {{{
  package org.apache.mina.examples.echoserver;
  
+ import org.apache.mina.common.ByteBuffer;
+ import org.apache.mina.common.IoHandlerAdapter;
- import org.apache.mina.common.*;
+ import org.apache.mina.common.IoSession;
- import org.apache.mina.io.*;
+ import org.apache.mina.common.TransportType;
+ import org.apache.mina.transport.socket.nio.SocketSessionConfig;
  
  public class EchoProtocolHandler extends IoHandlerAdapter
  {
      public void sessionCreated( IoSession session )
      {
+         if (session.getTransportType() == TransportType.SOCKET) {
+           ((SocketSessionConfig)session.getConfig()).setReceiveBufferSize(2048);
-         SessionConfig cfg = session.getConfig();
-         if( cfg instanceof SocketSessionConfig )
-         {
-             SocketSessionConfig scfg = ( SocketSessionConfig ) cfg ) ;
- scfg.setSessionReceiveBufferSize( 2048 );
-         }
      }
  
      public void exceptionCaught( IoSession session, Throwable cause )
@@ -84, +83 @@

          session.close();
      }
  
-     public void dataRead( IoSession session, ByteBuffer rb )
+     public void messageReceived( IoSession session, Object message )
+     {
+         if (!(message instanceof ByteBuffer))
+             return;
-     {
+      
+         ByteBuffer rb = (ByteBuffer)message;
          // Write the received data back to remote peer
          ByteBuffer wb = ByteBuffer.allocate( rb.remaining() );
          wb.put( rb );
          wb.flip();
-         session.write( wb, null );
+         session.write( wb );
      }
  }
  }}}
@@ -98, +101 @@

  刚刚我们使用MINA实现echo协议,现在我们将handler绑定到一个server端口上。
  
  {{{
+ 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
  {
@@ -113, +113 @@

  
      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 );
      }
@@ -139, +139 @@

  我们的echo协议handler不对任何IO事件进行日志。我们可以通过添加一个filter来增加日志能力。MINA提供了IoLoggingFilter来进行日志。我们只要添加日志filter到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" );
  }
  }}}
  
  想使用SSL?MINA也提供了一个SSL的filter,但它需要JDK1.5。
  
  {{{
- 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" );
  }
  }}}
@@ -200, +204 @@

  {{{
  package org.apache.mina.examples.reverser;
  
- import org.apache.mina.protocol.*;
+ import org.apache.mina.common.IoHandler;
+ import org.apache.mina.common.IoHandlerAdapter;
+ import org.apache.mina.common.IoSession;
  
- public class ReverseProtocolHandler extends ProtocolHandlerAdapter
+ public class ReverseProtocolHandler extends IoHandlerAdapter
  {
-     public void exceptionCaught( ProtocolSession session,
+     public void exceptionCaught( IoSession session, Throwable cause )
- Throwable cause )
      {
          // Close connection when unexpected exception is caught.
          session.close();
      }
  
-     public void messageReceived( ProtocolSession session,
+     public void messageReceived( IoSession session, Object message )
- Object message )
      {
-         // Reverse reveiced string
+         // Reverse received string
          String str = message.toString();
          StringBuffer buf = new StringBuffer( str.length() );
          for( int i = str.length() - 1; i >= 0; i-- )

Mime
View raw message