Class: NIO::Monitor
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | ext/nio4r/selector.c, ext/nio4r/monitor.c, lib/nio/monitor.rb |
Overview
Monitors watch IO objects for specific events
Class Method Summary
- .new(io, interests, selector_obj) constructor
Instance Attribute Summary
-
#closed? ⇒ Boolean
readonly
Is this monitor closed?
- #interests rw
-
#interests=(interests) ⇒ Symbol
rw
Replace the existing interest set with a new one.
- #io readonly
-
#readable? ⇒ Boolean
readonly
Is the IO object readable?
- #readiness rw
- #selector readonly
- #value rw
-
#writable? ⇒ Boolean
(also: #writeable?)
readonly
Is the IO object writable?
-
#writeable?
readonly
Alias for #writable?.
Instance Method Summary
-
#add_interest(interest) ⇒ self
Add new interests to the existing interest set.
-
#close(deregister = true)
Deactivate this monitor.
-
#remove_interest(interest) ⇒ self
Remove interests from the existing interest set.
- #initialize(io, interests, selector) ⇒ Monitor constructor Internal use only
Constructor Details
.new(io, interests, selector_obj)
[ GitHub ]# File 'ext/nio4r/monitor.c', line 105
static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE selector_obj)
{
struct NIO_Monitor *monitor;
struct NIO_Selector *selector;
ID interests_id;
interests_id = SYM2ID(interests);
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
if (interests_id == rb_intern("r")) {
monitor->interests = EV_READ;
} else if (interests_id == rb_intern("w")) {
monitor->interests = EV_WRITE;
} else if (interests_id == rb_intern("rw")) {
monitor->interests = EV_READ | EV_WRITE;
} else {
rb_raise(rb_eArgError, "invalid event type %s (must be :r, :w, or :rw)", RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0)));
}
int descriptor = rb_io_descriptor(rb_convert_type(io, T_FILE, "IO", "to_io"));
ev_io_init(&monitor->ev_io, NIO_Selector_monitor_callback, descriptor, monitor->interests);
rb_ivar_set(self, rb_intern("io"), io);
rb_ivar_set(self, rb_intern("interests"), interests);
rb_ivar_set(self, rb_intern("selector"), selector_obj);
selector = NIO_Selector_unwrap(selector_obj);
RB_OBJ_WRITE(self, &monitor->self, self);
monitor->ev_io.data = (void *)monitor;
/* We can safely hang onto this as we also hang onto a reference to the
object where it originally came from */
monitor->selector = selector;
if (monitor->interests) {
ev_io_start(selector->ev_loop, &monitor->ev_io);
}
return Qnil;
}
#initialize(io, interests, selector) ⇒ Monitor
This method is for internal use only.
# File 'lib/nio/monitor.rb', line 17
def initialize(io, interests, selector) unless defined?(::OpenSSL) && io.is_a?(::OpenSSL::SSL::SSLSocket) unless io.is_a?(IO) if IO.respond_to? :try_convert io = IO.try_convert(io) elsif io.respond_to? :to_io io = io.to_io end raise TypeError, "can't convert #{io.class} into IO" unless io.is_a? IO end end @io = io @interests = interests @selector = selector @closed = false end
Instance Attribute Details
#closed? ⇒ Boolean (readonly)
Is this monitor closed?
# File 'ext/nio4r/monitor.c', line 175
static VALUE NIO_Monitor_is_closed(VALUE self)
{
struct NIO_Monitor *monitor;
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
return monitor->selector == 0 ? Qtrue : Qfalse;
}
#interests (rw)
[ GitHub ]# File 'ext/nio4r/monitor.c', line 188
static VALUE NIO_Monitor_interests(VALUE self)
{
return rb_ivar_get(self, rb_intern("interests"));
}
#interests=(interests) ⇒ Symbol (rw)
Replace the existing interest set with a new one
# File 'ext/nio4r/monitor.c', line 193
static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests)
{
if (NIL_P(interests)) {
NIO_Monitor_update_interests(self, 0);
} else {
NIO_Monitor_update_interests(self, NIO_Monitor_symbol2interest(interests));
}
return rb_ivar_get(self, rb_intern("interests"));
}
#io (readonly)
[ GitHub ]# File 'ext/nio4r/monitor.c', line 183
static VALUE NIO_Monitor_io(VALUE self)
{
return rb_ivar_get(self, rb_intern("io"));
}
#readable? ⇒ Boolean (readonly)
Is the IO object readable?
# File 'ext/nio4r/monitor.c', line 257
static VALUE NIO_Monitor_is_readable(VALUE self)
{
struct NIO_Monitor *monitor;
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
if (monitor->revents & EV_READ) {
return Qtrue;
} else {
return Qfalse;
}
}
#readiness (rw)
[ GitHub ]# File 'ext/nio4r/monitor.c', line 241
static VALUE NIO_Monitor_readiness(VALUE self)
{
struct NIO_Monitor *monitor;
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
if ((monitor->revents & (EV_READ | EV_WRITE)) == (EV_READ | EV_WRITE)) {
return ID2SYM(rb_intern("rw"));
} else if (monitor->revents & EV_READ) {
return ID2SYM(rb_intern("r"));
} else if (monitor->revents & EV_WRITE) {
return ID2SYM(rb_intern("w"));
} else {
return Qnil;
}
}
#selector (readonly)
[ GitHub ]# File 'ext/nio4r/monitor.c', line 226
static VALUE NIO_Monitor_selector(VALUE self)
{
return rb_ivar_get(self, rb_intern("selector"));
}
#value (rw)
[ GitHub ]# File 'ext/nio4r/monitor.c', line 231
static VALUE NIO_Monitor_value(VALUE self)
{
return rb_ivar_get(self, rb_intern("value"));
}
#writable? ⇒ Boolean (readonly)
Also known as: #writeable?
Is the IO object writable?
# File 'ext/nio4r/monitor.c', line 269
static VALUE NIO_Monitor_is_writable(VALUE self)
{
struct NIO_Monitor *monitor;
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
if (monitor->revents & EV_WRITE) {
return Qtrue;
} else {
return Qfalse;
}
}
#writeable? (readonly)
Alias for #writable?.
Instance Method Details
#add_interest(interest) ⇒ self
Add new interests to the existing interest set
# File 'ext/nio4r/monitor.c', line 204
static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest)
{
struct NIO_Monitor *monitor;
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
interest = monitor->interests | NIO_Monitor_symbol2interest(interest);
NIO_Monitor_update_interests(self, (int)interest);
return rb_ivar_get(self, rb_intern("interests"));
}
#close(deregister = true)
Deactivate this monitor
# File 'ext/nio4r/monitor.c', line 148
static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self)
{
VALUE deregister, selector;
struct NIO_Monitor *monitor;
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
rb_scan_args(argc, argv, "01", &deregister);
selector = rb_ivar_get(self, rb_intern("selector"));
if (selector != Qnil) {
/* if ev_loop is 0, it means that the loop has been stopped already (see NIO_Selector_shutdown) */
if (monitor->interests && monitor->selector->ev_loop) {
ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io);
}
monitor->selector = 0;
rb_ivar_set(self, rb_intern("selector"), Qnil);
/* Default value is true */
if (deregister == Qtrue || deregister == Qnil) {
rb_funcall(selector, rb_intern("deregister"), 1, rb_ivar_get(self, rb_intern("io")));
}
}
return Qnil;
}
#remove_interest(interest) ⇒ self
Remove interests from the existing interest set
# File 'ext/nio4r/monitor.c', line 215
static VALUE NIO_Monitor_remove_interest(VALUE self, VALUE interest)
{
struct NIO_Monitor *monitor;
TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
interest = monitor->interests & ~NIO_Monitor_symbol2interest(interest);
NIO_Monitor_update_interests(self, (int)interest);
return rb_ivar_get(self, rb_intern("interests"));
}