-
Notifications
You must be signed in to change notification settings - Fork 0
/
reeler.rb
96 lines (79 loc) · 2.14 KB
/
reeler.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
require 'reel'
require 'lib/testier'
require 'websocket_parser'
require 'json'
require 'symboltable'
class Socketeer
include Celluloid
def initialize(socket, neighbors)
@remote_ip = socket.remote_ip
@testier = Testier.new
@neighbors = neighbors
@socket = socket
@neighbors[@remote_ip] = @socket
@socket.on_close do |a,m|
@socket.close
end
@socket.on_message do |m|
@socket.cancel_timer!
process(m)
@socket.read_every(1)
end
@socket.on_ping do |payload|
@socket.write ::WebSocket::Message.pong(payload).to_data
end
@socket.read_every(1)
end
def process(message)
command = SymbolTable.new(JSON.load(message))
case command.work.to_sym
when :linear
send_off @testier.linear(command.pages)
when :parallel
send_off @testier.parallel(command.pages)
when :pool
send_off @testier.pooly(5, command.pages)
end
end
def send_off(results)
highest=results.sort_by {|letter, ct| ct}.last
text = "#{@remote_ip} had a high of #{highest}<br/>"
@neighbors.keys.each {|socket_key| @neighbors[socket_key].write text}
end
end
class Reeler < Reel::Server
MY_IP="172.16.0.235"
MY_PORT=3100
def initialize
puts "Starting server at #{MY_IP}:#{MY_PORT}"
@sockets = {}
super(MY_IP, MY_PORT, &method(:on_connection))
end
def on_connection(connection)
while request = connection.request
if !request.websocket?
handle_homepage connection, request
return
else
connection.detach
handle_socket request.websocket
return
end
end
end
def handle_homepage(connection, request)
if request.url == "/"
connection.respond :ok, ::IO.read("public/index.html").split("|host|").join("#{MY_IP}:#{MY_PORT}")
else
puts "BAD PAGE"
connection.respond :not_found, "No Mr Peter... No... try /"
end
end
def handle_socket(socket)
i = Socketeer.new(socket, @sockets)
@sockets.delete(socket.remote_ip) if @sockets.keys.include?(socket.remote_ip)
@sockets[socket.remote_ip] = socket
end
end
Reeler.supervise_as :reelerer
sleep