Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local PROTOCOL = "wlp"
- for n,v in ipairs({"top","bottom","front","back","left","right"}) do
- if peripheral.getType(v) == "modem" and peripheral.call(v,"isWireless") then
- rednet.open(v)
- break
- elseif v=="right" then
- error("No wireless modem found",2)
- end
- end
- os.setComputerLabel("wlm"..tostring( os.getComputerID()))
- rednet.host(PROTOCOL,os.getComputerLabel())
- local func = {
- getNames = function(sId,msg)
- print("getNames()")
- local names = {}
- for n, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name) ~= "modem" then
- table.insert(names,name)
- end
- end
- rednet.send(sId,{func="getNames",modem=os.getComputerLabel(),funcr=names},PROTOCOL)
- end,
- isPresent = function(sId,msg)
- print("isPresent(" .. msg.periph .. ")")
- rednet.send(sId,{func="isPresent",modem=os.getComputerLabel(),funcr=peripheral.isPresent(msg.periph)},PROTOCOL)
- end,
- getType = function(sId,msg)
- print("getType(" .. msg.periph .. ")")
- rednet.send(sId,{func="getType",modem=os.getComputerLabel(),funcr=peripheral.getType(msg.periph)},PROTOCOL)
- end,
- getMethods = function(sId,msg)
- print("getMethods(" .. msg.periph .. ")")
- rednet.send(sId,{func="getMethods",modem=os.getComputerLabel(),funcr=peripheral.getType(msg.periph)},PROTOCOL)
- end,
- call = function(sId,msg)
- if msg.args == nil then msg.args = {}end
- print("call(" .. msg.periph..", " .. msg.method .. ", " .. table.concat(msg.args,", " ) .. ")")
- local status,res = pcall(function() local t = {peripheral.call(msg.periph,msg.method,unpack(msg.args))} return t end)
- rednet.send(sId,{func="call",modem=os.getComputerLabel(),funcr=res,err= not status},PROTOCOL)
- end,
- wrap = function(sId,msg)
- print("wrap(" .. msg.periph .. ")")
- rednet.send(sId,{func="wrap",modem=os.getComputerLabel(),funcr=peripheral.getMethods(msg.periph)},PROTOCOL)
- end,
- find = function(sId,msg)
- print("find(" .. msg.ptype .. ")")
- local result={}
- if msg.ptype ~= "modem" then
- for n,pName in ipairs(peripheral.getNames()) do
- if peripheral.getType(pName)== msg.ptype then
- table.insert(result, pName)
- end
- end
- end
- rednet.send(sId,{func="find",modem=os.getComputerLabel(),funcr=result},PROTOCOL)
- end
- }
- setmetatable(func, { __index = function()
- return function(sId,msg)
- rednet.send(event[2],{func=event[1].func,modem=os.getComputerLabel()},PROTOCOL)
- end
- end})
- while true do
- local event = {os.pullEventRaw()}
- if event[1] == "rednet_message" then
- if event[4] == PROTOCOL then
- if event[3].modem == os.getComputerLabel() then
- if event[3].periph == nil or peripheral.getType(event[3].periph) ~= "modem" then
- func[event[3].func](event[2],event[3])
- else
- rednet.send(event[2],{func=event[1].func,modem=os.getComputerLabel()},PROTOCOL)
- end
- end
- end
- elseif event[1] == "terminate" then
- return
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement