Advertisement
Bmorr

bip_host.lua

Dec 27th, 2020
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.38 KB | None | 0 0
  1. local protocol = "bip"
  2. local pastebin = "uMfjE44t"
  3.  
  4. -- Updating .bip.txt
  5. term.setTextColor(colors.green)
  6. print("Updating packages...\n")
  7. if fs.exists(".bip.txt") then
  8.     print("Removing the old file...")
  9.     fs.delete(".bip.txt")
  10. end
  11. shell.run("pastebin get "..pastebin.." .bip.txt")
  12. print("Finished updating!")
  13. sleep(1)
  14. term.clear()
  15. term.setCursorPos(1, 1)
  16.  
  17. -- Creating the table
  18. local pkg, count = {}, 0
  19. local db = io.open(".bip.txt", "r")
  20. for line in db:lines() do
  21.     local equals = line:find(" = ")
  22.     pkg[line:sub(1, equals - 1)] = line:sub(equals + 3)
  23.     count = count + 1
  24. end
  25. db:close()
  26.  
  27. -- Managing requests
  28. term.setTextColor(colors.white)
  29. peripheral.find("modem", rednet.open)
  30. print("Awaiting requests...")
  31. local last_id, count = nil, 0
  32. while true do
  33.     local id, msg, pro = rednet.receive(protocol, 5)
  34.     -- Printing request information
  35.     if id ~= last_id or msg == nil then
  36.         if last_id ~= nil then
  37.             print("Served", count, "requests from", last_id..".")
  38.         end
  39.         last_id = id
  40.         count = 1
  41.     else
  42.         count = count + 1
  43.     end
  44.     -- Handling the request
  45.     if msg ~= nil then
  46.         if pkg[msg] ~= nil then
  47.             rednet.send(id, pkg[msg], protocol)
  48.         else
  49.             rednet.send(id, "unknown package", protocol)
  50.         end
  51.     end
  52. end
  53.  
  54. peripheral.find("modem", rednet.close)
  55.  
  56.  
  57.  
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement