Advertisement
PeachGaming

Net 2.0

Feb 12th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. local event, side, sChannel, rChannel, data, dist
  2. local modems = {}
  3. for index,side in pairs(peripheral.getNames()) do
  4. if peripheral.getType(side) == "modem" then
  5. table.insert(modems, peripheral.wrap(side))
  6. end
  7. end
  8. local SSIDTable = {}
  9. local clientTable = {}
  10. local active
  11. local timeout
  12.  
  13. local function open(id)
  14. for index,modem in pairs(modems) do
  15. modem.open(id)
  16. end
  17. end
  18. open(os.getComputerID())
  19.  
  20. local function close(id)
  21. for index,modem in pairs(modems) do
  22. modem.close(id)
  23. end
  24. end
  25.  
  26. local function metaReceive()
  27. event, side, sChannel, rChannel, data, dist = os.pullEvent("modem_message")
  28. end
  29.  
  30. local function _sleep()
  31. sleep(3)
  32. timeout = true
  33. end
  34.  
  35. local function sleepReceive()
  36. metaReceive()
  37. timeout = false
  38. end
  39.  
  40. local function transmit(msg, to)
  41. for index,modem in pairs(modems) do
  42. modem.transmit(to, os.getComputerID(), msg)
  43. end
  44. end
  45.  
  46. function scan()
  47. SSIDTable = {}
  48. open(65435)
  49. transmit("scanSSID", 65435)
  50. while true do
  51. parallel.waitForAny(_sleep, sleepReceive)
  52. if timeout then
  53. break
  54. end
  55. SSIDTable[data] = rChannel
  56. close(65435)
  57. end
  58. end
  59.  
  60. function getSSIDTable()
  61. local _table = {}
  62. for index in pairs(SSIDTable) do
  63. table.insert(_table, index)
  64. end
  65. return _table
  66. end
  67.  
  68. function connect(ssid, passwd)
  69. scan()
  70. open(65435)
  71. transmit(passwd, SSIDTable[ssid])
  72. metaReceive()
  73. if rChannel == SSIDTable[ssid] and data == "success" then
  74. close(65435)
  75. active = rChannel
  76. return true
  77. else
  78. close(65435)
  79. return false
  80. end
  81. end
  82.  
  83. function disconnect()
  84. transmit("disconnect", active)
  85. active = nil
  86. end
  87.  
  88. function host(ssid, passwd)
  89. close(65535)
  90. open(65435)
  91. while true do
  92. metaReceive()
  93. if data == "scanSSID" then
  94. transmit(ssid, rChannel)
  95. print(rChannel .. " requested SSID")
  96. elseif data == passwd then
  97. transmit("success", rChannel)
  98. table.insert(clientTable, rChannel)
  99. print(rChannel .. " connected")
  100. elseif data == "disconnect" then
  101. print("dropped " .. rChannel .. " from clientTable")
  102. local tmp
  103. for index,id in pairs(clientTable) do
  104. if id == rChannel then
  105. tmp = index
  106. end
  107. end
  108. clientTable[tmp] = nil
  109. elseif type(data) == "table" then
  110. for cell,id in pairs(clientTable) do
  111. if id == data["to"] then
  112. transmit(data, data["to"])
  113. print(rChannel .. " relay to " .. data["to"])
  114. break
  115. end
  116. end
  117. end
  118. end
  119. end
  120.  
  121. function send(msg, to)
  122. local message = {
  123. from = os.getComputerID(),
  124. to = to,
  125. msg = msg
  126. }
  127. transmit(message, active)
  128. end
  129.  
  130. function receive()
  131. event, side, sChannel, rChannel, data, dist = os.pullEvent("modem_message")
  132. if sChannel ~= os.getComputerID() then
  133. else
  134. return data
  135. end
  136. return ""
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement