Class: Syskit::OutputReader

Inherits:
PortAccessor show all
Defined in:
lib/syskit/port.rb

Overview

A data source for a port attached to a component

Instance Attribute Summary collapse

Attributes inherited from PortAccessor

#actual_port, #policy, #port, #resolved_port

Instance Method Summary collapse

Methods inherited from PortAccessor

#initialize

Constructor Details

This class inherits a constructor from Syskit::PortAccessor

Instance Attribute Details

#readerOrocos::OutputReader (readonly)

The actual data reader itself

Returns:

  • (Orocos::OutputReader)


274
275
276
# File 'lib/syskit/port.rb', line 274

def reader
  @reader
end

Instance Method Details

#clearObject

Clear all samples from the reader

After this call, #read and #read_new return nil

It is a no-op if the port is not yet connected. This makes sense as new connections are cleared (have no samples) until the writer writes a new sample



335
336
337
# File 'lib/syskit/port.rb', line 335

def clear
    reader.clear if reader
end

#connected?Boolean

Whether the reader is connected to the underlying port

Output readers in Syskit are resolved asynchronously. As such, they may be created but not yet connected

When a port is not connected, #read, #read_new and #clear are no-ops.

Returns:

  • (Boolean)


354
355
356
# File 'lib/syskit/port.rb', line 354

def connected?
    reader && reader.connected?
end

#disconnectObject



287
288
289
290
291
292
293
294
295
296
# File 'lib/syskit/port.rb', line 287

def disconnect
    @disconnected = true
    if actual_reader = self.reader
        actual_port.component.execution_engine.promise(description: "disconnect #{self}") do
            begin actual_reader.disconnect
            rescue Orocos::ComError
            end
        end.on_success { @reader = nil }.execute
    end
end

#modelObject



276
277
278
# File 'lib/syskit/port.rb', line 276

def model
    Models::OutputReader.new(port.model, policy)
end

#read(sample = nil) ⇒ Object?

Get either a sample that has never been read, or the last read sample

Note that as with #read_new and #clear, this returns nil if the output reader is not yet connected.

Parameters:

  • sample (Object, nil) (defaults to: nil)

    a Typelib sample of the port's type. If provided, the sample will be copied into this object. Use this to avoid unnecessary object allocations in known-to-be long loops

Returns:

  • (Object, nil)

    the sample, or nil if there are no samples received on this read that have not already been read



324
325
326
# File 'lib/syskit/port.rb', line 324

def read(sample = nil)
    reader.read(sample) if reader
end

#read_new(sample = nil) ⇒ Object?

Get a sample that has never been read

Note that as with #read and #clear, this returns nil if the output reader is not yet connected.

Parameters:

  • sample (Object, nil) (defaults to: nil)

    a Typelib sample of the port's type. If provided, the sample will be copied into this object. Use this to avoid unnecessary object allocations in known-to-be long loops

Returns:

  • (Object, nil)

    the sample, or nil if there are no samples received on this read that have not already been read



309
310
311
# File 'lib/syskit/port.rb', line 309

def read_new(sample = nil)
    reader.read_new(sample) if reader
end

#ready?Boolean

Whether the reader may return a new sample

It is false if it is not yet connected and/or the underlying component is not yet running.

Returns:

  • (Boolean)


343
344
345
# File 'lib/syskit/port.rb', line 343

def ready?
    reader && actual_port.component.running?
end

#resolve(main, port) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves the underlying reader object



283
284
285
# File 'lib/syskit/port.rb', line 283

def resolve(main, port)
    super(main, port, :reader) { |r| @reader = r }
end