Skip to main content

NSBSimClient (Python)

The simulator-side client. Integrates into network simulator code to route payloads through a simulated network.


Constructorโ€‹

nsb_conn = nsb.NSBSimClient(identifier, server_address, server_port)

Parameters:

ParameterTypeDescription
identifierstrUnique identifier for this simulator client. Must match the corresponding NSBAppClient identifier in Per-Node mode. In System-Wide mode, one identifier is used for all.
server_addressstrIP address or hostname of the NSB Daemon.
server_portintPort number of the NSB Daemon.
note

In System-Wide mode, only one NSBSimClient may connect. In PUSH mode, the client must remain connected for the duration of the simulation.


fetch(src_id=None, timeout=None)โ€‹

Fetches a payload that was sent by an application client and is waiting to be routed through the simulated network.

entry = nsb_conn.fetch()
# or
entry = nsb_conn.fetch(src_id="node0", timeout=10)

Parameters:

ParameterTypeDefaultDescription
src_idstr | NoneNoneIdentifier of the source to fetch from. None = fetch the most recent message regardless of source. Overwritten with own ID in Per-Node mode.
timeoutint | NoneNoneSeconds to wait. None = wait indefinitely. 0 = non-blocking poll.

Returns: MessageEntry | None

Behavior by mode:

  • PULL mode: Sends a FETCH request to the daemon. Daemon responds with MESSAGE or NO_MESSAGE.
  • PUSH mode: Waits for forwarded payloads from the daemon/broker.

Per-Node override: When sim_mode is PER_NODE, src_id is automatically overwritten with the client's own identifier โ€” it only fetches on its own behalf.

Example:

entry = nsb_conn.fetch()
if entry:
src = entry.source
dst = entry.destination
payload = entry.payload
# Route payload through simulated network...
nsb_conn.post(src, dst, processed_payload)

listen()โ€‹

An asynchronous coroutine for fetching payloads.

async def simulator_loop():
entry = await nsb_conn.listen()
if entry:
src_id = entry.source
dest_id = entry.destination
payload = entry.payload
# Send through simulated network...

Returns: MessageEntry | None

Behavior: Similar to fetch() but for async/await contexts. See Async Listeners for a complete runnable example.


post(src_id, dest_id, payload)โ€‹

Notifies NSB that a payload has arrived at its destination within the simulated network and makes it available for reception by the application client.

nsb_conn.post(src_id, dest_id, payload)

Parameters:

ParameterTypeDescription
src_idstrIdentifier of the original source application client.
dest_idstrIdentifier of the destination application client.
payloadbytesThe payload data (may have been processed/modified by the simulator).

Behavior: Creates an NSB POST message with the source, destination, and payload, then transmits it to the daemon. The daemon stores or routes it so that NSBAppClient(dest_id).receive() can retrieve it.

Example:

nsb_conn.post(src_id="node0", dest_id="node1", payload=b"delivered payload")

Go Deeperโ€‹