Class AbstractTransportServerThread<A extends Address, S extends AbstractSocketEntry<A>>
java.lang.Object
org.snmp4j.transport.AbstractTransportServerThread<A,S>
- Type Parameters:
A- the address type supported.S- the socket entry supported.
- All Implemented Interfaces:
Runnable, WorkerTask
- Direct Known Subclasses:
DefaultTcpTransportMapping.ServerThread, TLSTM.ServerThread
public abstract class AbstractTransportServerThread<A extends Address, S extends AbstractSocketEntry<A>>
extends Object
implements WorkerTask
The
AbstractTransportServerThread is a WorkerTask that serves server connections
using Java NIO for ConnectionOrientedTransportMappings.- Since:
- 3.7.0
- Author:
- Frank Fock
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CountDownLatchprotected Throwableprotected final LinkedList<S> protected Selectorprotected final Aprotected booleanA flag indicating whether the server thread should initiate the shutdown process.protected ServerSocketChannelprotected booleanA flag indicating whether the server thread should stop its execution.protected final AbstractConnectionOrientedTransportMapping<A, S> -
Constructor Summary
ConstructorsConstructorDescriptionAbstractTransportServerThread(AbstractConnectionOrientedTransportMapping<A, S> transportMapping, A serverAddress) Create a server thread for anAbstractConnectionOrientedTransportMappingon the specified server address. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()protected voidcloseChannel(SelectableChannel channel) protected voidconnectChannel(SelectionKey sk, A incomingAddress) protected voidconnectSocketToSendMessage(A address, byte[] message, SocketChannel socketChannel, S entry, Map<A, S> sockets) protected abstract AcreateIncomingAddress(SocketChannel socket) protected abstract ScreateSocketEntry(A address, SocketChannel socketChannel, boolean useClientMode, TransportStateReference tmStateReference) protected voidDo the NIO server processing.protected voidvoidInterrupts this task.voidjoin()Waits until this task has been finished.protected abstract SocketChannelopenSocketChannel(ProtocolFamily family) protected abstract voidprotected abstract booleanreadMessage(SelectionKey sk, SocketChannel readChannel, A incomingAddress, S socketEntry) abstract SremoveSocketEntry(A incomingAddress) abstract voidrun()voidsendMessage(A address, byte[] message, TransportStateReference tmStateReference, Map<A, S> sockets) Send a message to the specified address and update to specified socket entry map if a new client connection needs to be created for that.voidTheWorkerPoolmight call this method to hint the activeWorkTaskinstance to complete execution as soon as possible.protected AwriteData(SelectionKey sk, A incomingAddress) protected voidwriteMessage(S entry, SocketChannel sc) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface WorkerTask
join, shutdown
-
Field Details
-
transportMapping
protected final AbstractConnectionOrientedTransportMapping<A extends Address, S extends AbstractSocketEntry<A>> transportMapping -
serverAddress
-
stop
protected volatile boolean stopA flag indicating whether the server thread should stop its execution. This variable is checked periodically by the thread's main loop to determine if it should terminate its operation. It is marked as volatile to ensure visibility across threads. -
shutdown
protected volatile boolean shutdownA flag indicating whether the server thread should initiate the shutdown process. When set totrue, this variable signals the thread to terminate its execution gracefully, allowing for any necessary cleanup operations to complete.This variable is declared as
volatileto ensure visibility of its value across multiple threads, enabling proper coordination between the server thread and the controlling threads that trigger or monitor the shutdown process.- Since:
- 3.10.0
-
selector
-
pending
-
lastError
-
ssc
-
exitLatch
-
-
Constructor Details
-
AbstractTransportServerThread
public AbstractTransportServerThread(AbstractConnectionOrientedTransportMapping<A, S> transportMapping, A serverAddress) throws IOExceptionCreate a server thread for anAbstractConnectionOrientedTransportMappingon the specified server address.- Parameters:
transportMapping- the transport mapping using this thread to serve server messages.serverAddress- the listen address for the server.- Throws:
IOException- if initializing NIO selector or listen address socket channel fails.
-
-
Method Details
-
connectSocketToSendMessage
protected void connectSocketToSendMessage(A address, byte[] message, SocketChannel socketChannel, S entry, Map<A, S> sockets) throws ClosedChannelException- Throws:
ClosedChannelException
-
createSocketEntry
protected abstract S createSocketEntry(A address, SocketChannel socketChannel, boolean useClientMode, TransportStateReference tmStateReference) -
sendMessage
public void sendMessage(A address, byte[] message, TransportStateReference tmStateReference, Map<A, S> sockets) throws IOExceptionSend a message to the specified address and update to specified socket entry map if a new client connection needs to be created for that.- Parameters:
address- the remote address to send the message to.message- the message to be sent.tmStateReference- transport mapping state reference needed by transport mappings supporting message protocolsMPv3or higher.sockets- the client connections available and to be updated.- Throws:
IOException- if sending the message failed because of IO exceptions.
-
getSelector
-
openSocketChannel
- Throws:
IOException
-
run
-
readMessage
protected abstract boolean readMessage(SelectionKey sk, SocketChannel readChannel, A incomingAddress, S socketEntry) throws IOException - Throws:
IOException
-
doServer
-
fireIncrementCounterSessionClose
protected void fireIncrementCounterSessionClose() -
processQueues
protected abstract void processQueues() -
removeSocketEntry
-
connectChannel
-
writeData
-
createIncomingAddress
- Throws:
IOException
-
closeChannel
-
writeMessage
- Throws:
IOException
-
close
public void close() -
terminate
public void terminate()Description copied from interface:WorkerTaskTheWorkerPoolmight call this method to hint the activeWorkTaskinstance to complete execution as soon as possible.- Specified by:
terminatein interfaceWorkerTask
-
join
Description copied from interface:WorkerTaskWaits until this task has been finished.- Specified by:
joinin interfaceWorkerTask- Throws:
InterruptedException- if the join has been interrupted by another thread.
-
interrupt
public void interrupt()Description copied from interface:WorkerTaskInterrupts this task.- Specified by:
interruptin interfaceWorkerTask- See Also:
-