turtle5204

Amethyst Test Client

Apr 22nd, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. -- Beware, horrible code lurks below, waiting to strike a carefree programmer.
  2.  
  3. local d = false
  4. local mod = nil
  5. for k, v in pairs(peripheral.getNames()) do
  6. if peripheral.getType(v) == "modem" then
  7. rednet.open(v)
  8. mod = peripheral.wrap(v)
  9. d = true
  10. end
  11. end
  12. if not d or not mod then
  13. error("A modem is required!")
  14. end
  15.  
  16. -- Some functions
  17.  
  18. local dns_channel = 331
  19. local args = {...}
  20. local targ = tonumber(args[1]) or 0
  21. local rids = {}
  22.  
  23. local function inTable_V(tab, _v)
  24. for k, v in pairs(tab) do
  25. if v == _v then
  26. return true
  27. end
  28. end
  29. return false
  30. end
  31.  
  32. local function inTable_K(tab, _k)
  33. for k, v in pairs(tab) do
  34. if k == _k then
  35. return true
  36. end
  37. end
  38. return false
  39. end
  40.  
  41. local function download_file(url, target)
  42. local ht = http.get(url)
  43. local data = ht.readAll()
  44. ht.close()
  45. local file = fs.open(target, "w")
  46. file.write(data)
  47. file.close()
  48. end
  49.  
  50. local function read_file(target)
  51. local file = fs.open(target, "r")
  52. local data = file.readAll()
  53. file.close()
  54. return data
  55. end
  56.  
  57. -- API Loading
  58. local function getAPI(name, url)
  59. local apiloc = "/" .. name
  60. if (not fs.exists(apiloc)) then
  61. local newapiloc = shell and shell.resolveProgram(name) or nil
  62. if type(newapiloc) ~= "string" then
  63. download_file(url, "/" .. name)
  64. else
  65. apiloc = newapiloc
  66. end
  67. end
  68. os.loadAPI(apiloc)
  69. if not _G[name] then
  70. error(name .. " failed to load!", 0)
  71. return
  72. end
  73. end
  74.  
  75. getAPI("ringnet", "https://pastebin.com/raw/bnDHMbNx")
  76. ringnet.openChannel(32031)
  77.  
  78. parallel.waitForAll(
  79. function()
  80. ringnet.connectionHandler(true)
  81. end,
  82. function()
  83. while true do
  84. local ev, from, data, dist = os.pullEvent("secure_receive")
  85. if (ev ~= nil) then
  86. if (type(data) ~= "table") then
  87. data = textutils.unserialize(tostring(data))
  88. end
  89. if not data then
  90. return
  91. end
  92. if data.type == "AMETHYSTDATA" then
  93. print(data.stuff)
  94. end
  95. end
  96. sleep(0)
  97. end
  98. end,
  99. function()
  100. ringnet.openTunnel(targ, 32031)
  101. local e, id = os.pullEventRaw("tunnel_finish")
  102. sleep(3)
  103. ringnet.sendData(
  104. id,
  105. textutils.serialize(
  106. {
  107. type = "AMETHYSTGET",
  108. path = "index"
  109. }
  110. )
  111. )
  112. end
  113. )
Advertisement
Add Comment
Please, Sign In to add comment