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) bind(protocol: 'tcp', address: '127.0.0.1', port: 5555)
Binds the socket to the given address.
-
- (Boolean) connect(protocol: 'tcp', address: '127.0.0.1', port: 5555)
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 [:bind, :connect].include? mode @context = [:context] || ZMQ::Context.new @socket = @context.socket type @encode = [:encode] || -> m { m } @decode = [:decode] || -> m { m } endpoint = .select { |k, _| [: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) bind(protocol: 'tcp', address: '127.0.0.1', port: 5555)
An address of 'localhost' is not reliable on all platforms. Prefer '127.0.0.1' instead.
port is ignored unless protocol is either 'tcp' or 'udp'.
Binds the socket to the given address.
83 84 85 86 87 |
# File 'lib/ezmq/socket.rb', line 83 def bind(protocol: 'tcp', address: '127.0.0.1', port: 5555) endpoint = "#{ protocol }://#{ address }" endpoint = "#{ endpoint }:#{ port }" if %w(tcp udp).include? protocol @socket.bind(endpoint) == 0 end |
- (Boolean) connect(protocol: 'tcp', address: '127.0.0.1', port: 5555)
port is ignored unless protocol is either 'tcp' or 'udp'.
Connects the socket to the given address.
98 99 100 101 102 |
# File 'lib/ezmq/socket.rb', line 98 def connect(protocol: 'tcp', address: '127.0.0.1', port: 5555) endpoint = "#{ protocol }://#{ address }" endpoint = "#{ endpoint }:#{ port }" if %w(tcp udp).include? protocol @socket.connect(endpoint) == 0 end |
- (void) listen {|message| ... }
This method returns an undefined value.
By default, waits for a message and prints it to STDOUT.
111 112 113 114 115 116 117 118 119 |
# File 'lib/ezmq/socket.rb', line 111 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 |