Skip to content

Instantly share code, notes, and snippets.

@skinnyjames
Created February 15, 2022 16:13
Show Gist options
  • Save skinnyjames/a96a9ee5f983fedd2d2208c26eb136f2 to your computer and use it in GitHub Desktop.
Save skinnyjames/a96a9ee5f983fedd2d2208c26eb136f2 to your computer and use it in GitHub Desktop.
server
require "sinatra/base"
require "json"
module Stuff
class App < Sinatra::Base
s = Mutex.new
@@last_address = nil
@@first_address = nil
get '/clear' do
@@last_address = nil
@@first_address = nil
end
get '/connect' do
puts "hello"
if @@last_address
puts "found last address #{@@last_address}"
response.headers['Peer-Address'] = @@last_address
# get peer socket
puts request
socket = request.env["puma.socket"]
s.synchronize do
@@first_address = [socket.peeraddr[3], socket.peeraddr[1]].join(":")
end
response.headers['My-Address'] = @@first_address
halt 200
else
# get peer socket
socket = request.env["puma.socket"]
puts request.host_with_port
puts "creating last address #{@@last_address}"
@@last_address = [socket.peeraddr[3], socket.peeraddr[1]].join(":")
puts "waiting for peer"addresses[1]?.try { |add| add.
b = Thread.new do
start = Time.now
while (Time.now - start) < 60
a = s.synchronize do
@@first_address
end
puts "peer address : #{a}"
break if a
end
end
b.join
a = s.synchronize do
@@first_address
end
response.headers['My-Address'] = @@last_address
response.headers['Peer-Address'] = @@first_address
halt 200
@@last_address =nil
end
end
run! if app_file == $0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment