-
Notifications
You must be signed in to change notification settings - Fork 6
/
master.rb
99 lines (86 loc) · 2.65 KB
/
master.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'socket'
require 'thread'
require 'webrick'
include WEBrick
http = HTTPServer.new(:Port => 1235)
udpsocket = UDPSocket.open
udpsocket.bind(nil, 1234)
servers = {}
servers_mutex = Mutex.new
class Server
attr_reader :clients, :players, :creatures
def initialize(clients, players, creatures, name)
@clients, @players, @creatures, @name =
clients, players, creatures, name
@created = Time.now
end
def expired?
@created + 240 < Time.now
end
def name
@name.tr('^a-zA-Z0-9 ', ' ')
end
end
listener = Thread.start do
while true do
begin
input, source = udpsocket.recvfrom(128)
raise 'invalid size' unless input[0] + 2 == input.size
raise 'invalid type' unless input[1] == 253
addr = "%s:%d" % [source[3], source[1]]
servers_mutex.synchronize do
servers[addr] = Server.new(input[2], input[3], input[4], input[5..-1])
end
rescue => e
p e
end
end
end
expire = Thread.start do
while true do
servers_mutex.synchronize do
servers.delete_if { |ip, server| server.expired? }
end
sleep 60
end
end
http.mount_proc("/") { |req, res|
res.body << <<-HTML
<html>
<script src='/servers.js'></script>
<body>
<div id="infon_score" style='border:1px solid black; width: 300px;'>
Loading Server List...
</div>
<script>
var servers = get_infon_servers();
var html = "<table width='100%'>";
html += "<td><i>IP</i></td><td><i>Name</i></td><td><i>Clients</i></td></tr>";
for (i = 0; i < servers.length; i++) {
html += "<tr><td>" + servers[i].ip + "</td><td>" +
servers[i].name + "</td><td>" +
servers[i].clients + "</td>";
}
html += "</table>";
document.getElementById("infon_score").innerHTML = html;
</script>
</body>
</html>
HTML
res['Content-Type'] = "text/html"
}
http.mount_proc("/servers.js") { |req, res|
res.body << "function get_infon_servers() { var servers = new Array();\n"
servers.sort_by { |ip, server| server.players }.each do |ip, server|
res.body << "var s = new Object(); servers[servers.length] = s;\n"
res.body << "s.ip = '%s'; s.name = '%s'\n" % [ip, server.name]
res.body << "s.clients = %d; s.players = %d; s.creatures = %d;\n" %
[server.clients, server.players, server.creatures]
end
res.body << "return servers; }\n"
res['Content-Type'] = "text/javascript"
}
trap("INT") { http.shutdown }
trap("TERM") { http.shutdown }
http.start
listener.kill