Class: ZMQ::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/zmq/timer.rb,
ext/rbczmq/timer.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ZMQ::Timer.new(1, 2) { ... } ⇒ ZMQ::Timer

Initializes a new ZMQ::Timer instance.

Examples

ZMQ::Timer.new(1, 2){ :fired }    =>  ZMQ::Timer

Yields:

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'ext/rbczmq/timer.c', line 36

VALUE rb_czmq_timer_s_new(int argc, VALUE *argv, VALUE timer)
{
    VALUE delay, times, proc, callback;
    size_t timer_delay;
    zmq_timer_wrapper *tr = NULL;
    rb_scan_args(argc, argv, "21&", &delay, &times, &proc, &callback);
    if (NIL_P(proc) && NIL_P(callback)) rb_raise(rb_eArgError, "no callback given!");
    if (NIL_P(proc)) {
        rb_need_block();
    } else {
        callback = proc;
    }
    if (TYPE(delay) != T_FIXNUM && TYPE(delay) != T_FLOAT) rb_raise(rb_eTypeError, "wrong delay type %s (expected Fixnum or Float)", RSTRING_PTR(rb_obj_as_string(delay)));
    Check_Type(times, T_FIXNUM);
    timer_delay = (size_t)(((TYPE(delay) == T_FIXNUM) ? FIX2LONG(delay) : RFLOAT_VALUE(delay)) * 1000); 
    timer = Data_Make_Struct(rb_cZmqTimer, zmq_timer_wrapper, rb_czmq_mark_timer, rb_czmq_free_timer_gc, tr);
    tr->cancelled = false;
    tr->delay = timer_delay;
    tr->times = FIX2INT(times);
    tr->callback = callback;
    rb_obj_call_init(timer, 0, NULL);
    return timer;
}

Instance Method Details

#cancelnil

Fires a timer.

Examples

timer = ZMQ::Timer.new(1, 2){|arg| :fired }    =>  ZMQ::Timer
timer.cancel   => nil

Returns:

  • (nil)


91
92
93
94
95
96
# File 'ext/rbczmq/timer.c', line 91

static VALUE rb_czmq_timer_cancel(VALUE obj)
{
    ZmqGetTimer(obj);
    timer->cancelled = true;
    return Qnil; 
}

#fireObject Also known as: call

Fires a timer.

Examples

timer = ZMQ::Timer.new(1, 2){|arg| :fired }    =>  ZMQ::Timer
timer.fire(arg)

Returns:

  • (Object)


72
73
74
75
76
77
# File 'ext/rbczmq/timer.c', line 72

static VALUE rb_czmq_timer_fire(VALUE obj, VALUE args)
{
    ZmqGetTimer(obj);
    if (timer->cancelled == true) rb_raise(rb_eZmqError, "cannot fire timer, already cancelled!");
    return rb_apply(timer->callback, intern_call, args); 
}

#on_error(exception) ⇒ Object

Callback for error conditions such as exceptions raised in timer callbacks. Receives an exception instance as argument and raises by default.

handler.on_error(err) => raise



9
10
11
# File 'lib/zmq/timer.rb', line 9

def on_error(exception)
  raise exception
end