Protocol Layer Design Pattern

来源:百度文库 编辑:神马文学网 时间:2024/04/27 23:01:57
Protocol Layer Documentation
Datalink Layer Documentation
Intent
Provide a common framework for implementing different layers of a protocolstack.
Also Known As
Layer Design Pattern
ISO/OSI Layer  Design Pattern
Motivation
A typical protocol layer interfaces with an upper layer and a lower layer ofthe protocol. In most designs there is a lot of dependency between differentlayers of the protocol. This makes the protocol stack implementation rigid andinflexible. Changes in one layer often percolate to other layers. The ProtocolLayer Design Pattern addresses these limitations by decoupling the individualprotocol layers.
Applicability
The Protocol Layer Design Pattern can be used to implement different layers of a protocol stack.
It can be also used when different operations on an entity need to be performed in a pipeline fashion. Each stage of the pipeline could be modeled as a layer.
Structure
Different layers of the protocol are structured as shown in the figure on the left hand side. Communication between layers takes place using standard interfaces defined by the Protocol Layer base class. All implementations of the protocol layer inherit from this class. The inheriting layer classes should implement the standard interfaces. The standard interfaces are: Transmit is invoked by the upper layer to transfer a packet to the lower layer.
Handle Receive is invoked by the lower layer to transfer a packet to the upper layer.
The example on the left hand side shows the structure of a three layer protocol stack. The sequence of actions in the Transmit direction are:
The application invokes the Network layer's Transmit method.
The Network layer performs its actions and invokes the Transmit method for the lower layer.
This invokes the Datalink layers transmit method. The Datalink layer performs the layer specific actions and invokes the lower layer's Transmit method.
The Physical layer's Transmit method is invoked. This layer programs the appropriate hardware device and transmits the message.
The receive processing is similar to the transmit processing described above. Here the Handle Receive method is used to transfer the packet between layers.
Participants
The key actors of this design pattern:
Protocol Layer: This is the base class for all protocol layers. The individual layers interface with each other via pointers to this class. The actual type of the upper layer and lower layer classes is not known to the implementers of a certain layer.
Protocol Packet: This class manages addition and removal of headers and trailers for various protocol layers.
Collaboration
The following diagram shows the relationship and collaboration betweenvarious classes needed for the Datalink layer example.

Consequences
Protocol Layer class serves as the foundation for the Protocol Layer designpattern. This class defines the common interface for interactions betweendifferent layers of a protocol. This has several advantages:
The implementation of one layer is completely decoupled from the other layers. The implementers of one layer do not even need to include the header files defining the other layers.
Layers can be added and removed without needing any changes to the code for individual layers. (e.g. an IPSec layer can be added between the IP and Physical layers, without making any changes to the IP or the Physical Layer code).
A single layer could interface with multiple upper or lower layer protocols using the same interface. (e.g. an IP layer implementation could interface with an ATM or Ethernet physical layer. No changes would be needed to the code for the IP layer.
Implementation
The following scenarios are supported by the Datalink Layer example of theProtocol Layer design pattern:
A packet is received from the upper layer
The upper layer uses its "Lower Layer" pointer to invoke the Transmit method for the lower layer. The "Protocol Packet" to be transmitted is passed to the Transmit method.
This invokes the Datalink Layer's Transmit method.
The Datalink Layer passes the "Protocol Packet" to the "Transmit Protocol Handler" object.
The "Transmit Protocol Handler" processes the "Protocol Packet" and adds the datalink layer header to the packet.
The "Transmit Protocol Handler" uses its parent layer to obtain a pointer to the lower layer.
The "Protocol Packet" is passed to the lower layer by invoking the Transmit method.
A packet is received from the lower layer
The lower layer uses its "Upper Layer" pointer to invoke the "Handle Receive" method for the upper layer. The received "Protocol Packet" is passed to the "Handle Receive" method.
This invokes the Datalink Layer's "Handle Receive" method.
The Datalink Layer passes the "Protocol Packet" to the "Receive Protocol Handler" object.
The "Receive Protocol Handler" object uses the parent layer to obtain a pointer to the upper layer.
The "Protocol Packet" is passed to the higher layer by invoking the "Handle Receive" method.
Sample Code and Usage
The code for the Protocol Layer class and the Datalink Layer example ispresented below:
Protocol Layer
00009 #ifndef PROTOCOL_LAYER_H 00010 #define PROTOCOL_LAYER_H 00011 00012 #include 00013 classProtocol_Packet; 00014 00031 00033 { ; 0003900044Protocol_Layer *m_p_Upper_Layer; 00045 00046 public:00052Protocol_Layer() 00053 { 00054m_p_Lower_Layer = NULL; 00055m_p_Upper_Layer = NULL; 00056 } 00057 00066 virtual voidTransmit(Protocol_Packet *p_Packet) = 0; 00067 00076 virtual voidHandle_Receive(Protocol_Packet *p_Packet) = 0; 0007700085 voidSet_Upper_Layer(Protocol_Layer *p_Layer) 00086 {m_p_Upper_Layer = p_Layer; } 00087 0009500096 voidSet_Lower_Layer(Protocol_Layer *p_Layer) 00097 {m_p_Lower_Layer = p_Layer; } 00098 0010600107Protocol_Layer *Get_Upper_Layer() const 00108 { returnm_p_Upper_Layer; } 00109 0011700118Protocol_Layer *Get_Lower_Layer() const 00119 { returnm_p_Lower_Layer; } 00120 00121 }; 00122 #endif
Datalink Layer Header File
00010 #ifndef DATALINK_LAYER_H 00011 #define DATALINK_LAYER_H 00012 00013 #include "Protocol_Layer.h" 00014 #include "Transmit_Protocol_Handler.h" 00015 #include "Receive_Protocol_Handler.h" 00016 00017 classProtocol_Packet; 00018 0002400025 classDatalink_Layer : publicProtocol_Layer 00026 {00028Transmit_Protocol_Handlerm_transmit_Protocol_Handler; 0002900031Receive_Protocol_Handlerm_receive_Protocol_Handler; 00032 00033 public: 00034 00035Datalink_Layer(); 00036 00037 // Process and transmit the packet passed by higher layer. 00038 voidTransmit(Protocol_Packet *p_Packet); 00039 00040 voidHandle_Receive(Protocol_Packet *p_Packet); 00041 00042 }; 00043 00044 #endif
Datalink Layer Source File
00010 #include "Datalink_Layer.h" 00011 #include "Protocol_Packet.h" 00012 0001800019Datalink_Layer::Datalink_Layer() : 00020 m_transmit_Protocol_Handler(this), 00021 m_receive_Protocol_Handler(this) 00022 { } 00023 0003000031 voidDatalink_Layer::Transmit(Protocol_Packet *p_Packet) 00032 { 00033m_transmit_Protocol_Handler.Handle_Transmit_Request((Datagram *)p_Packet); 00034 } 00035 0004200043 voidDatalink_Layer::Handle_Receive(Protocol_Packet *p_Packet) 00044 { 00045m_receive_Protocol_Handler.Handle_Received_Packet((Datagram *)p_Packet); 00046 }
Known Uses
Implementation of ISO-OSI protocol layers.
Modeling and implementing stages of a pipeline.
Related Patterns/Principles
Receive Protocol Handler Pattern
Transmit Protocol Handler Pattern
Protocol Packet
Dependency Inversion Principle
Explore More...
Protocol Layer Documentation
Datalink Layer Documentation