Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("client")
- local component = require("component")
- local computer = require("computer")
- local event = require("event")
- local m = component.modem
- local spaceing = "}|}!"
- local self_address
- local connectedToRouter = false
- local router_address = ""
- m.open(5)
- computer.beep()
- function split(s, delimiter)
- result = {};
- for match in (s..delimiter):gmatch("(.-)"..delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- function sendMessage(to, message)
- m.send(router_address, 5, "send" .. spaceing .. to .. spaceing .. message)
- end
- function sendBroadcast(message)
- m.send(router_address, 5, "broadcast" .. spaceing .. message)
- end
- function listenToRouter()
- local _, _, from, port, _, message = event.pull("modem_message")
- if port == 5 then
- command = split(message, spaceing)
- if command[1] == "transmit" then
- if command[2] == "send" then
- print("got message " .. command[4] .. " from " .. command[3])
- end
- if command[2] == "broadcast" then
- print("got broadcast_message " .. command[4] .. " from " .. command[3])
- end
- end
- if command[1] == "connect" then
- if command[2] == "2" then
- connectToRouter()
- end
- end
- end
- end
- function connectToRouter()
- print("searching for router...")
- m.broadcast(5, "connect" .. spaceing)
- local _, _, from, port, _, message = event.pull("modem_message")
- print(port .. message)
- if port == 5 then
- print(from .. " " .. message)
- command = split(message, spaceing)
- if command[1] == "connect" then
- if command[2] == "1" then
- router_address = from
- self_address = command[3]
- connectedToRouter = true
- print("connected with " .. from .. " !")
- end
- end
- end
- end
- connectToRouter()
- connectToRouter()
- sendMessage(self_address, "Hallo")
- listenToRouter()
- sendBroadcast("Hallo2")
- listenToRouter()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement