std/net

the simulated network

Node event: Received [m: msg] from [a: node] (used as when [n: Node] (net.Received m from a)).

Trace events: Sent [m: msg] from [a: node] to [b: node], Arrived [m: msg] from [a: node] to [b: node], Dropped [m: msg] from [a: node] to [b: node] because [r: text], Partitioned [a: list of node] from [b: list of node], Healed.

Phrases

net.Send [m: msg] from [a: node] to [b: node]

Send through the network from a, which must be the receiver

Gives
—
Contexts
node
Kind
phrase, native
Source
net/net.synod:3
Examples
net/faults, net/pingpong

net.Latency between [a: time] and [b: time]

Uniform delay per message (default 10ms–30ms)

Gives
—
Contexts
main
Kind
phrase, native
Source
net/net.synod:6
Examples
net/pingpong

net.Loss [p: number]

Probability each message is lost (default 0)

Gives
—
Contexts
main
Kind
phrase, native
Source
net/net.synod:9
Examples
net/faults

net.Partition [a: list of node] from [b: list of node]

Cut every link between the two groups

Gives
—
Contexts
main
Kind
phrase, native
Source
net/net.synod:12
Examples
net/faults

net.Heal

Remove all partitions

Gives
—
Contexts
main
Kind
phrase, native
Source
net/net.synod:15
Examples
net/faults

net.DropNext [type: text] from [a: node] to [b: node]

Drop the next message of that record type (unqualified name) on that link

Gives
—
Contexts
main
Kind
phrase, native
Source
net/net.synod:18
Examples
net/faults

net.Id [m: msg]

The message id of the net fact that bound m

Gives
number
Contexts
look
Kind
phrase, native
Source
net/net.synod:39
Examples
net/pingpong

net.Type [m: msg]

The unqualified record type name of m

Gives
text
Contexts
look
Kind
phrase, native
Source
net/net.synod:42
Examples
net/pingpong

Node events

net.Received [m: msg] from [a: node]

Node event: a message arrives at the receiving node

Gives
—
Contexts
node
Kind
node event, native
Source
net/net.synod:21
Examples
net/faults, net/pingpong

Trace events

net.Sent [m: msg] from [a: node] to [b: node]

Trace event: net.Send m from a to b

Gives
—
Contexts
look
Kind
trace event, native
Source
net/net.synod:24
Examples
net/pingpong

net.Arrived [m: msg] from [a: node] to [b: node]

Trace event: the message is received

Gives
—
Contexts
look
Kind
trace event, native
Source
net/net.synod:27
Examples
net/pingpong

net.Dropped [m: msg] from [a: node] to [b: node] because [r: text]

Trace event: loss, partition, crash or net.DropNext

Gives
—
Contexts
look
Kind
trace event, native
Source
net/net.synod:30
Examples
net/faults

net.Partitioned [a: list of node] from [b: list of node]

Trace event: net.Partition

Gives
—
Contexts
look
Kind
trace event, native
Source
net/net.synod:33
Examples
net/faults

net.Healed

Trace event: net.Heal

Gives
—
Contexts
look
Kind
trace event, native
Source
net/net.synod:36
Examples
net/faults

Examples

faults

Shows net.Loss, net.DropNext, net.Partition, net.Heal and the events net.Dropped, net.Partitioned and net.Healed

# package main
# Shows net.Loss, net.DropNext, net.Partition, net.Heal and the events net.Dropped, net.Partitioned and net.Healed
use example.com/library/net/faults
use std/lesson
use std/net
use std/sim

define main
  set a to (sim.Spawn faults)
  set b to (sim.Spawn faults)
  set a.faults.Peer to b
  set b.faults.Peer to a
  after 1s
    net.DropNext "Beat" from a to b
    lesson.Note "The next Beat from n1 to n2 is dropped."
  after 2s
    net.Partition {a} from {b}
    lesson.Note "The network splits in two."
  after 3s
    net.Heal
    net.Loss 0.5
    lesson.Note "Half of all messages are lost."
  sim.StopAt 5s
# package faults
use std/look
use std/net
use std/sim
use std/theme
use std/time

Beat has
  set N to 0

Alert has
  set N to 0

Node has
  set Seen to 0
  set Peer to sim.Nobody

define [n: Node] start
  time.Every 500ms
    if (n.Peer is not sim.Nobody)
      net.Send (message Beat with N n.Seen) from n to n.Peer

when [n: Node] (net.Received Beat from sender)
  set n.Seen to (n.Seen plus 1)

when sim.Started
  look.Layout "row"

when (sim.Added n)
  draw (look.Card for n) as card for n

# A dropped message stops halfway; the reason tells why
when (net.Dropped m from a to b because why)
  draw (look.Label why) as reason
  look.Place reason above (card of b)
  look.Remove reason

when (net.Partitioned left from right)
  draw (look.Label "partitioned") as banner
  look.Place banner above look.Center
  look.Fill banner with theme.Critical

when net.Healed
  draw (look.Label "healed") as banner
  look.Place banner above look.Center
  look.Fill banner with theme.Good

pingpong

Shows net.Send, the net.Received event, net.Latency and the trace events net.Sent and net.Arrived with net.Id and net.Type

# package main
# Shows net.Send, the net.Received event, net.Latency and the trace events net.Sent and net.Arrived with net.Id and net.Type
use example.com/library/net/pingpong
use std/net
use std/sim

define main
  net.Latency between 100ms and 300ms
  set a to (sim.Spawn pingpong)
  set b to (sim.Spawn pingpong)
  set a.pingpong.Peer to b
  sim.StopAt 5s
# package pingpong
use std/look
use std/net
use std/sim
use std/text
use std/theme
use std/time

Ping has
  set Round to 0

Pong has
  set Round to 0

Node has
  set Peer to sim.Nobody
  set Rounds to 0

define [n: Node] start
  time.Every 1s
    if (n.Peer is not sim.Nobody)
      net.Send (message Ping with Round n.Rounds) from n to n.Peer

# net.Received runs on the receiving node, with the sender bound
when [n: Node] (net.Received Ping from sender)
  net.Send (message Pong with Round Ping.Round) from n to sender

when [n: Node] (net.Received Pong from sender)
  set n.Rounds to (n.Rounds plus 1)

when sim.Started
  look.Layout "row"

when (sim.Added n)
  draw (look.Card for n) as card for n

# Each message is a dot that flies from the sender to the receiver
when (net.Sent m from a to b)
  draw (look.Circle 6) as dot
  look.Fill dot with theme.Accent
  look.Flight dot from (card of a) to (card of b) for m

# net.Id and net.Type name the message in the look
when (net.Arrived m from a to b)
  draw (look.Label (text.Join {(net.Type m), " #", (text.From (net.Id m))})) as tag
  look.Place tag above (card of b)
  look.Remove tag