Compare commits
2 Commits
c8806be02d
...
5dd175bb00
Author | SHA1 | Date | |
---|---|---|---|
5dd175bb00 | |||
9f2b3a8050 |
@ -14,9 +14,7 @@ The master node must have a modem attached and be within range of all slave node
|
|||||||
|
|
||||||
## Communication Protocol
|
## Communication Protocol
|
||||||
|
|
||||||
Port 42914 is used for Master => Slave broadcasting
|
Messages are sent over rednet with the protocol `ccstoragenet`.
|
||||||
|
|
||||||
Port 42915 is used for Slave => Master return broadcasting
|
|
||||||
|
|
||||||
### Packet Format
|
### Packet Format
|
||||||
|
|
||||||
@ -52,17 +50,15 @@ Packet format is uniform between master <-> slaves and is structured like so:
|
|||||||
| `itemquant` | false | int | Quantity of the aforementioned item |
|
| `itemquant` | false | int | Quantity of the aforementioned item |
|
||||||
| `destination` | false | int | The CC ID of the intended recipient of the 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
|
### Slave Node Initialization
|
||||||
|
|
||||||
* The slave starts up
|
* The slave starts up and immediately listens for messages
|
||||||
|
|
||||||
### Master Node Initialization
|
### Master Node Initialization
|
||||||
|
|
||||||
* The master node starts up
|
* The master node starts up
|
||||||
* The master node sends a packet with type `ping`.
|
* The master node sends a packet with type `ping`.
|
||||||
* The master node listens for `pong` packets for a configurable timeout.
|
* The master node listens for `pong` packets from all slaves.
|
||||||
* The `sourceid` of all `pong` packets is recorded for statistics displays. This cached data is only used for user display. Node availability is evaluated at request time.
|
* The `sourceid` of all `pong` packets is recorded for statistics displays. This cached data is only used for user display. Node availability is evaluated at request time.
|
||||||
|
|
||||||
### Inventory Search (query)
|
### Inventory Search (query)
|
||||||
|
@ -22,6 +22,7 @@ print("Computer " .. os.getComputerID() .. " configured as " .. mode)
|
|||||||
modem = peripheral.find("modem") or error("No modem attached", 0)
|
modem = peripheral.find("modem") or error("No modem attached", 0)
|
||||||
modem_side = peripheral.getName(modem)
|
modem_side = peripheral.getName(modem)
|
||||||
-- Master globals
|
-- Master globals
|
||||||
|
m_slaves = {}
|
||||||
-- Slave globals
|
-- Slave globals
|
||||||
|
|
||||||
-- Common functions
|
-- Common functions
|
||||||
@ -36,11 +37,11 @@ function c_waitForMessage()
|
|||||||
local sender, message = rednet.receive(packet_magic, 1)
|
local sender, message = rednet.receive(packet_magic, 1)
|
||||||
if message then
|
if message then
|
||||||
if
|
if
|
||||||
(not message["type"]) or -- Message type is required
|
(not message["type"]) or -- Message type is required
|
||||||
(message["sourcetype"] == mode) or -- Ignore packets from our class of machines
|
(message["sourcetype"] == mode) or -- Ignore packets from our class of machines
|
||||||
(message["networkid"] ~= networkid) -- Ignore packets from other networks
|
(message["networkid"] ~= networkid) or -- Ignore packets from other networks
|
||||||
|
(message["targetid"] and message["targetid"] ~= os.getComputerID) -- Ignore packets for other machines
|
||||||
then
|
then
|
||||||
print("Discarded nonconformant message")
|
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
return message
|
return message
|
||||||
@ -58,7 +59,7 @@ function c_sendMessage(message)
|
|||||||
for k,v in pairs(message) do
|
for k,v in pairs(message) do
|
||||||
msg[k] = v
|
msg[k] = v
|
||||||
end
|
end
|
||||||
print("Transmitting message: " .. textutils.serialize(msg))
|
--print("Transmitting message: " .. textutils.serialize(msg))
|
||||||
rednet.broadcast(msg, packet_magic)
|
rednet.broadcast(msg, packet_magic)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -71,9 +72,18 @@ function m_loop()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
if msg["type"] == "pong" then
|
if msg["type"] == "pong" then
|
||||||
print("Received pong from slave: " .. msg["sourceid"])
|
local source = msg["sourceid"]
|
||||||
|
print("Received pong from slave: " .. source)
|
||||||
|
m_slaves[source] = source
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
function m_ping()
|
||||||
|
-- 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
|
||||||
|
|
||||||
-- Slave functions
|
-- Slave functions
|
||||||
function s_loop()
|
function s_loop()
|
||||||
@ -99,8 +109,8 @@ function main ()
|
|||||||
rednet.open(modem_side)
|
rednet.open(modem_side)
|
||||||
|
|
||||||
print("Pinging for slaves...")
|
print("Pinging for slaves...")
|
||||||
c_sendMessage({type="ping"})
|
m_ping()
|
||||||
|
|
||||||
print("Entering main loop")
|
print("Entering main loop")
|
||||||
c_mainLoop(m_loop)
|
c_mainLoop(m_loop)
|
||||||
elseif (mode == "slave") then
|
elseif (mode == "slave") then
|
||||||
|
Loading…
Reference in New Issue
Block a user