Class: Roby::Interface::Async::NewJobListener

Inherits:
Object
  • Object
show all
Defined in:
lib/roby/interface/async/new_job_listener.rb

Overview

Listener object for Interface#on_job

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface, action_name, block) ⇒ NewJobListener

Returns a new instance of NewJobListener.



21
22
23
24
25
26
# File 'lib/roby/interface/async/new_job_listener.rb', line 21

def initialize(interface, action_name, block)
    @interface = interface
    @action_name = action_name
    @block = block
    @last_job_id = -1
end

Instance Attribute Details

#action_nameString? (readonly)

Returns the name of the action whose job we are tracking. If nil, tracks all actions.

Returns:

  • (String, nil)

    the name of the action whose job we are tracking. If nil, tracks all actions.



10
11
12
# File 'lib/roby/interface/async/new_job_listener.rb', line 10

def action_name
  @action_name
end

#block#call (readonly)

Returns the notification callback.

Returns:

  • (#call)

    the notification callback



12
13
14
# File 'lib/roby/interface/async/new_job_listener.rb', line 12

def block
  @block
end

#interfaceInterface (readonly)

Returns the interface we are connected to.

Returns:

  • (Interface)

    the interface we are connected to



7
8
9
# File 'lib/roby/interface/async/new_job_listener.rb', line 7

def interface
  @interface
end

#last_job_idObject (readonly)

The last ID of the jobs received by this listener.

This is used to avoid double-notifications of new jobs. The assumption is that the job IDs are ever-increasing and that they are fed in-order to #call.



19
20
21
# File 'lib/roby/interface/async/new_job_listener.rb', line 19

def last_job_id
  @last_job_id
end

Instance Method Details

#call(job) ⇒ Object

Call the listener for the given job

Parameters:



53
54
55
56
# File 'lib/roby/interface/async/new_job_listener.rb', line 53

def call(job)
    @last_job_id = job.job_id
    block.call(job)
end

#ignored(job) ⇒ Object

Tell this listener that the given job was received, but ignored.

This is an optimization to avoid re-considering this listener for the given job



63
64
65
# File 'lib/roby/interface/async/new_job_listener.rb', line 63

def ignored(job)
    @last_job_id = job.job_id
end

#matches?(job) ⇒ Boolean

Tests whether the provided job matches what this listener wants

Returns:

  • (Boolean)


46
47
48
# File 'lib/roby/interface/async/new_job_listener.rb', line 46

def matches?(job)
    !action_name || (job.action_name == action_name)
end

#resetObject

Resets the listener so that it can be used on a new connection

This currently only resets #last_job_id



31
32
33
# File 'lib/roby/interface/async/new_job_listener.rb', line 31

def reset
    @last_job_id = -1
end

#seen_job_with_id?(job_id) ⇒ Boolean

Tests whether this listener has already seen the job with the given ID

Parameters:

  • job_id (Integer)

Returns:

  • (Boolean)

See Also:



40
41
42
# File 'lib/roby/interface/async/new_job_listener.rb', line 40

def seen_job_with_id?(job_id)
    last_job_id >= job_id
end

#startObject

Start listening for jobs



68
69
70
# File 'lib/roby/interface/async/new_job_listener.rb', line 68

def start
    interface.add_new_job_listener(self)
end

#stopObject

Stop listening for jobs



73
74
75
# File 'lib/roby/interface/async/new_job_listener.rb', line 73

def stop
    interface.remove_new_job_listener(self)
end