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(protocol: '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| ... }
By default, waits for a message and prints it to STDOUT.
-
- (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 protocol is either 'tcp' or 'udp'.
Creates a 0MQ socket.
24 25 26 27 28 29 30 31 32 |
# File 'lib/ezmq/socket.rb', line 24 def initialize(mode, type, **) fail ArgumentError unless %i(bind connect).include? mode @context = [:context] || ZMQ::Context.new @socket = @context.socket type @encode = [:encode] || -> m { m } @decode = [:decode] || -> m { m } endpoint = .select { |k, _| %i(protocol address port).include? k } method(mode).call endpoint end |
Instance Attribute Details
- (Object) context
Returns the value of attribute context
5 6 7 |
# File 'lib/ezmq/socket.rb', line 5 def context @context end |
- (Object) decode
Returns the value of attribute decode
5 6 7 |
# File 'lib/ezmq/socket.rb', line 5 def decode @decode end |
- (Object) encode
Returns the value of attribute encode
5 6 7 |
# File 'lib/ezmq/socket.rb', line 5 def encode @encode end |
- (Object) socket
Returns the value of attribute socket
5 6 7 |
# File 'lib/ezmq/socket.rb', line 5 def socket @socket end |
Instance Method Details
- (Boolean) connect(protocol: '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 protocol is either 'tcp' or 'udp'.
Connects the socket to the given address.
86 87 88 89 90 |
# File 'lib/ezmq/socket.rb', line 86 def connect(protocol: 'tcp', address: '127.0.0.1', port: 5555) endpoint = "#{ protocol }://#{ address }" endpoint = "#{ endpoint }:#{ port }" if %w(tcp udp).include? protocol @socket.method(__callee__).call(endpoint) == 0 end |
- (void) listen {|message| ... }
This method returns an undefined value.
By default, waits for a message and prints it to STDOUT.
101 102 103 104 105 106 107 108 109 |
# File 'lib/ezmq/socket.rb', line 101 def listen loop do if block_given? yield receive else puts receive end end end |
- (Object) receive(**options) {|message| ... }
This method blocks until a message arrives.
Receive a message from the socket.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ezmq/socket.rb', line 61 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.
44 45 46 47 |
# File 'lib/ezmq/socket.rb', line 44 def send( = '', **) encoded = ([:encode] || @encode).call @socket.send_string encoded end |