heros.zenoh
===========

.. py:module:: heros.zenoh


Attributes
----------

.. autoapisummary::

   heros.zenoh.zenoh_default_config
   heros.zenoh.DEFAULT_SEP_MULTICAST
   heros.zenoh._TRUE_FLAGS
   heros.zenoh._FALSE_FLAGS
   heros.zenoh._ZENOH_MODES
   heros.zenoh.session_manager


Classes
-------

.. autoapisummary::

   heros.zenoh.ZenohSessionManager


Functions
---------

.. autoapisummary::

   heros.zenoh._parse_sep_multicast_env
   heros.zenoh._apply_mode_and_connect
   heros.zenoh.build_config


Module Contents
---------------

.. py:data:: zenoh_default_config

.. py:data:: DEFAULT_SEP_MULTICAST
   :type:  bool
   :value: True


.. py:data:: _TRUE_FLAGS
   :value: ('1', 'true', 'yes', 'on')


.. py:data:: _FALSE_FLAGS
   :value: ('0', 'false', 'no', 'off')


.. py:data:: _ZENOH_MODES
   :value: ('peer', 'client', 'router')


.. py:function:: _parse_sep_multicast_env() -> bool | None

   Parse `HEROS_SEP_MULTICAST` strictly.

   :returns: True or False for a recognized value, None if the variable is unset or empty

   :raises ValueError: If the value is not one of 1/true/yes/on or 0/false/no/off (case-insensitive)


.. py:function:: _apply_mode_and_connect(config: zenoh.Config) -> None

   Apply `HEROS_ZENOH_MODE` and `HEROS_ZENOH_CONNECT` to a config, unset or empty variables change nothing.

   :raises ValueError: If the mode is not peer, client or router, a connect item is empty or zenoh rejects a value


.. py:function:: build_config(realm: str, overrides: dict | None = None, *, sep_multicast: bool | None = None) -> zenoh.Config

   Assemble the zenoh config for a realm without opening a session.

   Environment variables are read at call time. Later layers win over earlier ones:
   zenoh defaults, then `HEROS_ZENOH_CONFIG`, then the realm specific multicast address
   (non-`heros` realms only), then `HEROS_ZENOH_MODE` and `HEROS_ZENOH_CONNECT`, then `overrides`.
   `HEROS_ZENOH_CONNECT` sets only `connect/endpoints`.

   The multicast address is used if `sep_multicast` is true. If it is None, `HEROS_SEP_MULTICAST`
   decides, and if that is unset, the module default does.

   :param realm: The realm the session is opened for
   :param overrides: Config keys and values applied last
   :param sep_multicast: Whether to use a realm specific multicast address, None to use the environment

   :returns: The assembled zenoh config

   :raises ValueError: If `HEROS_ZENOH_CONFIG` is not a valid zenoh config, `HEROS_SEP_MULTICAST` is not a recognized
       flag, or `HEROS_ZENOH_MODE` or `HEROS_ZENOH_CONNECT` hold an invalid value


.. py:class:: ZenohSessionManager(config_dict: dict | None = None, *, sep_multicast: bool | None = None)

   Share one zenoh session per realm between all HEROs of a process.

   The zenoh config is built when the first session of a realm opens and stays in use until
   every HERO of that realm has released it.

   :param config_dict: Zenoh config keys and values, applied over the environment
   :param sep_multicast: Whether sessions use a realm specific multicast address. None defers to
                         `HEROS_SEP_MULTICAST` and then to the default. Can be changed later via the
                         `sep_multicast` attribute and applies to sessions opened afterwards.


   .. py:attribute:: sep_multicast
      :value: None



   .. py:attribute:: _config_dict


   .. py:attribute:: _sessions


   .. py:attribute:: _referrers
      :value: []



   .. py:attribute:: _referrer_realms
      :value: []



   .. py:method:: request_session(obj: object, realm: str) -> zenoh.Session

      Request the global zenoh session.

      :param obj: The object that requests the session



   .. py:method:: release_session(obj: object) -> None

      Release from the global zenoh session.

      :param obj: The object that wants to release from the global zenoh session



   .. py:method:: update_config(config_dict: dict) -> None


   .. py:method:: force_close() -> None


.. py:data:: session_manager

