|
Home >
Real-time Mantra >
Design Patterns > Protocol Layer Design Pattern
|
Provide a common framework for implementing different layers of a protocol stack.
A typical protocol layer interfaces with an upper layer and a lower layer of the protocol. In most designs there is a lot of dependency between different layers of the protocol. This makes the protocol stack implementation rigid and inflexible. Changes in one layer often percolate to other layers. The Protocol Layer Design Pattern addresses these limitations by decoupling the individual protocol layers.
![]() |
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:
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 receive processing is similar to the transmit processing described above. Here the Handle Receive method is used to transfer the packet between layers. |
The key actors of this design pattern:
The following diagram shows the relationship and collaboration between various classes needed for the Datalink layer example.

Protocol Layer class serves as the foundation for the Protocol Layer design pattern. This class defines the common interface for interactions between different layers of a protocol. This has several advantages:
The following scenarios are supported by the Datalink Layer example of the Protocol Layer design pattern:
The code for the Protocol Layer class and the Datalink Layer example is presented below:
| Protocol Layer |
00009 #ifndef PROTOCOL_LAYER_H 00010 #define PROTOCOL_LAYER_H 00011 00012 #include <stdio.h> 00013 class Protocol_Packet; 00014 00031 00032 class Protocol_Layer 00033 { 00038 Protocol_Layer *m_p_Lower_Layer; 00039 00044 Protocol_Layer *m_p_Upper_Layer; 00045 00046 public: 00052 Protocol_Layer() 00053 { 00054 m_p_Lower_Layer = NULL; 00055 m_p_Upper_Layer = NULL; 00056 } 00057 00066 virtual void Transmit(Protocol_Packet *p_Packet) = 0; 00067 00076 virtual void Handle_Receive(Protocol_Packet *p_Packet) = 0; 00077 00085 void Set_Upper_Layer(Protocol_Layer *p_Layer) 00086 { m_p_Upper_Layer = p_Layer; } 00087 00095 00096 void Set_Lower_Layer(Protocol_Layer *p_Layer) 00097 { m_p_Lower_Layer = p_Layer; } 00098 00106 00107 Protocol_Layer *Get_Upper_Layer() const 00108 { return m_p_Upper_Layer; } 00109 00117 00118 Protocol_Layer *Get_Lower_Layer() const 00119 { return m_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 class Protocol_Packet; 00018 00024 00025 class Datalink_Layer : public Protocol_Layer 00026 { 00028 Transmit_Protocol_Handler m_transmit_Protocol_Handler; 00029 00031 Receive_Protocol_Handler m_receive_Protocol_Handler; 00032 00033 public: 00034 00035 Datalink_Layer(); 00036 00037 // Process and transmit the packet passed by higher layer. 00038 void Transmit(Protocol_Packet *p_Packet); 00039 00040 void Handle_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 00018 00019 Datalink_Layer::Datalink_Layer() : 00020 m_transmit_Protocol_Handler(this), 00021 m_receive_Protocol_Handler(this) 00022 { } 00023 00030 00031 void Datalink_Layer::Transmit(Protocol_Packet *p_Packet) 00032 { 00033 m_transmit_Protocol_Handler.Handle_Transmit_Request((Datagram *)p_Packet); 00034 } 00035 00042 00043 void Datalink_Layer::Handle_Receive(Protocol_Packet *p_Packet) 00044 { 00045 m_receive_Protocol_Handler.Handle_Received_Packet((Datagram *)p_Packet); 00046 } |
| Explore More... |
|