Compare commits
13 Commits
a72cd8bef5
...
master
Author | SHA1 | Date | |
---|---|---|---|
b47a8ad4fa | |||
a1bb89a4d2 | |||
53bfc85164 | |||
bcbb419179 | |||
e45b1ea995 | |||
4b4c656af1 | |||
1f593d0542 | |||
ba51252476 | |||
5dd175bb00 | |||
9f2b3a8050 | |||
c8806be02d | |||
bc46c8adef | |||
78be72c4b1 |
@@ -4,72 +4,10 @@ This is an implementation of a centralized storage network architecture that use
|
||||
|
||||
## Deployment
|
||||
|
||||
The master node must have a modem attached and be within range of all slave nodes.
|
||||
First, consider using [someone else's project](https://github.com/lewark/inv.lua) instead of this one. It's liable to be a lot better than mine.
|
||||
|
||||
1. Deploy at least one slave turtle
|
||||
* The inventory it is to monitor must be above the unit
|
||||
* The unit should be able to drop below itself to push items to the master node
|
||||
* Any subsequent slaves should be placed in front of the unit
|
||||
2. Set up a return system that pushes into the first slave node (can be a hopper or similar)
|
||||
|
||||
## Communication Protocol
|
||||
|
||||
Port 42914 is used for Master => Slave broadcasting
|
||||
|
||||
Port 42915 is used for Slave => Master return broadcasting
|
||||
|
||||
### Packet Format
|
||||
|
||||
Packet format is uniform between master <-> slaves and is structured like so:
|
||||
|
||||
```
|
||||
{
|
||||
// Metadata
|
||||
magic: str("ccstoragenet")
|
||||
networkid: int()
|
||||
sourceid: int()
|
||||
destid: int()
|
||||
|
||||
// Payload
|
||||
type: str()
|
||||
|
||||
// Optional Arguments
|
||||
itemname: str()
|
||||
itemquant: int()
|
||||
destination: int()
|
||||
location: [int(),int(),int()]
|
||||
}
|
||||
```
|
||||
|
||||
| Name | Required? | Type | Description
|
||||
| :-- | :--: | :-- | :--
|
||||
| `magic` | true | string | Must be `ccstoragenet`. If it is not, the packet is immediately discarded |
|
||||
| `networkid` | true | int | The ID of the network |
|
||||
| `sourceid` | true | int | The CC ID of the machine sending the packet |
|
||||
| `destid` | true | int | The CC ID of the intended recipient of the packet |
|
||||
| `type` | true | string | An arbitrary string literal corresponding to the type of the request. Common examplse include `ping`, `query`, etc. |
|
||||
| `itemname` | false | string | The unlocalized name of an item. Used for querying, crafting, movement, etc. |
|
||||
| `itemquant` | false | int | Quantity of the aforementioned item |
|
||||
| `destination` | false | int | The CC ID of the intended recipient of the item |
|
||||
|
||||
### Slave Node Initialization
|
||||
|
||||
* The slave starts up
|
||||
|
||||
### Master Node Initialization
|
||||
|
||||
* The master node starts up
|
||||
* The master node sends a packet with type `ping`.
|
||||
* The master node listens for `pong` packets for a configurable timeout.
|
||||
* The `sourceid` of all `pong` packets is recorded for statistics displays. This cached data is only used for user display. Node availability is evaluated at request time.
|
||||
|
||||
### Inventory Search (query)
|
||||
|
||||
* A sender node (master or slave) sends out a `ping`
|
||||
* All available slaves respond with `pong`
|
||||
* The sender node sends a packet with type `query`. Field `itemname` is populated with an item to query for.
|
||||
* Each available slave responds with type `query`.
|
||||
* `itemname` is populated with data from the previous packet
|
||||
* `itemquantity` is populated with the quantity of that item attached to storages the slave has access to.
|
||||
* The requester waits for a response from each slave that responded to the `ping` or until a configurable timeout is reached.
|
||||
* Results are consumed
|
||||
1. Deploy a Smart Computer
|
||||
2. Connect the computer to at least 1 chest and 1 hopper via physical modem lines. Chests need to be attached from the bottom and hoppers from their output direction
|
||||
3. Put a chest on top of the Smart Computer
|
||||
4. Install Basalt (`wget run https://basalt.madefor.cc/install.lua release latest.lua`)
|
||||
5. Deploy the script (as `startup`, of course)
|
@@ -3,123 +3,88 @@
|
||||
-- For information on this script, physical in-world setup, and configuration,
|
||||
-- see: https://git.desu.ltd/salt/mc-scripts/src/branch/master/storage-net
|
||||
|
||||
-- Default configuration values. Override in config.lua. DO NOT CHANGE HERE
|
||||
mode = "ConfigureMe" -- The mode of function for this node
|
||||
networkid = 0 -- Unique ID for this network
|
||||
port_broadcast = 42914 -- Port for M->S traffic
|
||||
port_return = 42915 -- Port for S->M traffic
|
||||
packet_magic = "ccstoragenet" -- Just a random string
|
||||
|
||||
-- This loads config.lua. See masterconfig.lua and slaveconfig.lua for example
|
||||
-- configurations.
|
||||
require "config"
|
||||
|
||||
-- Startup diagnostics
|
||||
print("Salt's CC Storage Net")
|
||||
print("Computer " .. os.getComputerID() .. " configured as " .. mode)
|
||||
|
||||
-- Common globals
|
||||
modem = peripheral.find("modem") or error("No modem attached", 0)
|
||||
-- Master globals
|
||||
-- Slave globals
|
||||
-- Global scope locals
|
||||
local output = peripheral.wrap("top") or error("Put a chest on top of this terminal for output items", 0)
|
||||
|
||||
-- Helper functions
|
||||
function getDepositHoppers()
|
||||
-- Get all connected inventories that push items into the system
|
||||
return {peripheral.find("inventory", function(name,i)
|
||||
return string.find(name, "minecraft:hopper")
|
||||
end)}
|
||||
end
|
||||
function getConnectedChests()
|
||||
-- Get all connected inventories that store items
|
||||
return {peripheral.find("inventory", function(name,i)
|
||||
return string.find(name, "minecraft:chest")
|
||||
end)}
|
||||
end
|
||||
|
||||
-- Common functions
|
||||
function c_mainLoop(loopfunc)
|
||||
-- Loops a thing forever
|
||||
while true do
|
||||
loopfunc()
|
||||
sleep(0.10)
|
||||
end
|
||||
end
|
||||
function c_waitForMessage()
|
||||
-- Waits for a message on the modem, timing out after 1 second
|
||||
local timeout = os.startTimer(1)
|
||||
local event, side, channel, replyChannel, message, distance = os.pullEvent()
|
||||
if (event == "modem_message") then
|
||||
function pushDepositsToChests()
|
||||
-- Take all the contents of all connected hoppers and stuff them into any other connected inventory
|
||||
-- These things are wrapped into tables because that's how you take multiple return values and stuff them in a table, apparently
|
||||
local hoppers = getDepositHoppers()
|
||||
local chests = getConnectedChests()
|
||||
-- For each hopper connected to the network...
|
||||
for k,hopper in ipairs(hoppers) do
|
||||
-- For each item in that hopper's inventory...
|
||||
for hslot,hitem in pairs(hopper.list()) do
|
||||
-- For each connected "chest"...
|
||||
for k,chest in ipairs(chests) do
|
||||
-- First, make an attempt to find slots that we can shove the item into
|
||||
for cslot,citem in pairs(chest.list()) do
|
||||
if
|
||||
(message["magic"] ~= packet_magic) or -- Magic is required per packet spec
|
||||
(not message["type"]) -- Message type is required
|
||||
citem["name"] == hitem["name"] and -- We have the same item
|
||||
citem["count"] < chest.getItemLimit(cslot) -- There's space in this slot
|
||||
then
|
||||
print("Discarded nonconformant message")
|
||||
return nil
|
||||
end
|
||||
return message
|
||||
end
|
||||
return nil
|
||||
end
|
||||
function c_sendMessage(message)
|
||||
-- Sends a message, either to the master or all slaves depending on mode
|
||||
local msg = {
|
||||
magic = packet_magic,
|
||||
networkid = networkid,
|
||||
sourceid = os.getComputerID(),
|
||||
--destid is optional
|
||||
}
|
||||
-- Override basic message object with args
|
||||
for k,v in pairs(message) do
|
||||
msg[k] = v
|
||||
end
|
||||
print("Transmitting message: " .. textutils.serialize(msg))
|
||||
-- Transmit on different ports depending on mode
|
||||
if (mode == "master") then
|
||||
modem.transmit(port_broadcast, port_return, msg)
|
||||
elseif (mode == "slave") then
|
||||
modem.transmit(port_return, port_broadcast, msg)
|
||||
hopper.pushItems(peripheral.getName(chest),hslot,hitem["count"],cslot)
|
||||
end
|
||||
end
|
||||
-- Master functions
|
||||
function m_loop()
|
||||
-- The main loop of the master server
|
||||
-- Listen for packets on the return net
|
||||
msg = c_waitForMessage()
|
||||
if not msg then return end
|
||||
sleep(1)
|
||||
end
|
||||
-- Slave functions
|
||||
function s_loop()
|
||||
-- The main loop of any slave nodes
|
||||
-- Listen for packets from the master
|
||||
msg = c_waitForMessage()
|
||||
if not msg then
|
||||
print(".")
|
||||
return
|
||||
-- We've fallen through trying to fill up existing stacks. Fragmentation is not a concern, put it wherever
|
||||
if hopper.getItemDetail(hslot) then
|
||||
for k,chest in ipairs(chests) do
|
||||
hopper.pushItems(peripheral.getName(chest),hslot)
|
||||
end
|
||||
if (msg[type] == "ping") then
|
||||
-- Respond to pings with pongs
|
||||
print("Received ping")
|
||||
c_sendMessage({type="pong"})
|
||||
end
|
||||
sleep(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
function requestItem(name, count)
|
||||
-- Pull a number of an item into the output chest above the machine
|
||||
remaining = count
|
||||
local chests = getConnectedChests()
|
||||
-- For each chest on the network...
|
||||
for k,chest in ipairs(chests) do
|
||||
-- For each slot in that chest...
|
||||
for cslot,citem in pairs(chest.list()) do
|
||||
if
|
||||
citem["name"] == name and
|
||||
citem["count"] > 0
|
||||
then
|
||||
desired = math.max(remaining, citem["count"])
|
||||
output.pullItems(peripheral.getName(chest),cslot,desired)
|
||||
remaining = remaining - desired
|
||||
end
|
||||
if remaining <= 0 then return true end
|
||||
end
|
||||
end
|
||||
if remaining > 0 then return false end
|
||||
end
|
||||
|
||||
-- Application entrypoint
|
||||
function main()
|
||||
if (mode == "master") then
|
||||
print("Beginning initialization as master...")
|
||||
modem.open(port_return)
|
||||
|
||||
print("Pinging for slaves...")
|
||||
c_sendMessage({type="ping"})
|
||||
sleep(3)
|
||||
local msg = true
|
||||
while msg ~= nil do
|
||||
msg = c_waitForMessage()
|
||||
if (msg and msg[type] == "pong") then
|
||||
print("Received pong from " .. msg["sourceid"])
|
||||
requestItem("minecraft:sandstone", 12)
|
||||
while true do
|
||||
-- Manage inventory cleanup tasks
|
||||
pushDepositsToChests() -- Take all deposit terminals and push them into the inventory network
|
||||
-- Sleep for a tick
|
||||
sleep(0.05) -- This is to stop CC from killing us
|
||||
end
|
||||
end
|
||||
print("Entering main loop")
|
||||
c_mainLoop(m_loop)
|
||||
elseif (mode == "slave") then
|
||||
print("Beginning initialization as slave...")
|
||||
modem.open(port_broadcast)
|
||||
|
||||
print("Entering main loop")
|
||||
c_mainLoop(s_loop)
|
||||
else
|
||||
error("Invalid mode: " .. mode .. ", please configure this node appropriately", 0)
|
||||
end
|
||||
modem.closeAll()
|
||||
end
|
||||
|
||||
main()
|
@@ -10,17 +10,11 @@ mcroot="$HOME/.var/app/org.prismlauncher.PrismLauncher/data/PrismLauncher/instan
|
||||
world="New World"
|
||||
|
||||
masterid="0"
|
||||
slaves=("1" "8" "9")
|
||||
|
||||
_ccroot="$mcroot/.minecraft/saves/$world/computercraft/computer"
|
||||
echo "Root: $_ccroot"
|
||||
for computer in "$masterid" ${slaves[*]}; do
|
||||
for computer in "$masterid"; do
|
||||
echo "Deploying to: $computer"
|
||||
mkdir -p "$_ccroot/$computer"
|
||||
cp common.lua "$_ccroot/$computer/autostart.lua"
|
||||
done
|
||||
cp masterconfig.lua "$_ccroot/$masterid/config.lua"
|
||||
for slave in ${slaves[*]}; do
|
||||
echo "Deploying slave config: $slave"
|
||||
cp slaveconfig.lua "$_ccroot/$slave/config.lua"
|
||||
cp common.lua "$_ccroot/$computer/startup"
|
||||
done
|
@@ -1 +0,0 @@
|
||||
mode = "master"
|
@@ -1 +0,0 @@
|
||||
mode = "slave"
|
Reference in New Issue
Block a user