Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- store_node.lua
- local modem = peripheral.find("modem") or error("No modem found")
- local drive = peripheral.find("drive") or error("No disk drive found")
- modem.open(1) -- Open channel 1 for communication
- local function sendResponse(id, response)
- modem.transmit(1, 1, {id = id, response = response})
- end
- local function handleRequest(sender, message)
- if message.type == "discovery" then
- modem.transmit(1, 1, {type = "discovery_response", id = os.getComputerID()})
- elseif message.targetNode == os.getComputerID() then
- if message.action == "write" then
- if not drive.isDiskPresent() then
- sendResponse(message.id, {success = false, error = "No disk present"})
- return
- end
- local path = fs.combine(drive.getMountPath(), message.path .. "_chunk_" .. message.chunk)
- local file = fs.open(path, "w")
- file.write(message.content)
- file.close()
- sendResponse(message.id, {success = true})
- elseif message.action == "read" then
- if not drive.isDiskPresent() then
- sendResponse(message.id, {success = false, error = "No disk present"})
- return
- end
- local path = fs.combine(drive.getMountPath(), message.path .. "_chunk_" .. message.chunk)
- if fs.exists(path) then
- local file = fs.open(path, "r")
- local content = file.readAll()
- file.close()
- sendResponse(message.id, {success = true, content = content})
- else
- sendResponse(message.id, {success = false, error = "File not found"})
- end
- elseif message.action == "delete" then
- if not drive.isDiskPresent() then
- sendResponse(message.id, {success = false, error = "No disk present"})
- return
- end
- local path = fs.combine(drive.getMountPath(), message.path .. "_chunk_" .. message.chunk)
- if fs.exists(path) then
- fs.delete(path)
- sendResponse(message.id, {success = true})
- else
- sendResponse(message.id, {success = false, error = "File not found"})
- end
- elseif message.action == "getSpace" then
- if not drive.isDiskPresent() then
- sendResponse(message.id, {success = false, error = "No disk present"})
- return
- end
- local freeSpace = fs.getFreeSpace(drive.getMountPath())
- sendResponse(message.id, {success = true, space = freeSpace})
- end
- end
- end
- print("Storage node ready. Waiting for requests...")
- print("Node ID: " .. os.getComputerID())
- while true do
- local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- if channel == 1 then
- handleRequest(side, message)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment