Compare commits
No commits in common. "c8806be02da5fa230a0b79c468817d802a14a863" and "a72cd8bef571d9ef627795a1a6fe532818d48cec" have entirely different histories.
c8806be02d
...
a72cd8bef5
@ -25,9 +25,9 @@ Packet format is uniform between master <-> slaves and is structured like so:
|
|||||||
```
|
```
|
||||||
{
|
{
|
||||||
// Metadata
|
// Metadata
|
||||||
|
magic: str("ccstoragenet")
|
||||||
networkid: int()
|
networkid: int()
|
||||||
sourceid: int()
|
sourceid: int()
|
||||||
sourcetype: str()
|
|
||||||
destid: int()
|
destid: int()
|
||||||
|
|
||||||
// Payload
|
// Payload
|
||||||
@ -43,17 +43,15 @@ Packet format is uniform between master <-> slaves and is structured like so:
|
|||||||
|
|
||||||
| Name | Required? | Type | Description
|
| 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 |
|
| `networkid` | true | int | The ID of the network |
|
||||||
| `sourceid` | true | int | The CC ID of the machine sending the packet |
|
| `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 |
|
| `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. |
|
| `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. |
|
| `itemname` | false | string | The unlocalized name of an item. Used for querying, crafting, movement, etc. |
|
||||||
| `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
|
||||||
|
@ -20,7 +20,6 @@ print("Computer " .. os.getComputerID() .. " configured as " .. mode)
|
|||||||
|
|
||||||
-- Common globals
|
-- Common globals
|
||||||
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)
|
|
||||||
-- Master globals
|
-- Master globals
|
||||||
-- Slave globals
|
-- Slave globals
|
||||||
|
|
||||||
@ -29,17 +28,18 @@ function c_mainLoop(loopfunc)
|
|||||||
-- Loops a thing forever
|
-- Loops a thing forever
|
||||||
while true do
|
while true do
|
||||||
loopfunc()
|
loopfunc()
|
||||||
|
sleep(0.10)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function c_waitForMessage()
|
function c_waitForMessage()
|
||||||
-- Waits for a message on the modem, timing out after 1 second
|
-- Waits for a message on the modem, timing out after 1 second
|
||||||
local sender, message = rednet.receive(packet_magic, 1)
|
local timeout = os.startTimer(1)
|
||||||
if message then
|
local event, side, channel, replyChannel, message, distance = os.pullEvent()
|
||||||
|
if (event == "modem_message") then
|
||||||
if
|
if
|
||||||
(not message["type"]) or -- Message type is required
|
(message["magic"] ~= packet_magic) or -- Magic is required per packet spec
|
||||||
(message["sourcetype"] == mode) or -- Ignore packets from our class of machines
|
(not message["type"]) -- Message type is required
|
||||||
(message["networkid"] ~= networkid) -- Ignore packets from other networks
|
then
|
||||||
then
|
|
||||||
print("Discarded nonconformant message")
|
print("Discarded nonconformant message")
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
@ -50,68 +50,76 @@ end
|
|||||||
function c_sendMessage(message)
|
function c_sendMessage(message)
|
||||||
-- Sends a message, either to the master or all slaves depending on mode
|
-- Sends a message, either to the master or all slaves depending on mode
|
||||||
local msg = {
|
local msg = {
|
||||||
|
magic = packet_magic,
|
||||||
networkid = networkid,
|
networkid = networkid,
|
||||||
sourceid = os.getComputerID(),
|
sourceid = os.getComputerID(),
|
||||||
sourcetype = mode,
|
--destid is optional
|
||||||
}
|
}
|
||||||
-- Override basic message object with args
|
-- Override basic message object with args
|
||||||
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)
|
-- 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
|
end
|
||||||
|
|
||||||
-- Master functions
|
-- Master functions
|
||||||
function m_loop()
|
function m_loop()
|
||||||
-- The main loop of the master server
|
-- The main loop of the master server
|
||||||
-- Listen for packets on the return net
|
-- Listen for packets on the return net
|
||||||
local msg = c_waitForMessage()
|
msg = c_waitForMessage()
|
||||||
if not msg then
|
if not msg then return end
|
||||||
return
|
sleep(1)
|
||||||
end
|
|
||||||
if msg["type"] == "pong" then
|
|
||||||
print("Received pong from slave: " .. msg["sourceid"])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Slave functions
|
-- Slave functions
|
||||||
function s_loop()
|
function s_loop()
|
||||||
-- The main loop of any slave nodes
|
-- The main loop of any slave nodes
|
||||||
-- Listen for packets from the master
|
-- Listen for packets from the master
|
||||||
local msg = c_waitForMessage()
|
msg = c_waitForMessage()
|
||||||
if not msg then
|
if not msg then
|
||||||
|
print(".")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if (msg["type"] == "ping") then
|
if (msg[type] == "ping") then
|
||||||
-- Respond to pings with pongs
|
-- Respond to pings with pongs
|
||||||
print("Received ping from master: " .. msg["sourceid"])
|
print("Received ping")
|
||||||
c_sendMessage({type="pong"})
|
c_sendMessage({type="pong"})
|
||||||
else
|
|
||||||
print("Unknown message: " .. textutils.serialize(msg))
|
|
||||||
end
|
end
|
||||||
|
sleep(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Application entrypoint
|
-- Application entrypoint
|
||||||
function main ()
|
function main ()
|
||||||
if (mode == "master") then
|
if (mode == "master") then
|
||||||
print("Beginning initialization as master...")
|
print("Beginning initialization as master...")
|
||||||
rednet.open(modem_side)
|
modem.open(port_return)
|
||||||
|
|
||||||
print("Pinging for slaves...")
|
print("Pinging for slaves...")
|
||||||
c_sendMessage({type="ping"})
|
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")
|
print("Entering main loop")
|
||||||
c_mainLoop(m_loop)
|
c_mainLoop(m_loop)
|
||||||
elseif (mode == "slave") then
|
elseif (mode == "slave") then
|
||||||
print("Beginning initialization as slave...")
|
print("Beginning initialization as slave...")
|
||||||
rednet.open(modem_side)
|
modem.open(port_broadcast)
|
||||||
|
|
||||||
print("Entering main loop")
|
print("Entering main loop")
|
||||||
c_mainLoop(s_loop)
|
c_mainLoop(s_loop)
|
||||||
else
|
else
|
||||||
error("Invalid mode: " .. mode .. ", please configure this node appropriately", 0)
|
error("Invalid mode: " .. mode .. ", please configure this node appropriately", 0)
|
||||||
end
|
end
|
||||||
|
modem.closeAll()
|
||||||
end
|
end
|
||||||
|
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue
Block a user