# E-MQTT: End-to-End Synchronous and Asynchronous Communication Mechanisms in MQTT Protocol
## 3 E-Mqtt
### Detailed Process of End-to-End Communication  
Displayed in Figure 9 are diagrams delineating the execution process of the E-MQTT from the perspective of the publisher. Firstly, the user's publish request, employing QoS 3, is directed to the main thread for processing. Within this main thread are the mandatory _waited_packet_num_ field and the optional _subscriber_id_ field(s). If the publisher configures  
Figure 8: The fixed header of PUBREC, PUBREL, and PUBCOMP packets of E-MQTT. Figure 6: The fixed header of PUBLISH packet of the existing MQTT. Figure 7: The fixed header of PUBREC, PUBREL, and PUBCOMP packets of the existing MQTT. the _waited_packet_num_ field to 0 (asynchronous mode), it does not stop its execution but proceeds to subsequent tasks after the delivery of the PUBLISH packet to a dedicated sending thread. Conversely, when the _waited_packet_num_ is greater than 0 (synchronous mode) and the _subscriber_id_ field is not empty, the publisher undertakes a comparison between the _waited_packet_num_ field and the _subscriber_id_ field(s). If the _waited_packet_num_ field exceeds the count of the _subscriber_id_ fields, the publisher returns an error and terminates the publish session. However, the opposite case (where the _waited_packet_num_ is less than or equal to the number of _subscriber_id_ fields) is acceptable because the publisher can wait for a lower number of responses among the designated subscribers. Following scrutiny of the packet fields, the main thread registers the PUBLISH packet within an unacknowledged (termed unacked) packet list. Subsequently, the main thread dispatches the PUBLISH packet to the sending thread, which then orchestrates transmission to the broker. In scenarios where the publisher specifies the minimum count of response packets to be awaited (indicating synchronous mode), the main thread registers the PUBLISH packet within a synchronizer object. Here, the main thread calls the _wait()_ function to enter a suspended state [54]. During this phase, the publisher sets a timer to define the upper limit of waiting time. Upon expiration of this timer, the publisher interprets it as an indication of some subscribers' unavailability, thus prompting termination of the publish session. The main thread resumes execution upon the receipt of a PUBREC packet from the broker, triggered by a processing thread. This resumption is facilitated by the invocation of the _notify()_ function, effectively awakening the main thread from its suspended state. The publisher's processing thread is responsible for handling incoming packets, which are received from a dedicated receiving thread. During the synchronous communication within E-MQTT (under QoS level 3), when the processing thread receives a PUBREC packet, it initiates the transmission of a corresponding PUBREL packet to the broker. Simultane  
Figure 9: Flowcharts showing the publisher-side execution flow of E-MQTT. ously, the received packet is logged within the unacked packet list. Upon the arrival of a PUBREC packet, the processing thread undertakes the removal of the associated PUBLISH packet from the unacked packet list. This action is accompanied by the invocation of the _notify()_ function, thereby rousing the main thread from its suspended state. Subsequently, upon receipt of a PUBCOMP packet, the processing thread removes the PUBREL packet from the unacked packet list. Within the asynchronous E-MQTT, the main thread seamlessly progresses to the subsequent operation after transmitting the PUBLISH packet. Upon the processing thread's reception of the PUBREC packet, it promptly dispatches the corresponding PUBREL packet to the broker, which is the same process of MQTT QoS 2 until it receives the PUBCOMP packet. Illustrated in Figure 10 is the operational sequence of the broker within the context of E-MQTT. Upon receipt of a QoS level 3 PUBLISH packet from the publisher, the broker stores the packet and then proceeds with its customary procedure of transmitting the packet to all subscribers.