Compare commits

..

4 Commits

Author SHA1 Message Date
a72cd8bef5 Work on init code 2024-02-25 23:33:32 -06:00
164006f4b0 Remove requirement for a GPS 2024-02-25 23:01:57 -06:00
b5ea97b5d9 More boilerplate 2024-02-25 23:01:37 -06:00
75b9634ea9 Minor polish 2024-02-25 22:47:00 -06:00
2 changed files with 85 additions and 11 deletions

View File

@ -4,15 +4,13 @@ This is an implementation of a centralized storage network architecture that use
## Deployment ## Deployment
A GPS will need to be established and be accurate within range of the entire network to the block.
Slave nodes must have a modem attached and be within range of the GPS and master.
The master node must have a modem attached and be within range of all slave nodes. The master node must have a modem attached and be within range of all slave nodes.
1. Deploy at least one slave node 1. Deploy at least one slave turtle
* The inventory it is to monitor must be above or in front of the unit * 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 * 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 ## Communication Protocol

View File

@ -4,10 +4,11 @@
-- see: https://git.desu.ltd/salt/mc-scripts/src/branch/master/storage-net -- see: https://git.desu.ltd/salt/mc-scripts/src/branch/master/storage-net
-- Default configuration values. Override in config.lua. DO NOT CHANGE HERE -- Default configuration values. Override in config.lua. DO NOT CHANGE HERE
mode = "undefined" -- The mode of function for this node mode = "ConfigureMe" -- The mode of function for this node
networkid = 0 -- Unique ID for this network networkid = 0 -- Unique ID for this network
port_broadcast = 42914 -- Port for M->S traffic port_broadcast = 42914 -- Port for M->S traffic
port_return = 42915 -- Port for S->M 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 -- This loads config.lua. See masterconfig.lua and slaveconfig.lua for example
-- configurations. -- configurations.
@ -15,35 +16,110 @@ require "config"
-- Startup diagnostics -- Startup diagnostics
print("Salt's CC Storage Net") print("Salt's CC Storage Net")
print("Computer ID: " .. os.getComputerID()) print("Computer " .. os.getComputerID() .. " configured as " .. mode)
print("Computer " .. os.getComputerID() .. " running as " .. mode)
-- Common globals
modem = peripheral.find("modem") or error("No modem attached", 0)
-- Master globals
-- Slave globals
-- Common functions -- Common functions
function c_mainLoop(loopfunc) function c_mainLoop(loopfunc)
-- Loops a thing forever -- Loops a thing forever
while true do while true do
loopfunc() loopfunc()
sleep(1) 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
if
(message["magic"] ~= packet_magic) or -- Magic is required per packet spec
(not message["type"]) -- Message type is required
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)
end end
end end
-- Master functions -- Master functions
function m_loop() function m_loop()
-- The main loop of the master server -- 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 end
-- Slave functions -- Slave functions
function s_loop() function s_loop()
-- The main loop of any slave nodes -- The main loop of any slave nodes
-- Listen for packets from the master
msg = c_waitForMessage()
if not msg then
print(".")
return
end
if (msg[type] == "ping") then
-- Respond to pings with pongs
print("Received ping")
c_sendMessage({type="pong"})
end
sleep(1)
end end
-- Application entrypoint -- Application entrypoint
function main () function main ()
if (mode == "master") then 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"])
end
end
print("Entering main loop")
c_mainLoop(m_loop) c_mainLoop(m_loop)
elseif (mode == "slave") then elseif (mode == "slave") then
print("Beginning initialization as slave...")
modem.open(port_broadcast)
print("Entering main loop")
c_mainLoop(s_loop) c_mainLoop(s_loop)
else else
error("Invalid mode: " .. mode .. ", please configure this node appropriately") error("Invalid mode: " .. mode .. ", please configure this node appropriately", 0)
end end
modem.closeAll()
end end
main() main()