Class: EZMQ::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/ezmq.rb

Overview

Wrapper class to simplify 0MQ sockets.

Direct Known Subclasses

Client, Publisher, Server, Subscriber

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Socket) initialize(mode, type, **options)

Creates a 0MQ socket.

Parameters:

  • mode (Symbol)

    the mode of the socket. `:bind` or `:connect`

  • type (Object)

    the type of socket to use.

  • options (Hash)

    optional parameters.

Options Hash (**options):

  • context (ZMQ::Context)

    a context to use for this socket (one will be created if not provided).

  • encode (lambda)

    how to encode messages. Default unaltered.

  • decode (lambda)

    how to decode messages. Default unaltered.

  • address (String)

    specifies protocol, address and port (if needed). Default is 'tcp://127.0.0.1:5555'



24
25
26
27
28
29
30
31
# File 'lib/ezmq.rb', line 24

def initialize(mode, type, **options)
  fail ArgumentError unless [:bind, :connect].include? mode
  @context = options[:context] || ZMQ::Context.new
  @socket = @context.socket type
  @encode = options[:encode] || -> m { m }
  @decode = options[:decode] || -> m { m }
  method(mode).call address: options[:address] || 'tcp://127.0.0.1:5555'
end

Instance Attribute Details

- (Object) context

Returns the value of attribute context



7
8
9
# File 'lib/ezmq.rb', line 7

def context
  @context
end

- (Object) decode

Returns the value of attribute decode



7
8
9
# File 'lib/ezmq.rb', line 7

def decode
  @decode
end

- (Object) encode

Returns the value of attribute encode



7
8
9
# File 'lib/ezmq.rb', line 7

def encode
  @encode
end

- (Object) socket

Returns the value of attribute socket



7
8
9
# File 'lib/ezmq.rb', line 7

def socket
  @socket
end

Instance Method Details

- (Boolean) bind(address: 'tcp://127.0.0.1:5555')

Note:

'localhost' does not always work as expected. Prefer '127.0.0.1'

Binds the socket to the given address.

Parameters:

  • address (String)

    specifies protocol, address and port (if needed). Default is 'tcp://127.0.0.1:5555'

Returns:

  • (Boolean)

    was binding successful?



69
70
71
# File 'lib/ezmq.rb', line 69

def bind(address: 'tcp://127.0.0.1:5555')
  @socket.bind(address) == 0 ? true : false
end

- (Boolean) connect(address: 'tcp://127.0.0.1:5555')

Connects the socket to the given address.

Parameters:

  • address (String)

    specifies protocol, address and port (if needed). Default is 'tcp://127.0.0.1:5555'

Returns:

  • (Boolean)

    was connection successful?



80
81
82
# File 'lib/ezmq.rb', line 80

def connect(address: 'tcp://127.0.0.1:5555')
  @socket.connect(address) == 0 ? true : false
end

- (void) receive(decode: @decode)

Note:

This method blocks until a message arrives.

This method returns an undefined value.

Receive a message from the socket.

Parameters:

  • decode (lambda)

    how to decode the message.



41
42
43
44
45
# File 'lib/ezmq.rb', line 41

def receive(decode: @decode)
  message = ''
  @socket.recv_string message
  decode.call message
end

- (Fixnum) send(message = '', encode: @encode)

Note:

If `message` is not a String, `encode` must convert it to one.

Sends a message on the socket.

Parameters:

  • message (String) (defaults to: '')

    the message to send.

  • encode (lambda)

    how to encode the message.

Returns:

  • (Fixnum)

    the size of the message.



56
57
58
# File 'lib/ezmq.rb', line 56

def send(message = '', encode: @encode)
  @socket.send_string encode.call message
end