Skip to content

Shared storage server

A Storage Server is shared by every program on its network. Here two device programs write into one ledger and the computer reads it.

You need: a computer, a repeater, a Storage Server ledger, an Inventory Module with an approved chest on its north side, an Entity Detector, a Display Module screen and two drives.

The warehouse (drive type inventory_module)

Section titled “The warehouse (drive type inventory_module)”

Every minute it writes the chest’s totals:

const ledger = storageServers.named("ledger")
on timer.every(1200) {
chest = device.inventory(Direction.NORTH)
ledger.write("stock", chest.items())
ledger.write("stock.day", device.time().day)
}

It keeps a short list of recent players:

const ledger = storageServers.named("ledger")
on device.entityEnter(entity) {
if not entity.player {
return
}
recent, err = ledger.read("visitors")
if err is not nil {
recent = []
}
recent.append(entity.name)
while recent.length > 20 {
recent.remove(0)
}
ledger.write("visitors", recent)
}
const ledger = storageServers.named("ledger")
const screen = displays.named("screen")
on timer.every(600) {
lines = "Ledger: " + str(ledger.keys().length) + " keys"
stock, err = ledger.read("stock")
if err is nil {
total = 0
for item in stock {
total += stock[item]
}
lines += "\nItems in stock: " + str(total)
}
visitors, err = ledger.read("visitors")
if err is nil and visitors.length > 0 {
lines += "\nLast visitor: " + visitors[visitors.length - 1]
}
space = ledger.usage()
lines += "\nUsed " + str(space.used) + " of " + str(space.capacity) + " bytes"
screen.write(lines)
}
  • Each program gets the server with storageServers.named("ledger"). Device programs may read and write it unless its owner switches Device programs to read in its form.
  • Programs never share a live list: every read gives a copy, and write stores a new copy. Two programs updating the same key at different times work, but the last write wins, so give each program its own keys.
  • usage() reports the space used of the server’s 256 KiB.