i_Jedi

api_network.lua

Dec 4th, 2020 (edited)
418
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.     Install: /api/network.lua
  3. --]]
  4.  
  5.  
  6.  
  7. --[[
  8.     Retuns the hostname for the given computer id on the given protocol
  9. --]]
  10. function lookupHostName(id, protocol)
  11.     local res = nil
  12.  
  13.     -- Do rednet lookup until we find what we need
  14.     rednet.send(id, {sType = "lookup", sProtocol = protocol}, "dns")
  15.     local message
  16.     repeat
  17.             message = {rednet.receive("dns")}
  18.     until type(message) == "table" and message[1] == id
  19.         and type(message[2]) == "table"
  20.         and message[2].sType == "lookup response"
  21.  
  22.     return message[2].sHostname
  23. end
  24.  
  25. --[[
  26.     Loops through computer sides to find a modem and then opens rednet
  27. --]]
  28. function openNetwork()
  29.     local sides = rs.getSides()
  30.     for i = 1, #sides, 1 do
  31.         local side = sides[i]
  32.         if peripheral.getType(side) == "modem" then
  33.             rednet.open(side)
  34.             return true
  35.         end
  36.     end
  37.     return false
  38. end
  39.  
  40. --[[
  41.     Builds host name and registers host on rednet.
  42.     Throws error if host already exists
  43. --]]
  44. function registerHost(protocol, type)
  45.  
  46.     -- Build host name
  47.     local hostType = nil
  48.     if type == "mainframe" then hostType = "main"
  49.     elseif type == "server" then hostType = "svr"
  50.     elseif type == "client" then hostType = "cl" end
  51.     if not hostType then error("Invalid host type: " .. type) end
  52.     local hostName = protocol.."_"..hostType.."_"..os.getComputerID()
  53.  
  54.     -- Make sure host doesn't exist
  55.     if rednet.lookup(protocol, hostName) then
  56.         error("Hostname '"..hostName.."' already exists on '"..protocol.."'")
  57.     end
  58.  
  59.     -- Good to go
  60.     rednet.host(protocol, hostName)
  61.     return hostName
  62.  
  63. end
  64.  
  65.  
  66. --[[
  67.     Splits the given command
  68. --]]
  69. function splitCommand(cmd)
  70.  
  71.     local baseCmd = nil
  72.     local args = { }
  73.     local cnt = 0
  74.  
  75.     if cmd then
  76.         for word in cmd:gmatch('%S+') do
  77.             if word:len() > 0 then
  78.                 if cnt == 0 then
  79.                     baseCmd = word
  80.                 else
  81.                     args[cnt] = word
  82.                 end
  83.                 cnt = cnt + 1
  84.             end
  85.         end
  86.     end
  87.  
  88.     return { cmd = baseCmd, args = args }
  89.  
  90. end
  91.  
  92. function parseArgs(args, startAt)
  93.     local res = {}
  94.     local resCnt = 1
  95.  
  96.     -- Split args on colon. IE: status:Running
  97.     for i = startAt, #args, 1 do
  98.         local arg = args[i]
  99.         local paramName = nil
  100.         local paramVal = nil
  101.         for word in arg:gmatch('([^:]+)') do
  102.             if not paramName then
  103.                 paramName = word
  104.             else
  105.                 paramVal = word
  106.                 break
  107.             end
  108.         end
  109.         res[resCnt] = { name = paramName, val = paramVal }
  110.         resCnt = resCnt + 1
  111.     end
  112.  
  113.     return res
  114. end
RAW Paste Data