Advertisement
Guest User

Untitled

a guest
Nov 17th, 2013
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.31 KB | None | 0 0
  1. NETWORK_SIDE = "right"
  2. MINER_SIDE = "back"
  3. NETWORK_BROADCAST_CHANNEL = 1
  4. NETWORK_LISTEN_CHANNEL = os.getComputerID() % 0xFFFF
  5.  
  6. MESSAGE_PING = "miner ping"
  7. MESSAGE_INITIALIZE = "miner initialize"
  8. MESSAGE_STATUS = "miner status"
  9. MESSAGE_STATUS_IDLE = "miner status idle"
  10. MESSAGE_DISCONNECT = "miner disconnect"
  11.  
  12. function WaitForConnection()
  13.   while true do
  14.     local event, side, in_channel, out_channel, message, distance = os.pullEvent("modem_message");
  15.     if (message == MESSAGE_PING) then
  16.       return out_channel
  17.     end
  18.   end
  19. end
  20.  
  21. miner = peripheral.wrap(MINER_SIDE)
  22. wireless = peripheral.wrap(NETWORK_SIDE)
  23. wireless.open(NETWORK_BROADCAST_CHANNEL);
  24. wireless.open(NETWORK_LISTEN_CHANNEL);
  25.  
  26. --while true do
  27.   local server_channel = WaitForConnection();
  28.   print("Connected to Server @ " .. server_channel);
  29.  
  30.   wireless.transmit(server_channel, NETWORK_LISTEN_CHANNEL, MESSAGE_INITIALIZE);
  31.  
  32.   while true do
  33.     local event, side, in_channel, out_channel, message, distance = os.pullEvent("modem_message");
  34.     if message == MESSAGE_DISCONNECT then
  35.       break
  36.     end
  37.  
  38.     if message == MESSAGE_STATUS then
  39.       print("Sending status");
  40.       wireless.transmit(server_channel, NETWORK_LISTEN_CHANNEL, MESSAGE_STATUS_IDLE);
  41.     end
  42.   end
  43.  
  44.   print("Disconnected from Server " .. server_channel);
  45. --end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement