Advertisement
DOGGYWOOF

Server DOS

Jul 8th, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 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 display advanced GUI on the monitor
  30. local function drawGUI()
  31. local monitor = peripheral.wrap("right")
  32. monitor.setTextScale(0.5)
  33. monitor.setBackgroundColor(colors.black)
  34. monitor.clear()
  35.  
  36. -- Header
  37. monitor.setCursorPos(1, 1)
  38. monitor.setTextColor(colors.white)
  39. monitor.write("Server ID: " .. tostring(serverID))
  40.  
  41. -- Status area
  42. monitor.setCursorPos(1, 3)
  43. monitor.setBackgroundColor(colors.gray)
  44. monitor.clearLine()
  45. monitor.write("Status: ")
  46.  
  47. -- Logs area
  48. monitor.setCursorPos(1, 5)
  49. monitor.setBackgroundColor(colors.gray)
  50. monitor.clearLine()
  51. monitor.write("Logs:")
  52. end
  53.  
  54. -- Function to update status in GUI
  55. local function updateStatus(status)
  56. local monitor = peripheral.wrap("right")
  57. monitor.setCursorPos(9, 3)
  58. monitor.setBackgroundColor(colors.gray)
  59. monitor.clearLine()
  60. monitor.write(status)
  61. end
  62.  
  63. -- Function to log file transfer in GUI
  64. local function logTransfer(fileName, success)
  65. local monitor = peripheral.wrap("right")
  66. monitor.setCursorPos(1, 7)
  67. monitor.setTextColor(colors.white)
  68. monitor.setBackgroundColor(colors.black)
  69. monitor.clearLine()
  70. monitor.write(string.format("File: %s | Status: %s", fileName, success and "Success" or "Failure"))
  71. end
  72.  
  73. -- Function to display file details and await confirmation
  74. local function confirmFileTransfer(fileName, fileContent)
  75. local monitor = peripheral.wrap("right")
  76. monitor.clear()
  77. monitor.setCursorPos(1, 1)
  78. monitor.write("Incoming file:")
  79. monitor.setCursorPos(1, 3)
  80. monitor.write("Name: " .. fileName)
  81. monitor.setCursorPos(1, 4)
  82. monitor.write("Size: " .. string.len(fileContent) .. " bytes")
  83. monitor.setCursorPos(1, 5)
  84. monitor.write("[Confirm]")
  85. monitor.setCursorPos(1, 6)
  86. monitor.write("[Decline]")
  87.  
  88. while true do
  89. local event, side, x, y = os.pullEvent("monitor_touch")
  90. if y == 5 then
  91. return true
  92. elseif y == 6 then
  93. return false
  94. end
  95. end
  96. end
  97.  
  98. -- Function to handle displaying critical information
  99. local function displayCriticalInformation(criticalMessage)
  100. print("Critical Error: " .. criticalMessage)
  101. local monitor = peripheral.wrap("right")
  102. monitor.setCursorPos(1, 9)
  103. monitor.setBackgroundColor(colors.red)
  104. monitor.clearLine()
  105. monitor.setTextColor(colors.white)
  106. monitor.write("Critical: " .. criticalMessage)
  107. end
  108.  
  109. -- Initialize GUI
  110. drawGUI()
  111.  
  112. -- Main loop to receive messages
  113. while true do
  114. local senderID, message, protocol = rednet.receive()
  115.  
  116. if protocol == "file_transfer" then
  117. print("Received file from ID: " .. senderID)
  118.  
  119. if type(message) == "table" and message.fileName and message.fileContent then
  120. -- Display file details and wait for confirmation
  121. local confirmed = confirmFileTransfer(message.fileName, message.fileContent)
  122.  
  123. if confirmed then
  124. -- Save file if confirmed
  125. local filePath = fs.combine(saveDirectory, message.fileName)
  126. local file = fs.open(filePath, "w")
  127. file.write(message.fileContent)
  128. file.close()
  129.  
  130. print("File saved as " .. message.fileName)
  131.  
  132. -- Update GUI
  133. updateStatus("File received")
  134. logTransfer(message.fileName, true)
  135. sleep(2)
  136. drawGUI()
  137.  
  138. -- Send confirmation to client
  139. rednet.send(senderID, { status = "Accepted" }, "confirmation")
  140. else
  141. print("File transfer declined.")
  142. logTransfer(message.fileName, false)
  143. updateStatus("File declined")
  144. sleep(2)
  145. drawGUI()
  146.  
  147. -- Send confirmation to client
  148. rednet.send(senderID, { status = "Declined" }, "confirmation")
  149. end
  150. else
  151. print("Invalid message received.")
  152. displayCriticalInformation("Invalid file transfer request.")
  153. end
  154. else
  155. -- Assuming other protocols indicate critical information
  156. displayCriticalInformation(message)
  157. end
  158. end
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement