class ZMQ::Context

ZeroMQ library context.

Public Class Methods

new(io_threads=1) click to toggle source

Initializes a new 0MQ context. The io_threads argument specifies the size of the 0MQ thread pool to handle I/O operations. If your application is using only the inproc transport for you may set this to zero; otherwise, set it to at least one.

static VALUE context_initialize (int argc_, VALUE* argv_, VALUE self_)
{
    VALUE io_threads;
    rb_scan_args (argc_, argv_, "01", &io_threads);

    assert (!DATA_PTR (self_));
    void *ctx = zmq_init (NIL_P (io_threads) ? 1 : NUM2INT (io_threads));
    if (!ctx) {
        rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
        return Qnil;
    }

    DATA_PTR (self_) = (void*) ctx;
    return self_;
}

Public Instance Methods

close() → nil click to toggle source

Terminates the 0MQ context.

Context termination is performed in the following steps:

  1. Any blocking operations currently in progress on sockets open within context shall return immediately with an error code of ETERM. With the exception of ZMQ::Socket#close, any further operations on sockets open within context shall fail with an error code of ETERM.

  2. After interrupting all blocking calls, zmq_term() shall block until the following conditions are satisfied:

    • All sockets open within context have been closed with ZMQ::Socket#close.

    • For each socket within context, all messages sent by the application with ZMQ::Socket#send have either been physically transferred to a network peer, or the socket’s linger period set with the ZMQ::LINGER socket option has expired.

For further details regarding socket linger behaviour refer to the ZMQ::LINGER option in ZMQ::Socket#setsockopt.

static VALUE context_close (VALUE self_)
{
    void * ctx = NULL;
    Data_Get_Struct (self_, void, ctx);
    
    if (ctx != NULL) {
        int rc = zmq_term (ctx);
        assert (rc == 0);

        DATA_PTR (self_) = NULL;
    }

    return Qnil;
}
socket(socket_type) click to toggle source

Creates a new 0MQ socket. The socket_type argument specifies the socket type, which determines the semantics of communication over the socket.

The newly created socket is initially unbound, and not associated with any endpoints. In order to establish a message flow a socket must first be connected to at least one endpoint with connect(), or at least one endpoint must be created for accepting incoming connections with bind().

For a description of the various socket types, see ZMQ::Socket.

static VALUE context_socket (VALUE self_, VALUE type_)
{
    void * c = NULL;
    Data_Get_Struct (self_, void, c);
    void * s = zmq_socket (c, NUM2INT (type_));
    if (!s) {
        rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
        return Qnil;
    }

    return Data_Wrap_Struct(socket_type, 0, socket_free, s);
}