Class IOHub

java.lang.Object
org.jenkinsci.remoting.protocol.IOHub
All Implemented Interfaces:
Closeable, AutoCloseable, Runnable, Executor, ByteBufferPool

public class IOHub extends Object implements Executor, Closeable, Runnable, ByteBufferPool
A hub for performing I/O. The hub has a selector thread and an executor service.
Since:
3.0
  • Method Details

    • create

      public static IOHub create(Executor executor) throws IOException
      Creates and starts a new IOHub instance.
      Parameters:
      executor - the Executor to use for running tasks.
      Returns:
      the new hub.
      Throws:
      IOException - if the hub's Selector cannot be opened.
    • acquire

      public ByteBuffer acquire(int size)
      Borrow a buffer from the pool.
      Specified by:
      acquire in interface ByteBufferPool
      Parameters:
      size - The minimum size and initial limit of the buffer.
      Returns:
      the buffer.
    • release

      public void release(ByteBuffer buffer)
      Returns a buffer to the pool.
      Specified by:
      release in interface ByteBufferPool
      Parameters:
      buffer - the buffer.
    • getSelector

      @NonNull public final Selector getSelector()
      Returns the Selector.
      Returns:
      the Selector
    • execute

      @OverrideMustInvoke public void execute(@NonNull Runnable task)
      Specified by:
      execute in interface Executor
    • executeOnSelector

      @OverrideMustInvoke public void executeOnSelector(Runnable task)
      Executes the given task at some time in the future. The task will execute in the selector thread.
      Parameters:
      task - the runnable task
      Throws:
      RejectedExecutionException - if this task cannot be accepted for execution
      NullPointerException - if task is null
    • executeLater

      @OverrideMustInvoke public Future<?> executeLater(Runnable task, long delay, TimeUnit units)
      Executes a task at a future point in time. The scheduling is handled by the selector thread, and as such this method should not be used for timing critical scheduling, rather it is intended to be used for things such as protocol timeouts.
      Parameters:
      task - the task.
      delay - the delay.
      units - the time units for the delay.
      Returns:
      a Future that completes when the task has run and can be used to cancel the execution.
      Throws:
      RejectedExecutionException - if this task cannot be accepted for execution
      NullPointerException - if task is null
    • isOpen

      @OverrideMustInvoke public boolean isOpen()
      Check if the hub is open.
      Returns:
      true if the hub is open.
    • close

      @OverrideMustInvoke public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • addInterestAccept

      public final void addInterestAccept(SelectionKey key)
      Reregister the provided key as interested in accepting connections.
      Parameters:
      key - the key.
    • removeInterestAccept

      public final void removeInterestAccept(SelectionKey key)
      Reregister the provided key as no longer interested in accepting connections.
      Parameters:
      key - the key.
    • addInterestConnect

      public final void addInterestConnect(SelectionKey key)
      Reregister the provided key as interested in the connection with a server being established.
      Parameters:
      key - the key.
    • removeInterestConnect

      public final void removeInterestConnect(SelectionKey key)
      Reregister the provided key as no longer interested in the connection with a server being established.
      Parameters:
      key - the key.
    • addInterestRead

      public final void addInterestRead(SelectionKey key)
      Reregister the provided key as interested in reading data.
      Parameters:
      key - the key.
    • removeInterestRead

      public final void removeInterestRead(SelectionKey key)
      Reregister the provided key as no longer interested in reading data.
      Parameters:
      key - the key.
    • addInterestWrite

      public final void addInterestWrite(SelectionKey key)
      Reregister the provided key as interested in writing data.
      Parameters:
      key - the key.
    • removeInterestWrite

      public final void removeInterestWrite(SelectionKey key)
      Reregister the provided key as no longer interested in writing data.
      Parameters:
      key - the key.
    • register

      public final void register(SelectableChannel channel, IOHubReadyListener listener, boolean accept, boolean connect, boolean read, boolean write, IOHubRegistrationCallback callback)
      Register the SelectableChannel for the requested operations using the supplied IOHubReadyListener, when the registration is complete the IOHubRegistrationCallback will be invoked.
      Parameters:
      channel - the SelectableChannel to register.
      listener - the IOHubReadyListener to call when the requested operations are available.
      accept - true to initially register for accepting connections from clients.
      connect - true to initially register for connection established with server.
      read - true to initially register for reading data.
      write - true to initially register for writing data.
      callback - the IOHubRegistrationCallback to notify on registration.
    • register

      public final Future<SelectionKey> register(SelectableChannel channel, IOHubReadyListener listener, boolean accept, boolean connect, boolean read, boolean write)
      Register the SelectableChannel for the requested operations using the supplied IOHubReadyListener.
      Parameters:
      channel - the SelectableChannel to register.
      listener - the IOHubReadyListener to call when the requested operations are available.
      accept - true to initially register for accepting connections from clients.
      connect - true to initially register for connection established with server.
      read - true to initially register for reading data.
      write - true to initially register for writing data.
      Returns:
      the Future for the SelectionKey.
    • unregister

      public final void unregister(SelectableChannel channel)
      Removes the SelectableChannel from the hub's Selector.
      Parameters:
      channel - the SelectableChannel to remove.
    • run

      @Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public final void run()
      Specified by:
      run in interface Runnable
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object