Alternatively, you could use the
textutils.serialize and textutils.unserialize commands to send the file over as a string. This way you can send whole scores of programs over rednet in only few operations. For example, I use this program to send updates through a set of machines running the same programs, but are also being updated in the background (this is just a direct copy-paste for illustration, so expect some things that don't necessarily make sense for your case):Spoiler
local prog = {"startup", "update", "build"} -- A table containing the names of programs that will be sent.
local filessent = 0
local currentfile = 0
function fileSend()
local file = fs.open(prog[currentfile], "r")
local data = textutils.serialize {
cmd = "getfile",
program = prog[currentfile],
content = file:readAll()
}
file:close()
rednet.send(rID, data)
local _, updateSuccess = rednet.receive() --The '_' here is to catch the computer's ID that is returned by rednet.receive(), but which we are not interested in right now; we only want to know if the update was succesful.
if updateSuccess and updateSuccess == "File update complete." then
filessent = filessent + 1
print(prog[currentfile].." was sent successfully ("..currentfile.."/"..table.getn(prog).." files)")
return true
else
print("File "..currentfile.." named "..prog[currentfile].." was not sent successfully.")
return false
end
end
rednet.open("left")
rednet.send(rID,table.getn(prog)) --This sends the client computer the number of files that await sending.
while filessent < table.getn(prog) do
currentfile = currentfile + 1
if fileSend() == false then
print("An error occured during file transmission. Update for "..rID.." was aborted.")
break
end
end
rednet.close("left")
(I have omitted some irrelevant lines of code such as gathering when a client actually needs an update, whether the client is eligible for update (only computer IDs known by the system get updated) and so forth, but that would only complicate my example and confuse things.)
This function will send all files the computer is told to send and display which file out of how many total files was sent - the screen reads "startup was sent succesfully (1/3)" -, or, if the update was not performed successfully, which file it was that broke the process (there will be actions taken if the function sendUpdate() returns false i.e. if the file was not sent successfully, but that's not relevant here). It does so by first sending the file and then listening for the receiving computer's response, which is given by:
Spoiler
local _, numberoffiles = rednet.receive() --Here is received how many files require receiving. Again, the underscore is there because we're not interested in the sender's ID and thus it is stored in a random variable we're not going to use.
fileamount = tonumber(numberoffiles)
writtenfiles = 0
--The update!
print("Receiving data from Master control...")
function receiveFiles()
while true do
local _, data = textutils.unserialize(rednet.receive())
--Now to ensure that there is no crash
if data and data.cmd and data.cmd == "getfile" then
print("Data received. Writing program.")
local file = fs.open(data.program, "w")
file:write(data.content)
file:close()
print("Program written.")
writtenfiles = writtenfiles + 1
rednet.send(mID,"File update complete.")
break
end
end
end
while writtenfiles < fileamount do
receiveFiles()
end
So in the end, what is sent here is a number of programs sent as strings that will be received and written out again as the same programs with the same name as the original that was sent for update. Now, in the sent program, you could include things that switch automatically to the new version of the program as it's still being executed etc. etc. to keep the system running during an update (I will elaborate if you want to know more). The boon to this is that you need only one master controller computer to update an entire system - in my case, I use this as a central hub for all my Turtles. All relevant Turtles receive one batch of updates, another receives another batch, all using these two simple programs.
Hope to have given you an alternative method and insight into sending and receiving files - good luck!