Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local event, side, sChannel, rChannel, data, dist
- local modems = {}
- for index,side in pairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- table.insert(modems, peripheral.wrap(side))
- end
- end
- local SSIDTable = {}
- local clientTable = {}
- local active
- local timeout
- local function open(id)
- for index,modem in pairs(modems) do
- modem.open(id)
- end
- end
- open(os.getComputerID())
- local function close(id)
- for index,modem in pairs(modems) do
- modem.close(id)
- end
- end
- local function metaReceive()
- event, side, sChannel, rChannel, data, dist = os.pullEvent("modem_message")
- end
- local function _sleep()
- sleep(3)
- timeout = true
- end
- local function sleepReceive()
- metaReceive()
- timeout = false
- end
- local function transmit(msg, to)
- for index,modem in pairs(modems) do
- modem.transmit(to, os.getComputerID(), msg)
- end
- end
- function scan()
- SSIDTable = {}
- open(65435)
- transmit("scanSSID", 65435)
- while true do
- parallel.waitForAny(_sleep, sleepReceive)
- if timeout then
- break
- end
- SSIDTable[data] = rChannel
- close(65435)
- end
- end
- function getSSIDTable()
- local _table = {}
- for index in pairs(SSIDTable) do
- table.insert(_table, index)
- end
- return _table
- end
- function connect(ssid, passwd)
- scan()
- open(65435)
- transmit(passwd, SSIDTable[ssid])
- metaReceive()
- if rChannel == SSIDTable[ssid] and data == "success" then
- close(65435)
- active = rChannel
- return true
- else
- close(65435)
- return false
- end
- end
- function disconnect()
- transmit("disconnect", active)
- active = nil
- end
- function host(ssid, passwd)
- close(65535)
- open(65435)
- while true do
- metaReceive()
- if data == "scanSSID" then
- transmit(ssid, rChannel)
- print(rChannel .. " requested SSID")
- elseif data == passwd then
- transmit("success", rChannel)
- table.insert(clientTable, rChannel)
- print(rChannel .. " connected")
- elseif data == "disconnect" then
- print("dropped " .. rChannel .. " from clientTable")
- local tmp
- for index,id in pairs(clientTable) do
- if id == rChannel then
- tmp = index
- end
- end
- clientTable[tmp] = nil
- elseif type(data) == "table" then
- for cell,id in pairs(clientTable) do
- if id == data["to"] then
- transmit(data, data["to"])
- print(rChannel .. " relay to " .. data["to"])
- break
- end
- end
- end
- end
- end
- function send(msg, to)
- local message = {
- from = os.getComputerID(),
- to = to,
- msg = msg
- }
- transmit(message, active)
- end
- function receive()
- event, side, sChannel, rChannel, data, dist = os.pullEvent("modem_message")
- if sChannel ~= os.getComputerID() then
- else
- return data
- end
- return ""
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement