📄 messagedecoder.java
字号:
package com.ict.netcom2.trash;
import java.io.*;
import java.nio.*;
import com.ict.netcom2.hardware.*;
import com.ict.netcom2.parser.*;
import com.ict.netcom2.task.*;
import com.ict.netcom2.message.*;
public class MessageDecoder {
DataInputStream dis;
public MessageDecoder(DataInputStream dis) {
this.dis = dis;
}
public void close() {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
public MessageDecoder(PipedInputStream in) {
dis = new DataInputStream(in);
}
*/
public int decodeConnectAck() {
int status = 4;
try {
dis.readInt();
dis.readInt();
dis.readInt();
status = dis.readByte()&255;
} catch (IOException e) {
e.printStackTrace();
}
return status;
}
/**
* 解析NetPro_Connect(NetPro->NetCom)消息
* @return
*/
public int decodeConnect() {
int netproId = -1;
try {
dis.readInt();
dis.readInt();
dis.readInt();
netproId = dis.readInt();
int version = ((int)dis.readByte())&255;
int len = ((int)dis.readByte())&255;
byte[] b = new byte[len];
dis.read(b);
String key = new String(b);
System.out.println("[decodeConnect]netproId:"
+netproId+" version:"+version+" keyLen:"+len+" key:"+key);
} catch (IOException e) {
e.printStackTrace();
}
return netproId;
}
/**
*
*
*/
public void decodeDisconnectAck() {
try {
dis.readInt();
dis.readInt();
dis.readInt();
} catch (IOException e) {
e.printStackTrace();
}
}
public int decodeRegisterAck() {
int status = -1;
try {
dis.readInt();
dis.readInt();
dis.readInt();
status = dis.readByte()&255;
} catch (IOException e) {
e.printStackTrace();
}
return status;
}
public void docodeUnRegisterAck() {
try {
dis.readInt();
dis.readInt();
dis.readInt();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean decodeLivingCheckAck() {
try {
dis.readInt();
dis.readInt();
dis.readInt();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public int decodeSetKey() {
int status = 2;
try {
dis.readInt();
dis.readInt();
dis.readInt();
status = ((int)dis.readByte())&255;
} catch (IOException e) {
e.printStackTrace();
}
return status;
}
/**
* @return
*/
public SysInfo decodeQuerySysInfoRep() {
SysInfo info = new SysInfo();
try {
dis.readInt();
dis.readInt();
dis.readInt();
info.netproType = dis.readByte()&255;
int b0 = dis.readByte()&255;
int b1 = dis.readByte()&255;
info.cpuUtility = Float.valueOf(b0 + "."
+Integer.toHexString(b1));
b0 = dis.readByte()&255;
b1 = dis.readByte()&255;
info.memUtility = Float.valueOf(b0 + "."
+Integer.toHexString(b1));
info.temperature = dis.readByte()&255;
} catch (IOException e) {
e.printStackTrace();
}
if (info.netproType == NetProType.NetPro100) {
System.out.println("netpro100 ...");
try {
info.statusPort1 = dis.readByte()&255;
info.statusPort2 = dis.readByte()&255;
} catch (IOException e) {
e.printStackTrace();
}
}
else if (info.netproType == NetProType.NetPro3000) {
System.out.println("netpro3000 ...");
try {
info.cardNum = dis.readByte()&255;
} catch (IOException e) {
e.printStackTrace();
}
for (int i=0; i<info.cardNum; i++) {
//info.addCard(dis);
}
}
return info;
}
public QueryTaskRep decodeQueryTaskRep() {
QueryTaskRep rep = new QueryTaskRep();
try {
long len = Long.parseLong(Integer.toBinaryString(
dis.readInt()), 2);
dis.readInt();
dis.readInt();
dis.readByte();
// no result number info...
long num = (len - 13)/6;
rep.taskNum = num;
int taskId, taskType, taskStatus = -1;
for (long i=0; i<num; i++) {
byte[] b = new byte[4];
dis.read(b);
// little-endian from netpro
ByteBuffer bf = ByteBuffer.wrap(b);
bf.order(ByteOrder.LITTLE_ENDIAN);
taskId = bf.getInt();
taskType = ((int)dis.readByte())&255;
taskStatus = ((int)dis.readByte())&255;
rep.addTask(taskId, taskType, taskStatus);
System.out.println("[decodeQueryTaskRep]taskId: "+taskId
+" taskType: "+TaskType.getType(taskType)
+" taskStatus: "+TaskListType.getType(taskStatus));
}
rep.taskListType = taskStatus;
} catch (IOException e) {
e.printStackTrace();
}
return rep;
}
public QueryThreadRep decodeQueryThreadRep() {
QueryThreadRep rep = new QueryThreadRep();
try {
/*
int len = 128;
byte[] b = new byte[len];
dis.read(b);
for (int i=0; i<len; i++) {
String str = Byte.toString(b[i]);
System.out.print(str+" ");
}
*/
dis.readInt();
dis.readInt();
dis.readInt();
rep.threadsNum = ((int)dis.readByte())&255;
System.out.println(
"[decodeQueryThread()]thread num: "+rep.threadsNum);
for (int i=0; i<rep.threadsNum; i++) {
int threadId = dis.readChar();
int taskId = dis.readChar();
System.out.println("[ThreadID]"+threadId+"-[TaskID]"+taskId);
rep.addThreadInfo(threadId, taskId);
}
} catch (IOException e) {
e.printStackTrace();
}
return rep;
}
public int[] decodeAddTaskAck() {
int[] rt = new int[2];
try {
dis.readInt();
dis.readInt();
dis.readInt();
rt[0] = dis.readInt();
rt[1] = dis.readByte()&255;
System.out.println("[decodeAddTaskAck]TaskId:"+rt[0]+" addAck:"+rt[1]);
} catch (IOException e) {
e.printStackTrace();
}
return rt;
}
public int[] decodeStopTaskAck() {
int[] rt = new int[2];
try {
dis.readInt();
dis.readInt();
dis.readInt();
rt[0] = dis.readInt();
rt[1] = dis.readByte()&255;
} catch (IOException e) {
e.printStackTrace();
}
return rt;
}
public GetResultAck decodeGetResultAck(Task task) {
GetResultAck ack = new GetResultAck();
try {
dis.readInt();
dis.readInt();
dis.readInt();
ack.authCode = dis.readInt();
ack.status = ((int)dis.readByte())&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 = ((int)dis.readByte())&255;
byte[] b = new byte[oidLen];
dis.read(b);
ack.oid = new String(b);
dis.readByte();
ack.resultLen = dis.readInt();
long len = Long.parseLong(Integer.toBinaryString(ack.resultLen), 2);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (long i=0; i<len; i++) {
baos.write(dis.readByte());
}
byte[] result = baos.toByteArray();
ack.result = ResultParser.parseMesureResult(task.getType(), result);
ack.result.setTaskType(task.getType());
System.out.println("[decodeGetResultAck]:\n\t"+ack);
} catch (IOException e) {
e.printStackTrace();
}
return ack;
}
/*
public Notify decodeNotify() {
try {
dis.readInt();
int notifyType = dis.readInt();
dis.readInt();
if (notifyType == CommandID.NetPro_TaskStatusNotify) {
return decodeTaskStatusNotify();
}
else if (notifyType == CommandID.NetPro_EventNotify) {
return decodeEventNotify();
}
} catch (EOFException e) {
throw new RuntimeException(e);
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
public TaskStatusNotify decodeTaskStatusNotify() {
TaskStatusNotify ntf = new TaskStatusNotify();
ntf.notifyType = CommandID.NetPro_TaskStatusNotify;
try {
ntf.netproId = dis.readInt();
ntf.taskId = dis.readInt();
ntf.statusInfo = ((int)dis.readByte())&255;
System.out.println("[decodeTaskStatusNotify]netproId:"
+ ntf.netproId + " taskId:"
+ ntf.taskId + " statusInfo:"
+ TaskStatusTransType.getType(ntf.statusInfo));
} catch (IOException e) {
e.printStackTrace();
}
return ntf;
}
public EventNotify decodeEventNotify() {
EventNotify ntf = new EventNotify();
ntf.notifyType = CommandID.NetPro_EventNotify;
try {
ntf.netproId = dis.readInt();
ntf.priority = ((int)dis.readByte())&255;
ntf.eventCode = ((int)dis.readByte())&255;
long len = Long.parseLong(Integer.toBinaryString(
dis.readInt()), 2);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (long i=0; i<len; i++) {
baos.write(dis.readByte());
}
ntf.message = new String(baos.toByteArray());
System.out.println("[decodeEventNotify]netproId:"
+ ntf.netproId + " priority:"
+ ntf.priority + " eventCode:"
+ ntf.eventCode + " message:"
+ ntf.message);
} catch (IOException e) {
e.printStackTrace();
}
return ntf;
}
*/
public static void main(String[] args) {
ByteBuffer bf = ByteBuffer.wrap(new byte[4]);
bf.putInt(1);
byte[] b = bf.array();
System.out.println(b[0]);
System.out.println(b[1]);
System.out.println(b[2]);
System.out.println(b[3]);
System.out.println(
Long.parseLong(Integer.toBinaryString(-1), 2));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -