Advertisement
Guest User

Untitled

a guest
Nov 17th, 2013
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.70 KB | None | 0 0
  1. NETWORK_SIDE = "right"
  2. NETWORK_BROADCAST_CHANNEL = 1
  3. NETWORK_LISTEN_CHANNEL = os.getComputerID() % 0xFFFF
  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.       if (message == MESSAGE_STATUS_IDLE) then
  44.         print("Miner " .. out_channel .. " is idle");
  45.       end
  46.     end
  47.   end
  48. end
  49.  
  50.  
  51. function EscPress()
  52.   while true do
  53.     local e, key = os.pullEvent("key")
  54.     if key ~= 1 then
  55.      return true
  56.     end
  57.   end
  58. end
  59.  
  60.  
  61. wireless = peripheral.wrap(NETWORK_SIDE)
  62. wireless.open(NETWORK_LISTEN_CHANNEL);
  63.  
  64. connections = {}
  65.  
  66. parallel.waitForAny(Broadcast,
  67.                     UpdateConnections,
  68.                     EscPress)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement