2019-05-02 00:51:40 +00:00
|
|
|
local disknet = {}
|
|
|
|
|
2019-05-02 04:04:12 +00:00
|
|
|
local tArg = {...}
|
2019-05-02 01:32:11 +00:00
|
|
|
|
2019-05-02 04:04:12 +00:00
|
|
|
disknet.mainPath = tArg[1] or "disk/DISKNET"
|
2019-05-02 00:51:40 +00:00
|
|
|
local limitChannelsToModem = false
|
2019-05-02 04:04:12 +00:00
|
|
|
local maximumBufferSize = 32
|
|
|
|
|
2019-05-02 00:51:40 +00:00
|
|
|
local openChannels = {}
|
|
|
|
local yourID = os.getComputerID()
|
2019-05-02 04:04:12 +00:00
|
|
|
local uniqueID = math.random(1, 2^31 - 1) -- prevents receiving your own messages
|
|
|
|
local msgCheckList = {} -- makes sure duplicate messages aren't received
|
2019-05-02 00:51:40 +00:00
|
|
|
|
2019-05-02 04:04:12 +00:00
|
|
|
-- do not think for one second that os.epoch("utc") would be a proper substitute
|
2019-05-02 00:51:40 +00:00
|
|
|
local getTime = function()
|
|
|
|
return os.time() + (-1 + os.day()) * 24
|
|
|
|
end
|
|
|
|
|
2019-05-02 04:04:12 +00:00
|
|
|
local readFile = function(path)
|
|
|
|
local file = fs.open(path, "r")
|
|
|
|
local contents = file.readAll()
|
|
|
|
file.close()
|
|
|
|
return contents
|
|
|
|
end
|
|
|
|
|
|
|
|
local writeFile = function(path, contents)
|
|
|
|
local file = fs.open(path, "w")
|
|
|
|
file.write(contents)
|
|
|
|
file.close()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if 'limitChannelsToModem', then will make sure that channel is a number between 0 and 65535
|
2019-05-02 00:51:40 +00:00
|
|
|
local checkValidChannel = function(channel)
|
|
|
|
if limitChannelsToModem then
|
|
|
|
if type(channel) == "number" then
|
|
|
|
if channel < 0 or channel > 65535 then
|
|
|
|
return false, "channel must be between 0 and 65535"
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return false, "channel must be number"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if type(channel) == "string" or type(channel) == "number" then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false, "channel must be castable to string"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
disknet.isOpen = function(channel)
|
|
|
|
local valid, grr = checkValidChannel(channel)
|
|
|
|
if valid then
|
|
|
|
for i = 1, #openChannels do
|
|
|
|
if openChannels[i] == channel then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
error(grr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
disknet.open = function(channel)
|
|
|
|
local valid, grr = checkValidChannel(channel)
|
|
|
|
if valid then
|
|
|
|
openChannels[#openChannels + 1] = channel
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
error(grr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
disknet.close = function(channel)
|
|
|
|
local valid, grr = checkValidChannel(channel)
|
|
|
|
if valid then
|
|
|
|
for i = 1, #openChannels do
|
|
|
|
if openChannels[i] == channel then
|
|
|
|
table.remove(openChannels, i)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
error(grr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
disknet.closeAll = function()
|
|
|
|
openChannels = {}
|
|
|
|
end
|
|
|
|
|
2019-05-02 04:04:12 +00:00
|
|
|
disknet.send = function(channel, message)
|
2019-05-02 00:51:40 +00:00
|
|
|
local valid, grr = checkValidChannel(channel)
|
|
|
|
if valid then
|
2019-05-02 04:17:37 +00:00
|
|
|
if not fs.exists(fs.combine(disknet.mainPath, tostring(channel))) then
|
|
|
|
fs.open(fs.combine(disknet.mainPath, tostring(channel)), "w").close()
|
|
|
|
end
|
2019-05-02 04:04:12 +00:00
|
|
|
local contents = textutils.unserialize(readFile(fs.combine(disknet.mainPath, tostring(channel))))
|
2019-05-02 00:51:40 +00:00
|
|
|
if disknet.isOpen(channel) then
|
2019-05-02 04:04:12 +00:00
|
|
|
local file = fs.open(fs.combine(disknet.mainPath, tostring(channel)), "w")
|
|
|
|
if contents then
|
|
|
|
contents[#contents + 1] = {
|
|
|
|
time = getTime(),
|
|
|
|
id = yourID,
|
|
|
|
uniqueID = uniqueID,
|
|
|
|
messageID = math.random(1, 2^31 - 1),
|
|
|
|
channel = channel,
|
|
|
|
message = message,
|
|
|
|
}
|
|
|
|
if #contents > maximumBufferSize then
|
|
|
|
table.remove(contents, 1)
|
|
|
|
end
|
|
|
|
file.write(textutils.serialize(contents))
|
|
|
|
else
|
|
|
|
file.write(textutils.serialize({{
|
|
|
|
time = getTime(),
|
|
|
|
id = yourID,
|
|
|
|
uniqueID = uniqueID,
|
|
|
|
messageID = math.random(1, 2^31 - 1),
|
|
|
|
channel = channel,
|
|
|
|
message = message,
|
|
|
|
}}))
|
|
|
|
end
|
2019-05-02 00:51:40 +00:00
|
|
|
file.close()
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
error(grr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 04:04:12 +00:00
|
|
|
local fList, pList = {}, {}
|
|
|
|
|
|
|
|
local loadFList = function()
|
|
|
|
fList, pList = {}, {}
|
|
|
|
if channel then
|
|
|
|
fList = {fs.open(fs.combine(disknet.mainPath, tostring(channel)), "r")}
|
|
|
|
pList = {fs.combine(disknet.mainPath, tostring(channel))}
|
|
|
|
else
|
|
|
|
for i = 1, #openChannels do
|
|
|
|
fList[i] = fs.open(fs.combine(disknet.mainPath, tostring(openChannels[i])), "r")
|
|
|
|
pList[i] = fs.combine(disknet.mainPath, tostring(openChannels[i]))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 00:51:40 +00:00
|
|
|
disknet.receive = function(channel)
|
|
|
|
local valid, grr = checkValidChannel(channel)
|
|
|
|
if valid or not channel then
|
2019-05-02 15:00:43 +00:00
|
|
|
|
2019-05-02 04:04:12 +00:00
|
|
|
local output, contents
|
|
|
|
local doRewrite = false
|
2019-05-02 15:00:43 +00:00
|
|
|
|
|
|
|
loadFList()
|
|
|
|
|
2019-05-02 00:51:40 +00:00
|
|
|
while true do
|
|
|
|
for i = 1, #fList do
|
|
|
|
contents = fList[i].readAll()
|
|
|
|
if contents ~= "" then
|
2019-05-02 15:00:43 +00:00
|
|
|
fList[i].close()
|
|
|
|
fList[i] = fs.open(pList[i], "r")
|
|
|
|
contents = textutils.unserialize(fList[i].readAll())
|
2019-05-02 04:04:12 +00:00
|
|
|
if type(contents) == "table" then
|
2019-05-02 04:53:13 +00:00
|
|
|
if contents[1] then
|
2019-05-02 04:35:01 +00:00
|
|
|
if not output then
|
2019-05-02 04:53:13 +00:00
|
|
|
for look = 1, #contents do
|
|
|
|
if (contents[look].uniqueID ~= uniqueID) and (not msgCheckList[contents[look].messageID]) then
|
2019-05-02 15:00:43 +00:00
|
|
|
if getTime() - (contents[look].time or 0) <= 0.001 then
|
2019-05-02 04:53:13 +00:00
|
|
|
msgCheckList[contents[look].messageID] = true
|
|
|
|
output = {}
|
|
|
|
for k,v in pairs(contents[look]) do
|
|
|
|
output[k] = v
|
|
|
|
end
|
|
|
|
break
|
2019-05-02 04:35:01 +00:00
|
|
|
end
|
2019-05-02 04:04:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-02 15:00:43 +00:00
|
|
|
|
2019-05-02 04:53:13 +00:00
|
|
|
-- delete old msesages
|
2019-05-02 04:04:12 +00:00
|
|
|
doRewrite = false
|
|
|
|
for t = #contents, 1, -1 do
|
2019-05-02 15:00:43 +00:00
|
|
|
if getTime() - (contents[t].time or 0) > 0.001 then
|
2019-05-02 14:45:12 +00:00
|
|
|
msgCheckList[contents[t].messageID] = nil
|
2019-05-02 04:04:12 +00:00
|
|
|
table.remove(contents, t)
|
|
|
|
doRewrite = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if doRewrite then
|
|
|
|
writeFile(pList[i], textutils.serialize(contents))
|
|
|
|
end
|
|
|
|
if output then
|
|
|
|
for i = 1, #fList do
|
|
|
|
fList[i].close()
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-02 00:51:40 +00:00
|
|
|
end
|
|
|
|
end
|
2019-05-02 04:04:12 +00:00
|
|
|
if output then
|
2019-05-02 00:51:40 +00:00
|
|
|
break
|
|
|
|
else
|
|
|
|
os.queueEvent("")
|
|
|
|
os.pullEvent("")
|
|
|
|
end
|
|
|
|
end
|
2019-05-02 15:00:43 +00:00
|
|
|
|
2019-05-02 00:51:40 +00:00
|
|
|
if contents then
|
2019-05-02 04:04:12 +00:00
|
|
|
return output.message, output.channel, output.id, output.time
|
2019-05-02 00:51:40 +00:00
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
error(grr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return disknet
|