Class KernelAdapter
- All Implemented Interfaces:
Runnable
This adapter assumes a simple protocol where two bytes define a (short) object size with the object data to follow. Note: this limits the size of serialized objects to 32676 bytes... even though, for example, datagram packets can hold twice that. :P
- Author:
- Paul Speed
-
Nested Class Summary
Nested classes/interfaces inherited from class Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
Fields inherited from class Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
ConstructorsConstructorDescriptionKernelAdapter(DefaultServer server, Kernel kernel, MessageProtocol protocol, MessageListener<HostedConnection> messageDispatcher, boolean reliable) Creates an adapter for a kernel used byDefaultServer. -
Method Summary
Modifier and TypeMethodDescriptionvoidbroadcast(Filter<? super Endpoint> filter, ByteBuffer data, boolean reliable, boolean copy) Broadcasts raw bytes through the wrapped kernel.voidclose()Closes the adapter and terminates the wrapped kernel.protected voidHandles endpoint closure and clears any buffered message state.protected voidcreateAndDispatch(EndpointEvent event) Handles a kernel endpoint event.protected voidDecodes an envelope into messages and dispatches them.protected voidNote on threading for those writing their own server or adapter implementations.protected voidDrains and processes all currently queued kernel events.protected HostedConnectionResolves a hosted connection from an endpoint.Returns the wrapped kernel.protected MessageBufferReturns the message buffer used to accumulate bytes for an endpoint.voidInitializes the wrapped kernel.protected voidreportError(Endpoint p, Object context, Exception e) Reports an adapter or kernel error and closes the endpoint when possible.voidrun()Methods inherited from class Thread
activeCount, checkAccess, clone, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, threadId, toString, yield
-
Constructor Details
-
KernelAdapter
public KernelAdapter(DefaultServer server, Kernel kernel, MessageProtocol protocol, MessageListener<HostedConnection> messageDispatcher, boolean reliable) Creates an adapter for a kernel used byDefaultServer.- Parameters:
server- the owning serverkernel- the kernel to wrapprotocol- the message protocol used to decode envelopesmessageDispatcher- the listener that receives decoded messagesreliable- true if this adapter represents a reliable channel
-
-
Method Details
-
getKernel
-
initialize
public void initialize()Initializes the wrapped kernel. -
broadcast
public void broadcast(Filter<? super Endpoint> filter, ByteBuffer data, boolean reliable, boolean copy) Broadcasts raw bytes through the wrapped kernel.- Parameters:
filter- the endpoint filter, or null to target all endpointsdata- the bytes to broadcastreliable- true to request reliable transportcopy- true to copy the buffer before dispatch
-
close
Closes the adapter and terminates the wrapped kernel.- Throws:
InterruptedException- if interrupted while waiting for shutdown
-
reportError
-
getConnection
Resolves a hosted connection from an endpoint.- Parameters:
p- the endpoint to resolve- Returns:
- the hosted connection, or null if none exists
-
connectionClosed
Handles endpoint closure and clears any buffered message state.- Parameters:
p- the closed endpoint
-
dispatch
Note on threading for those writing their own server or adapter implementations. The rule that a single connection be processed by only one thread at a time is more about ensuring that the messages are delivered in the order that they are received than for any user-code safety. 99% of the time the user code should be writing for multithreaded access anyway.The issue with the messages is that if an implementation is using a general thread pool then it would be possible for a naive implementation to have one thread grab an Envelope from connection 1's and another grab the next Envelope. Since an Envelope may contain several messages, delivering the second thread's messages before or during the first's would be really confusing and hard to code for in user code.
And that's why this note is here. DefaultServer does a rudimentary per-connection locking but it couldn't possibly guard against out of order Envelope processing.
- Parameters:
p- the endpoint that sent the messagem- the decoded message
-
getMessageBuffer
Returns the message buffer used to accumulate bytes for an endpoint.- Parameters:
p- the endpoint supplying bytes- Returns:
- the message buffer for that endpoint
-
createAndDispatch
Decodes an envelope into messages and dispatches them.- Parameters:
env- the received envelope
-
createAndDispatch
Handles a kernel endpoint event.- Parameters:
event- the endpoint event
-
flushEvents
protected void flushEvents()Drains and processes all currently queued kernel events. -
run
-