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)
-
- (Object) context
Returns the value of attribute context.
-
- (Object) decode
Returns the value of attribute decode.
-
- (Object) encode
Returns the value of attribute encode.
-
- (Object) socket
Returns the value of attribute socket.
Instance Method Summary (collapse)
-
- (Boolean) connect(transport: :tcp, address: '127.0.0.1', port: 5555)
(also: #bind)
Connects the socket to the given address.
-
- (Socket) initialize(mode, type, **options)
constructor
Creates a 0MQ socket.
-
- (void) listen {|message| ... }
Like receive, but doesn't stop at one message.
-
- (Object) receive(**options) {|message| ... }
Receive a message from the socket.
-
- (Fixnum) send(message = '', **options)
Sends a message to the socket.
Constructor Details
- (Socket) initialize(mode, type, **options)
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
- (Object) context
Returns the value of attribute context
7 8 9 |
# File 'lib/ezmq/socket.rb', line 7 def context @context end |
- (Object) decode
Returns the value of attribute decode
7 8 9 |
# File 'lib/ezmq/socket.rb', line 7 def decode @decode end |
- (Object) encode
Returns the value of attribute encode
7 8 9 |
# File 'lib/ezmq/socket.rb', line 7 def encode @encode end |
- (Object) socket
Returns the value of attribute socket
7 8 9 |
# File 'lib/ezmq/socket.rb', line 7 def socket @socket end |
Instance Method Details
- (Boolean) connect(transport: :tcp, address: '127.0.0.1', port: 5555) 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 |
- (void) listen {|message| ... }
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 |
- (Object) receive(**options) {|message| ... }
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 |
- (Fixnum) send(message = '', **options)
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 |