-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgateway_server.rb
56 lines (48 loc) · 1.19 KB
/
gateway_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'timeout'
require 'peer'
require 'gateway_common'
require 'gateway_client'
require 'thread'
require 'client_request'
module Gateway
class Server
include Gateway::Common
attr_reader(:port, :peer_socket, :server)
def initialize(port)
@port = port
@requests = {}
@mutex = Mutex.new
end
def start_stunt
port_client = PortClient.new("blastmefy.net:2000")
@peer_socket = Peer.new(port_client).start("testy", 2004)
end
def start
$stderr.puts("starting nat traversal")
start_stunt
@server = TCPServer.new(port)
$stderr.puts("gateway server started on port #{port}")
Thread.new do
handle_peer
end
loop do
begin
timeout(KEEPALIVE_TIMEOUT) do
$stderr.puts("waiting for connections")
client_socket = server.accept
Thread.new do
handle_client(ClientRequest.new(new_request_id, client_socket))
end
end
rescue Timeout::Error
$stderr.puts("sending keepalive")
keepalive
retry
end
end
end
end
end
if $0 == __FILE__
Gateway::Server.new(8080).start
end