Package org.jenkinsci.remoting.protocol
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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteBuffer
acquire(int size)
Borrow a buffer from the pool.void
addInterestAccept(SelectionKey key)
Reregister the provided key as interested in accepting connections.void
addInterestConnect(SelectionKey key)
Reregister the provided key as interested in the connection with a server being established.void
addInterestRead(SelectionKey key)
Reregister the provided key as interested in reading data.void
addInterestWrite(SelectionKey key)
Reregister the provided key as interested in writing data.void
close()
static IOHub
create(Executor executor)
Creates and starts a newIOHub
instance.boolean
equals(Object o)
void
execute(Runnable task)
Future<?>
executeLater(Runnable task, long delay, TimeUnit units)
Executes a task at a future point in time.void
executeOnSelector(Runnable task)
Executes the given task at some time in the future.Selector
getSelector()
Returns theSelector
.int
hashCode()
boolean
isOpen()
Check if the hub is open.Future<SelectionKey>
register(SelectableChannel channel, IOHubReadyListener listener, boolean accept, boolean connect, boolean read, boolean write)
Register theSelectableChannel
for the requested operations using the suppliedIOHubReadyListener
.void
register(SelectableChannel channel, IOHubReadyListener listener, boolean accept, boolean connect, boolean read, boolean write, IOHubRegistrationCallback callback)
Register theSelectableChannel
for the requested operations using the suppliedIOHubReadyListener
, when the registration is complete theIOHubRegistrationCallback
will be invoked.void
release(ByteBuffer buffer)
Returns a buffer to the pool.void
removeInterestAccept(SelectionKey key)
Reregister the provided key as no longer interested in accepting connections.void
removeInterestConnect(SelectionKey key)
Reregister the provided key as no longer interested in the connection with a server being established.void
removeInterestRead(SelectionKey key)
Reregister the provided key as no longer interested in reading data.void
removeInterestWrite(SelectionKey key)
Reregister the provided key as no longer interested in writing data.void
run()
String
toString()
void
unregister(SelectableChannel channel)
Removes theSelectableChannel
from the hub'sSelector
.
-
-
-
Method Detail
-
create
public static IOHub create(Executor executor) throws IOException
Creates and starts a newIOHub
instance.- Parameters:
executor
- theExecutor
to use for running tasks.- Returns:
- the new hub.
- Throws:
IOException
- if the hub'sSelector
cannot be opened.
-
acquire
public ByteBuffer acquire(int size)
Borrow a buffer from the pool.- Specified by:
acquire
in interfaceByteBufferPool
- 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 interfaceByteBufferPool
- Parameters:
buffer
- the buffer.
-
getSelector
@NonNull public final Selector getSelector()
Returns theSelector
.- Returns:
- the
Selector
-
execute
@OverrideMustInvoke public void execute(@NonNull Runnable task)
-
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 executionNullPointerException
- 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 executionNullPointerException
- 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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- 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 theSelectableChannel
for the requested operations using the suppliedIOHubReadyListener
, when the registration is complete theIOHubRegistrationCallback
will be invoked.- Parameters:
channel
- theSelectableChannel
to register.listener
- theIOHubReadyListener
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
- theIOHubRegistrationCallback
to notify on registration.
-
register
public final Future<SelectionKey> register(SelectableChannel channel, IOHubReadyListener listener, boolean accept, boolean connect, boolean read, boolean write)
Register theSelectableChannel
for the requested operations using the suppliedIOHubReadyListener
.- Parameters:
channel
- theSelectableChannel
to register.listener
- theIOHubReadyListener
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 theSelectionKey
.
-
unregister
public final void unregister(SelectableChannel channel)
Removes theSelectableChannel
from the hub'sSelector
.- Parameters:
channel
- theSelectableChannel
to remove.
-
run
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public final void run()
-
-