registry.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. //
  3. // Copyright (c) 2019 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "actor_base.h"
  8. #include "messages.hpp"
  9. #include <unordered_map>
  10. #include <set>
  11. namespace rotor {
  12. /** \struct registry_t
  13. * \brief keeps name-to-service_address mapping at runtime
  14. *
  15. * The class solves the following problem: one actor needs to know
  16. * somehow the address of other actor. Instead of tightly couple
  17. * them, i.e. manually binding addresses, it is possible to just
  18. * take the registry everywhere and ask it (or registere there)
  19. * just on actor startup (initialization)
  20. *
  21. * The "server-actor" must register it's address first. The name
  22. * must be unique (in the scope of registry); the same address
  23. * can be registered on different names, if need. It is good
  24. * practice to unregister addresses when actor is going to be
  25. * shutdown. It is possible to unregister single name, or all
  26. * names associated with an address.
  27. *
  28. * The discovery response returned to "client-actor" might contain
  29. * error code if there is no address associated with the asked name.
  30. *
  31. */
  32. struct registry_t : public actor_base_t {
  33. using actor_base_t::actor_base_t;
  34. virtual ~registry_t() override;
  35. virtual void init_start() noexcept override;
  36. /** \brief registers address on the service. Fails, if the name already exists */
  37. virtual void on_reg(message::registration_request_t &request) noexcept;
  38. /** \brief deregisters the name in the registry */
  39. virtual void on_dereg_service(message::deregistration_service_t &message) noexcept;
  40. /** \brief deregisters all names assosiccated with the service address */
  41. virtual void on_dereg(message::deregistration_notify_t &message) noexcept;
  42. /** \brief returns service address associated with the name or error */
  43. virtual void on_discovery(message::discovery_request_t &request) noexcept;
  44. protected:
  45. /** \brief name-to-address mapping type */
  46. using registered_map_t = std::unordered_map<std::string, address_ptr_t>;
  47. /** \brief set of registered names type (for single address) */
  48. using registered_names_t = std::set<std::string>;
  49. /** \brief service address to registered names mapping type */
  50. using revese_map_t = std::unordered_map<address_ptr_t, registered_names_t>;
  51. /** \brief name-to-address mapping */
  52. registered_map_t registered_map;
  53. /** \brief address-to-list_of_names mapping */
  54. revese_map_t revese_map;
  55. };
  56. } // namespace rotor