Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Install: /api/network.lua
- --]]
- --[[
- Retuns the hostname for the given computer id on the given protocol
- --]]
- function lookupHostName(id, protocol)
- local res = nil
- -- Do rednet lookup until we find what we need
- rednet.send(id, {sType = "lookup", sProtocol = protocol}, "dns")
- local message
- repeat
- message = {rednet.receive("dns")}
- until type(message) == "table" and message[1] == id
- and type(message[2]) == "table"
- and message[2].sType == "lookup response"
- return message[2].sHostname
- end
- --[[
- Loops through computer sides to find a modem and then opens rednet
- --]]
- function openNetwork()
- local sides = rs.getSides()
- for i = 1, #sides, 1 do
- local side = sides[i]
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- return true
- end
- end
- return false
- end
- --[[
- Builds host name and registers host on rednet.
- Throws error if host already exists
- --]]
- function registerHost(protocol, type)
- -- Build host name
- local hostType = nil
- if type == "mainframe" then hostType = "main"
- elseif type == "server" then hostType = "svr"
- elseif type == "client" then hostType = "cl" end
- if not hostType then error("Invalid host type: " .. type) end
- local hostName = protocol.."_"..hostType.."_"..os.getComputerID()
- -- Make sure host doesn't exist
- if rednet.lookup(protocol, hostName) then
- error("Hostname '"..hostName.."' already exists on '"..protocol.."'")
- end
- -- Good to go
- rednet.host(protocol, hostName)
- return hostName
- end
- --[[
- Splits the given command
- --]]
- function splitCommand(cmd)
- local baseCmd = nil
- local args = { }
- local cnt = 0
- if cmd then
- for word in cmd:gmatch('%S+') do
- if word:len() > 0 then
- if cnt == 0 then
- baseCmd = word
- else
- args[cnt] = word
- end
- cnt = cnt + 1
- end
- end
- end
- return { cmd = baseCmd, args = args }
- end
- function parseArgs(args, startAt)
- local res = {}
- local resCnt = 1
- -- Split args on colon. IE: status:Running
- for i = startAt, #args, 1 do
- local arg = args[i]
- local paramName = nil
- local paramVal = nil
- for word in arg:gmatch('([^:]+)') do
- if not paramName then
- paramName = word
- else
- paramVal = word
- break
- end
- end
- res[resCnt] = { name = paramName, val = paramVal }
- resCnt = resCnt + 1
- end
- return res
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement