Apache Mina: Implementing fixed byte length encoding and decoding

Source: Internet
Author: User


There are some decoders in Apache Mina.

Single byte decoder singlebytedecodingstate

Short-integer Decoder shortintegerdecodingstate

Integer decoder integerdecodingstate

Fixed length byte decoder fixedlengthdecodingstate


Server-side code:

public class Testserver{public static void Main (string[] args) throws ioexception{ioacceptor acceptor = new NIOSOCKETACCEP Tor (); Acceptor.getfilterchain (). AddLast ("Logger", New Loggingfilter ()), Acceptor.getfilterchain (). AddLast ("codec" , New Protocolcodecfilter (New Fixedlengthencoder (), New Fixedlengthdecoder ())); Acceptor.sethandler (new Fixedlengthiohandleradapter ()); Acceptor.bind (new Inetsocketaddress (9999));}}

Client code:

public class Testclient{public static void Main (string[] args) {niosocketconnector connector = new Niosocketconnector (); Connector.setconnecttimeoutmillis (10000); Connector.getfilterchain (). AddLast ("Codec", New Protocolcodecfilter (new Fixedlengthencoder (), New Fixedlengthdecoder ()); Connector.getfilterchain (). AddLast ("Logger", New Loggingfilter ()) ;        Connector.sethandler (New Fixedlengthiohandleradapter ()); Iosession session;for (;;) {Try{connectfuture future = Connector.connect (new inetsocketaddress ("localhost", 9999)); future.awaituninterruptibly (); session = Future.getsession (); break;} catch (Runtimeioexception e) {e.printstacktrace (); Try{thread.sleep (5000);} catch (Interruptedexception E1) {//TODO auto-generated catch Blocke1.printstacktrace ();        }}} for (int i = 0; i < i++) {session.write (i);} Wait until the summation is Donesession.getclosefuture (). awaituninterruptibly (); Connector.dispose ();}}

Fixed byte length decoder:

public class Fixedlengthdecoder extends cumulativeprotocoldecoder{private static final String prefix_decodingstate = " Prefix_decodingstate "; @Overrideprotected boolean Dodecode (iosession session, Iobuffer in, protocoldecoderoutput out) Throws Exception{fixedlengthdecodingstateimpl state = (Fixedlengthdecodingstateimpl) session.getattribute (PREFIX_ Decodingstate);        if (state ==null)        {State        =new Fixedlengthdecodingstateimpl (4);        Session.setattributeifabsent (prefix_decodingstate, State);        }        if (state!=null)        {        State.decode (in, out);        if (State.getstate (). Equals (fixedlengthdecodingstateimpl.success))        {        Session.removeattribute (prefix_ Decodingstate);        State =null;        return true;        }        } return false;}}
public class Fixedlengthdecodingstateimpl extends fixedlengthdecodingstate{public String state = "";p ublic final static S Tring SUCCESS = "SUCCESS";p ublic fixedlengthdecodingstateimpl (int length) {super (length);} Public String GetState () {return state;} public void SetState (String state) {this.state = state;} @Overrideprotected decodingstate Finishdecode (iobuffer product, protocoldecoderoutput out) throws exception{int i = Product.getint (); Out.write (i); this.state =fixedlengthdecodingstateimpl.success;return this;}}


Encoder:

public class Fixedlengthencoder extends protocolencoderadapter{@Overridepublic void encode (iosession session, Object Message, protocolencoderoutput out) throws exception{  Integer i = (integer) message;  Iobuffer buf = iobuffer.allocate (4). Setautoexpand (true);          Buf.putint (i);          Buf.flip ();          Out.write (BUF);}}

Business Logic Processing classes:

public class Fixedlengthiohandleradapter extends iohandleradapter{@Overridepublic void messagereceived (iosession Session, Object message) throws Exception{system.out.println ("messagereceived:" +message);}}


Apache Mina: Implementing fixed byte length encoding and decoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.