Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- if #tArgs < 1 or (tArgs[1] ~= "m" and tArgs[1] ~= "s") then error("Usage channelMaster <mode>\nMode: m/s = master/slave") end
- local masterMode = (tArgs[1]=="m")
- local commChannel = rednet.CHANNEL_BROADCAST-1
- local masterModemCount = 0
- local slaves = {}
- local mySlaveChannel = os.computerID()*3
- local dynamicChannels = {}
- local dynMax = 0
- local rangeIndex = 0
- local openDynChannels = 0
- local modems = {}
- for k,v in pairs(rs.getSides()) do
- if peripheral.getType(v) == "modem" then
- table.insert(modems,{per=peripheral.wrap(v),openChannels=0})
- end
- end
- for k,v in pairs(modems) do
- v.per.closeAll()
- end
- if masterMode then
- print("Master Mode\nListening on: "..commChannel.." & " .. rednet.CHANNEL_BROADCAST)
- modems[1].per.open(commChannel)
- modems[1].per.open(rednet.CHANNEL_BROADCAST)
- modems[1].openChannels = 2
- else
- print("Slave Mode\nListening on: "..commChannel.." & "..mySlaveChannel)
- modems[1].per.open(commChannel)
- modems[1].per.open(mySlaveChannel)
- modems[1].openChannels = 2
- end
- dynMax = (#modems * 128)-2
- print("Found "..#modems.. " modems")
- if masterMode then
- print("Ready to search...") os.pullEvent("key")
- term.clear()
- term.setCursorPos(1,1)
- print("Waiting for clients...")
- modems[1].per.transmit(commChannel,commChannel,"RPPING")
- while true do
- local args = {os.pullEvent()}
- local cmd = table.remove(args,1)
- if cmd == "key" then
- if args[1] == keys.enter then
- break
- end
- elseif cmd == "modem_message" then
- local p = textutils.unserialize(args[4])
- if type(p) == "table" and type(p.mType) == "string" and p.mType == "RPANSWER" and type(p.modemCount) == "number" and type(p.channelCount) == "number" then
- table.insert(slaves,{args[3],p.channelCount})
- masterModemCount = masterModemCount + p.modemCount
- end
- end
- end
- print("Found " .. #slaves .. " slaves with a total of "..masterModemCount .. " modems.") os.pullEvent("key")
- else
- print("Listening")
- while true do
- local ev,side,chan,replchan,msg,dist = os.pullEvent("modem_message")
- if chan == commChannel and msg == "RPPING" then
- modems[1].per.transmit(commChannel,mySlaveChannel,textutils.serialize({mType = "RPANSWER",modemCount=#modems,channelCount=dynMax}))
- break
- end
- end
- end
- local maxDynChannelRange = masterModemCount + dynMax
- function closeAllChannels()
- for k,v in pairs(dynamicChannels) do
- modems[v].per.close(k)
- end
- for k,v in pairs(modems) do
- v.openChannels = 0
- end
- modems[1].openChannels = 2
- dynamicChannels = {}
- openDynChannels = 0
- end
- function openRange(rStart,rWidth)
- print("Opening "..rStart .. "=>"..(rStart+rWidth-1))
- local modemI = 1
- for i=rStart,rStart+(rWidth-1),1 do
- if modems[modemI].openChannels == 128 then
- modemI = modemI+1
- print("Changing to modem #"..modemI)
- end
- dynamicChannels[i] = modemI
- modems[modemI].openChannels = modems[modemI].openChannels + 1
- modems[modemI].per.open(i)
- end
- end
- function masterListenRange(nR)
- closeAllChannels()
- rangeIndex = nR
- local cChan = (rangeIndex * maxDynChannelRange)
- openRange(cChan,dynMax)
- cChan = cChan + dynMax
- for k,v in pairs(slaves) do
- modems[1].per.transmit(commChannel,v[1],textutils.serialize({mType = "RPLRANGE",sChan = cChan,sWidth = v[2]}))
- cChan = cChan + v[2]
- end
- end
- if masterMode then
- sleep(2)
- masterListenRange(0)
- end
- function logMsg(chan,replchan,msg)
- print(chan.."=>"..replchan.."//"..msg)
- end
- print("Listening...")
- while true do
- local ev,side,chan,replchan,msg,dist = os.pullEvent()
- if ev == "modem_message" then
- local pp = textutils.unserialize(msg)
- if ((not masterMode) and chan == commChannel and replchan == mySlaveChannel and type(pp) == "table" and pp.mType == "RPLRANGE") then
- closeAllChannels()
- openRange(pp.sChan,pp.sWidth)
- else
- if not masterMode then
- if chan ~= commChannel then
- print("INTERCEPT")
- logMsg(chan,replchan,msg)
- modems[1].per.transmit(commChannel,mySlaveChannel,textutils.serialize({mType = "RPINTERCEPT", ["chan"]=chan,["replchan"]=replchan,["msg"]=msg }))
- end
- else
- if type(pp) == "table" and type(pp.mType) == "string" and pp.mType == "RPINTERCEPT" then
- logMsg(pp.chan,pp.replchan,pp.msg)
- else
- logMsg(chan,replchan,msg)
- end
- end
- end
- elseif ev == "key" then
- if side == keys.left then
- masterListenRange(math.max(rangeIndex-1,0))
- elseif side == keys.right then
- masterListenRange(rangeIndex+1)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement