Advertisement
Olacken

wirelessPeripheralModem

Jun 1st, 2016
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.81 KB | None | 0 0
  1. local PROTOCOL = "wlp"
  2. for n,v in ipairs({"top","bottom","front","back","left","right"}) do
  3.     if peripheral.getType(v) == "modem" and peripheral.call(v,"isWireless") then
  4.         rednet.open(v)
  5.         break
  6.     elseif v=="right" then
  7.         error("No wireless modem found",2)
  8.     end
  9. end
  10.        
  11. os.setComputerLabel("wlm"..tostring( os.getComputerID()))
  12. rednet.host(PROTOCOL,os.getComputerLabel())
  13.  
  14. local func = {
  15. getNames = function(sId,msg)
  16.     print("getNames()")
  17.     local names = {}
  18.     for n, name in ipairs(peripheral.getNames()) do
  19.         if peripheral.getType(name) ~= "modem" then
  20.             table.insert(names,name)
  21.         end
  22.     end
  23.     rednet.send(sId,{func="getNames",modem=os.getComputerLabel(),funcr=names},PROTOCOL)
  24. end,
  25.  
  26. isPresent = function(sId,msg)
  27.     print("isPresent(" .. msg.periph .. ")")
  28.     rednet.send(sId,{func="isPresent",modem=os.getComputerLabel(),funcr=peripheral.isPresent(msg.periph)},PROTOCOL)
  29. end,
  30.  
  31. getType = function(sId,msg)
  32.     print("getType(" .. msg.periph .. ")")
  33.     rednet.send(sId,{func="getType",modem=os.getComputerLabel(),funcr=peripheral.getType(msg.periph)},PROTOCOL)
  34. end,
  35.  
  36. getMethods = function(sId,msg)
  37.     print("getMethods(" .. msg.periph .. ")")
  38.     rednet.send(sId,{func="getMethods",modem=os.getComputerLabel(),funcr=peripheral.getType(msg.periph)},PROTOCOL)
  39. end,
  40.  
  41. call = function(sId,msg)
  42.     if msg.args == nil then msg.args = {}end
  43.     print("call(" .. msg.periph..", " .. msg.method .. ", " .. table.concat(msg.args,", " ) .. ")")
  44.     local status,res = pcall(function() local t = {peripheral.call(msg.periph,msg.method,unpack(msg.args))} return t end)
  45.     rednet.send(sId,{func="call",modem=os.getComputerLabel(),funcr=res,err= not status},PROTOCOL)
  46. end,
  47.  
  48. wrap = function(sId,msg)
  49.     print("wrap(" .. msg.periph .. ")")
  50.     rednet.send(sId,{func="wrap",modem=os.getComputerLabel(),funcr=peripheral.getMethods(msg.periph)},PROTOCOL)
  51. end,
  52.  
  53. find = function(sId,msg)
  54.     print("find(" .. msg.ptype .. ")")
  55.     local result={}
  56.     if msg.ptype ~= "modem" then
  57.         for n,pName in ipairs(peripheral.getNames()) do
  58.             if  peripheral.getType(pName)== msg.ptype then
  59.                 table.insert(result, pName)
  60.             end
  61.         end
  62.     end
  63.     rednet.send(sId,{func="find",modem=os.getComputerLabel(),funcr=result},PROTOCOL)
  64. end
  65. }
  66.  
  67. setmetatable(func, { __index = function()
  68.     return function(sId,msg)
  69.         rednet.send(event[2],{func=event[1].func,modem=os.getComputerLabel()},PROTOCOL)
  70.     end
  71. end})
  72.  
  73. while true do
  74.     local event = {os.pullEventRaw()}
  75.     if event[1] == "rednet_message" then
  76.         if event[4] == PROTOCOL then
  77.             if event[3].modem == os.getComputerLabel() then
  78.                 if event[3].periph == nil or peripheral.getType(event[3].periph) ~= "modem" then
  79.                     func[event[3].func](event[2],event[3])
  80.                 else
  81.                     rednet.send(event[2],{func=event[1].func,modem=os.getComputerLabel()},PROTOCOL)
  82.                 end
  83.             end
  84.         end
  85.     elseif event[1] == "terminate" then
  86.         return
  87.     end
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement