Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NETWORK_SIDE = "right"
- NETWORK_BROADCAST_CHANNEL = 1
- NETWORK_LISTEN_CHANNEL = os.getComputerID() % 0xFFFF
- MESSAGE_PING = "miner ping"
- MESSAGE_INITIALIZE = "miner initialize"
- MESSAGE_STATUS = "miner status"
- MESSAGE_STATUS_IDLE = "miner status idle"
- MESSAGE_DISCONNECT = "miner disconnect"
- function Broadcast()
- while true do
- print("Ping...");
- wireless.transmit(NETWORK_BROADCAST_CHANNEL, NETWORK_LISTEN_CHANNEL, MESSAGE_PING);
- for i = 1, #connections do
- print("Asking status on " .. connections[i].channel);
- wireless.transmit(connections[i].channel, NETWORK_LISTEN_CHANNEL, MESSAGE_STATUS);
- end
- os.sleep(3);
- end
- end
- function UpdateConnections()
- while true do
- local event, side, in_channel, out_channel, message, client_distance = os.pullEvent("modem_message");
- if (message == MESSAGE_INITIALIZE) then
- connections[1 + #connections] = {channel = out_channel, distance = client_distance}
- print("a miner has connected to " .. out_channel);
- end
- local connection
- for i = 1, #connections do
- if (connections[i].channel == out_channel) then
- connection = connections[i]
- break
- end
- end
- if (connection ~= nil) then
- if (message == MESSAGE_STATUS_IDLE) then
- print("Miner " .. out_channel .. " is idle");
- end
- end
- end
- end
- function EscPress()
- while true do
- local e, key = os.pullEvent("key")
- if key ~= 1 then
- return true
- end
- end
- end
- wireless = peripheral.wrap(NETWORK_SIDE)
- wireless.open(NETWORK_LISTEN_CHANNEL);
- connections = {}
- parallel.waitForAny(Broadcast,
- UpdateConnections,
- EscPress)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement