ncc协议x
This commit is contained in:
parent
e8ca05baa4
commit
bb9f1d9f5b
@ -3,20 +3,17 @@ package club.joylink.xiannccda.protocal.x;
|
||||
import club.joylink.xiannccda.ats.message.MessageId;
|
||||
import club.joylink.xiannccda.ats.message.line3.DateTimeUtil;
|
||||
import club.joylink.xiannccda.ats.message.line3.DispatcherReportResponse;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.buffer.UnpooledByteBufAllocator;
|
||||
import club.joylink.xiannccda.ats.message.line3.MessageCons;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import io.netty.buffer.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TestDispatcherReportResponse {
|
||||
public static void main(String[]args){
|
||||
final DispatcherReportResponse response = new DispatcherReportResponse();
|
||||
public static void main(String[]args) throws Exception {
|
||||
final ByteBufAllocator allocator = UnpooledByteBufAllocator.DEFAULT;
|
||||
final ByteBuf body = allocator.buffer(1024);
|
||||
System.out.println("==>>body ridx = "+body.readerIndex()+" widx = "+body.writerIndex());
|
||||
body.writeInt((int) (System.currentTimeMillis() / 1000));
|
||||
body.writeShort(0xff);
|
||||
body.writeShort(MessageId.DISPATCHER_REPORT.idValue());
|
||||
@ -25,14 +22,42 @@ public class TestDispatcherReportResponse {
|
||||
body.writeShort(2);
|
||||
body.writeShort(1);
|
||||
body.writeShort(2);//Count
|
||||
body.readBytes(DateTimeUtil.convert(LocalDateTime.now()));
|
||||
///////////////////////
|
||||
body.writeBytes(DateTimeUtil.convert(LocalDateTime.now()));
|
||||
System.out.println("==>>1body ridx = "+body.readerIndex()+" widx = "+body.writerIndex());
|
||||
//
|
||||
final byte[]userName = new byte[32];
|
||||
Arrays.fill(userName, (byte) '\0');
|
||||
final ByteBuf userNameBuf = Unpooled.wrappedBuffer(userName);
|
||||
userNameBuf.readBytes("调度员1".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
final byte[]userName1 = new byte[32];
|
||||
TestUtil.fill("调度员1", MessageCons.STRING_CHARSET,userName1);
|
||||
body.writeBytes(userName1);
|
||||
System.out.println("==>>2body ridx = "+body.readerIndex()+" widx = "+body.writerIndex());
|
||||
//
|
||||
|
||||
final byte[]logItem1 = new byte[256];
|
||||
TestUtil.fill("调度1日志内容", MessageCons.STRING_CHARSET,logItem1);
|
||||
body.writeBytes(logItem1);
|
||||
System.out.println("==>>3body ridx = "+body.readerIndex()+" widx = "+body.writerIndex());
|
||||
////////////////////////////////////////////
|
||||
//
|
||||
body.writeBytes(DateTimeUtil.convert(LocalDateTime.now().plusMinutes(40)));
|
||||
final byte[]userName2 = new byte[32];
|
||||
TestUtil.fill("调度员2", MessageCons.STRING_CHARSET,userName2);
|
||||
body.writeBytes(userName2);
|
||||
System.out.println("==>>4body ridx = "+body.readerIndex()+" widx = "+body.writerIndex());
|
||||
//
|
||||
final byte[]logItem2 = new byte[256];
|
||||
TestUtil.fill("调度2日志内容", MessageCons.STRING_CHARSET,logItem2);
|
||||
body.writeBytes(logItem2);
|
||||
System.out.println("==>>5body ridx = "+body.readerIndex()+" widx = "+body.writerIndex());
|
||||
//
|
||||
final ByteBuf headLen = allocator.buffer(2);
|
||||
headLen.writeShort(body.readableBytes());//Message_length
|
||||
//
|
||||
final CompositeByteBuf composite = allocator.compositeBuffer();
|
||||
composite.addComponents(true,headLen,body);
|
||||
System.out.println("==>>composite ridx = "+composite.readerIndex()+" widx = "+composite.writerIndex());
|
||||
//解码
|
||||
final DispatcherReportResponse response = new DispatcherReportResponse();
|
||||
response.decode(composite);
|
||||
//
|
||||
System.out.println(JSON.toJSONString(response));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,16 @@
|
||||
package club.joylink.xiannccda.protocal.x;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class TestUtil {
|
||||
/**
|
||||
* 将src通过charset编码后填充到to
|
||||
*/
|
||||
public static void fill(final String src, final Charset charset,final byte[]to){
|
||||
final byte[]srcBuf = src.getBytes(charset);
|
||||
if(to.length<srcBuf.length) throw new RuntimeException("to.length<srcBuf.length");
|
||||
for(int i=0;i<srcBuf.length;i++){
|
||||
to[i]=srcBuf[i];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user