在第一章中,我們對MINA有了一個大致的了解,在本章中,我們會對MINA中的客戶端/伺服器模型做一個細緻的分析。並且也會提供一些基於TCP,UDP的範例。
應用程式結構
服務端結構
客戶端結構
簡單的TCP伺服器
簡單的TCP客戶端
簡單的UDP伺服器
簡單的UDP框應用程式結構如下:
下面看看MINA的內部:
I/O 服務- 真正的I/O操作
I/O 過濾鏈- 過濾/傳輸數據I/O Handler - 在這裡完成程序的邏輯
所以,要創建一個MINA應用程序,你只需要做:
創建I/O服務- 選擇已經提供的服務(Acceptor)或自己創建的服務
創建過濾鏈- 選擇已經提供的過濾鍊或創建自己定制的過濾鏈
創建I/OHandler - 寫業務邏輯,處理各種不同的訊息以上是MINA的整體結構,
下面看看服務端的結構:
下面是客戶端模型:
其中客戶端會有一個IOConnector用來連接上服務端。而所有的處理仍然有IOHandler完成。
簡單的TCP伺服器
下面,創建一個簡單的TCP伺服器作為演示:首先你需要將一些需要的包導入到IDE或者配置你的CLASSPATH,具體方法就不詳述了,需要的包有:
MINA 2.x Core
JDK 1.5 or greater
SLF4J 1.3.0 or greater
Log4J 1.2 users: slf4j-api.jar, slf4j-log4j12., . 4j-api. jar, slf4j-log4j13.jar, and Log4J 1.3.x
java.util.logging users: slf4j-api.jar and slf4j-xk14.jar
IMPORTANT: Please make sure you matk14.jar
IMPORTANT: Please make sure you matare 是to your logging framework.
準備工作做完之後,我們開始寫程式碼
import java.net.InetSocketAddress; import org.apache.mina.core.service.IoAcceptor; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; public class MinaTimeServer { private static final int PORT = 9123; public static void main( String[] args ) throws IOException { IoAcceptor acceptor = new NioSocketAcceptor(); acceptor.bind( new InetSocketAddress(PORT) ); } }
import java.io.IOException; import java.net.InetSocketAddress; import java.nio.charset.Charset; import org.apache.mina.core.service.IoAcceptor; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; public class MinaTimeServer { public static void main( String[] args ) { IoAcceptor acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast( "logger", new LoggingFilter() ); //这里会建立所有的日志信息 acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" )))); //第二个过滤器用来传递数据 acceptor.bind( new InetSocketAddress(PORT) ); } }
import java.util.Date; import org.apache.mina.core.session.IdleStatus; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IoSession; public class TimeServerHandler extends IoHandlerAdapter { @Override public void exceptionCaught( IoSession session, Throwable cause ) throws Exception { cause.printStackTrace(); } @Override public void messageReceived( IoSession session, Object message ) throws Exception { String str = message.toString(); if( str.trim().equalsIgnoreCase("quit") ) { session.close(); return; } Date date = new Date(); session.write( date.toString() ); System.out.println("Message written..."); } @Override public void sessionIdle( IoSession session, IdleStatus status ) throws Exception { System.out.println( "IDLE " + session.getIdleCount( status )); } }
最後,完整的伺服器程式碼如下:
import java.io.IOException; import java.net.InetSocketAddress; import java.nio.charset.Charset; import org.apache.mina.core.service.IoAcceptor; import org.apache.mina.core.session.IdleStatus; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; public class MinaTimeServer { private static final int PORT = 9123; public static void main( String[] args ) throws IOException { IoAcceptor acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast( "logger", new LoggingFilter() ); acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" )))); acceptor.setHandler( new TimeServerHandler() ); //这里设置Handler acceptor.getSessionConfig().setReadBufferSize( 2048 ); //这是设置ssesion缓冲区 acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 ); acceptor.bind( new InetSocketAddress(PORT) ); } }
運行該伺服器,然後開啟終端機輸入指令:telnet 127.0.0.1 9123 即可看到,當你輸入非「quit」 的任何字元時,伺服器都會傳回目前的時間到終端來。
簡單的TCP客戶端
import java.net.InetSocketAddress; import org.apache.mina.core.RuntimeIoException; import org.apache.mina.core.future.ConnectFuture; import org.apache.mina.core.session.IoSession; import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory; import org.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.transport.socket.nio.NioSocketConnector; /** * (<strong>Entry Point</strong>) Starts SumUp client. * * @author <a href="http://mina.apache.org">Apache MINA Project</a> */ public class Client { private static final String HOSTNAME = "localhost"; private static final int PORT = 8080; private static final long CONNECT_TIMEOUT = 30*1000L; // 30 seconds // Set this to false to use object serialization instead of custom codec. private static final boolean USE_CUSTOM_CODEC = true; public static void main(String[] args) throws Throwable { if (args.length == 0) { System.out.println("Please specify the list of any integers"); return; } // prepare values to sum up int[] values = new int[args.length]; for (int i = 0; i < args.length; i++) { values[i] = Integer.parseInt(args[i]); } NioSocketConnector connector = new NioSocketConnector(); // Configure the service. connector.setConnectTimeoutMillis(CONNECT_TIMEOUT); if (USE_CUSTOM_CODEC) {
connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new SumUpProtocolCodecFactory(false))); } else { connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new ObjectSerializationCodecFactory())); } connector.getFilterChain().addLast("logger", new LoggingFilter()); connector.setHandler(new ClientSessionHandler(values)); IoSession session; for (;;) { try { ConnectFuture future = connector.connect(new InetSocketAddress( HOSTNAME, PORT)); future.awaitUninterruptibly(); session = future.getSession(); break; } catch (RuntimeIoException e) { System.err.println("Failed to connect."); e.printStackTrace(); Thread.sleep(5000); } } // wait until the summation is done session.getCloseFuture().awaitUninterruptibly(); connector.dispose(); } }
UDP的例子不寫了,需要的話到Apache官網去看看。