This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Galactica4's profile picture

Rednet file sharing question

Started by Galactica4, 13 July 2013 - 07:00 PM
Galactica4 #1
Posted 13 July 2013 - 09:00 PM
Hi everyone
I am making an Rednet program that multiple computers connect to a central computer that distributes files but they are all on the same channel and I need a way to have the central computer automatically detect which computer on the network sent it a message so it can automatically send a message back based on what you sent it. i already have a way to send files over rednet. this may be confusing so I am going to e explain in the order of events that I want to happen

1 . An local computer (we are going to call this client1) needs a file that is remotely stored on another computer (server computer)
2. Another computer (client2) also needs some files stored on the server computer
3. Client 1 send the keyword for the file that it needs to the server computer (the keyword is keyword)
4. Server computer receives an message (the keyword) on the file sharing channel and detects which computer (client1) sent the message and sends the appropriate file back.
5. Client 2 does the same thing but with a different keyword for file/files

Now I want a way for the server computer to recognize the difference and send the required file to the computer that requested it ie the files don't mix and go to both computers
:)/>
Confusing huh?
CCJJSax #2
Posted 13 July 2013 - 09:34 PM
I haven't really messed with rednet, I'm sad to say. However, I would assume your issue might require fs.open() or fs.write(). look here under the "F" section and it'll have some of the things you're looking for. I'm not the most knowledged in this area and I wouldn't be totally shocked if I'm wrong. I'm actually studying up as we speak on a similar issue.
Bubba #3
Posted 13 July 2013 - 09:39 PM
See below for a better response
SpoilerWell if you insist on using only one channel, it is possible to filter the received files via the individual computers. However, the server itself would not be able to control which computers received the file.

What I would do to filter out unwanted messages would be something like this:


local CUID = tostring(os.getComputerId())

local function validateRecipient(message)
  local recipient,newMessage = message:match("(%d*)|")
  if recipient and recipient == CUID then
    return newMessage
  end
end

local example_message = "25|The rest of the file is as follows. Hello!"
local validMessage = validateRecipient(example_message)
if validMessage then
  --#Save the message
end


So what this essentially does is filter each message by the computerID that is appended before it. You would need to communicate to the server what the intended recipient is somehow, but that shouldn't be too difficult.
Bubba #4
Posted 13 July 2013 - 09:44 PM
Actually, although my above post would work, now that I think on it there is a far easier way to go about this – just use the return channel to specify what computer should save the file.

For example, when you are sending a rednet message, you provide it with these arguments:

modem.transmit([recipient], [return channel], [message])

And on the receiving end, the client would pull an event like this:

local event, peripheral_side, sender, return_channel, message, distance = os.pullEvent()

So, just specify what computer should save the file with the return channel like this:

Example Client code:

local modem = peripheral.wrap([side])
local function makeRequest(file)
  modem.transmit(shared_channel, os.getComputerId(), "some_file") --#Transmit the request using the shared channel and the computerID as the return channel
  local event = {os.pullEvent("modem_message")} --#Wait for the server to send back the requested file
  if event[3] == shared_channel and event[4] == os.getComputerId() then --#If the sent on channel is the shared_channel and the return channel is the computer id...
    saveFile(event[5]) --#Go ahead and save the file
  end
end

Example server code:

local modem = peripheral.wrap([side])
local function onRequest(file, id) --Whenever the server gets a request, provide it with the message and the return id
  local file_content = getFileContents(file) --#Read the file
  modem.transmit(shared_channel,id,file_content) --#Transmit the file to the receiver, using the same return id
end
Galactica4 #5
Posted 13 July 2013 - 10:22 PM
Actually, although my above post would work, now that I think on it there is a far easier way to go about this – just use the return channel to specify what computer should save the file.

For example, when you are sending a rednet message, you provide it with these arguments:

modem.transmit([recipient], [return channel], [message])

And on the receiving end, the client would pull an event like this:

local event, peripheral_side, sender, return_channel, message, distance = os.pullEvent()

So, just specify what computer should save the file with the return channel like this:

Example Client code:

local modem = peripheral.wrap([side])
local function makeRequest(file)
  modem.transmit(shared_channel, os.getComputerId(), "some_file") --#Transmit the request using the shared channel and the computerID as the return channel
  local event = {os.pullEvent("modem_message")} --#Wait for the server to send back the requested file
  if event[3] == shared_channel and event[4] == os.getComputerId() then --#If the sent on channel is the shared_channel and the return channel is the computer id...
	saveFile(event[5]) --#Go ahead and save the file
  end
end

Example server code:

local modem = peripheral.wrap([side])
local function onRequest(file, id) --Whenever the server gets a request, provide it with the message and the return id
  local file_content = getFileContents(file) --#Read the file
  modem.transmit(shared_channel,id,file_content) --#Transmit the file to the receiver, using the same return id
end
See below for a better response
SpoilerWell if you insist on using only one channel, it is possible to filter the received files via the individual computers. However, the server itself would not be able to control which computers received the file.

What I would do to filter out unwanted messages would be something like this:


local CUID = tostring(os.getComputerId())

local function validateRecipient(message)
  local recipient,newMessage = message:match("(%d*)|")
  if recipient and recipient == CUID then
	return newMessage
  end
end

local example_message = "25|The rest of the file is as follows. Hello!"
local validMessage = validateRecipient(example_message)
if validMessage then
  --#Save the message
end


So what this essentially does is filter each message by the computerID that is appended before it. You would need to communicate to the server what the intended recipient is somehow, but that shouldn't be too difficult.

Wow, it is going to take a while to understand this, I also thought that i could sen the computer ID in the message but, this is going to take some time to make!

Btw the program/s iam making is called Fibr, Fibr Fetch and Fibr Share, to be released soon and then included in my upcoming OS :)/>
Bubba #6
Posted 13 July 2013 - 10:48 PM
I also thought that i could sen the computer ID in the message but

Well you certainly could. In fact, that was what I was originally suggesting. But using the return channel would probably be easier to implement because it wouldn't require comparison of strings or regex :)/> However, if you would prefer to include it in the message see the first post I made.