Advertisement
theoriginalbit

Three-Way Handshake Computer Sync

May 4th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.37 KB | None | 0 0
  1. local modemSide = 'left'
  2. local modem = peripheral.wrap(modemSide)
  3.  
  4. function sync_master(channel, alarmTime)
  5.   local timeout = os.startTimer(0.1)
  6.   print("Doing three-way handshake")
  7.   while true do
  8.     modem.transmit(channel, channel, 'SYN')
  9.     local e, p1, _, _, p4, _ = os.pullEvent()
  10.     if e == 'modem_message' then
  11.       if p4 == 'SYN-ACK' then
  12.         modem.transmit(channel, channel, 'ACK')
  13.         modem.transmit(channel, channel, tostring(alarmTime))
  14.       elseif p4 == 'ACK' then
  15.         break
  16.       end
  17.     elseif e == 'timer' and p1 == timeout then
  18.       modem.transmit(channel, channel, 'SYN')
  19.       timeout = os.startTimer(0.1)
  20.       alarmTime = os.time() + 0.025
  21.     end
  22.   end
  23.  
  24.   return alarmTime
  25. end
  26.  
  27. function sync_drone()
  28.   local gotAck = false
  29.   local syn = false
  30.   print("Listening for three-way handshake")
  31.   while true do
  32.     local _, _, p3, _, p4, _ = os.pullEvent('modem_message')
  33.     if p4 == 'SYN' then
  34.       modem.transmit(p3, p3, 'SYN-ACK')
  35.       syn = true
  36.     elseif p4 == 'ACK' and syn and not gotAck then
  37.       gotAck = true
  38.       syn = false
  39.     elseif tonumber(p4) and gotAck then
  40.       modem.transmit(p3, p3, 'ACK')
  41.       return tonumber(p4)
  42.     end
  43.   end
  44. end
  45.  
  46. function do_timer(dur, alarm)
  47.   os.setAlarm(alarm)
  48.   os.pullEvent('alarm')
  49.  
  50.   local curr = 0
  51.   local timer = os.startTimer(1)
  52.  
  53.   while true do
  54.     print('Launch in '..(dur - curr)..' seconds...')
  55.  
  56.     local _, p = os.pullEvent('timer')
  57.  
  58.     if p == timer then
  59.       timer = os.startTimer(1)
  60.       curr = curr + 1
  61.       if curr == dur then
  62.         return false
  63.       end
  64.     end
  65.   end
  66. end
  67.  
  68. -- call this with the communication channel and how long you want the countdown to be.
  69. function startSyncedTimer(channel, dur)
  70.   -- if the channel is not open, open it...
  71.   if not modem.isOpen(channel) then
  72.     modem.open(channel)
  73.   end
  74.  
  75.   -- figure out a time that both computers should start the timer, based off the Minecraft time
  76.   local alarmTime = os.time() + 0.1
  77.  
  78.   -- send this time out to the world
  79.   alarmTime = sync_master(channel, alarmTime)
  80.  
  81.   -- call the timer function.
  82.   -- Param 1: The duration of the timer, i.e. the countdown start time
  83.   -- Param 2: when to start the timer
  84.   local ok = do_timer(dur, alarmTime)
  85.  
  86.   -- if the timer finished with a boom, say boom (when you have the override make sure that you return true from the do_timer function so as to not make things go boom, then in this function make sure that the missile is launched)
  87.   if not ok then
  88.     print "BOOM!"
  89.   end
  90. end
  91.  
  92. -- call this with the communication channel and how long you want the countdown to be.
  93. -- the security in this function could be much better
  94. function listenForSync(channel, dur)
  95.   -- if the channel is not open, open it...
  96.   if not modem.isOpen(channel) then
  97.     modem.open(channel)
  98.   end
  99.  
  100.   -- create our alarm time variable and wait for its contents
  101.   local alarmTime = sync_drone()
  102.  
  103.   -- call the timer function.
  104.   -- Param 1: The duration of the timer, i.e. the countdown start time
  105.   -- Param 2: when to start the timer
  106.   local ok = do_timer(dur, alarmTime)
  107.  
  108.   -- if the timer finished with a boom, say boom (when you have the override make sure that you return true from the do_timer function so as to not make things go boom, then in this function make sure that the missile is launched)
  109.   if not ok then
  110.     print "LAUNCH!"
  111.   end
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement