lib/zmachine/connection_manager.rb in zmachine-0.3.0 vs lib/zmachine/connection_manager.rb in zmachine-0.3.2
- old
+ new
@@ -1,22 +1,23 @@
java_import java.nio.channels.ClosedChannelException
require 'zmachine/tcp_channel'
require 'zmachine/zmq_channel'
+require 'set'
module ZMachine
class ConnectionManager
attr_reader :connections
def initialize(selector)
ZMachine.logger.debug("zmachine:connection_manager:#{__method__}") if ZMachine.debug
@selector = selector
- @connections = []
- @zmq_connections = []
- @new_connections = []
- @unbound_connections = []
+ @connections = Set.new
+ @zmq_connections = Set.new
+ @new_connections = Set.new
+ @unbound_connections = Set.new
end
def idle?
@new_connections.size == 0 and
@zmq_connections.none? {|c| c.channel.can_recv? } # see comment in #process
@@ -79,11 +80,11 @@
ZMachine.logger.debug("zmachine:connection_manager:#{__method__}", connection: connection, reason: reason.inspect) if ZMachine.debug
@unbound_connections << [connection, reason]
end
def add_new_connections
- @new_connections.compact.each do |connection|
+ @new_connections.each do |connection|
ZMachine.logger.debug("zmachine:connection_manager:#{__method__}", connection: connection) if ZMachine.debug
begin
connection.register(@selector)
@connections << connection
if connection.channel.is_a?(ZMQChannel)
@@ -95,10 +96,14 @@
end
end
@new_connections.clear
end
+ def is_connected?(connection)
+ @connections.include?(connection)
+ end
+
def cleanup
return if @unbound_connections.empty?
ZMachine.logger.debug("zmachine:connection_manager:#{__method__}") if ZMachine.debug
@unbound_connections.each do |connection|
reason = nil
@@ -109,10 +114,10 @@
if connection.method(:unbind).arity != 0
connection.unbind(reason)
else
connection.unbind
end
- connection.close
+ connection.channel.close!
rescue Exception => e
ZMachine.logger.exception(e, "failed to unbind connection") if ZMachine.debug
end
end
@unbound_connections.clear