Advertisement
Guest User

Untitled

a guest
Nov 17th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.71 KB | None | 0 0
  1. NETWORK_SIDE = "right"
  2. NETWORK_BROADCAST_CHANNEL = 1
  3. NETWORK_LISTEN_CHANNEL = os.getComputerID() % 0x8000
  4.  
  5. MESSAGE_PING = "miner ping"
  6. MESSAGE_INITIALIZE = "miner initialize"
  7. MESSAGE_STATUS = "miner status"
  8. MESSAGE_STATUS_IDLE = "miner status idle"
  9. MESSAGE_DISCONNECT = "miner disconnect"
  10.  
  11.  
  12. function Broadcast()
  13.   while true do
  14.     print("Ping...");
  15.     wireless.transmit(NETWORK_BROADCAST_CHANNEL, NETWORK_LISTEN_CHANNEL, MESSAGE_PING);
  16.  
  17.     for i = 1, #connections do
  18.       print("Asking status on " .. connections[i].channel);
  19.       wireless.transmit(connections[i].channel, NETWORK_LISTEN_CHANNEL, MESSAGE_STATUS);
  20.     end
  21.  
  22.     os.sleep(3);
  23.   end
  24. end
  25.  
  26. function UpdateConnections()
  27.   while true do
  28.     local event, side, in_channel, out_channel, message, client_distance = os.pullEvent("modem_message");
  29.     if (message == MESSAGE_INITIALIZE) then
  30.       connections[1 + #connections] = {channel = out_channel, distance = client_distance}
  31.       print("a miner has connected to " .. out_channel);
  32.     end
  33.  
  34.     local connection
  35.     for i = 1, #connections do
  36.       if (connections[i].channel == out_channel) then
  37.         connection = connections[i]
  38.         break
  39.       end
  40.     end
  41.  
  42.     if (connection == nil) then
  43.       continue
  44.     end
  45.  
  46.     if (message == MESSAGE_STATUS_IDLE) then
  47.       print("Miner " .. out_channel .. " is idle");
  48.     end
  49.   end
  50. end
  51.  
  52.  
  53. function EscPress()
  54.   while true do
  55.     local e, key = os.pullEvent("key")
  56.     if key ~= 1 then
  57.      return true
  58.     end
  59.   end
  60. end
  61.  
  62.  
  63. wireless = peripheral.wrap(NETWORK_SIDE)
  64. wireless.open(NETWORK_LISTEN_CHANNEL);
  65.  
  66. connections = {}
  67.  
  68. parallel.waitForAny(Broadcast,
  69.                     UpdateConnections,
  70.                     EscPress)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement