Apache Mina自訂編解碼案例

來源:互聯網
上載者:User

Mina中已經內建的編解碼類:

TextLineCodecFactory:基於文本的,根據斷行符號換行來斷點傳輸資料

ProtocolCodecFactory:自訂協議的編解碼資料轉送

ObjectSerializationCodecFactory:對象序列化傳輸

DemuxingProtocolCodecFactory:複用傳輸

自訂通訊協定:

FlightSearch 1.0 \n
startcity:BJS \n
endcity:PEK \n
flightway:1 \n
date:2011-08-10 \n

Domain對象

package domain;/** * @function :  * @author   :jy * @company  :萬裡網 * @date     :2011-8-7 */public class Flight {public String startCity;public String endCity;public String flightway;public String date;public String fromDate;public String subclass1;public String flight1;/** * 返回出發城市 * @return */public String getStartCity() {return startCity;}public void setStartCity(String startCity) {this.startCity = startCity;}/** * 返回到達城市 * @return */public String getEndCity() {return endCity;}public void setEndCity(String endCity) {this.endCity = endCity;}/** * 返回行程類型 * @return */public String getFlightway() {return flightway;}public void setFlightway(String flightway) {this.flightway = flightway;}/** * 返回出發日期 * @return */public String getDate() {return date;}public void setDate(String date) {this.date = date;}@Overridepublic String toString() {return "Flight [startCity=" + startCity + ", endCity=" + endCity + ", flightway=" + flightway + ", date="+ date + "]";}/** * 返回往返日期 * @return */public String getFromDate() {return fromDate;}public void setFromDate(String fromDate) {this.fromDate = fromDate;}public String getFlight1() {return flight1;}public void setFlight1(String flight1) {this.flight1 = flight1;}public String getSubclass1() {return subclass1;}public void setSubclass1(String subclass1) {this.subclass1 = subclass1;}}

伺服器端編碼

package server;import java.nio.charset.Charset;import java.nio.charset.CharsetEncoder;import org.apache.mina.core.buffer.IoBuffer;import org.apache.mina.core.session.IoSession;import org.apache.mina.filter.codec.ProtocolEncoderAdapter;import org.apache.mina.filter.codec.ProtocolEncoderOutput;/** * @function :  * @author   :jy * @company  :萬裡網 * @date     :2011-8-7 */public class FlightEncoder extends ProtocolEncoderAdapter {private final Charset charset = Charset.forName("UTF-8");/*  * 伺服器端編碼無需處理,直接將接收到的資料向下傳遞 */@Overridepublic void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);CharsetEncoder ce = charset.newEncoder();buf.putString((String)message, ce);buf.flip();out.write(buf);}}

重點是伺服器端解碼

package server;import java.nio.charset.Charset;import java.nio.charset.CharsetDecoder;import org.apache.mina.core.buffer.IoBuffer;import org.apache.mina.core.session.IoSession;import org.apache.mina.filter.codec.CumulativeProtocolDecoder;import org.apache.mina.filter.codec.ProtocolDecoderAdapter;import org.apache.mina.filter.codec.ProtocolDecoderOutput;import domain.Flight;/** * @function :  * @author   :jy * @company  :萬裡網 * @date     :2011-8-7 */public class FlightDecoder extends CumulativeProtocolDecoder {@Overrideprotected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);CharsetDecoder cd = Charset.forName("UTF-8").newDecoder();int ColumnNumber = 0;String status="",startCity="",endCity="",flightway="",date="";int TextLineNumber = 1;Flight flight = new Flight();/** * FlightSearch 1.0 \n * startcity:BJS \n * endcity:PEK \n * flightway:1 \n * date:2011-08-10 \n */while(in.hasRemaining()){byte b = in.get();buf.put(b);if(b == 10 && TextLineNumber <= 5){ColumnNumber++;if(TextLineNumber == 1){buf.flip();status = buf.getString(ColumnNumber, cd);}if(TextLineNumber == 2){buf.flip();startCity = buf.getString(ColumnNumber, cd).split(":")[1];startCity = startCity.substring(0, startCity.length()-1);flight.setStartCity(startCity);}if(TextLineNumber == 3){buf.flip();endCity = buf.getString(ColumnNumber, cd).split(":")[1];endCity = endCity.substring(0, endCity.length()-1);flight.setEndCity(endCity);}if(TextLineNumber == 4){buf.flip();flightway = buf.getString(ColumnNumber, cd).split(":")[1];flightway = flightway.substring(0, flightway.length()-1);flight.setFlightway(flightway);}if(TextLineNumber == 5){buf.flip();date = buf.getString(ColumnNumber, cd).split(":")[1];date = date.substring(0, date.length()-1);flight.setDate(date);break;}ColumnNumber = 0;buf.clear();TextLineNumber++;}else{ColumnNumber++;}}out.write(flight);return false;}}

伺服器端編解碼工廠

package server;import org.apache.mina.core.session.IoSession;import org.apache.mina.filter.codec.ProtocolCodecFactory;import org.apache.mina.filter.codec.ProtocolDecoder;import org.apache.mina.filter.codec.ProtocolEncoder;/** * @function :  * @author   :jy * @company  :萬裡網 * @date     :2011-8-7 */public class FlightCodecFactory implements ProtocolCodecFactory {private final ProtocolEncoder encoder = new FlightEncoder();private final ProtocolDecoder decoder = new FlightDecoder();@Overridepublic ProtocolDecoder getDecoder(IoSession session) throws Exception {return decoder;}@Overridepublic ProtocolEncoder getEncoder(IoSession session) throws Exception {return encoder;}}

下面是用戶端的編解碼

重點是編碼,需要將資料群組裝成協議格式,發送給伺服器

package client;import java.nio.charset.Charset;import java.nio.charset.CharsetEncoder;import org.apache.mina.core.buffer.IoBuffer;import org.apache.mina.core.session.IoSession;import org.apache.mina.filter.codec.ProtocolEncoderAdapter;import org.apache.mina.filter.codec.ProtocolEncoderOutput;import domain.Flight;/** * @function :  * @author   :jy * @company  :萬裡網 * @date     :2011-8-7 */public class FlightClientEncoder extends ProtocolEncoderAdapter {private final Charset charset;public FlightClientEncoder(){this.charset = Charset.forName("UTF-8");}@Overridepublic void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {Flight flight = (Flight)message;IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);CharsetEncoder ce = charset.newEncoder();buf.putString("Flight Search 1.0" + '\n', ce);buf.putString("startcty:" + flight.getStartCity() + '\n', ce);buf.putString("endcity:" + flight.getEndCity() + '\n', ce);buf.putString("flightway:" + flight.getFlightway() + '\n', ce);buf.putString("date:" + flight.getDate() + '\n', ce);buf.flip();out.write(buf);}}

解碼無需特殊處理,接收完資料直接向下傳遞

package client;import java.nio.charset.Charset;import java.nio.charset.CharsetDecoder;import org.apache.mina.core.buffer.IoBuffer;import org.apache.mina.core.session.IoSession;import org.apache.mina.filter.codec.CumulativeProtocolDecoder;import org.apache.mina.filter.codec.ProtocolDecoderOutput;/** * @function :  * @author   :jy * @company  :萬裡網 * @date     :2011-8-7 */public class FlightClientDecoder extends CumulativeProtocolDecoder {/* (non-Javadoc) * @see org.apache.mina.filter.codec.ProtocolDecoder#decode(org.apache.mina.core.session.IoSession, org.apache.mina.core.buffer.IoBuffer, org.apache.mina.filter.codec.ProtocolDecoderOutput) */@Overrideprotected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {CharsetDecoder cd = Charset.forName("UTF-8").newDecoder();IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);while(in.hasRemaining()){buf.put(in.get());}buf.flip();out.write(buf.getString(cd));return false;}}

用戶端編解碼工廠

package client;import org.apache.mina.core.session.IoSession;import org.apache.mina.filter.codec.ProtocolCodecFactory;import org.apache.mina.filter.codec.ProtocolDecoder;import org.apache.mina.filter.codec.ProtocolEncoder;/** * @function :  * @author   :jy * @company  :萬裡網 * @date     :2011-8-7 */public class FlightClientCodecFactory implements ProtocolCodecFactory {private final ProtocolEncoder encoder = new FlightClientEncoder();private final ProtocolDecoder decoder = new FlightClientDecoder();/* (non-Javadoc) * @see org.apache.mina.filter.codec.ProtocolCodecFactory#getDecoder(org.apache.mina.core.session.IoSession) */@Overridepublic ProtocolDecoder getDecoder(IoSession arg0) throws Exception {return decoder;}/* (non-Javadoc) * @see org.apache.mina.filter.codec.ProtocolCodecFactory#getEncoder(org.apache.mina.core.session.IoSession) */@Overridepublic ProtocolEncoder getEncoder(IoSession arg0) throws Exception {return encoder;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.