BackendReceiver.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * @brief Defines the aircraft scheduling notifier module to receive plans from the backend
  3. * @file aman/com/BackendReceiver.h
  4. * @author Sven Czarnian <devel@svcz.de>
  5. * @copyright Copyright 2021 Sven Czarnian
  6. * @license This project is published under the GNU General Public License v3 (GPLv3)
  7. */
  8. #pragma once
  9. #include <thread>
  10. #include <memory>
  11. #include <zmq.hpp>
  12. #include <aman/types/Communication.h>
  13. #pragma warning(push, 0)
  14. #include "protobuf/Communication.pb.h"
  15. #pragma warning(pop)
  16. namespace aman {
  17. /**
  18. * @brief Defines the aircraft scheduling notification class to receive scheduling sequences
  19. * @ingroup com
  20. */
  21. class BackendReceiver {
  22. private:
  23. std::unique_ptr<zmq::socket_t> m_socket;
  24. std::thread m_receiverThread;
  25. std::atomic_bool m_stopReceiver;
  26. std::list<std::shared_ptr<aman::AircraftSequence>> m_sequences;
  27. std::mutex m_sequencesLock;
  28. BackendReceiver() noexcept;
  29. template <typename T>
  30. bool setSocketKey(const std::string& key, T entry) {
  31. try {
  32. this->m_socket->set(entry, key);
  33. return true;
  34. }
  35. catch (std::exception&) {
  36. return false;
  37. }
  38. }
  39. void receiveSequence();
  40. void run();
  41. public:
  42. BackendReceiver(const BackendReceiver&) = delete;
  43. BackendReceiver(BackendReceiver&&) = delete;
  44. BackendReceiver& operator=(const BackendReceiver&) = delete;
  45. BackendReceiver& operator=(BackendReceiver&&) = delete;
  46. /**
  47. * @brief Initializes the aircraft scheduler
  48. * @param[in] configuration The current AMAM communication configuration
  49. * @return True if the initialization is done, else false
  50. */
  51. bool initialize(const Communication& configuration);
  52. /**
  53. * @brief Terminates the scheduler connection
  54. */
  55. bool deinitialize();
  56. /**
  57. * @brief Checks if the scheduler is initialized
  58. * @return True if it is initialized, else false
  59. */
  60. bool initialized() const noexcept;
  61. /**
  62. * @brief Returns the current sequence of the receiver queue
  63. * @return The sequence queue
  64. */
  65. std::shared_ptr<aman::AircraftSequence> receive();
  66. /**
  67. * @brief Returns the scheduling instance
  68. * @return The system-wide instance
  69. */
  70. static BackendReceiver& instance() noexcept;
  71. };
  72. }