Skip to content

unaiverse.networking.p2p

This package is the transport layer of the SDK on a Python node. As the bottom layer of the stack it connects to other peers, carries their messages and publishes on topics. The work happens in a native networking library shipped inside the package as a shared object (_unailib.so on Linux, _unailib.dylib on macOS and _unailib.dll on Windows). The package loads that library when it is imported and wraps it in Python classes. In the browser this role is played by a transport written in JavaScript that exposes the same methods (see Networking).

Module Content
p2p P2P (one host of the library on one network) and the exceptions P2PError, P2PNoFileStream and P2PQueueFull.
messages Msg, the chat message with its codec in the binary message format.
files The folders of the received files and the choice of the samples that travel from disk.
lib_types TypeInterface, the conversion of arguments and replies between Python and the library.

The layers above use this package through SessionManager and NodeConn. The developer does not import it in an agent or a world.

What the import does

Importing unaiverse.networking.p2p (directly or through any module of the SDK that needs it) runs four steps once per process.

  1. It loads the shared library from the folder of the package with ctypes.CDLL. An unsupported operating system or a missing or broken file prints a message on standard error and raises ImportError("Failed to load the UNaIVERSE p2p shared library."). The usual cure is to reinstall the unaiverse package.
  2. It declares the argument and return types of every function of the library. Some functions exist only in recent builds of the library (the send queues, the separate pops of network messages and stream messages, the file messages, the capabilities of a peer and the cancellation of a message that is still being sent). The package declares them only when the library has them and P2P.setup_library turns the related features on only in that case. An older library therefore keeps working with synchronous sends and single-frame messages.
  3. In a source checkout it compares a hash of the native sources next to the library with the hash the library was built from. A difference raises a UserWarning that asks to reinstall the package from the repository root (pip install -e .). A library built before the hash existed counts as a difference. An installed package carries no native sources and skips the check.
  4. It assigns the loaded library to P2P.libp2p and TypeInterface.libp2p.

The library is not initialized yet at this point. P2P.setup_library() does that and the node calls it once before it creates its two P2P instances.

Exported names

__all__ lists P2P, P2PError and TypeInterface. The package also exposes Msg and the submodules messages, p2p and lib_types as attributes.

from unaiverse.networking.p2p import P2P, P2PError, Msg

The names are documented on the pages of their modules: P2P, P2PError, TypeInterface and Msg.