diff --git a/attachments/nano_attachment/nano_attachment.c b/attachments/nano_attachment/nano_attachment.c index 7c6cfd5..e00c1fd 100755 --- a/attachments/nano_attachment/nano_attachment.c +++ b/attachments/nano_attachment/nano_attachment.c @@ -18,6 +18,7 @@ #include "nano_blockpage.h" #include "compression_utils.h" #include "nano_compression.h" +#include "nano_attachment_io.h" NanoAttachment * InitNanoAttachment(uint8_t attachment_type, int worker_id, int num_of_workers, int logging_fd) @@ -282,12 +283,24 @@ SessionID PopFromNanoQueue(NanoAttachment *attachment) { AttachmentVerdictResponse response; - SessionID session_id = 0; + SessionID session_id; NanoCommunicationResult res; - // Pop from queue + response = PopResponseVerdictFromQueue(attachment); + if (response.session_id == 0) { + return 0; + } + + session_id = response.session_id; res = NanoAsyncAddResponse(attachment, session_id, &response); if (res != NANO_OK) { + write_dbg( + attachment, + session_id, + DBG_LEVEL_WARNING, + "Failed to add async response for session ID: %d", + session_id + ); return 0; } diff --git a/attachments/nano_attachment/nano_attachment_io.c b/attachments/nano_attachment/nano_attachment_io.c index 4390d94..2d149a7 100755 --- a/attachments/nano_attachment/nano_attachment_io.c +++ b/attachments/nano_attachment/nano_attachment_io.c @@ -1749,6 +1749,57 @@ nano_request_delayed_verdict( ); } +AttachmentVerdictResponse +PopResponseVerdictFromQueue(NanoAttachment *attachment) +{ + const char *reply_data; + uint16_t reply_size; + HttpReplyFromService *reply_p; + int pop_result; + SessionID session_id = 0; + + if (attachment == NULL || attachment->nano_service_ipc == NULL) { + return 0; + } + + if (!isDataAvailable(attachment->nano_service_ipc)) { + return 0; + } + + pop_result = receiveData(attachment->nano_service_ipc, &reply_size, &reply_data); + if (pop_result < 0 || reply_data == NULL) { + write_dbg( + attachment, + 0, + DBG_LEVEL_WARNING, + "Failed to receive data from queue" + ); + return 0; + } + + reply_p = (HttpReplyFromService *)reply_data; + + pop_result = popData(attachment->nano_service_ipc); + if (pop_result < 0) { + write_dbg( + attachment, + session_id, + DBG_LEVEL_WARNING, + "Failed to pop data from queue" + ); + return 0; + } + + response.verdict = reply_p->verdict; + response.session_id = reply_p->session_id; + + // TODO: Deal with data leak. + response.web_response_data = NULL; + response.modifications = NULL; + + return response; +} + void nano_send_metric_data_sender(NanoAttachment *attachment) { diff --git a/attachments/nano_attachment/nano_attachment_io.h b/attachments/nano_attachment/nano_attachment_io.h index cb5c20d..ef63e6a 100644 --- a/attachments/nano_attachment/nano_attachment_io.h +++ b/attachments/nano_attachment/nano_attachment_io.h @@ -231,4 +231,18 @@ nano_request_delayed_verdict( void nano_send_metric_data_sender(NanoAttachment *attachment); +/// +/// @brief Pops one element from the shared memory queue and returns its session ID. +/// +/// This function checks if data is available in the queue, receives the data, +/// extracts the session ID from the HttpReplyFromService structure, and pops +/// the element from the queue. +/// +/// @param attachment A pointer to a NanoAttachment structure representing the attachment to the nano service. +/// +/// @return The session ID of the popped element, or 0 if the queue is empty or an error occurred. +/// +AttachmentVerdictResponse +PopResponseVerdictFromQueue(NanoAttachment *attachment); + #endif // __NANO_ATTACHMENT_IO_H__