Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("router")
- local computer = require("computer")
- local component = require("component")
- local event = require("event")
- local m = component.modem
- local spaceing = "}|}!"
- local clients = {}
- 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 getClientById(id)
- local index = 0
- local result = nil
- while index < #clients do
- index = index + 1
- if split(clients[index], spaceing)[2] == id then
- result = clients[index]
- end
- end
- return result
- end
- function getClientByAddress(id)
- local index = 0
- local result = nil
- while index < #clients do
- index = index + 1
- if split(clients[index], spaceing)[1] == id then
- result = clients[index]
- end
- end
- return result
- end
- while true do
- local _, _, from, port, _, message = event.pull("modem_message")
- print(from .. " " .. message)
- command = split(message, spaceing)
- if command[1] == "broadcast" then
- local client_string = getClientByAddress(from)
- if client_string ~= nil then
- local index = 0
- local result = nil
- while index < #clients do
- index = index + 1
- m.send(split(clients[index], spaceing)[1], 5, "transmit" .. spaceing .. "broadcast" .. spaceing .. split(client_string, spaceing)[2] .. spaceing .. command[2])
- end
- print("broadcasted to " .. #clients .. " clients!")
- else
- m.send(from, 5, "connect" .. spaceing .. "2")
- end
- end
- if command[1] == "connect" then
- print("connecting: " .. from .. " ...")
- local client_string = getClientByAddress(from)
- if client_string ~= nil then
- if split(client_string, spaceing)[1] == from then
- client_string = getClientByAddress(from)
- print("client_string: " .. client_string)
- m.send(split(client_string, spaceing)[1], 5, "connect" .. spaceing .. "1" .. spaceing .. split(client_string, spaceing)[2])
- print("client connected " .. from .. " as " .. split(client_string, spaceing)[2] .. " !")
- end
- end
- if client_string == nil then
- client_string = from .. spaceing .. #clients
- table.insert(clients, client_string)
- print("client_string: " .. client_string)
- m.send(from, 5, "connect" .. spaceing .. "1" .. spaceing .. #clients)
- print("client connected " .. from .. " as " .. #clients .. " !")
- end
- end
- if command[1] == "send" then
- local client_string = getClientByAddress(from)
- if client_string ~= nil then
- local client2_string = getClientById(command[2])
- if client2_string ~= nil then
- print("sending for: " .. split(client_string, spaceing)[2])
- m.send(split(getClientById(command[2]), spaceing)[1], 5, "transmit" .. spaceing .. "send" .. spaceing .. split(client_string, spaceing)[2] .. spaceing .. command[3])
- print("sent from " .. split(client_string, spaceing)[2] .. " to " .. split(getClientById(command[2]), spaceing)[1] .. " " .. command[3])
- end
- else
- m.send(from, 5, "connect" .. spaceing .. "2")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement