9 · Real networks

On a radio, messages get lost, devices fall out of step, messages have a size limit and numbers get rounded. The simulator can do all of this, so you can see it before flashing a board.

Lost messages, missed rounds

In config.js, loss: 0.3 loses 30% of the messages and skip: 0.3 makes devices miss 30% of their rounds. As on a board, a device keeps the last message it heard from each neighbour, and forgets a neighbour that stays silent for ttl rounds (3).

The gradient still gets there, more slowly. Now and then a node loses its best neighbour for a few rounds and its value rises for a moment: that happens on real networks too.

When a source goes away

Click the source to switch it off. The values climb by one every round, for ever: each node trusts its neighbours, which trust it back. With a maximum, a value over it becomes Infinity and the climb stops:

Pick a maximum a little above the longest distance you expect: below it, far nodes would lose the source. distanceTo(source, metric, max) works the same way.

The Bluetooth format

With target: 'ble' in config.js, the simulator sends the same binary messages as a board: decimals as 32-bit floats, operator names as 2-byte hashes, ids as wide as real ones. A message over 23 bytes goes in several adverts, sent in turn: hover a node to see them.

Each advert holds whole operators. A neighbour that misses one keeps those operators' values from the round before; with loss, each advert can be lost on its own. Up to 4 adverts fit in a listening window, about 90 bytes: what does not fit is left out, and a board says so on its console.

Operator names

On Bluetooth an operator's name is a 2-byte hash of its path, and two operators may get the same one: their values would then mix. The simulator records every path it sees, and when two collide the playground says so and suggests a fix:

Put them in config.js: To device copies them into start(). Every board must use the same values.

Decimals change on the way

A decimal number does not arrive exactly as it left: over Bluetooth it becomes a 32-bit float (about 7 digits), and Espruino's JSON keeps fewer digits than the browser's. So never compare with === a decimal received from a neighbour, for example to recognise a value of your own. Use integers for that, which always arrive intact: Math.floor(Math.random() * 8192) rather than Math.random().

Several programs in one room

Devices running different programs would mix their messages. Give each program a group, and devices only hear their own:

start(program, { target: 'ble', group: 'swarm', period: 1000 });

It costs no bytes: the group changes the first byte of every message. Over WiFi it works the same way, or you can use a different port.

Try it

1. With loss: 0.5, how much slower does the gradient settle? And with a ttl of 10?

2. Count the devices that reach the source, show the count everywhere, and keep it from climbing when the source goes away.

Solutions
var program = () => {
  var source = sense('source'), d = hopsTo(source, 20);
  return broadcast(source, collectSum(d, 1));
};

For the first one, add loss: 0.5 and then ttl: 10 to config.js (the config.js tab of the playground): a longer ttl rides out the losses, but forgets a neighbour that has really gone later.