Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local serialization = require("serialization")
- local modem = component.proxy(component.modem.address)
- local fs = require("filesystem")
- local term = require("term")
- term.clear()
- modem.setStrength(128)
- modem.open(7766)
- event.timer(1, function() modem.broadcast(999, modem.address) end, math.huge)
- function SendError(from, msg)
- local toSend = serialization.serialize({ type = "error", error = msg })
- modem.send(from, 7766, toSend)
- end
- while true do
- local type, _, from, _, _, message = event.pull("modem_message")
- local obj = serialization.unserialize(message)
- local path = obj.path
- if path ~= nil then
- path = fs.concat("/share/", fs.canonical(path))
- end
- if obj.type == "info" then
- local share = fs.get("/share/")
- local info = { type = "info" }
- info.spaceTotal = share.spaceTotal()
- info.spaceUsed = share.spaceUsed()
- modem.send(from, 7766, serialization.serialize(info))
- elseif path == nil then
- print("Recieved invalid path")
- SendError(from, "Not a valid path")
- elseif obj.type == "list" then
- print("Recieved list for "..path)
- local fsList = {}
- for file in fs.list(path) do
- table.insert(fsList, file)
- end
- modem.send(from, 7766, serialization.serialize({ type = "list", list = fsList }))
- elseif obj.type == "type" then
- print("Recieved type for "..path)
- local toSend = { type = "type" }
- toSend.isDir = fs.isDirectory(path)
- toSend.size = fs.size(path)
- toSend.lastModified = fs.lastModified(path)
- modem.send(from, 7766, serialization.serialize(toSend))
- elseif obj.type == "get" then
- print("Recieved get for "..path)
- local file = fs.open(path, "rb")
- if file == nil then
- SendError(from, "Not a valid file")
- else
- local data = file:read(fs.size(path))
- file:close()
- modem.send(from, 7766, serialization.serialize({ type = "get", data = data }))
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement