#pragma once /* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ /** * @file * @copyright MPLv2 */ #include #include #include #include #include namespace BC { /** * @brief class BasicQueueReceiver * * Derived class from receiveable (libbattle-com++) to receive messages * published in the battle-com network. * * This class is a basic component for applications that need to connect to a * battle-com network, like e.g. the prototype CMS. * The receiver will be moved to a thread by the service it is used from. If * the receive method gets called and it is still busy with the previous * message, the new message will get lost. * * Therefor messages that are received by this BasicQueueReceiver are pushed * to a message queue (short receive method). From there the other components * of the application can take the messages as needed. This implementation * ensures that the receive method is short so no information get lost. */ class BasicQueueReceiver : public BC::receiveable { private: /// threadsafe queue to store received messages BC::BasicMessageQueue messages; public: /** * @brief default constructor for class Receiver */ BasicQueueReceiver(); /** * @brief tryGetMessage * * This method tries to get a message from the message queue. * Calles getMessage from message queue. This method rethrows the exception * that can be trown by called method. * * @result msg - raw pointer to new message from the network * @throws length_error - if queue is empty this method throws length_error exception */ [[deprecated("Replaced by bool tryGetMessage(BC::Message **msg)")]] BC::Message * tryGetMessage(); /** * @brief tryGetMessage * * This method tries to get a message from the message queue. * Calles getMessage from message queue. This method rethrows the exception * that can be trown by called method. * * @TODO: change param msg to smart pointer (when switching from raw to smart pointer in this project) * * @result true - a message was get successfully * @return false - there was no message in the queue, the pointer is invalid and equals nullptr * @param msg - raw pointer to new message from the network that should be get from the queue */ [[deprecated("Replaced by function that uses unique_ptr")]] bool tryGetMessage(BC::Message **msg); /** * @brief waitForNewMessage * * Calles waitForNewMessage method from corresponding message queue. After * notification the new message will be returned. * This method catches the exception: empty queue. * * @result msg - raw pointer to new message from the network * @rethrow - rethrows an exception that is not empty queue exception */ [[deprecated("Replaced by function that uses unique_ptr")]] BC::Message * waitForNewMessage(); /** * @brief getMessage * * This method gets a message form the message queue if there is a message * in the queue. If the message queue is empty this method throw an * exception * * @result - unique pointer to received message * @throws length_error - if queue is empty this method throws length_error exception */ std::unique_ptr getMessage(); /** * @brief waits given time in seconds for new message to be recieved * * This method tries to get one BC::Message from the receiver for a * specific time. If no message can be taken after this time the class * function returns a nullpointer. * * @param time - time duration in seconds the method tries to get a message from the queue * @result - unique pointer to BC::Message from message queue * @result nullptr - returns nullptr if message queue stays empty */ std::unique_ptr waitSecondsForNewMessage(std::uint16_t time); /** * @brief waits given time in milliseconds for new message to be recieved * * This method tries to get one BC::Message from the receiver for a * specific time. If no message can be taken after this time the class * function returns a nullpointer. * * @param time - time duration in milliseconds the method tries to get a message from the queue * @result - unique pointer to BC::Message from message queue * @result nullptr - returns nullptr if message queue stays empty */ std::unique_ptr waitMillisecondsForNewMessage(std::uint16_t time); /** * @brief gets number of messages in the queue * * This method returns the queue size which means the number of messages * stored at the queue. * * @result size - size of message queue */ unsigned int getQueueSize(); /** * @brief virtual function receive * * This function is called if there are new messages published. * These messages are pushed to the message queue. * * @param m - raw pointer to the received message */ virtual void receive(BC::Message *m) override; }; // class BasicQueueReceiver }; // namespace BC