Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- CC IP API has custom events:
- IP_connect connected with the network
- IP_connect_time_out failed to connect to the network
- IP_connection_lost lost connection to the network
- IP_message a message is recieved
- sender adress
- IP message channel
- IP message reply channel
- message
- is broadcast (not implemented)
- IP_host_resolved hostname has been resolved
- hostname
- real adress
- IP_host_error hostname cannot be resolved
- ]]--
- local function split(str, chr)
- r = {}
- at = 1
- for i = 1, str:len() do
- if string.sub(str, i, i) == chr then
- at = at + 1
- else
- if r[at] == nil then
- r[at] = string.sub(str, i, i)
- else
- r[at] = r[at].. string.sub(str, i, i)
- end
- end
- end
- return r
- end
- reqport = 8976
- adress = "."
- adress_giver = "."
- modem = nil
- modem_side = nil
- connect_timer = nil
- check_timer = nil
- dns_timer = nil
- connect_time = 5
- check_time = 3
- dns_time = 3
- get_ip = false
- log = false
- lmid = -1
- dns_cache = {}
- message_queue = {}
- function initialize(side)
- if pocket then
- side = "back"
- if log then
- print("pocket computer, forcing modem to \"back\"")
- end
- end
- if peripheral.getType(side) == "modem" then
- modem = peripheral.wrap(side)
- modem_side = side
- if log then
- print("successfully initialised IP API with modem \"".. side.. "\"")
- end
- modem.open(reqport)
- connect()
- else
- error("IP API: initialise: side ".. side.. " does not have a modem attached!")
- end
- end
- function open(port)
- modem.open(port + 1000)
- if log then
- print("opened port ".. port)
- end
- end
- function close(port)
- modem.close(port + 1000)
- if log then
- print("closed port ".. port)
- end
- end
- function transmit(to, port, reply, message)
- if not isIP(to) and to ~= "broadcast" then
- if dns_cache[to] ~= nil then
- to = dns_cache[to]
- else
- resolveHostname(to)
- if message_queue[to] == nil then
- message_queue[to] = {}
- end
- message_queue[to][#message_queue[to] + 1] = {}
- message_queue[to][#message_queue[to]].port = port
- message_queue[to][#message_queue[to]].reply = reply
- message_queue[to][#message_queue[to]].message = message
- return
- end
- end
- mmsgid = math.floor(math.random(0, 99999))
- lmid = mmsgid
- modem.transmit(port + 1000, reply + 1000, adress.. ":".. mmsgid.. ":".. to.. ":".. message)
- end
- function broadcast(port, reply, message)
- transmit("broadcast", port, reply, message)
- end
- function doLogs(b)
- log = b
- if log then
- print("logging set to true")
- end
- end
- function deInitialize()
- adress = "."
- adress_giver = "."
- modem = nil
- connect_timer = nil
- check_timer = nil
- get_ip = false
- if log then
- print("successfully de-initialised IP API")
- end
- end
- function getConnectionStatus()
- r1 = adress ~= "."
- r2 = adress
- return r1, r2
- end
- function checkConnectStatus()
- check_timer = os.startTimer(check_time)
- connect_timer = os.startTimer(connect_time)
- modem.transmit(reqport, reqport, adress.. ":".. adress.. ":".. adress_giver.. ":ping")
- if log then
- print("connection is OK")
- end
- end
- function clearCache()
- dns_cache = {}
- end
- function resolveHostname(name)
- if isIP(name) then
- os.queueEvent("IP_host_resolved", name, name)
- return true
- elseif dns_cache[name] ~= nil then
- os.queueEvent("IP_host_resolved", name, dns_cache[name])
- return true
- else
- dns_timer = os.startTimer(dns_time)
- modem.transmit(reqport, reqport, adress.. ":".. adress.. ":DNS:get:".. name)
- if log then
- print("awaiting DNS")
- end
- return false
- end
- end
- function isIP(name)
- s = split(name, ".")
- if #s < 4 then
- return false
- end
- n1 = tonumber(s[1])
- n2 = tonumber(s[1])
- n3 = tonumber(s[1])
- n4 = tonumber(s[1])
- return n1 >= 0 and n1 % 1 == 0 and n2 >= 0 and n2 % 1 == 0 and n3 >= 0 and n3 % 1 == 0 and n4 >= 0 and n4 % 1 == 0
- end
- function checkEvents(event, p1, p2, p3, p4, p5)
- if event == "IP_host_resolved" then
- if message_queue[p1] ~= nil then
- for i = 1, #message_queue[p1] do
- transmit(p1, message_queue[p1][i].port, message_queue[p1][i].reply, message_queue[p1][i].message)
- end
- end
- elseif event == "timer" then
- if p1 == dns_timer then
- os.queueEvent("IP_host_error")
- elseif p1 == check_timer then
- checkConnectStatus()
- elseif p1 == connect_timer then
- if get_ip then
- get_ip = false
- os.queueEvent("IP_connect_time_out")
- if log then
- print("connection timed out!")
- end
- else
- adress = "."
- os.queueEvent("IP_connection_lost")
- if log then
- print("connection lost!")
- end
- end
- if adress ~= "." then
- checkConnectStatus()
- else
- connect()
- end
- return true
- end
- elseif event == "modem_message" then
- if p1 == modem_side and p2 ~= reqport then
- sendChannel = p2
- replyChannel = p3
- trace, mraw = decodeMessage(p4)
- msg = mraw[3]
- for i = 4, #mraw do
- msg = msg..":".. mraw[i]
- end
- tarr = split(trace, ">")
- sendAdress = tarr[#tarr]
- if (mraw[2] == adress or mraw[2] == "broadcast") and tonumber(mraw[1]) ~= lmid then
- os.queueEvent("IP_message", sendAdress, sendChannel - 1000, replyChannel - 1000, msg)
- lmid = tonumber(mraw[1])
- end
- end
- if p1 == modem_side and p2 == reqport then
- trace, msg = decodeMessage(p4)
- if log then
- print("message trace: ".. trace)
- print("message:")
- for i = 1, #msg do
- print(msg[i])
- end
- end
- if #msg == 3 then
- if get_ip and msg[1] == tostring(os.getComputerID()) and msg[2] == "adress" then
- adress = msg[3]
- satr = split(trace, ">")
- adress_giver = satr[#satr]
- get_ip = false
- if log then
- print("connected and got adress ".. adress)
- print("from ".. adress_giver)
- end
- os.queueEvent("IP_connect", adress)
- check_timer = os.startTimer(check_time)
- connect_timer = nil
- return true
- end
- if not get_ip and msg[1] == adress_giver and msg[2] == adress and msg[3] == "pong" then
- ckeck_timer = os.startTimer(check_time)
- return true
- end
- end
- if #msg == 4 then
- if msg[1] == "DNS" and msg[2] == adress then
- dns_cache[msg[3]] = msg[4]
- os.queueEvent("IP_host_resolved", msg[3], msg[4])
- end
- end
- return true
- end
- return false
- end
- return false
- end
- function decodeMessage(raw)
- arr = split(raw, ":")
- sendTrace = arr[1]
- message = {}
- for i = 2, #arr do
- message[i - 1] = arr[i]
- end
- return sendTrace, message
- end
- function connect()
- get_ip = true
- connect_timer = os.startTimer(connect_time)
- modem.transmit(reqport, reqport, os.getComputerID().. ":getadress")
- if log then
- print("connecting...")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement