Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- modem = peripheral.wrap("top")
- local function clear(x,y)
- term.clear()
- term.setCursorPos(x,y)
- end
- local function listen(modem,chans)
- if chans[1] == "all" then
- local senderId, message, protocol = rednet.receive()
- return senderId, message, protocol
- else
- for i=1, #chans do -- open queued channels
- modem.open(chans[i])
- end
- -- Listen for message on those channels
- event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- modem.closeAll()
- end
- return event, modemSide, senderChannel, replyChannel, message, senderDistance
- end
- local channel = 20
- local replyChannel = 11
- while true do
- clear(1,1)
- term.write("Input a command. Type 'commands' for help")
- term.setCursorPos(1,2)
- input = read()
- if input == "commands" then
- clear(1,1)
- print("'request file' requests a file from the cloud server.")
- print("'send file' sends a file to the cloud server.")
- print("Press any key to continue")
- local event, key = os.pullEvent("key")
- elseif input == "send file" then
- clear(1,1)
- print("Input file name")
- term.setCursorPos(1,2)
- name = read()
- clear(1,1)
- print("Input name to send file as")
- term.setCursorPos(1,2)
- sendName = read()
- sleep(1)
- if fs.exists(name) then
- local cFile = fs.open(name, "r")
- fileTable = {name=sendName,content=cFile.readAll()}
- modem.transmit(channel,replyChannel,fileTable)
- else
- clear(1,1)
- print("File not found")
- sleep(1)
- end
- elseif input == "request file" then
- clear(1,1)
- print("Input file name")
- term.setCursorPos(1,2)
- input = read()
- fileTable = {request=input} --create table to send
- modem.transmit(channel,replyChannel,fileTable) --send request table
- chans = {replyChannel}
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = listen(modem,chans) --listen for response
- fileName = nil
- fileContent = nil
- for k,v in pairs(message) do
- if k == "name" then
- if fs.exists(v) then
- print("file "..v.." already exists")
- os.reboot()
- else
- fileName = v
- end
- elseif k == "content" then
- fileContent = v
- end
- end
- if fileName then
- local h = fs.open(fileName, fs.exists(fileName) and "a" or "w")
- h.write(fileContent)
- h.flush()
- print("Saved file "..fileName)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement