Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. -- DNS System for computercraft
  2. --
  3. -- The DNS System always listens on channel 0 and answers on channel 1.
  4. --
  5. -- PUT:
  6. --
  7. -- A programm can register itself by sending a "put" message to the DNS System
  8. -- A "put" message has the following form:
  9. -- {
  10. -- type = "put",
  11. -- name = "PUT_NAME_HERE"
  12. -- }
  13. --
  14. -- This message will associate the channel the message was send on, with the
  15. -- given name.
  16. --
  17. -- If a name was already given, it will be overwritten.
  18. --
  19. -- GET:
  20. --
  21. -- A programm can query the DNS for the channel of a programm.
  22. -- A "get" message has the following form:
  23. -- {
  24. -- type = "get"
  25. -- name = "PUT_NAME_HERE"
  26. -- }
  27. --
  28. -- The System will send message of this form back:
  29. --
  30. -- {
  31. -- type = "dns_response",
  32. -- channel = NUM,
  33. -- name = "SAME_NAME"
  34. -- }
  35. --
  36. -- If no programm has previously associated the name with a channel, then
  37. -- channel will be nil.
  38. --
  39. -- Reponse messages will be received by all clients listening on port 1.
  40. -- A client should wait until a response message containing its own name
  41. -- arrives.
  42. --
  43. local side_modem = "left"
  44. local modem = nil
  45. local query_channel = 0
  46. local response_channel = 1
  47.  
  48. local db = {}
  49. local next_channel = 2
  50.  
  51. local function run()
  52. modem = peripheral.wrap(side_modem)
  53. modem.open(query_channel)
  54.  
  55. while true do
  56. a, b, c, msg, d = os.pullEvent("modem_message")
  57.  
  58. print(a)
  59. print(b)
  60. print(c)
  61. print(msg)
  62. print(d)
  63.  
  64. if msg.type == "get" then
  65. modem.transmit(response_channel, modem_channel, {type = "get", channel = db[msg.name]})
  66. elseif msg.type == "put" then
  67. db[msg.name] = next_channel
  68. next_channel = next_channel + 1
  69. modem.transmit(response_channel, modem_channel, {type = "put", channel = db[msg.name], name = msg.name})
  70. end
  71. end
  72. end
  73.  
  74. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement