Utilisation de 3 fonctions de base de rednet - open(side)
, broadcast(message, protocol)
y receive(protocol, timeout)
il est possible de le faire.
Contrôle Code informatique :
print('Main Computer')
print(' Enter any command to broadcast to slaves')
rednet.open('top') -- Open modem on top
while true do -- Run this forever
io.write('>>> ')
command = io.read() -- Read a command from stdin
if command == 'exit' then -- If it's exit
rednet.close('top') -- Close top modem
return -- Exit
end
rednet.broadcast(command, 'slave') -- Otherwise, broadcast the command with protocol slave
end
Code esclave :
rednet.open('top') -- Same, open top
while true do -- Run forever
id, data = rednet.receive('slave') -- Receive message under protocol 'slave'
print('Received command ' .. data)
if data == 'quit' then -- If it's quit
rednet.close('top') -- Cleanup, close top modem
print('Quitting...')
return -- Exit
elseif string.match(data, 'math .+') then -- Regex: command starts with math
print('Math:')
equation = string.match(data, 'math (.+)') -- Regex: get actual equation
print(equation .. ' = ' .. loadstring('return ' .. equation)()) -- Eval it. I'm not responsible for attacks.
end
end