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

.. py:module:: heros.zenoh


Attributes
----------

.. autoapisummary::

   heros.zenoh.zenoh_default_config
   heros.zenoh.DEFAULT_SEP_MULTICAST
   heros.zenoh.DEFAULT_OPEN_TIMEOUT
   heros.zenoh._OPEN_TIMEOUT_KEY
   heros.zenoh._TRUE_FLAGS
   heros.zenoh._FALSE_FLAGS
   heros.zenoh._ZENOH_MODES
   heros.zenoh._managers
   heros.zenoh.session_manager


Classes
-------

.. autoapisummary::

   heros.zenoh.ZenohSessionManager


Functions
---------

.. autoapisummary::

   heros.zenoh._zenoh_default_open_timeout
   heros.zenoh._parse_sep_multicast_env
   heros.zenoh._apply_mode_and_connect
   heros.zenoh._apply_open_timeout
   heros.zenoh.build_config
   heros.zenoh._close
   heros.zenoh.close_all_sessions


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

.. py:data:: zenoh_default_config

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


.. py:data:: DEFAULT_OPEN_TIMEOUT
   :type:  int
   :value: 1000


.. py:data:: _OPEN_TIMEOUT_KEY
   :value: 'transport/unicast/open_timeout'


.. py:function:: _zenoh_default_open_timeout() -> int

   Return the link open timeout of an untouched zenoh config, in milliseconds.


.. 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:: _apply_open_timeout(config: zenoh.Config) -> None

   Lower the link open timeout to DEFAULT_OPEN_TIMEOUT, unless the config already sets its own.

   A value is treated as the caller's own as soon as it differs from the zenoh default, so
   `HEROS_ZENOH_CONFIG` keeps the last word.


.. 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:function:: _close(session: zenoh.Session) -> None

   Close a zenoh session, logging rather than raising if zenoh gives up on it.

   :param session: The session to close


.. py:data:: _managers
   :type:  weakref.WeakSet

.. py:function:: close_all_sessions() -> None

   Tear down every HERO and close every zenoh session of this process.


.. 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:: _referrer_index(obj: object) -> int | None

      Return the index of `obj` in the referrers, or None. By identity, as HEROs of the same name compare equal.



   .. 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

      Tear down every HERO that holds a session of this manager, then close the sessions.

      Closing a session does not reliably release the callback threads of the declarations made
      on it, undeclaring does. The HEROs therefore have to go first, which is why this does more
      than close the sessions.



.. py:data:: session_manager

