Class AbstractPortDecorator<PM extends PortMetrics>

java.lang.Object
org.refcodes.serial.AbstractPortDecorator<PM>
Type Parameters:
PM - The actual PortMetrics type to use.
All Implemented Interfaces:
Flushable, Comparable<Port<?>>, org.refcodes.component.Closable, org.refcodes.component.Closable.CloseAutomaton, org.refcodes.component.ClosedAccessor, org.refcodes.component.ConnectableComponent, org.refcodes.component.ConnectableComponent.ConnectableAutomaton, org.refcodes.component.ConnectionComponent<PM>, org.refcodes.component.ConnectionComponent.ConnectionAutomaton<PM>, org.refcodes.component.ConnectionOpenable<PM>, org.refcodes.component.ConnectionOpenable.ConnectionOpenAutomaton<PM>, org.refcodes.component.ConnectionOpenable.ConnectionOpenBuilder<PM,Port<PM>>, org.refcodes.component.ConnectionStatusAccessor, org.refcodes.component.Flushable, org.refcodes.component.LinkComponent, org.refcodes.component.LinkComponent.LinkAutomaton, org.refcodes.component.Openable, org.refcodes.component.Openable.OpenAutomaton, org.refcodes.component.Openable.OpenBuilder<Port<PM>>, org.refcodes.component.OpenedAccessor, org.refcodes.io.Availability, org.refcodes.io.ByteDestination, org.refcodes.io.ByteReceiver, org.refcodes.io.BytesDestination, org.refcodes.io.ByteSource, org.refcodes.io.BytesReceiver, org.refcodes.io.BytesSource, org.refcodes.io.BytesTransceiver, org.refcodes.io.BytesTransmitter, org.refcodes.io.ByteTransceiver, org.refcodes.io.ByteTransmitter, org.refcodes.io.Receivable, org.refcodes.io.Skippable, org.refcodes.io.TimeoutInputStreamAccessor, org.refcodes.io.Transmittable, org.refcodes.mixin.AliasAccessor, org.refcodes.mixin.InputStreamAccessor, org.refcodes.mixin.OutputStreamAccessor, Port<PM>, PortMetricsAccessor<PM>, SegmentDestination, SegmentReceiver, SegmentSource, SegmentTransceiver, SegmentTransmitter, SerialReceiver, SerialTransceiver, SerialTransmitter

public class AbstractPortDecorator<PM extends PortMetrics> extends Object implements Port<PM>
The AbstractPortDecorator decorates a Port for sub-classes to enrich Port functionality.
  • Field Details

  • Constructor Details

    • AbstractPortDecorator

      public AbstractPortDecorator(Port<PM> aPort)
      Decorates the given Port with the according functionality.
      Parameters:
      aPort - The Port to be decorated.
  • Method Details

    • receiveSegment

      public <SEGMENT extends Segment> void receiveSegment(SEGMENT aSegment) throws TransmissionException, IOException
      Receives a Segment (and blocks this thread) till all it's Sequence data has been received and updates the Segment by invoking Segment.fromTransmission(Sequence). This method blocks till all bytes are read.
      Specified by:
      receiveSegment in interface SegmentDestination
      Specified by:
      receiveSegment in interface SerialReceiver
      Specified by:
      receiveSegment in interface SerialTransceiver
      Type Parameters:
      SEGMENT - The Segment type describing the Segment subclass of implementing Segment types.
      Parameters:
      aSegment - The Segment to be (re-)initialized with the received data.
      Throws:
      TransmissionException - Thrown in case the received data cannot be used to (re-)initialize the given Segment.
      IOException - thrown in case of I/O issues (e.g. a timeout) while receiving.
    • transmitSegment

      public <SEGMENT extends Segment> void transmitSegment(SEGMENT aSegment) throws IOException
      Transmits a Segment (and blocks this thread) till all it's Sequence data (as of Transmission.toSequence()) has been sent.
      Specified by:
      transmitSegment in interface SegmentSource
      Specified by:
      transmitSegment in interface SerialTransceiver
      Specified by:
      transmitSegment in interface SerialTransmitter
      Type Parameters:
      SEGMENT - The Segment type describing the Segment subclass used.
      Parameters:
      aSegment - The Segment's data to be sent.
      Throws:
      IOException - thrown in case of I/O issues (e.g. a timeout) while sending.
    • transmitBytes

      public void transmitBytes(byte[] aBytes, int aOffset, int aLength) throws IOException
      Specified by:
      transmitBytes in interface org.refcodes.io.BytesSource
      Specified by:
      transmitBytes in interface org.refcodes.io.BytesTransmitter
      Specified by:
      transmitBytes in interface SerialTransmitter
      Throws:
      IOException
    • getOutputStream

      public OutputStream getOutputStream()
      Specified by:
      getOutputStream in interface org.refcodes.io.BytesTransmitter
      Specified by:
      getOutputStream in interface org.refcodes.mixin.OutputStreamAccessor
    • receiveSegmentWithin

      public <SEGMENT extends Segment> void receiveSegmentWithin(long aTimeoutInMs, SEGMENT aSegment) throws TransmissionException, IOException
      Receives a Segment (and blocks this thread) till all it's Sequence data has been received and updates the Segment by invoking Segment.fromTransmission(Sequence). This method blocks till all bytes are read or the timeout has been reached.
      Specified by:
      receiveSegmentWithin in interface SegmentReceiver
      Specified by:
      receiveSegmentWithin in interface SerialReceiver
      Type Parameters:
      SEGMENT - The Segment type describing the Segment subclass of implementing Segment types.
      Parameters:
      aTimeoutInMs - The default timeout for read operations not explicitly called with a timeout argument. With a value of -1 timeout handling is disabled (blocking mode) or a technical timeout occurs (implementation depended).
      aSegment - The Segment to be (re-)initialized with the received data.
      Throws:
      TransmissionException - Thrown in case the received data cannot be used to (re-)initialize the given Segment.
      IOException - thrown in case of I/O issues (e.g. a timeout) while receiving.
    • receiveSequenceWithin

      public Sequence receiveSequenceWithin(long aTimeoutInMs, int aLength) throws IOException
      Receives a Sequence with the number of bytes specified. This method blocks till all bytes are read or the timeout has been reached.
      Specified by:
      receiveSequenceWithin in interface SerialReceiver
      Parameters:
      aTimeoutInMs - The default timeout for read operations not explicitly called with a timeout argument. With a value of -1 timeout handling is disabled (blocking mode) or a technical timeout occurs (implementation depended).
      aLength - The number of bytes to receive.
      Returns:
      A Sequence containing the accordingly received bytes.
      Throws:
      IOException - thrown in case of I/O issues (e.g. a timeout) while receiving.
    • onReceiveSegment

      public <SEGMENT extends Segment> void onReceiveSegment(SEGMENT aSegment, SegmentConsumer<SEGMENT> aSegmentConsumer) throws IOException
      Asynchronously receives a Segment's Sequence data (does not block this thread) and updates the Segment by invoking Segment.fromTransmission(Sequence), passing the result to the callback SegmentConsumer.onSegment(Segment).
      Specified by:
      onReceiveSegment in interface SegmentReceiver
      Type Parameters:
      SEGMENT - The Segment type describing the Segment subclass of implementing Segment types.
      Parameters:
      aSegment - The Segment to be (re-)initialized with the received data.
      aSegmentConsumer - The callback asynchronously invoked upon having received the Segment.
      Throws:
      IOException - thrown in case of I/O issues (e.g. a closed connection) while receiving.
    • onReceiveSegment

      public <SEGMENT extends Segment> SegmentResult<SEGMENT> onReceiveSegment(SEGMENT aSegment) throws IOException
      Asynchronously receives a Segment's Sequence data (does not block this thread) and updates the Segment by invoking Segment.fromTransmission(Sequence). The result is provided by the returned SegmentResult instance providing the (asynchronously) (re-)initialized provided Segment instance.
      Specified by:
      onReceiveSegment in interface SegmentReceiver
      Type Parameters:
      SEGMENT - The Segment type describing the Segment subclass used.
      Parameters:
      aSegment - The Segment to be (re-)initialized with the received data.
      Returns:
      The SegmentResult which provides the result (asynchronously).
      Throws:
      IOException - thrown in case of I/O issues (e.g. a closed connection) while receiving.
    • receiveSequence

      public Sequence receiveSequence(int aLength) throws IOException
      Receives a Sequence with the number of bytes specified.This method blocks till all bytes are read.
      Specified by:
      receiveSequence in interface SerialReceiver
      Parameters:
      aLength - The number of bytes to receive.
      Returns:
      A Sequence containing the accordingly received bytes.
      Throws:
      IOException - thrown in case of I/O issues (e.g. a timeout) while receiving.
    • close

      public void close() throws IOException
      Attention: Implementations of this method should do a Object.notifyAll() in order to terminate any pending asynchronous operations such as SegmentReceiver.onReceiveSegment(Segment) or SegmentReceiver.onReceiveSegment(Segment, SegmentConsumer).
      Specified by:
      close in interface org.refcodes.component.Closable
      Specified by:
      close in interface SerialReceiver
      Throws:
      IOException
    • closeUnchecked

      public void closeUnchecked()
      Specified by:
      closeUnchecked in interface org.refcodes.component.Closable
    • closeQuietly

      public void closeQuietly()
      Specified by:
      closeQuietly in interface org.refcodes.component.Closable
    • closeIn

      public void closeIn(int aCloseInMillis)
      Specified by:
      closeIn in interface org.refcodes.component.Closable
    • getAlias

      public String getAlias()
      Specified by:
      getAlias in interface org.refcodes.mixin.AliasAccessor
    • open

      public void open() throws IOException
      Attention: Implementations of this method should do a Object.notifyAll() in order to terminate any pending asynchronous operations such as SegmentReceiver.onReceiveSegment(Segment) or SegmentReceiver.onReceiveSegment(Segment, SegmentConsumer).
      Specified by:
      open in interface org.refcodes.component.Openable
      Specified by:
      open in interface SerialReceiver
      Throws:
      IOException
    • openUnchecked

      public void openUnchecked()
      Specified by:
      openUnchecked in interface org.refcodes.component.Openable
    • skip

      public long skip(long aLength) throws IOException
      Specified by:
      skip in interface org.refcodes.io.BytesReceiver
      Specified by:
      skip in interface org.refcodes.io.Skippable
      Throws:
      IOException
    • getInputStream

      public InputStream getInputStream()
      Specified by:
      getInputStream in interface org.refcodes.io.BytesReceiver
      Specified by:
      getInputStream in interface org.refcodes.mixin.InputStreamAccessor
    • getInputStream

      public org.refcodes.io.TimeoutInputStream getInputStream(long aTimeoutInMs)
      Specified by:
      getInputStream in interface org.refcodes.io.BytesReceiver
      Specified by:
      getInputStream in interface org.refcodes.io.TimeoutInputStreamAccessor
    • receiveAllBytes

      public byte[] receiveAllBytes() throws IOException
      Specified by:
      receiveAllBytes in interface org.refcodes.io.BytesDestination
      Specified by:
      receiveAllBytes in interface org.refcodes.io.BytesReceiver
      Throws:
      IOException
    • receiveBytes

      public byte[] receiveBytes(int aMaxLength) throws IOException
      Specified by:
      receiveBytes in interface org.refcodes.io.BytesDestination
      Specified by:
      receiveBytes in interface org.refcodes.io.BytesReceiver
      Specified by:
      receiveBytes in interface SerialReceiver
      Throws:
      IOException
    • receiveByteWithin

      public byte receiveByteWithin(long aTimeoutInMs) throws IOException
      Specified by:
      receiveByteWithin in interface org.refcodes.io.BytesReceiver
      Throws:
      IOException
    • receiveBytesWithin

      public byte[] receiveBytesWithin(long aTimeoutInMs, int aLength) throws IOException
      Specified by:
      receiveBytesWithin in interface org.refcodes.io.BytesReceiver
      Throws:
      IOException
    • receiveBytesWithin

      public void receiveBytesWithin(long aTimeoutInMs, byte[] aBuffer, int aOffset, int aLength) throws IOException
      Specified by:
      receiveBytesWithin in interface org.refcodes.io.BytesReceiver
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface org.refcodes.io.ByteTransmitter
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in interface org.refcodes.component.Flushable
      Specified by:
      flush in interface SegmentTransmitter
      Specified by:
      flush in interface SerialTransmitter
      Throws:
      IOException
    • isOpened

      public boolean isOpened()
      Specified by:
      isOpened in interface org.refcodes.component.ConnectionStatusAccessor
      Specified by:
      isOpened in interface org.refcodes.component.OpenedAccessor
    • available

      public int available() throws IOException
      Returns the number of bytes which can be read directly e.g. which are already been stored in an internal buffer. May return (0 or) a value less than 0 (-1) when it is not supported. Similar to InputStream.available(): "Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes. Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream. A subclass' implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the close() method. The available method for class InputStream always returns 0. This method should be overridden by subclasses."
      Specified by:
      available in interface org.refcodes.io.Availability
      Specified by:
      available in interface SerialReceiver
      Specified by:
      available in interface org.refcodes.io.Skippable
      Returns:
      The number of available bytes: An estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream. Throws:
      Throws:
      IOException - - if an I/O error occurs.
    • hasAvailable

      public boolean hasAvailable() throws IOException
      Specified by:
      hasAvailable in interface org.refcodes.io.Availability
      Throws:
      IOException
    • getConnectionStatus

      public org.refcodes.component.ConnectionStatus getConnectionStatus()
      Specified by:
      getConnectionStatus in interface org.refcodes.component.ConnectionStatusAccessor
    • getPortMetrics

      public PM getPortMetrics()
      Retrieves the PortMetrics from the PortMetrics property.
      Specified by:
      getPortMetrics in interface PortMetricsAccessor<PM extends PortMetrics>
      Returns:
      The PortMetrics stored by the PortMetrics property.
    • doTransmitSegment

      public <SEGMENT extends Segment> void doTransmitSegment(SEGMENT aSegment, SegmentConsumer<SEGMENT> aSegmentConsumer) throws IOException
      Asynchronously sends a Segment's Sequence data (does not block this thread).
      Specified by:
      doTransmitSegment in interface SegmentTransmitter
      Type Parameters:
      SEGMENT - The Segment type describing the Segment subclass used.
      Parameters:
      aSegment - The Segment to be sent.
      aSegmentConsumer - The callback asynchronously invoked upon having sent the Segment.
      Throws:
      IOException - thrown in case of I/O issues (e.g. a closed connection upon the time of invocation).
    • doTransmitSegment

      public <SEGMENT extends Segment> SegmentResult<SEGMENT> doTransmitSegment(SEGMENT aSegment) throws IOException
      Asynchronously sends a Segment's Sequence data (does not block this thread).
      Specified by:
      doTransmitSegment in interface SegmentTransmitter
      Type Parameters:
      SEGMENT - The Segment type describing the Segment subclass used.
      Parameters:
      aSegment - The Segment to be sent.
      Returns:
      The SegmentResult which provides the result of the operation (asynchronously), e.g. the Segment being sent or an IOException having occurred.
      Throws:
      IOException - thrown in case of I/O issues (e.g. a closed connection upon the time of invocation).
    • transmitSequence

      public void transmitSequence(Sequence aSequence, int aOffset, int aLength) throws IOException
      Transmits a Sequence.
      Specified by:
      transmitSequence in interface SerialTransmitter
      Parameters:
      aSequence - The Sequence containing the data to be send.
      aOffset - The offset from where to take the chunk data.
      aLength - The number of bytes to take, beginning at the provided offset.
      Throws:
      IOException - thrown in case of I/O issues (e.g. a timeout) while sending
    • transmitSequence

      public void transmitSequence(Sequence aSequence) throws IOException
      Transmits a Sequence (and blocks this thread) till all it's data has been sent.
      Specified by:
      transmitSequence in interface SerialTransmitter
      Parameters:
      aSequence - The Sequence containing the data to be send.
      Throws:
      IOException - thrown in case of I/O issues (e.g. a timeout) while sending
    • isClosable

      public boolean isClosable()
      Specified by:
      isClosable in interface org.refcodes.component.Closable.CloseAutomaton
    • isClosed

      public boolean isClosed()
      Specified by:
      isClosed in interface org.refcodes.component.ClosedAccessor
    • flushUnchecked

      public void flushUnchecked()
      Specified by:
      flushUnchecked in interface org.refcodes.component.Flushable
    • isFlushable

      public boolean isFlushable()
      Specified by:
      isFlushable in interface org.refcodes.component.Flushable
    • skipAvailable

      public void skipAvailable() throws IOException
      Specified by:
      skipAvailable in interface org.refcodes.io.Skippable
      Throws:
      IOException
    • skipAvailableWithin

      public void skipAvailableWithin(long aSkipTimeSpanInMs) throws IOException
      Specified by:
      skipAvailableWithin in interface org.refcodes.io.Skippable
      Throws:
      IOException
    • skipAvailableWithin

      public void skipAvailableWithin(long aSkipTimeSpanInMs, long aTimeoutInMs) throws IOException
      Specified by:
      skipAvailableWithin in interface org.refcodes.io.Skippable
      Throws:
      IOException
    • skipAvailableTill

      public void skipAvailableTill(long aSkipTimeSpanInMs) throws IOException
      Specified by:
      skipAvailableTill in interface org.refcodes.io.Skippable
      Throws:
      IOException
    • skipAvailableTillSilenceFor

      public void skipAvailableTillSilenceFor(long aSilenceTimeSpanInMs) throws IOException
      Specified by:
      skipAvailableTillSilenceFor in interface org.refcodes.io.Skippable
      Throws:
      IOException
    • skipAvailableTillSilenceFor

      public void skipAvailableTillSilenceFor(long aSilenceTimeSpanInMs, long aTimeoutInMs) throws IOException, org.refcodes.exception.TimeoutIOException
      Specified by:
      skipAvailableTillSilenceFor in interface org.refcodes.io.Skippable
      Throws:
      IOException
      org.refcodes.exception.TimeoutIOException
    • skipAvailableExcept

      public void skipAvailableExcept(int aLength) throws IOException
      Specified by:
      skipAvailableExcept in interface org.refcodes.io.Skippable
      Throws:
      IOException
    • receiveByte

      public byte receiveByte() throws IOException
      Specified by:
      receiveByte in interface org.refcodes.io.ByteDestination
      Specified by:
      receiveByte in interface org.refcodes.io.BytesDestination
      Specified by:
      receiveByte in interface SerialReceiver
      Throws:
      IOException
    • receiveBytes

      public void receiveBytes(byte[] aBuffer, int aOffset, int aLength) throws IOException
      Specified by:
      receiveBytes in interface org.refcodes.io.BytesDestination
      Throws:
      IOException
    • isOpenable

      public boolean isOpenable()
      Specified by:
      isOpenable in interface org.refcodes.component.Openable.OpenAutomaton
    • withOpen

      public Port<PM> withOpen() throws IOException
      Specified by:
      withOpen in interface org.refcodes.component.Openable.OpenBuilder<PM extends PortMetrics>
      Throws:
      IOException
    • withOpenUnchecked

      public Port<PM> withOpenUnchecked()
      Specified by:
      withOpenUnchecked in interface org.refcodes.component.Openable.OpenBuilder<PM extends PortMetrics>
    • withOpen

      public Port<PM> withOpen(PM aPortMetrics) throws IOException
      Specified by:
      withOpen in interface org.refcodes.component.ConnectionOpenable.ConnectionOpenBuilder<PM extends PortMetrics,Port<PM extends PortMetrics>>
      Throws:
      IOException
    • withOpenUnchecked

      public Port<PM> withOpenUnchecked(PM aPortMetrics)
      Specified by:
      withOpenUnchecked in interface org.refcodes.component.ConnectionOpenable.ConnectionOpenBuilder<PM extends PortMetrics,Port<PM extends PortMetrics>>
    • open

      public void open(PM aPortMetrics) throws IOException
      Specified by:
      open in interface org.refcodes.component.ConnectionOpenable<PM extends PortMetrics>
      Throws:
      IOException
    • openUnchecked

      public void openUnchecked(PM aPortMetrics)
      Specified by:
      openUnchecked in interface org.refcodes.component.ConnectionOpenable<PM extends PortMetrics>
    • isOpenable

      public boolean isOpenable(PM aPortMetrics)
      Specified by:
      isOpenable in interface org.refcodes.component.ConnectionOpenable.ConnectionOpenAutomaton<PM extends PortMetrics>