Compare commits

..

No commits in common. "c8806be02da5fa230a0b79c468817d802a14a863" and "a72cd8bef571d9ef627795a1a6fe532818d48cec" have entirely different histories.

2 changed files with 36 additions and 30 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,17 +43,15 @@ 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,7 +20,6 @@ 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
@ -29,17 +28,18 @@ 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 sender, message = rednet.receive(packet_magic, 1)
if message then
local timeout = os.startTimer(1)
local event, side, channel, replyChannel, message, distance = os.pullEvent()
if (event == "modem_message") then
if
(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
(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
@ -50,68 +50,76 @@ 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(),
sourcetype = mode,
--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))
rednet.broadcast(msg, packet_magic)
-- 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
-- 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
print("Received pong from slave: " .. msg["sourceid"])
end
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
local msg = c_waitForMessage()
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 from master: " .. msg["sourceid"])
print("Received ping")
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...")
rednet.open(modem_side)
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)
elseif (mode == "slave") then
print("Beginning initialization as slave...")
rednet.open(modem_side)
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()