Advertisement
Sanwiches

sulphurClient

Jan 6th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. modem = peripheral.wrap("top")
  2.  
  3. local function clear(x,y)
  4. term.clear()
  5. term.setCursorPos(x,y)
  6. end
  7.  
  8. local function listen(modem,chans)
  9. if chans[1] == "all" then
  10. local senderId, message, protocol = rednet.receive()
  11. return senderId, message, protocol
  12. else
  13. for i=1, #chans do -- open queued channels
  14. modem.open(chans[i])
  15. end
  16. -- Listen for message on those channels
  17. event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  18.  
  19. modem.closeAll()
  20. end
  21.  
  22. return event, modemSide, senderChannel, replyChannel, message, senderDistance
  23. end
  24.  
  25. local channel = 20
  26. local replyChannel = 11
  27.  
  28. while true do
  29. clear(1,1)
  30. term.write("Input a command. Type 'commands' for help")
  31. term.setCursorPos(1,2)
  32. input = read()
  33.  
  34. if input == "commands" then
  35.  
  36. clear(1,1)
  37. print("'request file' requests a file from the cloud server.")
  38. print("'send file' sends a file to the cloud server.")
  39. print("Press any key to continue")
  40. local event, key = os.pullEvent("key")
  41.  
  42. elseif input == "send file" then
  43.  
  44. clear(1,1)
  45. print("Input file name")
  46. term.setCursorPos(1,2)
  47. name = read()
  48.  
  49. clear(1,1)
  50. print("Input name to send file as")
  51. term.setCursorPos(1,2)
  52. sendName = read()
  53.  
  54. sleep(1)
  55. if fs.exists(name) then
  56. local cFile = fs.open(name, "r")
  57. fileTable = {name=sendName,content=cFile.readAll()}
  58. modem.transmit(channel,replyChannel,fileTable)
  59. else
  60. clear(1,1)
  61. print("File not found")
  62. sleep(1)
  63. end
  64.  
  65. elseif input == "request file" then
  66.  
  67. clear(1,1)
  68. print("Input file name")
  69. term.setCursorPos(1,2)
  70. input = read()
  71. fileTable = {request=input} --create table to send
  72. modem.transmit(channel,replyChannel,fileTable) --send request table
  73. chans = {replyChannel}
  74. local event, modemSide, senderChannel, replyChannel, message, senderDistance = listen(modem,chans) --listen for response
  75.  
  76. fileName = nil
  77. fileContent = nil
  78. for k,v in pairs(message) do
  79. if k == "name" then
  80. if fs.exists(v) then
  81. print("file "..v.." already exists")
  82. os.reboot()
  83. else
  84. fileName = v
  85. end
  86. elseif k == "content" then
  87. fileContent = v
  88. end
  89. end
  90.  
  91. if fileName then
  92. local h = fs.open(fileName, fs.exists(fileName) and "a" or "w")
  93. h.write(fileContent)
  94. h.flush()
  95. print("Saved file "..fileName)
  96. end
  97. end
  98.  
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement