Bmorr

network_util.lua

Apr 30th, 2023 (edited)
1,066
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. --[[
  2.     pastebin get PFC9cjHM network_util
  3.     lua
  4.     net = require("network_util.lua")
  5.     net.example()
  6. ]]
  7. peripheral.find("modem", rednet.open)
  8.  
  9. protocol = "strippy"
  10.  
  11. function find_host(my_job, position)
  12.     print("Looking for "..protocol.." Hosts!")
  13.     local computers = {rednet.lookup(protocol)}
  14.     print("\tFound "..tostring(#computers).." potential hosts!")
  15.  
  16.     local my_data = {
  17.         job=my_job,
  18.         pos=position
  19.     }
  20.  
  21.     for _, computer in pairs(computers) do
  22.         print("Pinging "..computer)
  23.         rednet.send(computer, textutils.serialize(my_data), protocol)
  24.        
  25.         local id, response = rednet.receive(protocol, 10)
  26.         if id == computer and response == "true" then
  27.             print("Found Host!")
  28.             return computer
  29.         else
  30.             print("Denied: ")
  31.             print(response)
  32.         end
  33.     end
  34.     print("Could not find Host!")
  35.     return nil
  36. end
  37.  
  38. function start_host(my_job, position)
  39.     print("Hosting "..protocol.."!")
  40.     rednet.host(protocol, tostring(os.computerID()))
  41.  
  42.     count = 1
  43.     partners = {}
  44.     partners[my_job] = os.computerID()
  45.  
  46.     repeat
  47.         local id, message = rednet.receive(protocol)
  48.         local partner = textutils.unserialize(message)
  49.  
  50.         print("Just received request from:", id, "\nWho is a:", partner.job, "\nWho is at:", partner.pos)
  51.  
  52.         local dist = partner.pos.x - position.x + partner.pos.y - position.y + partner.pos.z - position.z
  53.  
  54.         print(dist)
  55.  
  56.         if dist < 10 then
  57.             print("Found Partner!")
  58.             partners[partner.job] = id
  59.             rednet.send(id, "true", protocol)
  60.             count = count + 1
  61.         else
  62.             rednet.send(id, "false", protocol)
  63.         end
  64.  
  65.     until count == 3
  66.     rednet.unhost(protocol)
  67.  
  68.     print("Found All Partners!")
  69.     for job, partner in pairs(partners) do
  70.         rednet.send(partner, textutils.serialize(partners), protocol)
  71.     end
  72.  
  73.     return partners
  74. end
  75.  
  76. function wait_for_partners(partner_id)
  77.     print("Waiting for Host!")
  78.     while true do
  79.         local id, message = rednet.receive(protocol)
  80.         if id == partner_id then
  81.             return textutils.unserialize(message)  -- This should return the list of partners
  82.         end
  83.     end
  84. end
  85.  
  86. function example(job)
  87.     job = job or "chunker"
  88.     local position = vector.new(gps.locate())
  89.  
  90.     local host = find_host(job, position)
  91.     if host then
  92.         return wait_for_partners(host)
  93.     end
  94.  
  95.     return start_host(job, position)
  96. end
  97.  
  98. return {
  99.     find_host = find_host,
  100.     start_host = start_host,
  101.     wait_for_partners = wait_for_partners,
  102.     example = example
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment