Advertisement
fyrkantis

DatabaseServer

Aug 4th, 2024 (edited)
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1, 1)
  3. disk = ""
  4. if fs.exists("disk/disk.txt") then
  5.     local h = fs.open("disk/disk.txt", "r")
  6.     if h.readAll() == "production\n" then
  7.         disk = "disk/"
  8.         print("Selected disk 1")
  9.     end
  10. end
  11. if fs.exists("disk2/disk.txt") then
  12.     local h = fs.open("disk2/disk.txt", "r")
  13.     if h.readAll() == "production\n" then
  14.         disk = "disk2/"
  15.         print("Selected disk 2")
  16.     end
  17. end
  18.  
  19. PATH = disk.."data/"
  20.  
  21. function split(input, maxSplits, sep)
  22.     if sep == nil then
  23.         sep = ";"
  24.     end
  25.     splits = {}
  26.     splitCount = 0
  27.     for str in string.gmatch(input, "([^"..sep.."]+)") do
  28.         if maxSplits ~= nil and splitCount > maxSplits then
  29.             splits[splitCount] = splits[splitCount]..sep..str
  30.         else
  31.             table.insert(splits, str)
  32.             splitCount = splitCount + 1
  33.         end
  34.     end
  35.     return splits
  36. end
  37.  
  38. local serverId = tostring(os.getComputerID())
  39. peripheral.find("modem", rednet.open)
  40. rednet.broadcast(serverId, "dns-record-database")
  41. rednet.broadcast(nil, "news-update")
  42. speaker = peripheral.find("speaker")
  43.  
  44. function note(pitch)
  45.     if speaker ~= nil then
  46.         speaker.playNote("bit", 1, pitch)
  47.     end
  48. end
  49.  
  50. function handleRequest(id, body, protocol)
  51.     if protocol == "dns-lookup" and body == "database" then -- Broadcast request for address of database server.
  52.         write("ID"..id..": DNS Lookup")
  53.         note(8)
  54.         rednet.send(id, serverId, "dns-record-database") -- Hi, I'm the database server.
  55.         print(" - "..serverId)
  56.     elseif protocol == "database-file-request" then -- Request for file.
  57.         write("ID"..id..": File request \""..body.."\"")
  58.         note(4)
  59.         local path = PATH..body
  60.         if fs.exists(path) then
  61.             if not fs.isDir(path) then
  62.                 local h = fs.open(path, "r")
  63.                 rednet.send(id, "200;"..h.readAll(), "database-file-response")
  64.                 h.close()
  65.                 print(" - 200")
  66.                 note(16)
  67.             else
  68.                 rednet.send(id, "405;File requested but directory was found", "database-file-response")
  69.                 print(" - 405")
  70.                 note(3)
  71.             end
  72.         else
  73.             rednet.send(id, "404;File not found", "database-file-response")
  74.             print(" - 404")
  75.             note(1)
  76.         end
  77.     elseif protocol == "database-file-post" then
  78.         write("ID"..id..": File post")
  79.         note(12)
  80.         filename, contents = table.unpack(split(body, 2))
  81.         write(" \""..filename.."\" - \"\n"..contents.."\n\"")
  82.         note(16)
  83.         local h = fs.open(PATH..filename, "w")
  84.         h.write(contents)
  85.         h.close()
  86.         note(19)
  87.     elseif protocol == "database-dir-request" then
  88.         write("ID"..id..": Directory request \""..body.."\"")
  89.         note(10)
  90.         local path = PATH..body
  91.         if fs.exists(path) then
  92.             if fs.isDir(path) then
  93.                 local results = table.concat(fs.list(path), ";")
  94.                 rednet.send(id, "200;"..results, "database-dir-response")
  95.                 print(" - 200")-- ("..results..")")
  96.                 note(22)
  97.             else
  98.                 rednet.send(id, "405;Directory requested but file was found", "database-dir-response")
  99.                 print(" - 405")
  100.                 note(9)
  101.             end
  102.         else
  103.             rednet.send(id, "404;Directory not found", "database-dir-response")
  104.             print(" - 404")
  105.             note(7)
  106.         end
  107.     end
  108. end
  109.  
  110. print("Server running")
  111.  
  112. while true do
  113.     handleRequest(rednet.receive())
  114.     --if not pcall(handleRequest, rednet.receive()) then
  115.     --    print("Unexpected error")
  116.     --end
  117. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement