发表时间:2010-02-02
最后修改:2010-02-02
继续上篇文章
在注册自定义协议的工厂的实现类:通过继承MINA的 DemuxingProtocolCodecFactory实现自定义协议的编码的类。
消息结果的:
package cn.com.unutrip.java.opensource.mina.sumup.codec;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import cn.com.unutrip.java.opensource.mina.sumup.message.AbstractMessage;
import cn.com.unutrip.java.opensource.mina.sumup.message.ResultMessage;
import static cn.com.unutrip.java.opensource.mina.sumup.codec.Constants.RESULT;
import static cn.com.unutrip.java.opensource.mina.sumup.codec.Constants.RESULT_CODE_LEN;
/**
*
* @author longgangbai
*
*/
public class ResultMessageDecoder extends AbstractMessageDecoder{
private int code;
private boolean readCode;
public ResultMessageDecoder(){
super(Constants.RESULT);
}
/**
* 解析消息的body体的信息
* @param session
* @param in
* @return
*/
protected AbstractMessage decodeBody(IoSession session ,IoBuffer in)
{
if(!readCode)
{
if(in.remaining()<RESULT)
{
return null;
}
code=in.getShort();
readCode=true;
}
if(code==Constants.RESULT_OK)
{
if(in.remaining()<RESULT_CODE_LEN)
{
return null;
}
ResultMessage m=new ResultMessage();
m.setOk(true);
m.setValue(in.getInt());
readCode=false;
return m;
}else{
ResultMessage m=new ResultMessage();
m.setOk(false);
readCode=false;
return m;
}
}
@Override
public void finishDecode(IoSession arg0, ProtocolDecoderOutput arg1)
throws Exception {
}
}
package cn.com.unutrip.java.opensource.mina.sumup.codec;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import cn.com.unutrip.java.opensource.mina.sumup.message.ResultMessage;
/**
* 加密消息头和消息的体的信息
* @author longgangbai
*
* @param <T>
*/
public class ResultMessageEncoder<T extends ResultMessage> extends AbstractMessageEncoder<T> {
public ResultMessageEncoder(){
super(Constants.RESULT);
}
/**
* 解析消息的体的方法的
* @param session
* @param message
* @param out
*/
protected void encodeBody(IoSession session ,T message ,IoBuffer out)
{
if(message.isOk())
{
out.putShort((short)Constants.RESULT_OK);
out.putInt(message.getValue());
}else{
out.putShort((short)Constants.RESULT_ERROR);
}
}
/**
*
* @throws Exception
*/
public void dispose()throws Exception{}
}
package cn.com.unutrip.java.opensource.mina.sumup.codec;
/**
* 定义解析的常量接口
* @author longgangbai
*
*/
public interface Constants {
public static final int TYPE_LEN = 2;
public static final int SEQUENCE_LEN = 4;
public static final int HEADER_LEN = TYPE_LEN + SEQUENCE_LEN;
public static final int BODY_LEN = 12;
public static final int RESULT = 0;
public static final int ADD = 1;
public static final int RESULT_CODE_LEN = 2;
public static final int RESULT_VALUE_LEN = 4;
public static final int ADD_BODY_LEN = 4;
public static final int RESULT_OK = 0;
public static final int RESULT_ERROR = 1;
}
package cn.com.unutrip.java.opensource.mina.sumup.codec;
import org.apache.mina.filter.codec.demux.DemuxingProtocolCodecFactory;
import cn.com.unutrip.java.opensource.mina.sumup.message.AddMessage;
import cn.com.unutrip.java.opensource.mina.sumup.message.ResultMessage;
/**
* 加密解密的解析工厂类
* @author longgangbai
*
*/
public class SumUpProtocolCodecFactory extends DemuxingProtocolCodecFactory {
public SumUpProtocolCodecFactory(boolean server)
{
//注册服务端的操作
// 备注客户端和服务端的解析和加密的相反的消息体
if(server)
{
super.addMessageDecoder(AddMessageDecoder.class);
super.addMessageEncoder(ResultMessage.class, ResultMessageEncoder.class);
//注册客户端的操作
}else{
super.addMessageEncoder(AddMessage.class,AddMessageEncoder.class);
super.addMessageDecoder(ResultMessageDecoder.class);
}
}
}
消息实体类:
package cn.com.unutrip.java.opensource.mina.sumup.message;
import java.io.Serializable;
/**
* 各种信息的基类
* @author longgangbai
*
*/
public abstract class AbstractMessage implements Serializable {
private int sequence;
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
}
package cn.com.unutrip.java.opensource.mina.sumup.message;
/**
* 发送信息的模型
* @author longgangbai
*
*/
public class AddMessage extends AbstractMessage {
/**
*
*/
private static final long serialVersionUID = 1L;
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String toString(){
return getSequence()+"ADD ( "+value+")";
}
}
package cn.com.unutrip.java.opensource.mina.sumup.message;
/**
* 存储结构的模型的信息
* @author longgangbai
*
*/
public class ResultMessage extends AbstractMessage {
/**
*
*/
private static final long serialVersionUID = 1L;
private boolean ok;
private int value;
public boolean isOk() {
return ok;
}
public void setOk(boolean ok) {
this.ok = ok;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String toString(){
if(ok){
return getSequence()+" :RESULT ( " +value+")";
}else{
return getSequence()+" :RESULT ( ERROR)";
}
}
}
文章尚未写完,请关注下文。