mc-scripts/storage-net/common.lua

127 lines
4.0 KiB
Lua
Raw Normal View History

2024-02-25 22:44:46 -06:00
-- Salt's ComputerCraft Storage Network Script
--
-- 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
2024-02-25 22:47:00 -06:00
mode = "ConfigureMe" -- The mode of function for this node
2024-02-25 22:44:46 -06:00
networkid = 0 -- Unique ID for this network
port_broadcast = 42914 -- Port for M->S traffic
port_return = 42915 -- Port for S->M traffic
2024-02-25 23:33:32 -06:00
packet_magic = "ccstoragenet" -- Just a random string
2024-02-25 22:44:46 -06:00
-- This loads config.lua. See masterconfig.lua and slaveconfig.lua for example
-- configurations.
2024-02-25 22:22:02 -06:00
require "config"
2024-02-25 22:44:46 -06:00
-- Startup diagnostics
print("Salt's CC Storage Net")
2024-02-25 23:33:32 -06:00
print("Computer " .. os.getComputerID() .. " configured as " .. mode)
2024-02-25 22:44:46 -06:00
2024-02-25 22:47:00 -06:00
-- Common globals
2024-02-25 23:01:37 -06:00
modem = peripheral.find("modem") or error("No modem attached", 0)
modem_side = peripheral.getName(modem)
2024-02-25 22:47:00 -06:00
-- Master globals
m_slaves = {}
2024-02-25 22:47:00 -06:00
-- Slave globals
2024-02-25 22:44:46 -06:00
-- Common functions
function c_mainLoop(loopfunc)
-- Loops a thing forever
while true do
loopfunc()
end
end
2024-02-25 23:33:32 -06:00
function c_waitForMessage()
-- Waits for a message on the modem, timing out after 1 second
local sender, message = rednet.receive(packet_magic, 1)
if message then
2024-02-25 23:33:32 -06:00
if
(not message["type"]) or -- Message type is required
(message["sourcetype"] == mode) or -- Ignore packets from our class of machines
(message["networkid"] ~= networkid) or -- Ignore packets from other networks
(message["targetid"] and message["targetid"] ~= os.getComputerID) -- Ignore packets for other machines
then
2024-02-25 23:33:32 -06:00
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 = {
networkid = networkid,
sourceid = os.getComputerID(),
sourcetype = mode,
2024-02-25 23:33:32 -06:00
}
-- Override basic message object with args
for k,v in pairs(message) do
msg[k] = v
end
2024-02-26 00:40:16 -06:00
--print("Transmitting message: " .. textutils.serialize(msg))
rednet.broadcast(msg, packet_magic)
2024-02-25 23:33:32 -06:00
end
2024-02-25 23:33:32 -06:00
-- Master functions
function m_loop()
-- The main loop of the master server
-- Listen for packets on the return net
local msg = c_waitForMessage()
if not msg then
return
end
if msg["type"] == "pong" then
2024-02-26 00:40:16 -06:00
local source = msg["sourceid"]
print("Received pong from slave: " .. source)
m_slaves[source] = source
end
2024-02-25 22:44:46 -06:00
end
function m_ping()
2024-02-26 00:40:16 -06:00
-- Clear out the list of all slaves and send out a fresh ping
-- Ping information isn't used for much, so delay in clearing cache and
-- repopulating the data isn't a big deal.
m_slaves = {}
c_sendMessage({type="ping"})
end
2024-02-25 22:44:46 -06:00
-- Slave functions
function s_loop()
-- The main loop of any slave nodes
2024-02-25 23:01:37 -06:00
-- Listen for packets from the master
local msg = c_waitForMessage()
2024-02-25 23:33:32 -06:00
if not msg then
return
end
if (msg["type"] == "ping") then
2024-02-25 23:33:32 -06:00
-- Respond to pings with pongs
print("Received ping from master: " .. msg["sourceid"])
2024-02-25 23:33:32 -06:00
c_sendMessage({type="pong"})
else
print("Unknown message: " .. textutils.serialize(msg))
2024-02-25 23:33:32 -06:00
end
2024-02-25 22:44:46 -06:00
end
-- Application entrypoint
function main ()
if (mode == "master") then
2024-02-25 23:33:32 -06:00
print("Beginning initialization as master...")
rednet.open(modem_side)
2024-02-25 23:33:32 -06:00
print("Pinging for slaves...")
2024-02-26 00:40:16 -06:00
m_ping()
2024-02-25 23:33:32 -06:00
print("Entering main loop")
2024-02-25 22:44:46 -06:00
c_mainLoop(m_loop)
elseif (mode == "slave") then
2024-02-25 23:33:32 -06:00
print("Beginning initialization as slave...")
rednet.open(modem_side)
2024-02-25 23:33:32 -06:00
print("Entering main loop")
2024-02-25 22:44:46 -06:00
c_mainLoop(s_loop)
else
2024-02-25 23:01:37 -06:00
error("Invalid mode: " .. mode .. ", please configure this node appropriately", 0)
2024-02-25 22:44:46 -06:00
end
end
main()