Class ZMQ::Socket
In: lib/ffi-rzmq/socket.rb
Parent: Object

Methods

Attributes

name  [R] 
socket  [R] 

Public Class methods

Allocates a socket of type type for sending and receiving data.

type can be one of ZMQ::REQ, ZMQ::REP, ZMQ::PUB, ZMQ::SUB, ZMQ::PAIR, ZMQ::PULL, ZMQ::PUSH, ZMQ::XREQ, ZMQ::REP, ZMQ::DEALER or ZMQ::ROUTER.

By default, this class uses ZMQ::Message for manual memory management. For automatic garbage collection of received messages, it is possible to override the :receiver_class to use ZMQ::ManagedMessage.

 sock = Socket.create(Context.create, ZMQ::REQ, :receiver_class => ZMQ::ManagedMessage)

Advanced users may want to replace the receiver class with their own custom class. The custom class must conform to the same public API as ZMQ::Message.

Creation of a new Socket object can return nil when socket creation fails.

 if (socket = Socket.new(context.pointer, ZMQ::REQ))
   ...
 else
   STDERR.puts "Socket creation failed"
 end

To avoid rescuing exceptions, use the factory method create for all socket creation.

Allocates a socket of type type for sending and receiving data.

type can be one of ZMQ::REQ, ZMQ::REP, ZMQ::PUB, ZMQ::SUB, ZMQ::PAIR, ZMQ::PULL, ZMQ::PUSH, ZMQ::XREQ, ZMQ::REP, ZMQ::DEALER or ZMQ::ROUTER.

By default, this class uses ZMQ::Message for manual memory management. For automatic garbage collection of received messages, it is possible to override the :receiver_class to use ZMQ::ManagedMessage.

 sock = Socket.new(Context.new, ZMQ::REQ, :receiver_class => ZMQ::ManagedMessage)

Advanced users may want to replace the receiver class with their own custom class. The custom class must conform to the same public API as ZMQ::Message.

Creation of a new Socket object can raise an exception. This occurs when the context_ptr is null or when the allocation of the 0mq socket within the context fails.

 begin
   socket = Socket.new(context.pointer, ZMQ::REQ)
 rescue ContextError => e
   # error handling
 end

Public Instance methods

Binds the socket to an address.

 socket.bind("tcp://127.0.0.1:5555")

Closes the socket. Any unprocessed messages in queue are sent or dropped depending upon the value of the socket option ZMQ::LINGER.

Returns 0 upon success or when the socket has already been closed. Returns -1 when the operation fails. Check ZMQ::Util.errno for the error code.

 rc = socket.close
 puts("Given socket was invalid!") unless 0 == rc

Connects the socket to an address.

 rc = socket.connect("tcp://127.0.0.1:5555")

Disconnect the socket from the given endpoint.

Get the options set on this socket.

name determines the socket option to request array should be an empty array; a result of the proper type (numeric, string, boolean) will be inserted into the first position.

Valid option_name values:

 ZMQ::RCVMORE - true or false
 ZMQ::HWM - integer
 ZMQ::SWAP - integer
 ZMQ::AFFINITY - bitmap in an integer
 ZMQ::IDENTITY - string
 ZMQ::RATE - integer
 ZMQ::RECOVERY_IVL - integer
 ZMQ::SNDBUF - integer
 ZMQ::RCVBUF - integer
 ZMQ::FD     - fd in an integer
 ZMQ::EVENTS - bitmap integer
 ZMQ::LINGER - integer measured in milliseconds
 ZMQ::RECONNECT_IVL - integer measured in milliseconds
 ZMQ::BACKLOG - integer
 ZMQ::RECOVER_IVL_MSEC - integer measured in milliseconds
 ZMQ::IPV4ONLY - integer

Returns 0 when the operation completed successfully. Returns -1 when this operation failed.

With a -1 return code, the user must check ZMQ::Util.errno to determine the cause.

 # retrieve high water mark
 array = []
 rc = socket.getsockopt(ZMQ::HWM, array)
 hwm = array.first if ZMQ::Util.resultcode_ok?(rc)

Convenience method for getting the value of the socket IDENTITY.

Convenience method for setting the value of the socket IDENTITY.

Convenience method for checking on additional message parts.

Equivalent to calling Socket#getsockopt with ZMQ::RCVMORE.

Warning: if the call to getsockopt fails, this method will return false and swallow the error.

 message_parts = []
 message = Message.new
 rc = socket.recvmsg(message)
 if ZMQ::Util.resultcode_ok?(rc)
   message_parts << message
   while more_parts?
     message = Message.new
     rc = socket.recvmsg(message)
     message_parts.push(message) if resulcode_ok?(rc)
   end
 end

Should only be used for XREQ, XREP, DEALER and ROUTER type sockets. Takes a list for receiving the message body parts and a routing_envelope for receiving the message parts comprising the 0mq routing information.

Helper method to make a new Message instance and convert its payload to a string.

flags may be ZMQ::DONTWAIT.

Returns 0 when the message was successfully dequeued. Returns -1 under two conditions.

  1. The message could not be dequeued
  2. When flags is set with ZMQ::DONTWAIT and the socket returned EAGAIN.

With a -1 return code, the user must check ZMQ::Util.errno to determine the cause.

The application code is responsible for handling the message object lifecycle when recv returns an error code.

Receive a multipart message as a list of strings.

flag may be ZMQ::DONTWAIT. Any other flag will be removed.

Dequeues a message from the underlying queue. By default, this is a blocking operation.

flags may take two values:

 0 (default) - blocking operation
 ZMQ::DONTWAIT - non-blocking operation

Returns 0 when the message was successfully dequeued. Returns -1 under two conditions.

  1. The message could not be dequeued
  2. When flags is set with ZMQ::DONTWAIT and the socket returned EAGAIN.

With a -1 return code, the user must check ZMQ::Util.errno to determine the cause.

The application code is responsible for handling the message object lifecycle when recv returns an error code.

Receive a multipart message as an array of objects (by default these are instances of Message).

flag may be ZMQ::DONTWAIT. Any other flag will be removed.

Sends a message. This will automatically close the message for both successful and failed sends.

Returns 0 when the message was successfully enqueued. Returns -1 under two conditions.

  1. The message could not be enqueued
  2. When flags is set with ZMQ::DONTWAIT and the socket returned EAGAIN.

With a -1 return code, the user must check ZMQ::Util.errno to determine the cause.

Helper method to make a new Message instance out of the string passed in for transmission.

flags may be ZMQ::DONTWAIT and ZMQ::SNDMORE.

Returns 0 when the message was successfully enqueued. Returns -1 under two conditions.

  1. The message could not be enqueued
  2. When flags is set with ZMQ::DONTWAIT and the socket returned EAGAIN.

With a -1 return code, the user must check ZMQ::Util.errno to determine the cause.

Send a sequence of strings as a multipart message out of the parts passed in for transmission. Every element of parts should be a String.

flags may be ZMQ::DONTWAIT.

Returns 0 when the messages were successfully enqueued. Returns -1 under two conditions.

  1. A message could not be enqueued
  2. When flags is set with ZMQ::DONTWAIT and the socket returned EAGAIN.

With a -1 return code, the user must check ZMQ::Util.errno to determine the cause.

Queues the message for transmission. Message is assumed to conform to the same public API as Message.

flags may take two values:

  • 0 (default) - blocking operation
  • ZMQ::DONTWAIT - non-blocking operation
  • ZMQ::SNDMORE - this message is part of a multi-part message

Returns 0 when the message was successfully enqueued. Returns -1 under two conditions.

  1. The message could not be enqueued
  2. When flags is set with ZMQ::DONTWAIT and the socket returned EAGAIN.

With a -1 return code, the user must check ZMQ::Util.errno to determine the cause.

Send a sequence of messages as a multipart message out of the parts passed in for transmission. Every element of parts should be a Message (or subclass).

flags may be ZMQ::DONTWAIT.

Returns 0 when the messages were successfully enqueued. Returns -1 under two conditions.

  1. A message could not be enqueued
  2. When flags is set with ZMQ::DONTWAIT and the socket returned EAGAIN.

With a -1 return code, the user must check ZMQ::Util.errno to determine the cause.

Set the queue options on this socket.

Valid name values that take a numeric value are:

 ZMQ::HWM
 ZMQ::SWAP (version 2 only)
 ZMQ::AFFINITY
 ZMQ::RATE
 ZMQ::RECOVERY_IVL
 ZMQ::MCAST_LOOP (version 2 only)
 ZMQ::LINGER
 ZMQ::RECONNECT_IVL
 ZMQ::BACKLOG
 ZMQ::RECOVER_IVL_MSEC (version 2 only)
 ZMQ::RECONNECT_IVL_MAX (version 3 only)
 ZMQ::MAXMSGSIZE (version 3 only)
 ZMQ::SNDHWM (version 3 only)
 ZMQ::RCVHWM (version 3 only)
 ZMQ::MULTICAST_HOPS (version 3 only)
 ZMQ::RCVTIMEO (version 3 only)
 ZMQ::SNDTIMEO (version 3 only)

Valid name values that take a string value are:

 ZMQ::IDENTITY (version 2/3 only)
 ZMQ::SUBSCRIBE
 ZMQ::UNSUBSCRIBE

Returns 0 when the operation completed successfully. Returns -1 when this operation failed.

With a -1 return code, the user must check ZMQ::Util.errno to determine the cause.

 rc = socket.setsockopt(ZMQ::LINGER, 1_000)
 ZMQ::Util.resultcode_ok?(rc) ? puts("succeeded") : puts("failed")

Unbind the socket from the given endpoint.

[Validate]