Class: ZMQ::Pollitem
- Inherits:
-
Object
- Object
- ZMQ::Pollitem
- Defined in:
- lib/zmq/pollitem.rb,
ext/rbczmq/pollitem.c
Class Method Summary collapse
-
.ZMQ::Pollitem.coerce(sock) ⇒ ZMQ::Pollitem
Attempts to coerce a ZMQ::Socket or IO to a ZMQ::Pollitem instance.
-
.ZMQ::Pollitem.new(io, ZMQ: POLLIN) ⇒ ZMQ::Pollitem
A generic poll item that supports Ruby I/O objects as well as native ZMQ sockets.
Instance Method Summary collapse
-
#events ⇒ Fixnum
Returns the I/O events the pollable entity is interested in.
-
#handler ⇒ Object?
Returns the callback handler currently associated with this poll item.
-
#handler=(MyFrameHandler) ⇒ nil
Associates a callback handler with this poll item.
-
#pollable ⇒ ZMQ::Socket
Returns the pollable entity for this poll item.
-
#recv ⇒ Object
API that allows poll items to recv data regardless of the underlying pollable item type (ZMQ::Socket or IO).
-
#send(*args) ⇒ Object
API that allows poll items to send data regardless of the underlying pollable item type (ZMQ::Socket or IO).
-
#verbose=(true) ⇒ nil
Logs pollitem activity to stdout - useful for debugging, but can be quite noisy with lots of activity.
Class Method Details
.ZMQ::Pollitem.coerce(sock) ⇒ ZMQ::Pollitem
114 115 116 117 118 119 120 121 122 |
# File 'ext/rbczmq/pollitem.c', line 114
VALUE rb_czmq_pollitem_s_coerce(ZMQ_UNUSED VALUE obj, VALUE pollable)
{
VALUE pollitem;
VALUE args[1];
if (rb_obj_is_kind_of(pollable, rb_cZmqPollitem)) return pollable;
args[0] = pollable;
pollitem = Qnil;
return rb_czmq_pollitem_s_new(1, args, pollitem);
}
|
.ZMQ::Pollitem.new(io, ZMQ: POLLIN) ⇒ ZMQ::Pollitem
A generic poll item that supports Ruby I/O objects as well as native ZMQ sockets. Poll items are primarily used for registering pollable entities with ZMQ::Poller and ZMQ::Loop instances. If no events given, we default to observing both readable and writable state.
Examples
Supported pollable types :
IO : any Ruby I/O object (must respond to #fileno as well) ZMQ::Socket : native ZMQ socket
Supported events :
ZMQ::POLLIN : register for readable events ZMQ::POLLOUT : register for writable events
ZMQ::Pollitem.new(io) => ZMQ::Pollitem
ZMQ::Pollitem.new(io, ZMQ:POLLIN) => ZMQ::Pollitem
ZMQ::Pollitem.new(socket, ZMQ:POLLOUT) => ZMQ::Pollitem
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'ext/rbczmq/pollitem.c', line 58
static VALUE rb_czmq_pollitem_s_new(int argc, VALUE *argv, VALUE obj)
{
zmq_sock_wrapper *sock = NULL;
VALUE pollable, events;
int evts;
zmq_pollitem_wrapper *pollitem = NULL;
rb_scan_args(argc, argv, "11", &pollable, &events);
if (NIL_P(events)) events = INT2NUM((ZMQ_POLLIN | ZMQ_POLLOUT));
Check_Type(events, T_FIXNUM);
evts = NUM2INT(events);
if (!(evts & ZMQ_POLLIN) && !(evts & ZMQ_POLLOUT))
rb_raise(rb_eZmqError, "invalid socket event: Only ZMQ::POLLIN and ZMQ::POLLOUT events are supported!");
/* XXX: Cleanup allocated struct on any failures below */
obj = Data_Make_Struct(rb_cZmqPollitem, zmq_pollitem_wrapper, rb_czmq_mark_pollitem, rb_czmq_free_pollitem_gc, pollitem);
pollitem->events = events;
pollitem->handler = Qnil;
pollitem->item = ALLOC(zmq_pollitem_t);
if (!pollitem->item) rb_memerror();
pollitem->item->events = evts;
if (rb_obj_is_kind_of(pollable, rb_cZmqSocket)) {
GetZmqSocket(pollable);
ZmqAssertSocketNotPending(sock, "socket in a pending state (not bound or connected) and thus cannot be registered as a poll item!");
ZmqSockGuardCrossThread(sock);
pollitem->socket = pollable;
pollitem->io = Qnil;
pollitem->item->fd = 0;
pollitem->item->socket = sock->socket;
/* Do not block on socket close */
zsockopt_set_linger(sock->socket, 1);
} else if (rb_obj_is_kind_of(pollable, rb_cIO)) {
pollitem->io = pollable;
pollitem->socket = Qnil;
pollitem->item->socket = NULL;
pollitem->item->fd = NUM2INT(rb_funcall(pollable, rb_intern("fileno"), 0));
/* XXX: handle coercion of other I/O like objects as well ? respond_to?(:fileno) ? */
} else {
rb_raise(rb_eTypeError, "wrong pollable type %s (%s). Only objects of type ZMQ::Socket and IO supported.", rb_obj_classname(pollable), RSTRING_PTR(rb_obj_as_string(pollable)));
}
rb_obj_call_init(obj, 0, NULL);
return obj;
}
|
Instance Method Details
#events ⇒ Fixnum
170 171 172 173 174 |
# File 'ext/rbczmq/pollitem.c', line 170
VALUE rb_czmq_pollitem_events(VALUE obj)
{
ZmqGetPollitem(obj);
return pollitem->events;
}
|
#handler ⇒ Object?
189 190 191 192 193 |
# File 'ext/rbczmq/pollitem.c', line 189
VALUE rb_czmq_pollitem_handler(VALUE obj)
{
ZmqGetPollitem(obj);
return pollitem->handler;
}
|
#handler=(MyFrameHandler) ⇒ nil
207 208 209 210 211 212 213 214 215 |
# File 'ext/rbczmq/pollitem.c', line 207
VALUE rb_czmq_pollitem_handler_equals(VALUE obj, VALUE handler)
{
ZmqGetPollitem(obj);
ZmqAssertHandler(obj, pollitem, handler, intern_error);
ZmqAssertHandler(obj, pollitem, handler, intern_readable);
ZmqAssertHandler(obj, pollitem, handler, intern_writable);
pollitem->handler = handler;
return Qnil;
}
|
#pollable ⇒ ZMQ::Socket
150 151 152 153 154 155 |
# File 'ext/rbczmq/pollitem.c', line 150
VALUE rb_czmq_pollitem_pollable(VALUE obj)
{
ZmqGetPollitem(obj);
if (NIL_P(pollitem->socket)) return pollitem->io;
return pollitem->socket;
}
|
#recv ⇒ Object
API that allows poll items to recv data regardless of the underlying pollable item type (ZMQ::Socket or IO).
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/zmq/pollitem.rb', line 19 def recv case pollable when BasicSocket # XXX assumed page size pollable.recv_nonblock(4096) when IO # XXX assumed page size pollable.read_nonblock(4096) when ZMQ::Socket pollable.recv_nonblock end end |
#send(*args) ⇒ Object
API that allows poll items to send data regardless of the underlying pollable item type (ZMQ::Socket or IO).
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/zmq/pollitem.rb', line 6 def send(*args) case pollable when BasicSocket pollable.send(args.shift, 0) when IO pollable.write_nonblock(*args) when ZMQ::Socket pollable.send(*args) end end |
#verbose=(true) ⇒ nil
230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'ext/rbczmq/pollitem.c', line 230
VALUE rb_czmq_pollitem_set_verbose(VALUE obj, VALUE level)
{
Bool vlevel;
zmq_sock_wrapper *sock = NULL;
ZmqGetPollitem(obj);
vlevel = (level == Qtrue) ? TRUE : FALSE;
if (rb_obj_is_kind_of(pollitem->socket, rb_cZmqSocket)) {
GetZmqSocket(pollitem->socket);
sock->verbose = vlevel;
}
return Qnil;
}
|