Skip to content

Tutorial

Zentetsu edited this page Nov 7, 2024 · 8 revisions

Short example of Sender-Listener (Publisher-Subscriber) communication.

Sender side

JSON file

It's important to define the JSON as follow:

{
    "name": "Module_1",
    "sender": {
        "shared_list": [0, 1, 2]
    },
    "listener": {}
}

Structure:

  • name: Define the name of the module that will be loaded
  • sender: Define the SharedMemory that will be used by the module (updating data)
  • listener: Define the SharedMemory that will be observed

Optional:

  • remote: This option can be defined in a case of remote communication between module see here

Sender Module

WARNING: Be careful with the initial size of a shared data as it cannot be changed dynamically. It's possible to assign a specific size by using the option size. However, it can be only used with a JSON file.

from IRONBark import Module

# Initialize Module 1 and create the associated SharedMemory
m = Module(file="Module_1.json")

# Make the SharedMemory available
m.startModule()

# Get a copy of the stored data from the SharedMemory
shared_data = m.getValue("sender_1")

# Update the value inside the SharedMemory
m.setValue("sender_1", [0, 0, 0])

# Close the access and clear the Sharedmemory
m.stopModule()

Listener side

JSON file

It's important to define the JSON as follow:

{
    "name": "Module_2",
    "sender": {},
    "listener": {
        "shared_list": []
    }
}

listener:

  • Is a dict that will contain all the observed data
  • Each data are associated with a list which can be define like here in the case of a remote communicaiton

Sender module

from IRONBark import Module

# Initialize Module 1 to be ready to access to the SharedMemory
m = Module(file="Module_2.json")

# Attempt to link and access to the SharedMemory
m.startModule()

# Get a copy of the stored data from the SharedMemory only if the sahred space is already created
shared_data = m.getValue("sender_1")

# Close the access to the Sharedmemory
m.stopModule()

It's only possible to update the data from the SharedMemory from the Listener side during a local communication

Additional examples

JSON with remote option for Sender module

{
    "name": "Module_1",
    "remote": {
        "ip": "127.0.0.1",
        "port": "12345"
    },
    "sender": {
        "shared_list": [0, 1, 2]
    },
    "listener": {}
}

JSON with remote option for Listener module

{
    "name": "Module_2",
    "sender": {},
    "listener": {
        "shared_list": ["127.0.0.1", "12345"]
    }
}

Clone this wiki locally