Advertisement
cyber_Ahn

caOS_router

Jul 20th, 2016
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.25 KB | None | 0 0
  1. modem = peripheral.wrap("right")
  2. local power = true
  3. local partner = "0"
  4. --split
  5. function split(pString,pPattern)
  6. local Table = {}
  7. local fpat = "(.-)"..pPattern
  8. local last_end = 1
  9. local s,e,cap = pString:find(fpat,1)
  10. while s do
  11. if s ~= 1 or cap ~= "" then
  12. table.insert(Table,cap)
  13. end
  14. last_end = e+1
  15. s,e,cap = pString:find(fpat,last_end)
  16. end
  17. if last_end <= #pString then
  18. cap = pString:sub(last_end)
  19. table.insert(Table,cap)
  20. end
  21. return Table
  22. end
  23.  
  24. -- open modems
  25. function set_chan()
  26. rednet.open("top")
  27. file = fs.open("port.db","r")
  28. fileDataX = {}
  29. local line = file.readLine()
  30. repeat
  31. table.insert(fileDataX,line)
  32. line = file.readLine()
  33. until line == nil
  34. file.close()
  35. for iu=1,#fileDataX do
  36. dataX = fileDataX[iu]
  37. print("Open Port: "..dataX)
  38. modem.open(tonumber(dataX))
  39. end
  40. end
  41.  
  42. --decoder
  43. function recive_wlan_lan()
  44. while power == true do
  45. event, id, message = os.pullEvent()
  46. if event == "rednet_message" then
  47. if tostring(id) ~= partner then
  48. print("sending in lan: "..message)
  49. data = split(message,"|")
  50. modem.transmit(tonumber(data[2]),tonumber(data[3]),data[4])
  51. end
  52. end
  53. end
  54. end
  55.  
  56. --decoder
  57. function recive_lan_wlan()
  58. while power == true do
  59. event, modemside , sendchan , replaychan ,  message , dist = os.pullEvent()
  60. if modemside == "right" then
  61. if tonumber(dist) > 6 then
  62. senddata = os.getComputerID().."|"..sendchan.."|"..replaychan.."|"..message
  63. print("sending in wlan: "..senddata)
  64. rednet.broadcast(senddata)
  65. end
  66. end
  67. end
  68. end
  69.  
  70. --start program
  71. shell.run("delete port.db")
  72. shell.run("pastebin get zaxKi9mC port.db")
  73. set_chan()
  74. local found = fs.exists("config.cfg")
  75. if found == false then
  76. print("start router on ID:"..os.getComputerID())
  77. print("enter partner ID:")
  78. partner_w = read()
  79. print("1. wlan to lan")
  80. print("2. lan to wlan")
  81. modus_w = read()
  82. local file = fs.open("config.cfg","w")
  83. file.writeLine(partner_w)
  84. file.writeLine(modus_w)
  85. file.close()
  86. end
  87. file = fs.open("config.cfg","r")
  88. local fileData = {}
  89. local line = file.readLine()
  90. repeat
  91. table.insert(fileData,line)
  92. line = file.readLine()
  93. until line == nil
  94. file.close()
  95. partner = fileData[1]
  96. modus = fileData[2]
  97. if modus == "1" then
  98. print("Start wlan to lan")
  99. recive_wlan_lan()
  100. end
  101. if modus == "2" then
  102. print("Start lan to wlan")
  103. recive_lan_wlan()
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement