Advertisement
DOGGYWOOF

RECOVERY SERVER

Jul 9th, 2024 (edited)
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. -- Server Program
  2.  
  3. -- Function to find and return the modem peripheral
  4. local function findModem()
  5. for _, side in ipairs(peripheral.getNames()) do
  6. if peripheral.getType(side) == "modem" then
  7. return side
  8. end
  9. end
  10. return nil
  11. end
  12.  
  13. local modemSide = findModem()
  14. if not modemSide then
  15. print("Error: No modem found. Please attach a modem.")
  16. return
  17. end
  18.  
  19. rednet.open(modemSide)
  20.  
  21. local saveDirectory = "DGOS"
  22. local serverID = os.getComputerID()
  23.  
  24. -- Create save directory if it doesn't exist
  25. if not fs.exists(saveDirectory) then
  26. fs.makeDir(saveDirectory)
  27. end
  28.  
  29. -- Function to read the target client ID from file
  30. local function readClientID()
  31. local path = "/Network/Recovery.ID"
  32. if fs.exists(path) then
  33. local file = fs.open(path, "r")
  34. local id = tonumber(file.readAll())
  35. file.close()
  36. return id
  37. else
  38. return nil
  39. end
  40. end
  41.  
  42. local clientID = readClientID()
  43. if not clientID then
  44. print("Error: No client ID found in /Network/Recovery.ID")
  45. return
  46. end
  47.  
  48. -- Main loop to receive messages
  49. while true do
  50. local senderID, message, protocol = rednet.receive()
  51.  
  52. if protocol == "file_transfer" then
  53. print("Received file request from ID: " .. senderID)
  54.  
  55. if type(message) == "table" and message.fileName and message.fileSize then
  56. print("File Name: " .. message.fileName)
  57. print("File Size: " .. message.fileSize .. " bytes")
  58.  
  59. -- Automatically accept the file transfer
  60. local filePath = fs.combine(saveDirectory, message.fileName)
  61. local file = fs.open(filePath, "w")
  62. file.write(message.fileContent)
  63. file.close()
  64. rednet.send(senderID, { status = "Accepted" }, "confirmation")
  65. print("File transfer accepted and saved to " .. filePath)
  66. else
  67. print("Invalid message received.")
  68. rednet.send(senderID, { status = "Invalid file transfer request" }, "confirmation")
  69. end
  70. else
  71. print("Received unknown protocol message.")
  72. rednet.send(senderID, { status = "Unknown protocol" }, "confirmation")
  73. end
  74. end
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement