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

com.gateway.utils.ProtoUtils Maven / Gradle / Ivy

package com.gateway.utils;

import java.util.HashMap;

import com.alibaba.fastjson.JSON;
import com.gateway.connector.proto.Cmd;
import com.gateway.connector.proto.Format;
import com.gateway.connector.proto.Proto;
import com.gateway.utils.GzipUtils;
import com.gateway.utils.SeqUtils;
import com.gateway.constant.Constants;

/**
 * 
 * 
 * @author deshuai.kong
 *
 */
public class ProtoUtils {
	public static Proto generateConnect() {
		Proto proto = new Proto();
		proto.setCmd(Cmd.CONNECT);
		proto.setFormat(Format.REQUEST);
		proto.setSeq(SeqUtils.getSeq());

		return proto;
	}

	public static Proto generateHeartbeat(String sid, int seq) {
		Proto proto = new Proto();
		proto.setCmd(Cmd.HEARTBEAT);
		proto.setFormat(Format.SEND);
		proto.setSeq(seq);
		proto.setSessionId(sid);
		return proto;
	}

	public static Proto generateReplySuccess(String sid, int cmd, int seq) {
		Proto proto = new Proto();
		proto.setCmd(cmd);
		proto.setFormat(Format.REPLY);
		proto.setSeq(seq);
		proto.setSessionId(sid);
		HashMap hm = new HashMap<>();
		hm.put("Code", "0000");
		hm.put("Msg", "Success");
		proto.setBody(JSON.toJSONBytes(hm));
		return proto;
	}

	public static Proto generateNotify(String sid, byte[] body) {
		Proto proto = new Proto();
		proto.setCmd(Cmd.SEND);
		proto.setFormat(Format.NOTIFY);
		proto.setSeq(SeqUtils.getSeq());
		proto.setSessionId(sid);
		proto.setBody(body);

		return proto;
	}
	public static byte[] gzipBody(boolean isGzip,byte[] body) {
		if ( isGzip) {
			return GzipUtils.gzip(body);
		} else {
			return body;
		}
	}

	public static byte[] ungzipBody(boolean isGzip,byte[] body) {
		if (isGzip) {
			return GzipUtils.ungzip(body);
		} else {
			return body;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy