Class: ZMQMachine::Timer

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/zm/timers.rb

Overview

Used to track the specific expiration time and execution code for each timer.

This should never be instantiated directly by user code. A timer must be known to the #Reactor in which it is running, so use the #Reactor#oneshot_timer and #Reactor#periodical_timer convenience methods. It ensures that new timers are installed in the correct #Reactor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timers, delay, periodical, timer_proc = nil, &blk) ⇒ Timer

delay is in milliseconds



252
253
254
255
256
257
258
# File 'lib/zm/timers.rb', line 252

def initialize timers, delay, periodical, timer_proc = nil, &blk
  @timers = timers
  @delay = delay.to_i
  @periodical = periodical
  @timer_proc = timer_proc || blk
  schedule_firing_time
end

Instance Attribute Details

#fire_timeObject (readonly)

Returns the value of attribute fire_time.



248
249
250
# File 'lib/zm/timers.rb', line 248

def fire_time
  @fire_time
end

#timer_procObject (readonly)

Returns the value of attribute timer_proc.



248
249
250
# File 'lib/zm/timers.rb', line 248

def timer_proc
  @timer_proc
end

Instance Method Details

#<=>(other) ⇒ Object



278
279
280
# File 'lib/zm/timers.rb', line 278

def <=>(other)
  @fire_time <=> other.fire_time
end

#==(other) ⇒ Object



282
283
284
285
286
287
288
# File 'lib/zm/timers.rb', line 282

def ==(other)
  # need a more specific equivalence test since multiple timers could be
  # scheduled to go off at exactly the same time
  @fire_time == other.fire_time &&
  @timer_proc == other.timer_proc &&
  periodical? == other.periodical?
end

#cancelObject

Cancels this timer from firing.



274
275
276
# File 'lib/zm/timers.rb', line 274

def cancel
  @timers.cancel self
end

#expired?(time) ⇒ Boolean

True when the timer should be fired; false otherwise.

Returns:

  • (Boolean)


292
293
294
295
# File 'lib/zm/timers.rb', line 292

def expired? time
  time ||= Timers.now
  time >= @fire_time
end

#fireObject

Executes the callback.

Returns true when the timer is a one-shot; Returns false when the timer is periodical and has rescheduled itself.



266
267
268
269
270
# File 'lib/zm/timers.rb', line 266

def fire
  @timer_proc.call

  schedule_firing_time if @periodical
end

#inspectObject



315
# File 'lib/zm/timers.rb', line 315

def inspect; to_s; end

#periodical?Boolean

True when this is a periodical timer; false otherwise.

Returns:

  • (Boolean)


299
300
301
# File 'lib/zm/timers.rb', line 299

def periodical?
  @periodical
end

#rescheduleObject



303
304
305
# File 'lib/zm/timers.rb', line 303

def reschedule
  schedule_firing_time
end

#to_sObject



307
308
309
310
311
312
313
# File 'lib/zm/timers.rb', line 307

def to_s
  ftime = Time.at(@fire_time / 1000)
  fdelay = @fire_time - Timers.now
  name = @timer_proc.respond_to?(:name) ? @timer_proc.name : @timer_proc.to_s
  
  "[delay [#{@delay}], periodical? [#{@periodical}], fire_time [#{ftime}] fire_delay_ms [#{fdelay}]] proc [#{name}]"
end