Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ComputerCraft-LyqydNet/apis/net
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
165 lines (154 sloc)
3.87 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| routeTable = {} | |
| daemonTable = {} | |
| socketTable = {} | |
| networkDaemonTimeout = nil | |
| local modemSide | |
| function netInit (_side) | |
| --initialize network communications | |
| if net.daemonTable.netd then | |
| local open = false | |
| if not _side then | |
| for n,side in pairs(rs.getSides()) do | |
| if peripheral.getType(side) == "modem" then | |
| modemSide = side | |
| rednet.open(side) | |
| open = true | |
| break | |
| end | |
| end | |
| else | |
| modemSide = _side | |
| rednet.open(_side) | |
| open = true | |
| end | |
| --keep in mind that netd must be an exception since packets coming to it do not always have route numbers yet. | |
| if open then coroutine.resume(net.daemonTable.netd, "lyqydnet_packet", packet.new("SI", 0, "announce")) return true end | |
| end | |
| return false | |
| end | |
| function daemonAdd (dName, dFunction, socket) | |
| daemonTable[dName] = coroutine.create(dFunction) | |
| if socket then socketTable[tonumber(socket)] = daemonTable[dName] end | |
| coroutine.resume(daemonTable[dName], "lyqydnet_packet", packet.new("SI", 0, "start")) | |
| end | |
| function daemonRemove (dName) | |
| if daemonTable[dName] then | |
| for sNum, sInfo in pairs(socketTable) do | |
| if sInfo == dName then | |
| sNum = nil | |
| end | |
| end | |
| daemonTable[dName] = nil | |
| return true | |
| else | |
| return false | |
| end | |
| end | |
| function routeFromName (name) | |
| for rNum,rInfo in ipairs(routeTable) do | |
| if rInfo and rInfo.name == tostring(name) then return rNum end | |
| end | |
| return false | |
| end | |
| function routeFromCID (compID) | |
| for rNum,rInfo in ipairs(routeTable) do | |
| if rInfo and rInfo.idNum == tonumber(compID) then return rNum end | |
| end | |
| return false | |
| end | |
| function nameFromRoute (route) | |
| if routeTable[route] then | |
| return routeTable[route].name | |
| else | |
| return false | |
| end | |
| end | |
| function CIDFromRoute (route) | |
| if routeTable[route] then | |
| return routeTable[route].idNum | |
| else | |
| return false | |
| end | |
| end | |
| function send(pack, ll) | |
| local routeNum = net.routeFromCID(pack.destination) | |
| if routeNum then | |
| local route = net.routeTable[routeNum] | |
| if route.cost > 0 then | |
| if ll then pack.confirm = true end | |
| net.raw_send(route.gateway, pack) | |
| return true | |
| else | |
| if route.type == "T" or route.type == "P" then | |
| coroutine.resume(daemonTable.netd, "lyqydnet_packet", packet.new("SI", 0, {instruction = "endpoint", data = pack})) | |
| end | |
| net.raw_send(route.idNum, pack) | |
| return true | |
| end | |
| elseif pack.type == "RM" then | |
| if term.current then | |
| local id = math.random(1, 2147483547) | |
| local messTable = { | |
| nMessageID = id, | |
| nRecipient = pack.destination, | |
| message = pack.payload, | |
| sProtocol = pack.protocol, | |
| } | |
| net.raw_send(pack.destination, messTable) | |
| net.raw_send(65533, messTable) | |
| else | |
| net.raw_send(pack.destination, pack.payload) | |
| end | |
| return true | |
| end | |
| return false, "could not send packet" | |
| end | |
| function raw_send (recipient, message) | |
| peripheral.call(modemSide, "transmit", recipient, os.computerID(), message) | |
| end | |
| function add_route (idNumber, compType, compName, gateway, cost) | |
| idNumber = tonumber(idNumber) | |
| compType = tostring(compType) | |
| compName = tostring(compName) | |
| gateway = tonumber(gateway) or -1 | |
| cost = tonumber(cost) or 0 | |
| for rNum,rInfo in ipairs(routeTable) do | |
| if rInfo then | |
| if idNumber == rInfo.idNum then | |
| if rInfo.name ~= compName then | |
| rInfo.name = compName | |
| end | |
| if rInfo.type ~= compType then | |
| rInfo.type = compType | |
| end | |
| if rInfo.cost > cost then | |
| rInfo.gateway = gateway | |
| rInfo.cost = cost | |
| end | |
| return rNum | |
| end | |
| end | |
| end | |
| local route = {} | |
| route.idNum = idNumber | |
| route.type = compType | |
| route.name = compName | |
| route.gateway = gateway | |
| route.cost = cost | |
| table.insert(routeTable, route) | |
| return #routeTable | |
| end | |
| function remove_route (route) | |
| if route then | |
| local file = io.open("/etc/hosts", "w" ) | |
| if file then | |
| for rNum, rInfo in ipairs(routeTable) do | |
| if rNum ~= route then | |
| file:write(rInfo.idNum..":"..rInfo.gateway..","..rInfo.cost..";"..rInfo.type.." "..rInfo.name.."\n") | |
| end | |
| end | |
| file:close() | |
| end | |
| routeTable[route] = false | |
| end | |
| end |