Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- term.setCursorPos(1, 1)
- disk = ""
- if fs.exists("disk/disk.txt") then
- local h = fs.open("disk/disk.txt", "r")
- if h.readAll() == "production\n" then
- disk = "disk/"
- print("Selected disk 1")
- end
- end
- if fs.exists("disk2/disk.txt") then
- local h = fs.open("disk2/disk.txt", "r")
- if h.readAll() == "production\n" then
- disk = "disk2/"
- print("Selected disk 2")
- end
- end
- PATH = disk.."data/"
- function split(input, maxSplits, sep)
- if sep == nil then
- sep = ";"
- end
- splits = {}
- splitCount = 0
- for str in string.gmatch(input, "([^"..sep.."]+)") do
- if maxSplits ~= nil and splitCount > maxSplits then
- splits[splitCount] = splits[splitCount]..sep..str
- else
- table.insert(splits, str)
- splitCount = splitCount + 1
- end
- end
- return splits
- end
- local serverId = tostring(os.getComputerID())
- peripheral.find("modem", rednet.open)
- rednet.broadcast(serverId, "dns-record-database")
- rednet.broadcast(nil, "news-update")
- speaker = peripheral.find("speaker")
- function note(pitch)
- if speaker ~= nil then
- speaker.playNote("bit", 1, pitch)
- end
- end
- function handleRequest(id, body, protocol)
- if protocol == "dns-lookup" and body == "database" then -- Broadcast request for address of database server.
- write("ID"..id..": DNS Lookup")
- note(8)
- rednet.send(id, serverId, "dns-record-database") -- Hi, I'm the database server.
- print(" - "..serverId)
- elseif protocol == "database-file-request" then -- Request for file.
- write("ID"..id..": File request \""..body.."\"")
- note(4)
- local path = PATH..body
- if fs.exists(path) then
- if not fs.isDir(path) then
- local h = fs.open(path, "r")
- rednet.send(id, "200;"..h.readAll(), "database-file-response")
- h.close()
- print(" - 200")
- note(16)
- else
- rednet.send(id, "405;File requested but directory was found", "database-file-response")
- print(" - 405")
- note(3)
- end
- else
- rednet.send(id, "404;File not found", "database-file-response")
- print(" - 404")
- note(1)
- end
- elseif protocol == "database-file-post" then
- write("ID"..id..": File post")
- note(12)
- filename, contents = table.unpack(split(body, 2))
- write(" \""..filename.."\" - \"\n"..contents.."\n\"")
- note(16)
- local h = fs.open(PATH..filename, "w")
- h.write(contents)
- h.close()
- note(19)
- elseif protocol == "database-dir-request" then
- write("ID"..id..": Directory request \""..body.."\"")
- note(10)
- local path = PATH..body
- if fs.exists(path) then
- if fs.isDir(path) then
- local results = table.concat(fs.list(path), ";")
- rednet.send(id, "200;"..results, "database-dir-response")
- print(" - 200")-- ("..results..")")
- note(22)
- else
- rednet.send(id, "405;Directory requested but file was found", "database-dir-response")
- print(" - 405")
- note(9)
- end
- else
- rednet.send(id, "404;Directory not found", "database-dir-response")
- print(" - 404")
- note(7)
- end
- end
- end
- print("Server running")
- while true do
- handleRequest(rednet.receive())
- --if not pcall(handleRequest, rednet.receive()) then
- -- print("Unexpected error")
- --end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement