Class: Orocos::Async::PortProxy

Inherits:
ObjectBase show all
Extended by:
Forwardable
Defined in:
lib/orocos/async/task_context_proxy.rb

Direct Known Subclasses

SubPortProxy

Instance Attribute Summary

Attributes inherited from ObjectBase

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

Instance Method Summary collapse

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_listener, #remove_proxy_event, #valid_delegator?, valid_event?, #valid_event?, validate_event, #validate_event, #wait

Constructor Details

#initialize(task_proxy, port_name, options = Hash.new) ⇒ PortProxy

Returns a new instance of PortProxy



162
163
164
165
166
167
168
# File 'lib/orocos/async/task_context_proxy.rb', line 162

def initialize(task_proxy,port_name,options=Hash.new)
    super(port_name,task_proxy.event_loop)
    @task_proxy = task_proxy
    @type = options.delete(:type)
    @options = options
    @raw_last_sample = nil
end

Instance Method Details

#full_nameObject



178
179
180
# File 'lib/orocos/async/task_context_proxy.rb', line 178

def full_name
    "#{@task_proxy.name}.#{name}"
end

#input?Boolean

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
# File 'lib/orocos/async/task_context_proxy.rb', line 208

def input?
    if !valid_delegator?
        true
    elsif @delegator_obj.respond_to?(:writer)
        true
    else
        false
    end
end

#last_sampleObject



278
279
280
281
282
# File 'lib/orocos/async/task_context_proxy.rb', line 278

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

#new_sampleObject



192
193
194
# File 'lib/orocos/async/task_context_proxy.rb', line 192

def new_sample
    type.new
end

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



307
308
309
310
311
# File 'lib/orocos/async/task_context_proxy.rb', line 307

def on_data(policy = Hash.new,&block)
    on_raw_data policy do |sample|
        yield Typelib::to_ruby(sample,type)
    end
end

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

Raises:

  • (RuntimeError)


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/orocos/async/task_context_proxy.rb', line 313

def on_raw_data(policy = Hash.new,&block)
    raise RuntimeError , "Port #{name} is not an output port" if !output?
    @options = if policy.empty?
                   @options
               elsif @options.empty? && !valid_delegator?
                   policy
               elsif @options == policy
                   @options
               else
                   Orocos.warn "Changing global reader policy for #{full_name} from #{@options} to #{policy}"
                   @delegator_obj.options = policy
                   policy
               end
    on_event :raw_data,&block
end

#output?Boolean

Returns:

  • (Boolean)


218
219
220
221
222
223
224
225
226
# File 'lib/orocos/async/task_context_proxy.rb', line 218

def output?
    if !valid_delegator?
        true
    elsif @delegator_obj.respond_to?(:reader)
        true
    else
        false
    end
end

#periodObject



264
265
266
267
268
269
270
# File 'lib/orocos/async/task_context_proxy.rb', line 264

def period
    if @options.has_key? :period
        @options[:period]
    else
        nil
    end
end

#period=(period) ⇒ Object

Raises:

  • (RuntimeError)


272
273
274
275
276
# File 'lib/orocos/async/task_context_proxy.rb', line 272

def period=(period)
    raise RuntimeError, "Port #{name} is not an output port" if !output?
    @options[:period] = period
    @delegator_obj.period = period if valid_delegator?
end

#raw_last_sampleObject



284
285
286
# File 'lib/orocos/async/task_context_proxy.rb', line 284

def raw_last_sample
    @raw_last_sample
end

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



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/orocos/async/task_context_proxy.rb', line 232

def reachable!(port,options = Hash.new)
    raise ArgumentError, "port must not be kind of PortProxy" if port.is_a? PortProxy
    if @type && @type != port.type && @type.name != port.orocos_type_name
        raise RuntimeError, "the given type #{@type} for port #{port.full_name} differes from the real type name #{port.type}"
    end

    remove_proxy_event(@delegator_obj,@delegator_obj.event_names) if valid_delegator?
    super(port,options)
    proxy_event(@delegator_obj,@delegator_obj.event_names-[:reachable])
    @type = port.type

    #check which port we have
    if port.respond_to?(:reader)
        @raw_last_sample = port.raw_last_sample
    elsif number_of_listeners(:data) != 0
        raise RuntimeError, "Port #{name} is an input port but callbacks for on_data are registered"
    end
rescue Orocos::NotFound
    unreachable!
end

#reachable?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/orocos/async/task_context_proxy.rb', line 228

def reachable?
    super && @delegator_obj.reachable?
end

#really_add_listener(listener) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/orocos/async/task_context_proxy.rb', line 288

def really_add_listener(listener)
    return super unless listener.use_last_value?

    if listener.event == :data
        if sample = last_sample
            event_loop.once do
                listener.call sample
            end
        end
    elsif listener.event == :raw_data
        if sample = raw_last_sample
            event_loop.once do
                listener.call sample
            end
        end
    end
    super
end

#sub_port(subfield) ⇒ Object

returns a sub port for the given subfield

Raises:

  • (RuntimeError)


259
260
261
262
# File 'lib/orocos/async/task_context_proxy.rb', line 259

def sub_port(subfield)
    raise RuntimeError , "Port #{name} is not an output port" if !output?
    SubPortProxy.new(self,subfield)
end

#taskObject



204
205
206
# File 'lib/orocos/async/task_context_proxy.rb', line 204

def task
    @task_proxy
end

#to_async(options = Hash.new) ⇒ Object



196
197
198
# File 'lib/orocos/async/task_context_proxy.rb', line 196

def to_async(options=Hash.new)
    task.to_async(options).port(self.name)
end

#to_proxy(options = Hash.new) ⇒ Object



200
201
202
# File 'lib/orocos/async/task_context_proxy.rb', line 200

def to_proxy(options=Hash.new)
    self
end

#to_sObject



170
171
172
# File 'lib/orocos/async/task_context_proxy.rb', line 170

def to_s
    "#<Orocos::Async::PortProxy #{full_name}[#{type.name}]>"
end

#typeObject

Raises:



182
183
184
185
# File 'lib/orocos/async/task_context_proxy.rb', line 182

def type
    raise Orocos::NotFound, "#{self} is not reachable" unless @type
    @type
end

#type?Boolean

returns true if the proxy stored the type

Returns:

  • (Boolean)


188
189
190
# File 'lib/orocos/async/task_context_proxy.rb', line 188

def type?
    !!@type
end

#type_nameObject



174
175
176
# File 'lib/orocos/async/task_context_proxy.rb', line 174

def type_name
    type.name
end

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



253
254
255
256
# File 'lib/orocos/async/task_context_proxy.rb', line 253

def unreachable!(options = Hash.new)
    remove_proxy_event(@delegator_obj,@delegator_obj.event_names) if valid_delegator?
    super(options)
end