Class: EZMQ::Socket
- Inherits:
-
Object
- Object
- EZMQ::Socket
- Defined in:
- lib/ezmq/socket.rb
Overview
Wrapper class to simplify 0MQ sockets.
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#decode ⇒ Object
Returns the value of attribute decode.
-
#encode ⇒ Object
Returns the value of attribute encode.
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
-
#connect(transport: :tcp, address: '127.0.0.1', port: 5555) ⇒ Boolean
(also: #bind)
Connects the socket to the given address.
-
#initialize(mode, type, **options) ⇒ Socket
constructor
Creates a 0MQ socket.
-
#listen {|message| ... } ⇒ void
Like receive, but doesn’t stop at one message.
-
#receive(**options) {|message| ... } ⇒ Object
Receive a message from the socket.
-
#send(message, **options) ⇒ Fixnum
Sends a message to the socket.
Constructor Details
#initialize(mode, type, **options) ⇒ Socket
port is ignored unless transport is one of :tcp, :pgm or :epgm
Creates a 0MQ socket.
26 27 28 29 30 31 32 33 34 |
# File 'lib/ezmq/socket.rb', line 26 def initialize(mode, type, **) fail ArgumentError unless %i(bind connect).include? mode @context = [:context] || EZMQ::Context.new @socket = @context.socket type @encode = [:encode] || -> m { m } @decode = [:decode] || -> m { m } endpoint = .select { |k, _| %i(transport address port).include? k } method(mode).call endpoint end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
7 8 9 |
# File 'lib/ezmq/socket.rb', line 7 def context @context end |
#decode ⇒ Object
Returns the value of attribute decode.
7 8 9 |
# File 'lib/ezmq/socket.rb', line 7 def decode @decode end |
#encode ⇒ Object
Returns the value of attribute encode.
7 8 9 |
# File 'lib/ezmq/socket.rb', line 7 def encode @encode end |
#socket ⇒ Object
Returns the value of attribute socket.
7 8 9 |
# File 'lib/ezmq/socket.rb', line 7 def socket @socket end |
Instance Method Details
#connect(transport: :tcp, address: '127.0.0.1', port: 5555) ⇒ Boolean Also known as: bind
This method can be called as #bind, in which case it binds to the specified address instead.
Binding to ‘localhost’ is not consistent on all platforms. Prefer ‘127.0.0.1’ instead.
port is ignored unless transport is one of :tcp, :pgm or :epgm
Connects the socket to the given address.
88 89 90 91 92 |
# File 'lib/ezmq/socket.rb', line 88 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 |
#listen {|message| ... } ⇒ void
This method returns an undefined value.
Like receive, but doesn’t stop at one message.
103 104 105 106 107 |
# File 'lib/ezmq/socket.rb', line 103 def listen(&block) loop do block.call receive end end |
#receive(**options) {|message| ... } ⇒ Object
This method blocks until a message arrives.
Receive a message from the socket.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ezmq/socket.rb', line 63 def receive(**) = '' @socket.recv_string decoded = ([:decode] || @decode).call if block_given? yield decoded else decoded end end |
#send(message, **options) ⇒ Fixnum
If message is not a String, #encode must convert it to one.
Sends a message to the socket.
46 47 48 49 |
# File 'lib/ezmq/socket.rb', line 46 def send(, **) encoded = ([:encode] || @encode).call @socket.send_string encoded end |