Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- https://stackoverflow.com/a/7615129/13347795
- 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
- function refresh()
- h = fs.open("messages.txt", "r")
- term.clear()
- term.setCursorPos(1, 1)
- repeat
- line = h.readLine()
- if line ~= nil and line ~= "" then
- date, time, id, message = table.unpack(split(line, 3))
- print(message)
- end
- until line == nil
- h.close()
- end
- write("Starting...")
- if not fs.exists("messages.txt") then
- local h = fs.open("messages.txt", "w")
- h.close()
- end
- peripheral.find("modem", rednet.open)
- local h = fs.open("messages.txt", "r")
- rednet.broadcast(h.readAll(), "response-messenger-backlog")
- h.close()
- refresh()
- while true do
- id, message, protocol = rednet.receive()
- if protocol == "request-messenger-backlog" then
- local h = fs.open("messages.txt", "r")
- rednet.send(id, h.readAll(), "response-messenger-backlog")
- h.close()
- elseif protocol == "broadcast-messenger-message" then
- local h = fs.open("messages.txt", "a")
- h.writeLine(os.date("%F;%T;")..id..";"..message)
- h.close()
- end
- refresh()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement