forked from threedaymonk/battleship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.rb
34 lines (29 loc) · 822 Bytes
/
util.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
require "socket"
module Battleship
module Util
PLAYER_METHODS = [:name, :new_game, :take_turn]
def find_player_classes
Module.constants.
select { |sym| sym.to_s =~ /Player$/ }.
map { |sym| Module.const_get(sym) }.
select { |klass|
(klass.instance_methods & PLAYER_METHODS) == PLAYER_METHODS
}
end
def wait_for_socket(host, port, timeout=1)
socket = Socket.new(:INET, :STREAM)
address = Socket.pack_sockaddr_in(port, host)
optval = [timeout, 0].pack("l_2")
socket.setsockopt :SOCKET, :RCVTIMEO, optval
socket.setsockopt :SOCKET, :SNDTIMEO, optval
loop do
begin
socket.connect(address)
return
rescue SystemCallError => e
end
end
end
extend self
end
end