Advertisement
DOGGYWOOF

Untitled

Jan 29th, 2025 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. local protocol = "sync_protocol"
  2. local serverPath = "/users/"
  3.  
  4. -- Open the modem
  5. rednet.open(peripheral.getName(peripheral.find("modem") or error("No modem found")))
  6.  
  7. local serverID = os.getComputerID()
  8. print("Server running. Computer ID:", serverID)
  9.  
  10. -- Function to compute file hashes
  11. local function getFileHash(filePath)
  12. if not fs.exists(filePath) then return nil end
  13. local f = fs.open(filePath, "r")
  14. local content = f.readAll()
  15. f.close()
  16. return textutils.serialize(content)
  17. end
  18.  
  19. -- Get file list with hashes
  20. local function getFileList(path)
  21. local files = {}
  22. local function scan(dir)
  23. for _, file in pairs(fs.list(dir)) do
  24. local fullPath = dir .. file
  25. if fs.isDir(fullPath) then
  26. scan(fullPath .. "/")
  27. else
  28. files[fullPath:sub(#serverPath + 1)] = getFileHash(fullPath)
  29. end
  30. end
  31. end
  32. scan(path)
  33. return files
  34. end
  35.  
  36. -- Handle sync requests
  37. while true do
  38. local id, request = rednet.receive(protocol)
  39. if request then
  40. if request.action == "sync_request" then
  41. print("Sync request received from client", id)
  42.  
  43. -- Check what action the client requested
  44. local syncMode = request.mode or "full" -- Default to full sync if no mode provided
  45.  
  46. local serverFiles = getFileList(serverPath)
  47. local files_to_client = {}
  48. local files_to_delete = {}
  49.  
  50. -- Identify files that need to be sent or deleted
  51. for file, hash in pairs(serverFiles) do
  52. if request.files[file] ~= hash then
  53. local f = fs.open(serverPath .. file, "r")
  54. files_to_client[file] = f.readAll()
  55. f.close()
  56. end
  57. end
  58.  
  59. -- Identify files that need to be deleted (on server)
  60. for file, hash in pairs(request.files) do
  61. if not serverFiles[file] then
  62. table.insert(files_to_delete, file)
  63. end
  64. end
  65.  
  66. -- Send response to the client
  67. rednet.send(id, {
  68. action = "sync_response",
  69. files_to_client = files_to_client,
  70. files_to_delete = files_to_delete
  71. }, protocol)
  72.  
  73. -- Handle upload request (client to server)
  74. if syncMode == "upload" or syncMode == "full" then
  75. rednet.send(id, { action = "sync_upload_request", files_needed = {}, files_to_server = {} }, protocol)
  76.  
  77. local id2, clientUpload = rednet.receive(protocol, 5)
  78. if id2 == id and clientUpload.action == "sync_upload" then
  79. for file, content in pairs(clientUpload.files_to_server) do
  80. local filePath = serverPath .. file
  81. local f = fs.open(filePath, "w")
  82. f.write(content)
  83. f.close()
  84. print("Updated from client:", file)
  85. end
  86. end
  87. end
  88. end
  89. end
  90. end
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement