lib/ezmq/socket.rb in ezmq-0.3.7 vs lib/ezmq/socket.rb in ezmq-0.4.0
- old
+ new
@@ -14,24 +14,24 @@
#
# @option options [ZMQ::Context] context a context to use for this socket
# (one will be created if not provided).
# @option options [lambda] encode how to encode messages.
# @option options [lambda] decode how to decode messages.
- # @option options [String] protocol ('tcp') protocol for transport.
+ # @option options [Symbol] transport (:tcp) transport for transport.
# @option options [String] address ('127.0.0.1') address for endpoint.
# @option options [Fixnum] port (5555) port for endpoint.
- # @note port is ignored unless protocol is one of 'tcp', 'pgm' or 'epgm'.
+ # @note port is ignored unless transport is one of :tcp, :pgm or :epgm
#
# @return [Socket] a new instance of Socket.
#
def initialize(mode, type, **options)
fail ArgumentError unless %i(bind connect).include? mode
@context = options[:context] || EZMQ::Context.new
@socket = @context.socket type
@encode = options[:encode] || -> m { m }
@decode = options[:decode] || -> m { m }
- endpoint = options.select { |k, _| %i(protocol address port).include? k }
+ endpoint = options.select { |k, _| %i(transport address port).include? k }
method(mode).call endpoint
end
# Sends a message to the socket.
#
@@ -74,40 +74,36 @@
# Connects the socket to the given address.
#
# @note This method can be called as #bind, in which case it binds to the
# specified address instead.
#
- # @param [String] protocol ('tcp') protocol for transport.
+ # @param [Symbol] transport (:tcp) transport for transport.
# @param [String] address ('127.0.0.1') address for endpoint.
# @note Binding to 'localhost' is not consistent on all platforms.
# Prefer '127.0.0.1' instead.
# @param [Fixnum] port (5555) port for endpoint.
- # @note port is ignored unless protocol is one of 'tcp', 'pgm' or 'epgm'.
+ # @note port is ignored unless transport is one of :tcp, :pgm or :epgm
#
# @return [Boolean] was connection successful?
#
- def connect(protocol: 'tcp', address: '127.0.0.1', port: 5555)
- endpoint = "#{ protocol }://#{ address }"
- endpoint = "#{ endpoint }:#{ port }" if %w(tcp pgm epgm).include? protocol
+ def connect(transport: :tcp, address: '127.0.0.1', port: 5555)
+ endpoint = "#{ transport }://#{ address }"
+ endpoint << ":#{ port }" if %i(tcp pgm epgm).include? transport
@socket.method(__callee__).call(endpoint) == 0
end
alias_method :bind, :connect
- # By default, waits for a message and prints it to STDOUT.
+ # Like receive, but doesn't stop at one message.
#
# @yield message passes the message received to the block.
# @yieldparam [String] message the message received.
#
# @return [void]
#
- def listen
+ def listen(&block)
loop do
- if block_given?
- yield receive
- else
- puts receive
- end
+ block.call receive
end
end
end
end