Receive Protocol Handler Design Pattern

来源:百度文库 编辑:神马文学网 时间:2024/04/29 21:49:02
Receive Protocol Handler Pattern
Receive Protocol Handler Documentation
Intent
Provide a common framework for receive direction sliding window protocolimplementation.
Also Known As
Receive Protocol Design Pattern
Receiver Pattern
Sliding Window Receiver Pattern
Motivation
Different sliding window protocols have a lot of similarity. This similaritycan be captured in a common design pattern for their implementation. Here wewill focus on the receive side of the protocol.
Applicability
Receive Protocol Handler Pattern can be used to implement protocols at anylayer.
Structure
This pattern is implemented by just one class, Receive Protocol Handler. Thisclass receives the packets from the other end and performs the followingoperations:
Check validity of the received packet
Ask Transmit Protocol Handler to acknowledge the received packet
Check if the remote end has acknowledged that was sent by Transmit Protocol Handler.
Inform Transmit Protocol Handler about acknowledged packet.
To achieve this functionality, the following sequence numbers are maintained:
Next Expected Sequence Number: Transmit sequence number expected in the next packet from the remote end.
Last Acknowledged Sequence Number: Last receive sequence number received from the remote end. This sequence number is used by the remote end to acknowledge packets.
Participants
The Transmit and Receive Protocol Handlers are the main participants in thispattern. The received messages are added to the Receive Queue. The receivedmessage will be picked by the next higher layer.
Collaboration
The following diagram shows the relationship and collaboration between the Transmit Protocol Handler,Receive Protocol Handler and the Receive Queue classes.

Consequences
Using this pattern simplifies the implementation of receive end point of asliding window protocol. The design is flexible enough to adjust to anyprotocol.
Implementation
Handling of the received message is described below:
Receive Protocol Handler's "Handle Received Packet" is invoked.
Compare the packet's transmit sequence number with the expected sequence number.
If the sequence number matches, take the following actions: Request Transmit Sequence Handler to acknowledge the just received packet.
Add the message to the receive queue
Modulo increment the next expected sequence number
Compare the packet's receive sequence number with the last acknowledged sequence number. (Change in sequence number implies that an acknowledgement has been received from the other end).
If a change is detected in the received sequence number, the following action is taken: Store the new receive sequence number as the last acknowledged sequence number.
Inform the Transmit Protocol Handler about the acknowledgements received from the other end.
Sample Code and Usage
Here we present the code for a typical implementation of this pattern. Codeis provided for the Receive Protocol Handler class. Receive Queue can beimplemented using themessagequeue pattern.
Receive Protocol Handler
00009 #include "Transmit_Protocol_Handler.h" 00010 00019 00021 { 00022 enum {L2_HEADER_LENGTH=8}; 00023 ; 0002600030 intm_last_Acknowledged_Sequence_Number; 0003100033Transmit_Protocol_Handler *m_p_Transmit_Protocol_Handler; 0003400036Protocol_Layer *m_p_Layer; 00037 00038 public: 00039 0005700058 voidHandle_Received_Packet(Datagram *p_Packet) 00059 { 00060 // Extract the header needed by this layer 00061 p_Packet->Extract_Header(L2_HEADER_LENGTH); 00062 00063 // Compare the packet's transmit sequence number with 00064 // the expected sequence number 00065 if (p_Packet->Get_Transmit_Sequence_Number() ==m_next_Expected_Sequence_Number) 00066 { 00067 // Request Transmit Sequence Handler to acknowledge 00068 // the just received packet 00069m_p_Transmit_Protocol_Handler->Handle_Send_Ack_Request(m_next_Expected_Sequence_Number); 00070 00071 // Pass the message to the higher layer. The message is passed only if the upper layer 00072 // has been specified. 00073Protocol_Layer *p_Upper_Layer =m_p_Layer->Get_Upper_Layer(); 00074 if (p_Upper_Layer) 00075 { 00076 p_Upper_Layer->Handle_Receive(p_Packet); 00077 } 00078 00079 // Modulo increment the next expected sequence number 00080 Modulo_Increment(m_next_Expected_Sequence_Number); 00081 } 00082 00083 // Compare the packet's receive sequence number with the 00084 // last acknowledged sequence number. (Change in sequence number 00085 // implies that an acknowledgement has been received from the other end) 00086 if (p_Packet->Get_Receive_Sequence_Number() !=m_last_Acknowledged_Sequence_Number) 00087 { 00088 // Update the last acknowledged sequence number 00089m_last_Acknowledged_Sequence_Number = p_Packet->Get_Receive_Sequence_Number(); 00090 00091 // Inform the Transmit Protocol Handler about the 00092 // acknowledgements received from the other end 00093m_p_Transmit_Protocol_Handler->Handle_Received_Ack_Notification( 00094m_last_Acknowledged_Sequence_Number); 00095 } 00096 } 00097 0010600107Receive_Protocol_Handler(Protocol_Layer *p_Layer) : 00108m_p_Layer(p_Layer) 00109 { 00110 } 00111 };
Known Uses
Datalink layer sliding window protocol implementation
Higher layer sliding window protocol implementation
Related Patterns
Transmit Protocol Handler Pattern
Protocol Layer Design Pattern
Serial Port Design Pattern
High Speed Serial Port Design Pattern
Protocol Packet
Explore More...
Receive Protocol Handler Documentation