Compare commits

..

3 Commits

Author SHA1 Message Date
c8806be02d Remove sleep from loop func
This solves all our rednet issues, seemingly
2024-02-26 00:24:40 -06:00
bc46c8adef Remove sleeps from loops, as waitformessage will do that for us 2024-02-26 00:22:10 -06:00
78be72c4b1 Move to rednet
This sucks.
It has broadcast, which is required for the project, but if you're not listening when a packet ships then you drop it
2024-02-26 00:20:08 -06:00
2 changed files with 30 additions and 36 deletions

View File

@ -25,9 +25,9 @@ Packet format is uniform between master <-> slaves and is structured like so:
```
{
// Metadata
magic: str("ccstoragenet")
networkid: int()
sourceid: int()
sourcetype: str()
destid: int()
// Payload
@ -43,15 +43,17 @@ Packet format is uniform between master <-> slaves and is structured like so:
| 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 |
| `sourcetype` | true | string | The mode 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 |
Messages are sent over rednet with the protocol `ccstoragenet`.
### Slave Node Initialization
* The slave starts up

View File

@ -20,6 +20,7 @@ print("Computer " .. os.getComputerID() .. " configured as " .. mode)
-- Common globals
modem = peripheral.find("modem") or error("No modem attached", 0)
modem_side = peripheral.getName(modem)
-- Master globals
-- Slave globals
@ -28,17 +29,16 @@ 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
local sender, message = rednet.receive(packet_magic, 1)
if message then
if
(message["magic"] ~= packet_magic) or -- Magic is required per packet spec
(not message["type"]) -- Message type is required
(not message["type"]) or -- Message type is required
(message["sourcetype"] == mode) or -- Ignore packets from our class of machines
(message["networkid"] ~= networkid) -- Ignore packets from other networks
then
print("Discarded nonconformant message")
return nil
@ -50,76 +50,68 @@ 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
sourcetype = mode,
}
-- 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
rednet.broadcast(msg, packet_magic)
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)
local msg = c_waitForMessage()
if not msg then
return
end
if msg["type"] == "pong" then
print("Received pong from slave: " .. msg["sourceid"])
end
end
-- Slave functions
function s_loop()
-- The main loop of any slave nodes
-- Listen for packets from the master
msg = c_waitForMessage()
local msg = c_waitForMessage()
if not msg then
print(".")
return
end
if (msg[type] == "ping") then
if (msg["type"] == "ping") then
-- Respond to pings with pongs
print("Received ping")
print("Received ping from master: " .. msg["sourceid"])
c_sendMessage({type="pong"})
else
print("Unknown message: " .. textutils.serialize(msg))
end
sleep(1)
end
-- Application entrypoint
function main ()
if (mode == "master") then
print("Beginning initialization as master...")
modem.open(port_return)
rednet.open(modem_side)
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)
elseif (mode == "slave") then
print("Beginning initialization as slave...")
modem.open(port_broadcast)
rednet.open(modem_side)
print("Entering main loop")
c_mainLoop(s_loop)
else
error("Invalid mode: " .. mode .. ", please configure this node appropriately", 0)
end
modem.closeAll()
end
main()