Class: Orocos::Async::CORBA::AttributeBase

Inherits:
ObjectBase
  • Object
show all
Extended by:
ObjectBase::Periodic::ClassMethods, Utilrb::EventLoop::Forwardable
Includes:
ObjectBase::Periodic
Defined in:
lib/orocos/async/attributes.rb

Direct Known Subclasses

Attribute, Property

Instance Attribute Summary collapse

Attributes included from ObjectBase::Periodic::ClassMethods

#default_period

Attributes inherited from ObjectBase

#emitting, #event_loop, #name, #options, #pending_adds

Instance Method Summary collapse

Methods included from ObjectBase::Periodic

#default_period, #period

Methods inherited from ObjectBase

#add_listener, define_event, define_events, #disable_emitting, #event, event_names, #event_names, #invalidate_delegator!, #listener?, #listeners, #number_of_listeners, #on_event, #proxy_event, #remove_all_listeners, #remove_proxy_event, #valid_delegator?, valid_event?, #valid_event?, validate_event, #validate_event

Constructor Details

#initialize(async_task, attribute, options = Hash.new) ⇒ AttributeBase

Returns a new instance of AttributeBase



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/orocos/async/attributes.rb', line 11

def initialize(async_task,attribute,options=Hash.new)
    super(attribute.name,async_task.event_loop)
    @options = Kernel.validate_options options, :period => default_period
    @task = async_task
    @mutex = Mutex.new

    disable_emitting do
        reachable!(attribute)
    end
    @poll_timer = @event_loop.async_every(method(:raw_read), {:period => period, :start => false,
                                          :known_errors => Orocos::Async::KNOWN_ERRORS}) do |data,error|
        if error
            @poll_timer.cancel
            self.period = @poll_timer.period
            @event_loop.once do
                event :error,error
            end
        else
            if data
                if @raw_last_sample != data
                    @raw_last_sample = data
                    event :raw_change,data
                    event :change,Typelib.to_ruby(data)
                end
            end
        end
    end
    @poll_timer.doc = attribute.full_name
    @task.on_unreachable do
        unreachable!
    end
rescue Orocos::NotFound => e
    emit_error e
end

Instance Attribute Details

#raw_last_sampleObject (readonly)

Returns the value of attribute raw_last_sample



9
10
11
# File 'lib/orocos/async/attributes.rb', line 9

def raw_last_sample
  @raw_last_sample
end

Instance Method Details

#last_sampleObject



46
47
48
49
50
# File 'lib/orocos/async/attributes.rb', line 46

def last_sample
    if @raw_last_sample
        Typelib.to_ruby(@raw_last_sample)
    end
end

#on_change(policy = Hash.new, &block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/orocos/async/attributes.rb', line 133

def on_change(policy = Hash.new,&block)
    @policy = if policy.empty?
                   options
               elsif !@policy
                   policy
               elsif @policy == policy
                   @policy
               else
                   Orocos.warn "Property #{full_name} cannot emit :change with different policies."
                   Orocos.warn "The current policy is: #{@policy}."
                   Orocos.warn "Ignoring policy: #{policy}."
                   @policy
               end
    on_event :change,&block
end

#on_raw_change(policy = Hash.new, &block) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/orocos/async/attributes.rb', line 117

def on_raw_change(policy = Hash.new,&block)
    @policy = if policy.empty?
                   options
               elsif !@policy
                   policy
               elsif @policy == policy
                   @policy
               else
                   Orocos.warn "Property #{full_name} cannot emit :raw_change with different policies."
                   Orocos.warn "The current policy is: #{@policy}."
                   Orocos.warn "Ignoring policy: #{policy}."
                   @policy
               end
    on_event :raw_change,&block
end

#period=(period) ⇒ Object



67
68
69
70
# File 'lib/orocos/async/attributes.rb', line 67

def period=(period)
    super
    @poll_timer.period = self.period
end

#reachable!(attribute, options = Hash.new) ⇒ Object



58
59
60
61
# File 'lib/orocos/async/attributes.rb', line 58

def reachable!(attribute,options = Hash.new)
    super
    @raw_last_sample = nil
end

#reachable?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/orocos/async/attributes.rb', line 63

def reachable?
    super && @raw_last_sample
end

#really_add_listener(listener) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/orocos/async/attributes.rb', line 92

def really_add_listener(listener)
    super
    if listener.event == :raw_change
        if !@poll_timer.running?
            @poll_timer.start(period)
        end
        listener.call(@raw_last_sample) if @raw_last_sample && listener.use_last_value?
    elsif listener.event == :change
        if !@poll_timer.running?
            @poll_timer.start(period)
        end
        listener.call(Typelib.to_ruby(@raw_last_sample)) if @raw_last_sample && listener.use_last_value?
    end
end

#remove_listener(listener) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/orocos/async/attributes.rb', line 107

def remove_listener(listener)
    super
    if number_of_listeners(:change) == 0
        if @poll_timer.running?
            @poll_timer.stop
        end
        @policy = nil
    end
end

#unreachable!(options = Hash.new) ⇒ Object



52
53
54
55
56
# File 'lib/orocos/async/attributes.rb', line 52

def unreachable!(options = Hash.new)
    super
    @raw_last_sample = nil
    @poll_timer.cancel
end

#wait(timeout = 5.0) ⇒ Object

waits until object gets reachable raises Orocos::NotFound if the object was not reachable after the given time spawn



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/orocos/async/attributes.rb', line 74

def wait(timeout = 5.0)
    # make sure the poll timer is running otherwise wait
    # will always fail
    poll_timer_running  = @poll_timer.running?
    @poll_timer.start(0.01) unless poll_timer_running
    time = Time.now
    @event_loop.wait_for do
        if timeout && timeout <= Time.now-time
            Utilrb::EventLoop.cleanup_backtrace do
                raise Orocos::NotFound,"#{self.class}: #{respond_to?(:full_name) ? full_name : name} is not reachable after #{timeout} seconds"
            end
        end
        reachable?
    end
    @poll_timer.stop unless poll_timer_running
    self
end