⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagedecoder2.java

📁 利用Java Socket写的一段通讯协议
💻 JAVA
字号:
package com.ict.netcom2.message;

import java.nio.*;
import com.ict.netcom2.hardware.*;
import com.ict.netcom2.parser.*;
import com.ict.netcom2.task.*;

public class MessageDecoder2 {

	public static int decodeConnectAck(byte[] body) {
		int status = body[0] & 255;
		return status;
	}

	/**
	 * 解析NetPro_Connect(NetPro->NetCom)消息
	 * @return int
	 */
	public static int decodeConnect(byte[] body) {
		ByteBuffer buf = ByteBuffer.wrap(body);
		
		buf.getInt();
		buf.getInt();
		
		int netproId = buf.getInt();
		int version = buf.get() & 255;
		int len = buf.get() & 255;
		
		byte[] b = new byte[len];
		buf.get(b);
		String comm = new String(b);
		
		System.out.println("[decodeConnect]netproId:" + netproId + "  version:"
				+ version + "  communityLen:" + len + "  community:" + comm);

		return netproId;
	}


	public static int decodeRegisterAck(byte[] body) {
		int status = body[0] & 255;
		return status;
	}

	public static int decodeSetKey(byte[] body) {
		int status = body[0] & 255;
		return status;
	}

	/**
	 * @return
	 */
	public static SysInfo decodeQuerySysInfoRep(byte[] body) {		
		SysInfo info = new SysInfo();
		ByteBuffer buf = ByteBuffer.wrap(body);

		info.netproType = buf.get() & 255;
		
		int b0 = buf.get() & 255;
		int b1 = buf.get() & 255;		
		info.cpuUtility = Float.valueOf(b0 + "." + Integer.toHexString(b1));
		
		b0 = buf.get() & 255;
		b1 = buf.get() & 255;
		info.memUtility = Float.valueOf(b0 + "." + Integer.toHexString(b1));
		
		info.temperature = buf.get() & 255;

		if (info.netproType == NetProType.NetPro100) {
			info.statusPort1 = buf.get() & 255;
			info.statusPort2 = buf.get() & 255;
		} 
		else if (info.netproType == NetProType.NetPro3000) {
			info.cardNum = buf.get() & 255;

			for (int i = 0; i < info.cardNum; i++) {
				info.addCard(buf);
			}
		}

		System.out.println(info);
		
		return info;
	}

	public static QueryTaskRep decodeQueryTaskRep(byte[] body) {
		ByteBuffer buf = ByteBuffer.wrap(body);
		QueryTaskRep rep = new QueryTaskRep();
		
		buf.get();

		// little-endian from netpro
		buf.order(ByteOrder.LITTLE_ENDIAN);
		
		// no result number info...
		int num = (buf.capacity() - 1) / 6;
		rep.taskNum = num;
		int taskId, taskType, taskStatus = -1;
		for (int i = 0; i < num; i++) {			
			taskId = buf.getInt();			
			taskType = buf.get() & 255;
			taskStatus = buf.get() & 255;
			rep.addTask(taskId, taskType, taskStatus);

		}
		rep.taskListType = taskStatus;
		return rep;
	}

	public static QueryThreadRep decodeQueryThreadRep(byte[] body) {
		ByteBuffer buf = ByteBuffer.wrap(body);
		QueryThreadRep rep = new QueryThreadRep();
		rep.threadsNum = buf.get() & 255;
		
		System.out.println("[decodeQueryThget()]thget num: " + rep.threadsNum);
		
		for (int i = 0; i < rep.threadsNum; i++) {
			//TODO endian???
			int thgetId = buf.getChar();
			int taskId = buf.getChar();
			rep.addThreadInfo(thgetId, taskId);

			System.out.println("[ThgetID]" + thgetId + "-[TaskID]" + taskId);
		}

		return rep;
	}

	public static int[] decodeAddTaskAck(byte[] body) {
		ByteBuffer buf = ByteBuffer.wrap(body);
		int[] rt = new int[2];
		rt[0] = buf.getInt();
		rt[1] = buf.get() & 255;
		
		System.out.println("[decodeAddTaskAck]TaskId:" + rt[0] + "   addAck:"
				+ rt[1]);

		return rt;
	}

	public static int[] decodeStopTaskAck(byte[] body) {
		ByteBuffer buf = ByteBuffer.wrap(body);
		int[] rt = new int[2];
		rt[0] = buf.getInt();
		rt[1] = buf.get() & 255;
		return rt;
	}

	public static GetResultAck decodeGetResultAck(byte[] body, Task task) {
		ByteBuffer buf = ByteBuffer.wrap(body);
		GetResultAck ack = new GetResultAck();

		ack.authCode = buf.getInt();
		ack.status = buf.get() & 255;

		// TODO status handle
		if (ack.status == 1) {
			System.out.println("No result.");
			return ack;
		} else if (ack.status == 2) {
			System.out.println("Permission denied.");
			return ack;
		} else if (ack.status != 0) {
			System.out.println("Other error.");
			return ack;
		}

		int oidLen = buf.get() & 255;
		byte[] b = new byte[oidLen];
		buf.get(b);
		ack.oid = new String(b);

		// Is last byte
		buf.get();
		
		ack.resultLen = buf.getInt();

		b = new byte[ack.resultLen];
		buf.get(b);
		ack.result = ResultParser.parseMesureResult(task.getType(), b);
		ack.result.setTaskType(task.getType());

		System.out.println("[decodeGetResultAck]:\n\t" + ack);

		return ack;
	}

	public static Notify decodeNotify(byte[] body) {

		ByteBuffer buf = ByteBuffer.wrap(body);
		int notifyType = buf.getInt();
		
		byte[] b = new byte[body.length - 8];
		buf.get(b, 8, b.length);
		
		if (notifyType == CommandID.NetPro_TaskStatusNotify) {
			return decodeTaskStatusNotify(b);
		} else if (notifyType == CommandID.NetPro_EventNotify) {
			return decodeEventNotify(b);
		}

		return null;
	}

	public static TaskStatusNotify decodeTaskStatusNotify(byte[] body) {
		ByteBuffer buf = ByteBuffer.wrap(body);
		TaskStatusNotify ntf = new TaskStatusNotify();
		
		ntf.notifyType = CommandID.NetPro_TaskStatusNotify;
		ntf.netproId = buf.getInt();
		ntf.taskId = buf.getInt();
		ntf.statusInfo = buf.get() & 255;

		System.out.println("[decodeTaskStatusNotify]netproId:" + ntf.netproId
				+ "  taskId:" + ntf.taskId + "  statusInfo:"
				+ TaskStatusTransType.getType(ntf.statusInfo));
		
		return ntf;
	}

	public static EventNotify decodeEventNotify(byte[] body) {
		ByteBuffer buf = ByteBuffer.wrap(body);
		EventNotify ntf = new EventNotify();
		
		ntf.notifyType = CommandID.NetPro_EventNotify;
		ntf.netproId = buf.getInt();
		ntf.priority = buf.get() & 255;
		ntf.eventCode = buf.get() & 255;
		
		int len = buf.getInt();
		byte[] b = new byte[len];
		buf.get(b);
		ntf.message = new String(b);

		System.out.println("[decodeEventNotify]netproId:" + ntf.netproId
				+ "  priority:" + ntf.priority + "  eventCode:" + ntf.eventCode
				+ "  message:" + ntf.message);

		return ntf;
	}

	public static void main(String[] args) {
		ByteBuffer bf = ByteBuffer.wrap(new byte[4]);
		bf.putInt(1);
		byte[] body = bf.array();
		System.out.println(body[0]);
		System.out.println(body[1]);
		System.out.println(body[2]);
		System.out.println(body[3]);
		System.out.println(Long.parseLong(Integer.toBinaryString(-1), 2));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -