All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.tom.protocol.co.TMessage Maven / Gradle / Ivy

The newest version!
package cn.tom.protocol.co;


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import cn.tom.kit.IoBuffer;
import cn.tom.transport.Id;

public class TMessage extends Message implements Id{
	
	public List> params  = new ArrayList<>();
	
	public TMessage() {
		Header header = new Header();
		setHeader(header);
	}
	
	public TMessage(int ask, int compress) {
		Header header = new Header();
		header.setAsk((byte)ask);
		header.setCompress((byte)compress);
		setHeader(header);
	}
	
	
	public void serialize() {
		if(params.size() == 0) return;
		Row row = new Row();
		ArrayList fields = new ArrayList<>();
		for (Map.Entry m : params.get(0).entrySet()) {
			fields.add(new Field(m.getKey(), ProtoUtil.getTypeByObj(m.getValue())));
		}
		for (Map map : params) {
			for (Map.Entry m : map.entrySet()) {
				Object val = m.getValue();
				row.setCell(new Row.Cell(val));
			}
		}

		setFields(fields.toArray(new Field[0]));
		setBody(row);
	}
	
	
	public void deserialize(IoBuffer buf) {
		buf.readByte(); // 0xff 头标识符
		int len = buf.readInt(); // len 4   
		long id = buf.readLong(); // id 8  
		byte compress = buf.readByte(); //c 1
		byte ask = buf.readByte(); //1 ask 0-ask 1-resp   
		getHeader().setCompress(compress);
		getHeader().setId(id);
		getHeader().setAsk(ask);
		
		int mlentgh = buf.readByte() & 0xff; //1
		len = len -(8+1+1+1);
		if(mlentgh >0){
			len = len - mlentgh;
			byte method[] = buf.readBytes(mlentgh);
			getHeader().setMethod(method);
		}
		
		IoBuffer bodybuf = buf;
		if(compress == 1){ // compress 解压缩  
			byte [] unbyte = uncompress(buf.readBytes(len));
			bodybuf = IoBuffer.wrap(unbyte);
		}
		if(len == 0) return;
		int pos = bodybuf.position();
		///// start field 
		int fLen = bodybuf.readShort();  //
		ArrayList fields = new ArrayList<>();
		for (int i = 0; i < fLen; i++) {
			int fcLen = bodybuf.readByte();
			String columnName = new String(bodybuf.readBytes(fcLen));
			int typeModifier  = bodybuf.readShort();  // type
			fields.add(new Field(columnName, typeModifier));
		}
		//// start rows
		while(bodybuf.position() - pos  map = new HashMap<>();
			for(Field field : fields){
				int cLen = bodybuf.readInt();
				byte[] cbytes = bodybuf.readBytes(cLen);
				int type = field.getTypeModifier();
				map.put(field.getColumnName(), ProtoUtil.getValueByType(cbytes, type));
			}
			params.add(map);
		}
	}

	public List> getParams() {
		return params;
	}

	public void setParams(List> params) {
		this.params = params;
	}
	
	public void setParam(Map map) {
		this.params.add(map);
	}
	
	public Map getParam() {
		if(params.size() > 0){
			return params.get(0);
		}
		return Collections.emptyMap();
	}


	@Override
	public String getMsgId() {
		if(getHeader().getId() ==0 ) return null;
		return Long.toString(getHeader().getId()) ;
	}
	
	@Override
	public Id setMsgId(String id) {
		getHeader().setId(Long.parseLong(id));
		return this;
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy