Class SelectorKernel
java.lang.Object
com.jme3.network.kernel.AbstractKernel
com.jme3.network.kernel.tcp.SelectorKernel
- All Implemented Interfaces:
Kernel
A Kernel implementation based on NIO selectors.
- Author:
- Paul Speed
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classThis class is purposely tucked neatly away because messing with the selector from other threads for any reason is very bad. -
Field Summary
Fields inherited from interface Kernel
EVENTS_PENDING -
Constructor Summary
ConstructorsConstructorDescriptionSelectorKernel(int port) Creates a selector kernel bound to the specified port on the wildcard address.SelectorKernel(InetAddress host, int port) Creates a selector kernel for the specified host and port.SelectorKernel(InetSocketAddress address) Creates a selector kernel for the specified socket address. -
Method Summary
Modifier and TypeMethodDescriptionprotected NioEndpointCreates and registers a new endpoint for the accepted channel.voidbroadcast(Filter<? super Endpoint> filter, ByteBuffer data, boolean reliable, boolean copy) Dispatches the data to all endpoints managed by the kernel that match the specified endpoint filter.protected voidCalled by the endpoints when they need to be closed.protected SelectorKernel.SelectorThreadCreates the selector thread implementation used by this kernel.voidInitializes the kernel and starts any internal processing.protected voidnewData(NioEndpoint p, SocketChannel c, ByteBuffer shared, int size) Queues newly read endpoint data as an envelope.protected voidRemoves bookkeeping for a closed endpoint.voidGracefully terminates the kernel and stops any internal daemon processing.protected voidUsed internally by the endpoints to wake up the selector when they have data to send.Methods inherited from class AbstractKernel
addEnvelope, addEvent, hasEnvelopes, nextEndpointId, nextEvent, read, reportError, wakeupReader
-
Constructor Details
-
SelectorKernel
Creates a selector kernel for the specified host and port.- Parameters:
host- the local bind addressport- the local bind port
-
SelectorKernel
Creates a selector kernel bound to the specified port on the wildcard address.- Parameters:
port- the local bind port- Throws:
IOException- if the address cannot be prepared
-
SelectorKernel
Creates a selector kernel for the specified socket address.- Parameters:
address- the local socket address
-
-
Method Details
-
createSelectorThread
Creates the selector thread implementation used by this kernel.- Returns:
- the selector thread
-
initialize
public void initialize()Description copied from interface:KernelInitializes the kernel and starts any internal processing. -
terminate
Description copied from interface:KernelGracefully terminates the kernel and stops any internal daemon processing. This method will not return until all internal threads have been shut down.- Throws:
InterruptedException- if the calling thread is interrupted while waiting
-
broadcast
public void broadcast(Filter<? super Endpoint> filter, ByteBuffer data, boolean reliable, boolean copy) Description copied from interface:KernelDispatches the data to all endpoints managed by the kernel that match the specified endpoint filter. If 'copy' is true then the implementation will copy the byte buffer before delivering it to endpoints. This allows the caller to reuse the data buffer. Though it is important that the buffer not be changed by another thread while this call is running. Only the bytes from data.position() to data.remaining() are sent.- Parameters:
filter- the endpoint filter used to select recipientsdata- the payload to sendreliable- true to use reliable transport semanticscopy- true to copy the buffer before dispatching it
-
addEndpoint
Creates and registers a new endpoint for the accepted channel.- Parameters:
c- the accepted socket channel- Returns:
- the created endpoint
-
removeEndpoint
Removes bookkeeping for a closed endpoint.- Parameters:
p- the endpoint being removedc- the underlying socket channel
-
closeEndpoint
Called by the endpoints when they need to be closed.- Parameters:
p- the endpoint to close- Throws:
IOException- if the endpoint cannot be closed cleanly
-
wakeupSelector
protected void wakeupSelector()Used internally by the endpoints to wake up the selector when they have data to send. -
newData
Queues newly read endpoint data as an envelope.- Parameters:
p- the source endpointc- the source socket channelshared- the shared read buffersize- the number of valid bytes inshared
-