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
This commit is contained in:
parent
a72cd8bef5
commit
78be72c4b1
@ -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
|
||||
|
@ -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
|
||||
|
||||
@ -33,13 +34,13 @@ function c_mainLoop(loopfunc)
|
||||
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
|
||||
then
|
||||
(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
|
||||
end
|
||||
@ -50,76 +51,70 @@ 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
|
||||
sleep(1)
|
||||
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(".")
|
||||
sleep(1)
|
||||
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()
|
Loading…
Reference in New Issue
Block a user