Advertisement
pedrosgali

PedrOS netAPI

Nov 21st, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.39 KB | None | 0 0
  1. --GLOBAL VARIABLES--
  2.  
  3. myId = os.computerID() + 10
  4.  
  5. --MODEM FUNCTIONS--
  6.  
  7. --Opens the modem and channels on selected side
  8. function open(channel, side)
  9.     m = peripheral.wrap(side)
  10.         m.open(channel)
  11.     m.open(myId)
  12. end
  13.  
  14. --Closes all channels on selected side
  15. function close(side)
  16.     m = peripheral.wrap(side)
  17.     m.closeAll()
  18. end
  19.  
  20. --PING FUNCTIONS
  21.  
  22. --Get names of all peripherals attached to this computer
  23. --Returns: table
  24. function ping(side)
  25.     m = peripheral.wrap(side)
  26.     return(m.getNamesRemote())
  27. end
  28.  
  29. --Get names of all peripherals attached to this computer
  30. --Returns: table
  31. function dnsPing(room, side)
  32.     m = peripheral.wrap(side)
  33.     netTab = m.getNamesRemote()
  34.     netMap = {}
  35.     for i = 1, #netTab do
  36.         name = netTab[i]
  37.         netMap[i] = {}
  38.         perType = string.match(name, '%a+')
  39.         perNum = tonumber(string.match(name, "%d+"))
  40.         netMap[i][1] = netTab[i]
  41.         netMap[i][2] = perType
  42.         netMap[i][3] = room.." "..perType.." "..(perNum + 1)
  43.     end
  44.     return netMap
  45. end
  46.  
  47. --Get names of specific peripherals attached to this computer
  48. --Returns: table
  49. function dnsPingType(room, request, side)
  50.     m = peripheral.wrap("side")
  51.     oldTab = dnsPing(room, side)
  52.     newTab = {}
  53.     j = 1
  54.     for i = 1, #oldTab do
  55.         newTab[i] = {}
  56.         if oldTab[i][2] == request then
  57.             newTab[j][1] = oldTab[i][1]
  58.             newTab[j][2] = oldTab[i][2]
  59.             newTab[j][3] = oldTab[i][3]
  60.             j = j + 1
  61.         end
  62.     end
  63.     return(newTab)
  64. end
  65.  
  66. --SEND FUNCTIONS--
  67.  
  68. --Sends message over specified channel
  69. function send(channel, msg, side)
  70.     open(channel, side)
  71.     m.transmit(channel, myId, msg)
  72.     close(side)
  73. end
  74.  
  75. --Sends table over specified channel
  76. function sendTable(channel, tableData, side)
  77.     open(channel, side)
  78.        m.transmit(channel, retCh, textutils.serialize(tableData))
  79.        close(side)
  80. end
  81.  
  82. --Sends file over specified channel
  83. function sendCode(channel, fileName, side)
  84.     open(channel, side)
  85.     data = fs.open(fileName, "r")
  86.     message = data.readAll()
  87.     data.close()
  88.     m.transmit(channel, myId, message)
  89.     close(side)
  90. end
  91.  
  92. --RECEIVE FUNCTIONS--
  93.  
  94. --Receives message over specified channel
  95. function receive(channel, side)
  96.     open(channel, side)
  97.     event, mSide, sendCh, retCh, rMsg, dist = os.pullEvent("modem_message")
  98.     close(side)
  99.     return(rMsg, retCh)
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement