Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function call()
- local dg = string.format("%s $", "call")
- rednet.broadcast(dg)
- id, msg = rednet.receive(10)
- if msg then
- cmd, etc = msg:match("^(%S*) (.*)")
- if cmd == "call" then
- rednet.send(id, "call")
- return id
- end
- end
- end
- function open(timeout)
- if timeout >= 100 then
- error("Timeout too high")
- else
- for k, v in pairs(rs.getSides()) do
- rednet.open(v)
- end
- os.startTimer(timeout)
- while true do
- event, p1, p2 = os.pullEvent()
- if event == "rednet_message" then
- -- p1 = id
- -- p2 = msg
- if p2 then
- cmd, etc = p2:match("^(%S*) (.*)")
- if cmd == "view" then
- local val = fs.list("/")
- local _val = textutils.serialize(val)
- local dg = string.format("%s %s", "view", _val)
- rednet.send(p1, dg)
- elseif cmd == "transfer" then
- local file = fs.open(etc, "r")
- local dg = string.format("%s %s", "transfer", file.readAll())
- file.close()
- rednet.send(p1, dg)
- elseif cmd == "call" then
- local dg = string.format("%s $", "call")
- rednet.send(p1, dg)
- end
- end
- elseif event == "timeout" then
- break
- end
- end
- end
- end
- function view(id)
- local dg = string.format("%s $", "view")
- rednet.send(id, dg)
- id, msg = rednet.receive(10)
- if msg then
- cmd, etc = msg:match("^(%S*) (.*)")
- if cmd == "view" then
- tbl = textutils.unserialize(etc)
- return tbl
- end
- end
- end
- function transfer(id, file)
- local dg = string.format("%s %s", "transfer", file)
- rednet.send(id, dg)
- id, msg = rednet.receive(10)
- if msg then
- cmd, etc = msg:match("^(%S*) (.*)")
- if cmd == "transfer" then
- if fs.exists(file) then
- return false
- else
- hFile = fs.open(file, "w")
- hFile.write(etc)
- hFile.close()
- return true
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment