Search code examples
unetstack

How to make data structure global in unetstack so that multiple agents can update information in that data structure?


Currently i have created 2D array containing neighbor information in one of the agents. I want this array to be global so that other agents can also access this array and update the information in the array. Please tell how to make a data structure or a variable global so others agents can also access that.


Solution

  • In a distributed system, agents can, in theory, run on different containers and even different machines, and hence sharing a data structure or array isn't a good idea.

    Having said that, if you knew that the agents accessing the data structure were guaranteed to be running in the same container, you could share a reference to the data structure between agents and manipulate/access the data from different agents. The reference may be shared during setup of the stack when the agents are constructed, by providing getters/setters where the reference could be obtained and passed to other agents. If you did this, you'd also need to ensure synchronization, as each agent runs in its own thread, and you could have data races. Although this is easily doable, I would strongly advise against this approach.

    A better approach would be to send messages between agents to get/update the shared information, or expose the shared information as agent parameters. For example, the NodeInfo agent does exactly this, by exposing commonly needed information (e.g. node address, node location, node orientation, etc) about a node as parameters that many other agents access and a few update.