Advertisement
Piorjade

OpenComputers - NET API

Mar 31st, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. --[[
  2.     Wireless-Modem-API
  3.  
  4.   This is a simple interface for wireless modems and makes use
  5.   of the DNS servers that you have to set up seperately.
  6.  
  7.   With this you can register an URL at the specific DNS server so
  8.   other clients can look up your long modem address just by entering
  9.   your URL. (e.g. "google.mc" > "abcd-2q3as-2qejh-ßjadf-blah")
  10.  
  11.   By Piorjade
  12. ]]
  13.  
  14. --CONFIGURATION
  15.  
  16. local conf =  {
  17.   _DNS = "",             -----ENTER THE ADDRESS OF THE DNS SERVER'S MODEM!!!
  18.   _PORT = 8080,            -----PORT MUST BE SAME AS THE PORT OF THE DNS SERVER!
  19. }
  20.  
  21. --CONFIGURATION END
  22.  
  23. local component = require("component")
  24. local computer = require("computer")
  25. local ser = require("serialization")
  26. local event = require("event")
  27. local os = require("os")
  28.  
  29. local COMMMANDS = {
  30.   registerURL = "rURL",
  31.   deleteURL = "dURL",
  32.   getAddress = "gADDR",
  33.   getURL = "gURL",
  34.   getAll = "gAll",
  35. }
  36.  
  37. local errorMessages = {
  38.   DNS_URL_NOT_FOUND = true,
  39.   DNS_DELETE_FAILED = true,
  40.   DNS_ADDR_NOT_FOUND = true,
  41.  
  42. }
  43.  
  44. local card
  45. for addr, a in component.list("modem") do
  46.   card = component.proxy(addr)
  47.   break
  48. end
  49. if not card then return error("no card detected") end
  50. local net = {}
  51.  
  52. card.open(conf._PORT) --open up the port
  53. if not card.isOpen(conf._PORT) then print("FAILED TO OPEN PORT!") return end
  54.  
  55. net.foundServer = nil
  56. function net.listenForDNS()
  57.   net.foundServer = nil
  58.   local _, myAddr, sAddr, port, distance, msg, meta = event.pull("modem_message", nil, nil, nil, nil, "DNS_SERVER")
  59.   if port == conf._PORT and msg == "DNS_SERVER" and meta then
  60.     meta = ser.unserialize(meta)
  61.     if meta.type == "dns_server" then
  62.       net.foundServer =  {type = "dns_server", host = meta.host, address = sAddr}
  63.     end
  64.   end
  65. end
  66.  
  67. function net.registerURL(URL)
  68.   if conf._DNS ~= nil then
  69.     card.send(conf._DNS, conf._PORT, "rURL", URL)
  70.     local _, myAddr, sAddr, port, distance, msg, meta = event.pull(2, "modem_message", nil, conf._DNS)
  71.     if _ and port == conf._PORT and sAddr == conf._DNS then
  72.       if not errorMessages[msg] then
  73.         return true, msg
  74.       else
  75.         return false, msg
  76.       end
  77.     elseif not _ then
  78.       return false, "NO_RESPONSE"
  79.     end
  80.   else
  81.     return false, "NO_DNS"
  82.   end
  83. end
  84.  
  85. function net.removeURL(URL)
  86.   if conf._DNS ~= nil then
  87.     card.send(conf._DNS, conf._PORT, "dURL", URL)
  88.     local _, myAddr, sAddr, port, distance, msg, meta = event.pull(2, "modem_message", nil, conf._DNS)
  89.     if _ and port == conf._PORT and sAddr == conf._DNS then
  90.       if not errorMessages[msg] then
  91.         return true, msg
  92.       else
  93.         return false, msg
  94.       end
  95.     elseif not _ then
  96.       return false, "NO_RESPONSE"
  97.     end
  98.   else
  99.     return false, "NO_DNS"
  100.   end
  101. end
  102.  
  103. function net.setDNS(addr)
  104.     conf._DNS = addr
  105. end
  106.  
  107. function net.getDNS()
  108.     return conf._DNS
  109. end
  110.  
  111. function net.getPort()
  112.     return conf._PORT
  113. end
  114.  
  115. function net.setPort(port)
  116.     if type(port) == "number" then
  117.         card.close(conf._PORT)
  118.         conf._PORT = port
  119.         card.open(conf._PORT)
  120.     end
  121. end
  122.  
  123. function net.getCard()
  124.     return card
  125. end
  126.  
  127. function net.resolve(address)
  128.   if conf._DNS ~= nil then
  129.     card.send(conf._DNS, conf._PORT, "gADDR", address)
  130.     local _, myAddr, sAddr, port, distance, msg, meta = event.pull(2, "modem_message", nil, conf._DNS)
  131.     if _ and port == conf._PORT and sAddr == conf._DNS then
  132.       if not errorMessages[msg] then
  133.         return true, msg
  134.       else
  135.         return false, msg
  136.       end
  137.     elseif not _ then
  138.       return false, "NO_RESPONSE"
  139.     end
  140.   else
  141.     return false, "NO_DNS"
  142.   end
  143. end
  144.  
  145. function net.getURL(address)
  146.   if conf._DNS ~= nil then
  147.     card.send(conf._DNS, conf._PORT, "gURL", address)
  148.     local _, myAddr, sAddr, port, distance, msg, meta = event.pull(2, "modem_message", nil, conf._DNS)
  149.     if _ and port == conf._PORT and sAddr == conf._DNS then
  150.       if not errorMessages[msg] then
  151.         return true, msg
  152.       else
  153.         return false, msg
  154.       end
  155.     elseif not _ then
  156.       return false, "NO_RESPONSE"
  157.     end
  158.   else
  159.     return false, "NO_DNS"
  160.   end
  161. end
  162.  
  163. function net.getALL()
  164.   if conf._DNS ~= nil then
  165.     card.send(conf._DNS, conf._PORT, "gAll")
  166.     local _, myAddr, sAddr, port, distance, msg, meta = event.pull(2, "modem_message", nil, conf._DNS)
  167.     if _ and port == conf._PORT and sAddr == conf._DNS then
  168.       if not errorMessages[msg] then
  169.         return true, ser.unserialize(msg)
  170.       else
  171.         return false, msg
  172.       end
  173.     elseif not _ then
  174.       return false, "NO_RESPONSE"
  175.     end
  176.   else
  177.     return false, "NO_DNS"
  178.   end
  179. end
  180.  
  181. return net
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement