Module: ZMQ

Defined in:
lib/zmq.rb,
lib/zmq/version.rb,
ext/rbczmq/rbczmq_ext.c

Defined Under Namespace

Modules: DownstreamSocket, UpstreamSocket Classes: Context, DefaultHandler, Error, Frame, Handler, Loop, Message, Monitor, Poller, Pollitem, Socket, Timer

Constant Summary collapse

VERSION =
"1.3"
POLLIN =
INT2NUM(ZMQ_POLLIN)
POLLOUT =
INT2NUM(ZMQ_POLLOUT)
POLLERR =
INT2NUM(ZMQ_POLLERR)
PAIR =
INT2NUM(ZMQ_PAIR)
SUB =
INT2NUM(ZMQ_SUB)
PUB =
INT2NUM(ZMQ_PUB)
REQ =
INT2NUM(ZMQ_REQ)
REP =
INT2NUM(ZMQ_REP)
DEALER =
INT2NUM(ZMQ_DEALER)
ROUTER =
INT2NUM(ZMQ_ROUTER)
PUSH =
INT2NUM(ZMQ_PUSH)
PULL =
INT2NUM(ZMQ_PULL)
EFSM =
INT2NUM(EFSM)
ENOCOMPATPROTO =
INT2NUM(ENOCOMPATPROTO)
ETERM =
INT2NUM(ETERM)
EMTHREAD =
INT2NUM(EMTHREAD)
EVENT_CONNECTED =
INT2NUM(ZMQ_EVENT_CONNECTED)
EVENT_CONNECT_DELAYED =
INT2NUM(ZMQ_EVENT_CONNECT_DELAYED)
EVENT_CONNECT_RETRIED =
INT2NUM(ZMQ_EVENT_CONNECT_RETRIED)
EVENT_LISTENING =
INT2NUM(ZMQ_EVENT_LISTENING)
EVENT_BIND_FAILED =
INT2NUM(ZMQ_EVENT_BIND_FAILED)
EVENT_ACCEPTED =
INT2NUM(ZMQ_EVENT_ACCEPTED)
EVENT_ACCEPT_FAILED =
INT2NUM(ZMQ_EVENT_ACCEPT_FAILED)
EVENT_CLOSED =
INT2NUM(ZMQ_EVENT_CLOSED)
EVENT_CLOSE_FAILED =
INT2NUM(ZMQ_EVENT_CLOSE_FAILED)
EVENT_DISCONNECTED =
INT2NUM(ZMQ_EVENT_DISCONNECTED)
EVENT_ALL =
INT2NUM(ZMQ_EVENT_ALL)

Class Method Summary collapse

Class Method Details

.contextObject

Returns the ZMQ context for this process, if any


58
59
60
# File 'lib/zmq.rb', line 58

def self.context
  @__zmq_ctx_process[Process.pid]
end

.errnoFixnum

Returns the last known ZMQ errno (if any) as a Fixnum.

Examples

ZMQ.errno    =>  0

Returns:

  • (Fixnum)

134
135
136
137
# File 'ext/rbczmq/rbczmq_ext.c', line 134

static VALUE rb_czmq_m_errno(ZMQ_UNUSED VALUE obj)
{
    return INT2NUM(zmq_errno());
}

.errorZMQ::Error

Returns the last known ZMQ error (if any) as a ZMQ::Error instance.

Examples

ZMQ.error    =>  ZMQ::Error or nil

Returns:


115
116
117
118
119
120
121
# File 'ext/rbczmq/rbczmq_ext.c', line 115

static VALUE rb_czmq_m_error(ZMQ_UNUSED VALUE obj)
{
    int err;
    err = zmq_errno();
    if (err == 0) return Qnil;
    return rb_exc_new2(rb_eZmqError, zmq_strerror(zmq_errno()));
}

.Frame(data = nil) ⇒ Object

Sugaring for creating new ZMQ frames

ZMQ::Frame(“frame”) => ZMQ::Frame


34
35
36
# File 'lib/zmq.rb', line 34

def self.Frame(data = nil)
  ZMQ::Frame.new(data)
end

.interrupted!nil

Callback for Ruby signal handlers for terminating blocking functions and the reactor loop in libczmq.

Examples

ZMQ.interrupted!    =>  nil

Returns:

  • (nil)

150
151
152
153
154
# File 'ext/rbczmq/rbczmq_ext.c', line 150

static VALUE rb_czmq_m_interrupted_bang(ZMQ_UNUSED VALUE obj)
{
    zctx_interrupted = 1;
    return Qnil;
}

.interrupted?Boolean

Returns true if the process was interrupted by signal.

Examples

ZMQ.interrupted?    =>  boolean

Returns:

  • (Boolean)

47
48
49
50
# File 'ext/rbczmq/rbczmq_ext.c', line 47

static VALUE rb_czmq_m_interrupted_p(ZMQ_UNUSED VALUE obj)
{
    return (zctx_interrupted == true) ? Qtrue : Qfalse;
}

.log("msg") ⇒ nil

Logs a timestamped message to stdout.

Examples

ZMQ.log("msg")    =>  nil # 11-12-06 21:20:55 msg

Returns:

  • (nil)

97
98
99
100
101
102
# File 'ext/rbczmq/rbczmq_ext.c', line 97

static VALUE rb_czmq_m_log(ZMQ_UNUSED VALUE obj, VALUE msg)
{
    Check_Type(msg, T_STRING);
    zclock_log(StringValueCStr(msg));
    return Qnil;
}

.loopObject

Higher level loop API.

XXX: Handle cases where context is nil


66
67
68
# File 'lib/zmq.rb', line 66

def self.loop
  @loop ||= ZMQ::Loop.new(context)
end

.Message(*parts) ⇒ Object

Sugaring for creating new ZMQ messages

ZMQ::Message(“one”, “two”, “three”) => ZMQ::Message


42
43
44
45
46
# File 'lib/zmq.rb', line 42

def self.Message(*parts)
  m = ZMQ::Message.new
  parts.each{|p| m.addstr(p) }
  m
end

.nowFixnum

Returns a current timestamp as a Fixnum

Examples

ZMQ.now    =>  1323206405148

Returns:

  • (Fixnum)

81
82
83
84
# File 'ext/rbczmq/rbczmq_ext.c', line 81

static VALUE rb_czmq_m_now(ZMQ_UNUSED VALUE obj)
{
    return INT2NUM(zclock_time());
}

.Pollitem(pollable, events = nil) ⇒ Object

Sugaring for creating new poll items

ZMQ::Pollitem(STDIN, ZMQ::POLLIN) => ZMQ::Pollitem


52
53
54
# File 'lib/zmq.rb', line 52

def self.Pollitem(pollable, events = nil)
  ZMQ::Pollitem.new(pollable, events)
end

.resolverObject


80
81
82
83
84
85
# File 'lib/zmq.rb', line 80

def self.resolver
  @resolver ||= begin
    require 'resolv'
    Resolv::DNS.new
  end
end

.select(read = [], write = [], error = [], timeout = nil) ⇒ Object

API sugaring: IO.select compatible API, but for ZMQ sockets.


72
73
74
75
76
77
78
# File 'lib/zmq.rb', line 72

def self.select(read = [], write = [], error = [], timeout = nil)
  poller = ZMQ::Poller.new
  read.each{|s| poller.register_readable(s) } if read
  write.each{|s| poller.register_writable(s) } if write
  ready = poller.poll(timeout)
  [poller.readables, poller.writables, []] if ready
end

.stable_version?Boolean

Returns:

  • (Boolean)

20
21
22
23
24
# File 'lib/zmq.rb', line 20

def self.stable_version?
  version[0] == 3 &&
  version[1] == 2 &&
  version[2] == 2
end

.versionArray

Returns the libzmq version linked against.

Examples

ZMQ.version    =>  [2,1,11]

Returns:

  • (Array)

63
64
65
66
67
68
# File 'ext/rbczmq/rbczmq_ext.c', line 63

static VALUE rb_czmq_m_version(ZMQ_UNUSED VALUE obj)
{
    int major, minor, patch;
    zmq_version(&major, &minor, &patch);
    return rb_ary_new3(3, INT2NUM(major), INT2NUM(minor), INT2NUM(patch));
}

.version2?Boolean

Returns:

  • (Boolean)

26
27
28
# File 'lib/zmq.rb', line 26

def self.version2?
  !version3?
end

.version3?Boolean

Returns:

  • (Boolean)

16
17
18
# File 'lib/zmq.rb', line 16

def self.version3?
  version[0] == 3
end