Sha256: 0d96fc54e9594a6275501bfe9c1cfb81d332e7995f8e7b7898b378208cbb87df
Contents?: true
Size: 1.63 KB
Versions: 6
Compression:
Stored size: 1.63 KB
Contents
require 'thread' require 'system_timer' class Client class << self attr_accessor :clients, :client_index def remove(client) puts "--- #{client.addr} has disconnected" clients.delete client client_index.delete client.addr end def add(client) clients << client client_index[client.addr] = client end end Client.clients = [] Client.client_index = {} attr_reader :name def initialize(demo_socket) @socket = demo_socket Client.add self @lock = Mutex.new @outbox = Queue.new @inbox = Queue.new @comm_thread = start_comm @pulse_thread = start_heartbeat end def lock(&block) @lock.synchronize &block end def send(data) @outbox << data end def recv SystemTimer.timeout 1 do @inbox.pop end rescue Timeout::Error puts "#{addr} timed out" false end def msg(data) lock { send data recv } end def addr @socket.addr end def hostname @socket.hostname end def start_comm Thread.new { loop do msg = @outbox.pop begin @socket.send msg @inbox << @socket.recv rescue DemoSocket::SocketError => e Client.remove(self) @pulse_thread.kill @comm_thread.kill end end } end def start_heartbeat client = self Thread.new { loop do response = msg('t' => 'HRT') if response @name = response['name'] else Client.remove(self) @comm_thread.kill @pulse_thread.kill end sleep 2 end } end end
Version data entries
6 entries across 6 versions & 1 rubygems
Version | Path |
---|---|
bum-0.0.17 | lib/client.rb |
bum-0.0.16 | lib/client.rb |
bum-0.0.15 | lib/client.rb |
bum-0.0.14 | lib/client.rb |
bum-0.0.13 | lib/client.rb |
bum-0.0.12 | lib/client.rb |