class NIO::Monitor

Monitors watch IO objects for specific events

Attributes

readiness[RW]

Public Class Methods

new(p1, p2, p3) click to toggle source
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;
    rb_io_t *fptr;

    interests_id = SYM2ID(interests);

    Data_Get_Struct(self, struct NIO_Monitor, 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)));
    }

    GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
    ev_io_init(&monitor->ev_io, NIO_Selector_monitor_callback, FPTR_TO_FD(fptr), 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);

    Data_Get_Struct(selector_obj, struct NIO_Selector, selector);

    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;
}
new(io, interests, selector) click to toggle source

:nodoc

# File lib/nio/monitor.rb, line 10
def initialize(io, interests, selector)
  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

  @io        = io
  @interests = interests
  @selector  = selector
  @closed    = false
end

Public Instance Methods

add_interest(p1) click to toggle source
static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest) {
    struct NIO_Monitor *monitor;
    Data_Get_Struct(self, struct NIO_Monitor, monitor);

    interest = monitor->interests | NIO_Monitor_symbol2interest(interest);
    NIO_Monitor_update_interests(self, interest);

    return rb_ivar_get(self, rb_intern("interests"));
}
close(p1 = v1) click to toggle source
static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self)
{
    VALUE deregister, selector;
    struct NIO_Monitor *monitor;
    Data_Get_Struct(self, struct NIO_Monitor, 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;
}
closed?() click to toggle source
static VALUE NIO_Monitor_is_closed(VALUE self)
{
    struct NIO_Monitor *monitor;
    Data_Get_Struct(self, struct NIO_Monitor, monitor);

    return monitor->selector == 0 ? Qtrue : Qfalse;
}
interests() click to toggle source
static VALUE NIO_Monitor_interests(VALUE self)
{
    return rb_ivar_get(self, rb_intern("interests"));
}
interests=(p1) click to toggle source
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() click to toggle source
static VALUE NIO_Monitor_io(VALUE self)
{
    return rb_ivar_get(self, rb_intern("io"));
}
readable?() click to toggle source
static VALUE NIO_Monitor_is_readable(VALUE self)
{
    struct NIO_Monitor *monitor;
    Data_Get_Struct(self, struct NIO_Monitor, monitor);

    if(monitor->revents & EV_READ) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}
readiness() click to toggle source
static VALUE NIO_Monitor_readiness(VALUE self)
{
    struct NIO_Monitor *monitor;
    Data_Get_Struct(self, struct NIO_Monitor, 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;
    }
}
remove_interest(p1) click to toggle source
static VALUE NIO_Monitor_remove_interest(VALUE self, VALUE interest) {
    struct NIO_Monitor *monitor;
    Data_Get_Struct(self, struct NIO_Monitor, monitor);

    interest = monitor->interests & ~NIO_Monitor_symbol2interest(interest);
    NIO_Monitor_update_interests(self, interest);

    return rb_ivar_get(self, rb_intern("interests"));
}
selector() click to toggle source
static VALUE NIO_Monitor_selector(VALUE self)
{
    return rb_ivar_get(self, rb_intern("selector"));
}
value() click to toggle source
static VALUE NIO_Monitor_value(VALUE self)
{
    return rb_ivar_get(self, rb_intern("value"));
}
value=(p1) click to toggle source
static VALUE NIO_Monitor_set_value(VALUE self, VALUE obj)
{
    return rb_ivar_set(self, rb_intern("value"), obj);
}
writable?() click to toggle source
static VALUE NIO_Monitor_is_writable(VALUE self)
{
    struct NIO_Monitor *monitor;
    Data_Get_Struct(self, struct NIO_Monitor, monitor);

    if(monitor->revents & EV_WRITE) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}
Also aliased as: writeable?
writeable?() click to toggle source
static VALUE NIO_Monitor_is_writable(VALUE self)
{
    struct NIO_Monitor *monitor;
    Data_Get_Struct(self, struct NIO_Monitor, monitor);

    if(monitor->revents & EV_WRITE) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}