Compare commits
3 Commits
a72cd8bef5
...
c8806be02d
Author | SHA1 | Date | |
---|---|---|---|
c8806be02d | |||
bc46c8adef | |||
78be72c4b1 |
@ -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,15 +43,17 @@ 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,6 +20,7 @@ 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
|
||||||
|
|
||||||
@ -28,17 +29,16 @@ 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 timeout = os.startTimer(1)
|
local sender, message = rednet.receive(packet_magic, 1)
|
||||||
local event, side, channel, replyChannel, message, distance = os.pullEvent()
|
if message then
|
||||||
if (event == "modem_message") then
|
|
||||||
if
|
if
|
||||||
(message["magic"] ~= packet_magic) or -- Magic is required per packet spec
|
(not message["type"]) or -- Message type is required
|
||||||
(not message["type"]) -- Message type is required
|
(message["sourcetype"] == mode) or -- Ignore packets from our class of machines
|
||||||
|
(message["networkid"] ~= networkid) -- Ignore packets from other networks
|
||||||
then
|
then
|
||||||
print("Discarded nonconformant message")
|
print("Discarded nonconformant message")
|
||||||
return nil
|
return nil
|
||||||
@ -50,76 +50,68 @@ 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(),
|
||||||
--destid is optional
|
sourcetype = mode,
|
||||||
}
|
}
|
||||||
-- 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))
|
||||||
-- Transmit on different ports depending on mode
|
rednet.broadcast(msg, packet_magic)
|
||||||
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
|
||||||
msg = c_waitForMessage()
|
local msg = c_waitForMessage()
|
||||||
if not msg then return end
|
if not msg then
|
||||||
sleep(1)
|
return
|
||||||
end
|
end
|
||||||
|
if msg["type"] == "pong" then
|
||||||
|
print("Received pong from slave: " .. msg["sourceid"])
|
||||||
|
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
|
||||||
msg = c_waitForMessage()
|
local 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")
|
print("Received ping from master: " .. msg["sourceid"])
|
||||||
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...")
|
||||||
modem.open(port_return)
|
rednet.open(modem_side)
|
||||||
|
|
||||||
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...")
|
||||||
modem.open(port_broadcast)
|
rednet.open(modem_side)
|
||||||
|
|
||||||
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