Usage
#####

Building your first HERO is very simple. Just write your custom class and make it inherit from ``heros.LocalHero``.

A simple example can be seen

.. literalinclude:: ../../examples/share_local_hero_simple.py
   :language: python

Due to the infinite loop, the script will not terminate and keep the object ``obj`` alive. Since it inherits from ``heros.LocalHERO``, the object was analyzed upon instantiation and provided as a HERO to the network.
This means, we can now simply access the method attributes of the object from a remote site.

To get remote access we can run the following in a different process or on a different machine in the same network (UDP broadcast needs to reach the other machine for discovery):

.. literalinclude:: ../../examples/access_remote_hero_simple.py
    :language: python

By default, ``RemoteHERO`` waits :py:data:`heros.heros.DEFAULT_DISCOVER_TIMEOUT` seconds for the remote object to answer the discovery query and raises a ``NameError`` otherwise.
On slow or routed networks, set a longer wait time in seconds with the :ref:`HEROS_DISCOVER_TIMEOUT <heros-conf>` environment variable.
It applies to every HERO and observer created in the process.

.. code:: bash

    HEROS_DISCOVER_TIMEOUT=10 python my_script.py

Nested HEROs
------------

HEROS is able to serialize HEROs as references to a HERO.
This allows to pass a HERO between the local and the remote site.
When either of the sites receives such a reference, it creates a ``RemoteHERO`` to access the referenced HERO.
This allows things like

 1. Deep access to HEROs nested inside of HEROs.
 2. Passing HEROs as arguments into methods exposed by a HERO.
 3. Retrieving HEROs returned by a HERO.

A nested ``RemoteHERO`` uses the session manager of the HERO or observer it was received through.

.. _sec-nested-callbacks:

HEROs that arrive in an event, or as an argument or callback of a ``LocalHERO`` method, are created inside a zenoh callback.
Creating them blocks that callback for up to ``HEROS_DISCOVER_TIMEOUT`` for the discovery and then for up to the :ref:`zenoh query timeout <sec-query-timeout>` for the capabilities.
Keep ``HEROS_DISCOVER_TIMEOUT`` short in processes that receive HEROs this way.


HERO references
---------------

:py:class:`heros.ref.HERORef` identifies a HERO by name and realm without connecting to it.
Use it where only the identity of a HERO is needed and a ``RemoteHERO`` would be created for nothing.
Name and realm are required, and the reference is immutable and hashable.

.. code-block:: python

    from heros import HERORef

    ref = HERORef("my_hero", "my_realm")

A ``HERORef`` has no CBOR encoding.
It is handled like any other unserializable object, see below.


Unserializable Objects
----------------------

A HERO attribute or method might return an object that is not a HERO and can not be serialized.
In that case, the returned object is cached on the side of the ``LocalHERO`` and an identifier is sent to the remote side. The remote side can store the reference locally.
If the reference is sent back to the ``LocalHERO`` side, the corresponding object is taken from the cache and inserted into the request instead of the reference.
This allows to instruct the LocalHERO to do something with an object that cannot be transferred.
This allows, for example, to hand over an unserializable object retrieved earlier as argument to a function.

.. note::

   The cache that keeps the object references uses only weak references to to avoid memory leaks.
   That means that an object can be garbage collected if not any other instance keeps a reference on it.


Getting a list of all running HEROs in a network
------------------------------------------------

Either use a `HERO monitor <https://gitlab.com/atomiq-project/hero-monitor>`_ or open a python console with HEROS installed and run

.. code:: python

   from heros.heros import HEROObserver
   ob = HEROObserver()
   ob.known_objects.keys()
