Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local m = peripheral.find "modem"
- local function prompt(what, default)
- write(what .. "> ")
- local result = read()
- if result == "" then
- print("Defaulted to", textutils.serialise(default))
- return default
- end
- return result
- end
- local protocol = prompt("Protocol", nil)
- local sender = tonumber(prompt("Sender", os.getComputerID()))
- local recipient = tonumber(prompt("Recipient", rednet.CHANNEL_BROADCAST))
- m.open(rednet.CHANNEL_REPEAT)
- local function send(from, protocol, content, to)
- local packet = {
- nMessageID = math.random( 1, 2147483647 ),
- nRecipient = to,
- message = content,
- sProtocol = protocol,
- }
- m.transmit(to, from, packet)
- m.transmit(rednet.CHANNEL_REPEAT, from, packet)
- end
- local function receive()
- local _, _, chan, replyChan, message = os.pullEvent "modem_message"
- return message.nRecipient, replyChan, message.message, message.sProtocol
- end
- local function recvLoop()
- while true do
- local recipient, sender, message, protocol = receive()
- local text = sender .. ">" .. recipient
- if protocol then text = text .. " (" .. protocol .. ")" end
- text = text .. " " .. textutils.serialise(message)
- print(text)
- end
- end
- local function sendLoop()
- while true do
- write "|> "
- local msg = read()
- send(sender, protocol, msg, recipient)
- end
- end
- parallel.waitForAll(sendLoop, recvLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement