Summary of the invention
The objective of the invention is at the packing of existing network bytes and the method complexity that unpacks, in the programming model complexity, protocol fields needs under the situation of often change, use inconvenience, can not satisfy the problem that simple, easy expansion requires, propose a kind of protocol Data Unit PDU implementation method that is used for the network bytes packing and unpacks that solves the complex network programming.
Technical scheme of the present invention is:
A kind of protocol Data Unit PDU implementation method that is used for the network bytes packing and unpacks, it may further comprise the steps:
(a). define the expandable mark language XML field configuration file of required host-host protocol, comprise order, length and the data type of field in the host-host protocol;
(b). packing: read the field configuration file of this agreement, obtain the data type and the data length of each field, the java fundamental type numerical value of this each field of agreement is set then, read all fields of this agreement successively:
If this field is the java fundamental type, the java fundamental type numerical value of this field is converted to byte data; If this field is a son bag type, read all fields of son bag, be packaged into byte data successively; Travel through all fields of this agreement, obtain final byte arrays;
(c). unpack: the byte arrays that acquisition need unpack, according to the protocol fields configuration file that reads in advance, obtain the data length of an only field:
If the java basic data type that this field is is converted to predefined java basic data type with byte data; If the type of this field is a son bag type, resolve all fields of this child bag successively, obtain the java basic data type; Travel through all fields of this agreement, resolve successively and obtain the java basic data type.
Beneficial effect of the present invention:
The invention solves the programming model complexity that exists in the legacy network programming, the problem of protocol fields change inconvenience provides the packing of a kind of network bytes stream simple, easily expansion, unpacking method.
The present invention improves the efficient of network bytes stream programming, make the network bytes packing, unpack and become simple; Shielded the difference of high low byte, good Debugging message is provided and unified traffic monitoring is provided.
Embodiment
The present invention is further illustrated below in conjunction with drawings and Examples.
Define order, length, the data type of field in the agreement by the XML configuration file.The complexity read-write of shielding original data type is automatically converted to intelligible basic data type with it, realizes that high low byte preface arbitrarily changes, and can adapt to protocol configuration flexibly.
Shown in figure one, the relation in each object in the PDU implementation method has been described, relevant noun is described as follows:
Protocol object (Protocol): abstract by the byte stream that one or more PDU objects are formed, can represent a complete agreement, such as the MML agreement, the SMPP agreement
Son bag (PduBean): for the distincter protocol object of organizing, perhaps reuse protocol definition, can use the form of son bag to organize protocol fields
Protocol fields object (PduField): not subdivisible PDU object, the Field Definition of an atom of expression
PDU: interface definition, protocol object, Zi Bao, protocol fields all realize the IPdu interface, can realize self-contained between such object like this, can think a kind of realization of component pattern.
During concrete enforcement:
Definition, the exploitation how the example explanation is used the network packing, separated packet technology realization agreement that be embodied as with a MML agreement.
One, as shown in the table as the byte that requires in the agreement:
The message opening flag |
Verification and |
Operation information |
The affairs head |
Can the thread of a conversation |
Message header |
Message-length |
The message opening flag |
←
The message sending order
Two, definition protocol configuration file is as follows:
<?xml?version=″1.0″encoding=″UTF-8″?>
<zsmart>
<protocol>
<bufferDefine?name=″MML″>
<!--defining main message body--〉
<buffer?id=″MML_REQ″seq=″1″fieldname=″MsgBeginFlag″type=″5″
Length=" 4 " defaultValue=" ' SC " ' comments=" message opening flag "/〉
<buffer?id=″MML_REQ″seq=″2″fieldname=″TotalLen″type=″5″
Length=" 4 " defaultValue=" 0000 " comments=" total length field "/〉
<buffer?id=″MML_REQ″seq=″3″fieldname=″msgHeader″type=″0″
Length=" 4 " defaultValue=" " comments=" message header, son bag "/〉
<buffer?id=″MML_REQ″seq=″4″fieldname=″MML_SessionHeader″
Type=" 0 " length=" 4 " defaultValue=" ' SC " ' comments=" the meeting thread of a conversation, son bag "/〉
<buffer?id=″MML_REQ″seq=″5″fieldname=″MML_transHeader″
Type=" 0 " length=" 4 " defaultValue=" ' SC " ' comments=" affairs head, son bag "/〉
<buffer?id=″MML_REQ″seq=″6″fieldname=″MML_CMD″type=″5″
Length=" 0 " defaultValue=" " comments=" MML instruction "/〉
<buffer?id=″MML_REQ″seq=″7″fieldname=″CheckSum″type=″5″
Length=" 8 " defaultValue=" 9B94EDA4 " comments=" verification and "/〉
<!--definition message chieftain bag--〉
<buffer?id=″msgHeader″seq=″1″fieldname=″version″type=″5″
Length=" 4 " defaultValue=" 1.00 " comments=" version number "/〉
<buffer?id=″msgHeader″seq=″2″fieldname=″endFlag″type=″5″
Length=" 8 " defaultValue=" JS123456 " comments=" end mark "/〉
<buffer?id=″msgHeader″seq=″3″fieldname=″serviceName″type=″5″
Length=" 8 " defaultValue=" PPS " comments=" Service name "/〉
<!--definition session chieftain bag--〉
<buffer?id=″MML_SessionHeader″seq=″1″fieldname=″session_id″
Type=" 5 " length=" 8 " defaultValue=" 00000000 " comments=" session id "/〉
<buffer id=″MML_SessionHeader″ seq=″2″
fieldname=″session_control_word″type=″5″length=″6″defaultValue=″DlgCon″
Comments=" session control word "/〉
<buffer?id=″MML_SessionHeader″seq=″3″fieldname=″session_reserved″
Type=" 5 " length=" 4 " defaultValue=" FFFF " comments=" session reserved word "/〉
<!--definition affairs chieftain bag--〉
<buffer?id=″MML_transHeader″seq=″1″fieldname=″trans_id″type=″5″
Length=" 8 " defaultValue=" 00000001 " comments=" affairs ID "/〉
<buffer?id=″MML_transHeader″seq=″2″fieldname=″trans_control_word″
Type=" 5 " length=" 6 " defaultValue=" TxBeg " comments=" affairs control word "/〉
<buffer?id=″MML_transHeader″seq=″3″fieldname=″trans_reserved″
Type=" 5 " length=" 4 " defaultValue=" FFFF " comments=" affairs reserved word "/〉
<!--definition heartbeat message--〉
<buffer?id=″MML_HEART″seq=″1″fieldname=″MsgBeginFlag″
Type=" 5 " length=" 4 " defaultValue=" ' SC " ' comments=" message opening flag "/〉
<buffer?id=″MML_HEART″seq=″2″fieldname=″TotalLen″type=″5″
Length=" 4 " defaultValue=" 0004 " comments=" total length field "/〉
<buffer?id=″MML_HEART″seq=″3″fieldname=″msgBody″type=″5″
Length=" 4 " defaultValue=" HBHB " comments=" total length field "/〉
<buffer?id=″MML_HEART″seq=″4″fieldname=″CheckSum″type=″5″
Length=" 8 " defaultValue=" 00000000 " comments=" verification and "/〉
</bufferDefine>
</protocol>
</zsmart>
Profile Description:
1, bufferDesine: define a procotol bag
2, buffer: define a single protocol fields
3, Id: define this field and belong to that son bag
4, Seq: the serial number in the child bag (in a son bag, sequence number can not repeat)
5, Fieldname: field name, in a son bag, field name can not repeat
Type: corresponding java data type 1:Byte 2:Short 3:Int 4:Long 5:String (variable length) 6:byte[] array 7:CString, with the character string of NULL end
7, length: field length, if the character string of variable length is made as 0 with length, if field type is Byte, Short, Int, Long can use the default-length of java
8, defaultValue: default value, can the field default value be set by this field, if do not give this field settings, will use predefined default value during packing.
Owing to specified the length and the order of each field when protocol definition, all PDU can be according to finishing automatic packing in proper order and unpacking.
Three, in program, quote protocol object, finish protocol packing, unpack.
Packing process is as follows:
Read configuration file, obtain data type, the data length of a field; The java fundamental type numerical value of each field is set; Java fundamental type numerical value with each field is converted to byte data successively; If this field is a son bag type, obtain all fields that change the son bag, be packaged into byte data successively.Travel through all fields, obtain final byte arrays.
It is as follows to separate packet procedures:
The byte arrays that acquisition need unpack according to the protocol fields configuration of reading in advance, obtains the data length of an only field, and byte data is converted to predefined java basic data type; Similarly,, resolve all fields of this child bag successively, obtain the java basic data type if the type of this field is a son bag field.Travel through all fields, resolve successively and obtain the java basic data type.
Four, the PDU interface programming for example
The simple protocol packing
// acquisition MML heartbeat protocol definition
Protocol?mmlReq=ProtocolTools.getProtocol(″MML.MML_HEART″,1);
// the msgBody field value is set, its residual value uses the default configuration of configuration file
mmlReq.getPduField(″msgBody″).setValue(″HBHB″);
// packetizes messages
byte[]heartBytes=mmlReq.pack();
logger.info(″MML Heart ″+heartBytes.length+″Bytes
″+PubFunc.hexToString(heartBytes));
// print protocol bag details
logger.info(mmlReq);
Top code has been demonstrated the process of using the PDU programming model to pack, can see, and utilizes PDU programming model framework, can realize protocol packing very easily.Promote the efficient of programming.
Execution result
-MML?Heart?16Bytes?27?53?43?27?30?30?30?30?48?42?48?42?31?32?32?32
-mmlHeart:
,[MsgBeginFlag,S,′SC′,,len=4,Hex?=27 53 43
27 ],[TotalLen,S,0000,,len=4,Hex=30 30 30
30 ],[msgBody,S,HBHB,,len=4,Hex=48 42 48
42],[CheckSum,S,1222,,len=4,Hex=31?32?32?32]
Top output result has shown title, length, corresponding java value, 16 hex value of each protocol fields, contrasts 16 binary information of whole bag, can obtain needed Debugging message very easily.
The part that the present invention does not relate to prior art that maybe can adopt all same as the prior art is realized.