Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- DNS System for computercraft
- --
- -- The DNS System always listens on channel 0 and answers on channel 1.
- --
- -- PUT:
- --
- -- A programm can register itself by sending a "put" message to the DNS System
- -- A "put" message has the following form:
- -- {
- -- type = "put",
- -- name = "PUT_NAME_HERE"
- -- }
- --
- -- This message will associate the channel the message was send on, with the
- -- given name.
- --
- -- If a name was already given, it will be overwritten.
- --
- -- GET:
- --
- -- A programm can query the DNS for the channel of a programm.
- -- A "get" message has the following form:
- -- {
- -- type = "get"
- -- name = "PUT_NAME_HERE"
- -- }
- --
- -- The System will send message of this form back:
- --
- -- {
- -- type = "dns_response",
- -- channel = NUM,
- -- name = "SAME_NAME"
- -- }
- --
- -- If no programm has previously associated the name with a channel, then
- -- channel will be nil.
- --
- -- Reponse messages will be received by all clients listening on port 1.
- -- A client should wait until a response message containing its own name
- -- arrives.
- --
- local side_modem = "left"
- local modem = nil
- local query_channel = 0
- local response_channel = 1
- local db = {}
- local next_channel = 2
- local function run()
- modem = peripheral.wrap(side_modem)
- modem.open(query_channel)
- while true do
- a, b, c, msg, d = os.pullEvent("modem_message")
- print(a)
- print(b)
- print(c)
- print(msg)
- print(d)
- if msg.type == "get" then
- modem.transmit(response_channel, modem_channel, {type = "get", channel = db[msg.name]})
- elseif msg.type == "put" then
- db[msg.name] = next_channel
- next_channel = next_channel + 1
- modem.transmit(response_channel, modem_channel, {type = "put", channel = db[msg.name], name = msg.name})
- end
- end
- end
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement