Advertisement
Guest User

Untitled

a guest
May 28th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.04 KB | None | 0 0
  1. def check_again_soon(response, id)
  2.   EM.add_timer(0.5) do
  3.     if $peers.length < 2
  4.       $log.debug "Still not enough peers, rescheduling check again..."
  5.       check_again_soon(response, id)
  6.     else
  7.       $log.debug "Deleting self from collection..."
  8.       $peers.delete(id)
  9.       respond_with_random_peer(response, id)
  10.     end
  11.   end
  12. end
  13.  
  14. def respond_with_random_peer(response, id)
  15.   $log.debug "Responding with peer..."
  16.   peer = $peers.keys.first
  17.   $peers[peer] = id
  18.   response << peer
  19.   response.done
  20. end
  21.  
  22. app = proc do |env|
  23.   id = Rack::Request.new(env).POST['id']
  24.   #return [ 404, {}, [] ] if id.nil?
  25.   id = "1a2a3a4a" if id.nil?
  26.  
  27.   response = Thin::AsyncResponse.new(env)
  28.   response.status = 200
  29.   unless $peers.empty?
  30.     $log.debug "Peers collection not empty"
  31.     respond_with_random_peer(response, id)
  32.   else
  33.     $log.debug "Peers collection is empty, adding self to collection and scheduling another check soon..."
  34.     $peers[id] = nil
  35.  
  36.     check_again_soon(response, id)
  37.   end
  38.   response.finish
  39. end
  40.  
  41. run app
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement