Exchanging Messages¶
The examples here all refer to the messenger
module.
Send & receive a message¶
Process A¶
import networkzero as nw0
address = nw0.advertise("messenger1")
message = nw0.wait_for_message_from(address)
print("Message received: ", message)
nw0.send_reply_to(address, "Received: %s" % message)
Process B¶
import networkzero as nw0
service = nw0.discover("messenger1")
reply = nw0.send_message_to(service, "This is a message")
print("Reply: ", reply)
Discussion¶
The message exchange in networkzero works on a very simple message-reply model: one process waits for a message from another and sends a reply. The process sending the original message waits for the reply before carrying on. This is less flexible that network communications usually are (the sending process must wait for a reply before carrying on) but cuts out the complications which arise when several processes are sending messages to the same listening process.
Send a message without expecting a reply¶
Process A¶
import networkzero as nw0
address = nw0.advertise("messenger2")
message = nw0.wait_for_message_from(address, autoreply=True)
print("Message received: ", message)
Process B¶
import networkzero as nw0
service = nw0.discover("messenger2")
nw0.send_message_to(service, "This is a command")
Discussion¶
The message exchange in networkzero always requires a reply to any message. But sometimes, no reply is needed, for example when sending commands to a robot. It would be possible for the process which receives a message to send a dummy reply and for the process which sent the message to ignore any reply.
This situation is so common that there is a special parameter autoreply to the wait_for_message_from function. Behind the scenes, networkzero completes the message-reply contract for you by sending an empty reply. The process which sent the message can simply ignore any reply.
Send news¶
Process A¶
import networkzero as nw0
import random
import time
address = nw0.advertise("news1")
#
# In lieu of an actual temperature sensor!
#
temperatures = range(15, 20)
while True:
temperature = random.choice(temperatures) + random.random()
nw0.send_news_to(address, "temperature", temperature)
time.sleep(1.0)
Process B and C and D…¶
import networkzero as nw0
address = nw0.discover("news1")
while True:
topic, temperature = nw0.wait_for_news_from(address)
print("Temperature is:", temperature)
Discussion¶
One process can send news to any process which is listening. It doesn’t matter if no-one is listening or if 100 other machines are listening. This could be used, for example, to have one machine linked directly to a temperature sensor outside a classroom which broadcasts the temperature once a minute.
Send news on a specific topic¶
Process A¶
import networkzero as nw0
import random
import time
address = nw0.advertise("news2")
#
# In lieu of an actual temperature sensor!
#
temperatures = range(15, 20)
humidities = range(0, 100)
while True:
temperature = random.choice(temperatures) + random.random()
humidity = random.choice(humidities)
nw0.send_news_to(address, "temperature", temperature)
time.sleep(random.random())
nw0.send_news_to(address, "humidity", humidity)
time.sleep(random.random())
Process B¶
import networkzero as nw0
address = nw0.discover("news2")
while True:
topic, temperature = nw0.wait_for_news_from(address, "temperature")
print("Temperature is:", temperature)
Process C¶
import networkzero as nw0
address = nw0.discover("news2")
while True:
topic, humidity = nw0.wait_for_news_from(address, "humidity")
print("Humidity is:", humidity)
Discussion¶
One process can send news on a variety of topics. Other processes can choose to listen to a specific topic or to all topics. In this example, Process A is sending news on temperature and humidity. Process B is listening to the temperature feed while process C is listening to the data about humidity.