Well this is the addition that you (and every hacker who ever used ComputerCraft) has been waiting for. The joy of having to create a whole new layer of security on top of our rednet enabled programs is finally upon us, and oh what glorious fun we will have. Those awful, infallibly secure modems will no longer be tolerated! What is this addition that I speak of? The introduction of channels to rednet!
What are channels exactly? Well in terms of ComputerCraft, channels are, quite simply, an open network which you can send and receive rednet messages on. There are several advantages to this, but I will leave it to you to deduce what they are.
How do channels work? Well first off, you're going to have to come to terms with the fact that you cannot access channel functionality through the rednet API. To use this awesome new feature, we're going to have to do some hardcore peripheral wrapping.
--So you want to use channels, eh?
--The first thing you'll need to do is wrap your modem '
--My modem just happens to be on the top of the computer, but you can place modems on any side
local modem = peripheral.wrap("top") --Wow. That was easy.
Yup. That's it. Now you have access to all of the functions which a modem has through the "modem" variable. These exposed functions are:
modem.open(integer channel)
modem.isOpen(integer channel)
modem.transmit(integer channel, integer replyChannel, string message)
modem.close(integer channel)
modem.closeAll()
Opening channels:
Opening channels is simple. All you have to do is…
local desiredChannel = 1
modem.open(desiredChannel)
But what does opening a channel mean exactly? Well, until you've opened a channel you cannot receive any messages on that channel. You can send messages though, so if all you need to do is send a bunch of messages then there is no need to open a channel.Note: You can open 128 channels at any given time and receive messages on them.
Note 2: The largest channel you can open is 65535
Transmiting messages:
Transmitting messages is pretty simple too. Use the transmit function like so:
local sendOnChannel = 1
local replyChannel = 2
modem.transmit(sendOnChannel, replyChannel, "Ping!")
--[["
You've just sent a message on channel 1. Any computers that have opened
channel 1 can receive the message. You've also specified that the computer
should reply on channel 2.
"--]]
Receiving messages:
Receiving a message is simple as well. Everything boils down to events!
local modem = peripheral.wrap("top")
modem.open(1) --Receive messages from computers sending on channel 1
modem.open(2) --Opening channel 2 just for the heck of it
local messageArguments = {os.pullEvent("modem_message")}
for i,v in pairs(messageArguments) do
print(v)
end
This will output the following if you use my transmit message code from above:
modem_message
top
1
2
Ping!
3 --This is the distance that two computers are apart.
And that's it! Channels are actually fairly simple, but as you can imagine they can be quite powerful.
Edit: Since I noticed that there was some confusion about being able to open more than one channel at the same time, here is an example of doing just that.
local modem = peripheral.wrap("top")
local userChannels = { --Define the channels that regular users send on
[1] = true;
[3] = true;
[5] = true;
[7] = true;
}
local adminChannels = { --Define the channels that admins send on
[2] = true;
[4] = true;
[6] = true;
[8] = true;
}
for i=1,8 do --Open all of the aforementioned channels
modem.open(i)
end
while true do
local message = {os.pullEvent("modem_message")} --Receive messages
if userChannels[message[3]] then --If that message is sent on a channel that is defined in userChannels then
print("User channel")
else
print("Admin channel")
end
end
Adieu,
Bubba