diff options
author | claiff <claiff@mail.ru> | 2022-10-18 16:52:57 +0300 |
---|---|---|
committer | claiff <claiff@mail.ru> | 2022-10-18 16:52:57 +0300 |
commit | 0c4f54cc2ac42309fee93c7800ae23cc64672142 (patch) | |
tree | a1fb1fe43cb4066e7cc86e068898aee40eebb2f7 | |
parent | 416a20b211539c8db06c26a2297de9109ef69f32 (diff) | |
parent | 1ca701a3056725f7560d10929baf3660d52bc4be (diff) | |
download | obmc-sila-smtp-0c4f54cc2ac42309fee93c7800ae23cc64672142.tar.xz |
Merge branch 'bugfix/queue_empty_recipients'
-rw-r--r-- | CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/management/mail.cpp | 22 | ||||
-rw-r--r-- | src/management/mail.hpp | 6 | ||||
-rw-r--r-- | src/message/sender.cpp | 9 | ||||
-rw-r--r-- | src/message/sender.hpp | 8 | ||||
-rw-r--r-- | src/service/smtp.hpp | 4 | ||||
-rw-r--r-- | src/thread/queue.cpp (renamed from src/message/thread_safe_queue.cpp) | 8 | ||||
-rw-r--r-- | src/thread/queue.hpp (renamed from src/message/thread_safe_queue.hpp) | 8 |
8 files changed, 45 insertions, 26 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index ea033d7..b188908 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,6 +98,7 @@ set(CONVERTER_DIR src/converter) set(LOGGER_DIR src/logger) set(CHECKER_DIR src/checker) set(GENERAL_DIR src/general) +set(THREAD_DIR src/thread) set(SRC_FILES ${SRC_DIR}/main.cpp @@ -107,8 +108,6 @@ set(SRC_FILES ${MESSAGE_DIR}/sender.hpp ${MESSAGE_DIR}/sender.cpp - ${MESSAGE_DIR}/thread_safe_queue.hpp - ${MESSAGE_DIR}/thread_safe_queue.cpp ${MESSAGE_DIR}/builder/date.hpp ${MESSAGE_DIR}/builder/date.cpp @@ -191,6 +190,9 @@ set(SRC_FILES ${GENERAL_DIR}/struct.hpp ${GENERAL_DIR}/idecorator.hpp + + ${THREAD_DIR}/queue.hpp + ${THREAD_DIR}/queue.cpp ) if( ${ADD_PHOSPHOR_LOGGING} STREQUAL "ON") diff --git a/src/management/mail.cpp b/src/management/mail.cpp index eefb666..e3ef9f6 100644 --- a/src/management/mail.cpp +++ b/src/management/mail.cpp @@ -11,8 +11,14 @@ namespace smtp::manage Mail::Mail( file::mail::types::IFileManipulatorPtr const& file_reader ) : mFileReader( file_reader ) , mMails( mFileReader->Read() ) - { - } + { + } + + Mail::Mail(const Mail &mails) + { + mMails = mails.mMails; + mFileReader = mails.mFileReader; + } // @@ -28,7 +34,9 @@ namespace smtp::manage { auto merged_mails = GetMergedMails( mails_to_add ); - return RefreshMails( merged_mails ); + auto result = RefreshMails( merged_mails ); + mCondition.notify_one(); + return result; } bool Mail::DeleteMailToSend( general::MailsSet const& mail_to_delete ) @@ -38,7 +46,13 @@ namespace smtp::manage auto result_set_after_delete = GetMailsAfterDelete( mails_to_delete ); return RefreshMails( result_set_after_delete ); - } + } + + void Mail::WaitWhileEmptyMails() + { + std::unique_lock<std::mutex> lock{mMutex}; + mCondition.wait(lock, [this](){return !mMails.empty();}); + } // //Private methods diff --git a/src/management/mail.hpp b/src/management/mail.hpp index 39d4048..0a5c890 100644 --- a/src/management/mail.hpp +++ b/src/management/mail.hpp @@ -1,6 +1,8 @@ #pragma once #include <string> +#include <mutex> +#include <condition_variable> #include "file/mail/types/ifile_manipulator.hpp" #include "checker/registrator_mails.hpp" @@ -11,11 +13,13 @@ namespace smtp::manage { public: explicit Mail( file::mail::types::IFileManipulatorPtr const& file_reader ); + Mail(Mail const& mails); ~Mail() = default; general::MailsSet GetMailToSend() const; bool AddMailsToSend( general::MailsSet const& mails ); bool DeleteMailToSend( general::MailsSet const& mail_to_delete ); + void WaitWhileEmptyMails(); private: general::MailsSet GetMergedMails( general::MailsSet const& mails_to_add ); general::MailsSet GetItersectionMails( general::MailsSet const& mail_to_delete ) const; @@ -24,6 +28,8 @@ namespace smtp::manage file::mail::types::IFileManipulatorPtr mFileReader; general::MailsSet mMails; + mutable std::mutex mMutex; + std::condition_variable mCondition; }; } diff --git a/src/message/sender.cpp b/src/message/sender.cpp index a200092..7df9819 100644 --- a/src/message/sender.cpp +++ b/src/message/sender.cpp @@ -16,7 +16,7 @@ namespace smtp::message // // Constructors/Destructors // - Sender::Sender( manage::Settings const& settings_storage, manage::Mail const& mail_to, ThreadSafeQueue& message_queue ) + Sender::Sender( manage::Settings const& settings_storage, manage::Mail& mail_to, thread::Queue& message_queue ) : mSettingsStorage( settings_storage ) , mMailTo( mail_to ) , mMessageQueue( message_queue ) @@ -46,14 +46,11 @@ namespace smtp::message logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "Error to initializate message service" ); } //TODO раздать mail_to другим методам + + mMailTo.WaitWhileEmptyMails(); auto mail_to = mMailTo.GetMailToSend(); - if( mail_to.empty()) - { - logger::LoggerSet::GetInstance()->LogError( METHOD_NAME, "We haven't any mail to" ); - } FillRecipients( curl, recipients ); - UpdateMailText( mSettingsStorage.GetUserName(), message->subject, message->text ); result = curl_easy_perform( curl ); diff --git a/src/message/sender.hpp b/src/message/sender.hpp index 2419e4c..69f9346 100644 --- a/src/message/sender.hpp +++ b/src/message/sender.hpp @@ -7,7 +7,7 @@ #include "management/settings.hpp" #include "management/mail.hpp" -#include "thread_safe_queue.hpp" +#include "thread/queue.hpp" namespace smtp::message { @@ -19,7 +19,7 @@ namespace smtp::message class Sender { public: - Sender( manage::Settings const& settings_storage, manage::Mail const& mail_to, ThreadSafeQueue& message_queue ); + Sender( manage::Settings const& settings_storage, manage::Mail& mail_to, thread::Queue& message_queue ); ~Sender() = default; void Send(); @@ -31,8 +31,8 @@ namespace smtp::message static size_t ReadCallBack( void *ptr, size_t size, size_t nmemb, void *userp ); manage::Settings const& mSettingsStorage; - manage::Mail const& mMailTo; - ThreadSafeQueue& mMessageQueue; + manage::Mail& mMailTo; + thread::Queue& mMessageQueue; }; } diff --git a/src/service/smtp.hpp b/src/service/smtp.hpp index 0d8aea1..6329c7e 100644 --- a/src/service/smtp.hpp +++ b/src/service/smtp.hpp @@ -7,7 +7,7 @@ #include "management/mail.hpp" #include "management/settings.hpp" -#include "message/thread_safe_queue.hpp" +#include "thread/queue.hpp" namespace smtp::service { @@ -37,6 +37,6 @@ namespace smtp::service manage::Settings mSettingsStorage; manage::Mail mMailTo; - message::ThreadSafeQueue mMessageQueue; + thread::Queue mMessageQueue; }; } diff --git a/src/message/thread_safe_queue.cpp b/src/thread/queue.cpp index 092a2ac..d2ed7d2 100644 --- a/src/message/thread_safe_queue.cpp +++ b/src/thread/queue.cpp @@ -1,15 +1,15 @@ -#include "thread_safe_queue.hpp" +#include "queue.hpp" -namespace smtp::message +namespace smtp::thread { - void ThreadSafeQueue::Push( Message const &message) + void Queue::Push( Message const &message) { std::lock_guard<std::mutex> lock{mMutex}; mMessageQueue.push( message ); mCondition.notify_one(); } - std::shared_ptr<Message> ThreadSafeQueue::WaitAndPop() + std::shared_ptr<Message> Queue::WaitAndPop() { std::unique_lock<std::mutex> lock{mMutex}; mCondition.wait(lock, [this](){return !mMessageQueue.empty();}); diff --git a/src/message/thread_safe_queue.hpp b/src/thread/queue.hpp index 5bb1ee4..9ed3a76 100644 --- a/src/message/thread_safe_queue.hpp +++ b/src/thread/queue.hpp @@ -5,7 +5,7 @@ #include <queue> #include <condition_variable> -namespace smtp::message +namespace smtp::thread { struct Message { @@ -13,11 +13,11 @@ namespace smtp::message std::string text; }; - class ThreadSafeQueue + class Queue { public: - ThreadSafeQueue() = default; - ~ThreadSafeQueue() = default; + Queue() = default; + ~Queue() = default; void Push( Message const& message ); std::shared_ptr<Message> WaitAndPop(); |