Navigation

  • index
  • modules |
  • next |
  • previous |
  • NetworkZero 1.0beta1 documentation »
  • NetworkZero »
  • Usage »

Table of Contents

  • Advertise & Discover Services: Advanced Usage
    • Discover a group of services
      • On machine A
      • On machine B
      • On machine C
      • Discussion
    • Use a different separator for a group of services
      • Discussion
    • Dynamically discover services
      • Node A
      • Node B
      • Node C
      • Master
      • Discussion

Previous topic

Exchanging Messages

Next topic

Exchanging Messages: Advanced Usage

This Page

  • Show Source

Quick search

Advertise & Discover Services: Advanced Usage¶

The examples here all refer to the networkzero.discovery module.

Discover a group of services¶

On machine A¶

import networkzero as nw0

alice = nw0.advertise("chat/alice")

On machine B¶

import networkzero as nw0

bob = nw0.advertise("chat/bob")

On machine C¶

import networkzero as nw0

services = dict(nw0.discover_group("chat"))
print(services)

Discussion¶

By adopting a common convention, it is possible to group advertised services together. This could be used, as in this example, to identify all members of a particular chat session. Or—in a classroom or club situation—to have teams identify their own services so as not to confuse them with other team’s services of the same name.

Use a different separator for a group of services¶

import networkzero as nw0

alice = nw0.advertise("chat:alice")
bob = nw0.advertise("chat:bob")
doug = nw0.advertise("chat:doug")
services = dict(nw0.discover_group("chat", separator=":"))

print(services)

Discussion¶

Perhaps it’s not convenient to use the “/” character to separate the group from the service name. You can use any character to separate the parts of the name. Then you specify that character when you call discover_group.

Dynamically discover services¶

Node A¶

import networkzero as nw0

name = "A"

nw0.advertise("cluster/%s" % name)
master = nw0.discover("cluster/master")

while True:
    command = nw0.wait_for_message_from(master, autoreply=True)

    #
    # ... something goes wrong
    #
    raise RuntimeError

Node B¶

import networkzero as nw0

name = "B"

nw0.advertise("cluster/%s" % name)
master = nw0.discover("cluster/master")

while True:
    command = nw0.wait_for_message_from(master, autoreply=True)
    #
    # Do useful things until told to stop
    #
    if command == "STOP":
        break

Node C¶

import networkzero as nw0

name = "C"

#
# This machine only starts up a few seconds after the others
#
time.sleep(3)
nw0.advertise("cluster/%s" % name)
master = nw0.discover("cluster/master")

while True:
    command = nw0.wait_for_message_from(master, autoreply=True)
    #
    # Do useful things until told to stop
    #
    if command == "STOP":
        break

Master¶

import networkzero as nw0

name = "cluster/master"
address = nw0.advertise(name)

#
# On the cluster master
#
nodes = set(nw0.discover_group("cluster", exclude=name))
old_nodes = nodes
print("Nodes:", ", ".join(nodes))

#
# Send a command to A .... which will unaccountably fail
#
node_a = nw0.discover("cluster/A")
nw0.send_message_to(node_a, "STOP")

#
# Wait a few seconds for node C to wake up
#
time.sleep(5)

#
# On the cluster master
#
nodes = set(nw0.discover_group("cluster"))
for name, address in old_nodes - nodes:
    print("%s has left the cluster" % name)
for name, address in nodes - old_nodes:
    print("%s has joined the cluster" % name)

for name, address in nodes:
    nw0.send_message_to(address, "STOP")

Discussion¶

A cluster is a group of computers working together. Each computer registers a service called “cluster/X” where “X” is the computer’s name. If that machine stops running, its advert will time out and no longer be available.

The computer which is co-ordinating the cluster checks every so often to see which computers have left the cluster and which have joined.

Navigation

  • index
  • modules |
  • next |
  • previous |
  • NetworkZero 1.0beta1 documentation »
  • NetworkZero »
  • Usage »
© Copyright 2016, Tim Golden. Created using Sphinx 1.8.6.