Class: EZMQ::Socket

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

Overview

Wrapper class to simplify 0MQ sockets.

Direct Known Subclasses

Client, Pair, Publisher, Puller, Pusher, Server, Subscriber

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

Note:

port is ignored unless transport is one of :tcp, :pgm or :epgm

Creates a 0MQ socket.

Parameters:

  • mode (:bind, :connect)

    the mode of the socket.

  • 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.

  • decode (lambda)

    how to decode messages.

  • transport (Symbol) — default: :tcp

    transport for transport.

  • address (String) — default: '127.0.0.1'

    address for endpoint.

  • port (Fixnum) — default: 5555

    port for endpoint.



26
27
28
29
30
31
32
33
34
# File 'lib/ezmq/socket.rb', line 26

def initialize(mode, type, **options)
  fail ArgumentError unless %i(bind connect).include? mode
  @context = options[:context] || EZMQ::Context.new
  @socket = @context.socket type
  @encode = options[:encode] || -> m { m }
  @decode = options[:decode] || -> m { m }
  endpoint = options.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

Note:

This method can be called as #bind, in which case it binds to the specified address instead.

Note:

Binding to 'localhost' is not consistent on all platforms. Prefer '127.0.0.1' instead.

Note:

port is ignored unless transport is one of :tcp, :pgm or :epgm

Connects the socket to the given address.

Parameters:

  • transport (Symbol)

    (:tcp) transport for transport.

  • address (String)

    ('127.0.0.1') address for endpoint.

  • port (Fixnum)

    (5555) port for endpoint.

Returns:

  • (Boolean)

    was connection successful?



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.

Yields:

  • message passes the message received to the block.

Yield Parameters:

  • message (String)

    the message received.



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| ... }

Note:

This method blocks until a message arrives.

Receive a message from the socket.

Parameters:

  • options (Hash)

    optional parameters.

Options Hash (**options):

  • decode (lambda)

    how to decode the message.

Yields:

  • message passes the message received to the block.

Yield Parameters:

  • message (Object)

    the message received (decoded).

Returns:

  • (Object)

    the message received (decoded).



63
64
65
66
67
68
69
70
71
72
# File 'lib/ezmq/socket.rb', line 63

def receive(**options)
  message = ''
  @socket.recv_string message
  decoded = (options[:decode] || @decode).call message
  if block_given?
    yield decoded
  else
    decoded
  end
end

- (Fixnum) send(message = '', **options)

Note:

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

Sends a message to the socket.

Parameters:

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

    the message to send.

  • options (Hash)

    optional parameters.

Options Hash (**options):

  • encode (lambda)

    how to encode the message.

Returns:

  • (Fixnum)

    the size of the message.



46
47
48
49
# File 'lib/ezmq/socket.rb', line 46

def send(message = '', **options)
  encoded = (options[:encode] || @encode).call message
  @socket.send_string encoded
end