Module org.snmp4j
Package org.snmp4j.transport
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
public abstract class AbstractTransportServerThread<A extends Address,S extends AbstractSocketEntry<A>> extends Object implements WorkerTask
TheAbstractTransportServerThread
is aWorkerTask
that serves server connections using Java NIO forConnectionOrientedTransportMapping
s.- Since:
- 3.7.0
- Author:
- Frank Fock
-
-
Field Summary
Fields Modifier and Type Field Description protected Throwable
lastError
protected LinkedList<S>
pending
protected Selector
selector
protected A
serverAddress
protected ServerSocketChannel
ssc
protected boolean
stop
protected AbstractConnectionOrientedTransportMapping<A,S>
transportMapping
-
Constructor Summary
Constructors Constructor Description AbstractTransportServerThread(AbstractConnectionOrientedTransportMapping<A,S> transportMapping, A serverAddress)
Create a server thread for anAbstractConnectionOrientedTransportMapping
on the specified server address.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
protected void
closeChannel(SelectableChannel channel)
protected void
connectChannel(SelectionKey sk, A incomingAddress)
protected void
connectSocketToSendMessage(A address, byte[] message, SocketChannel socketChannel, S entry, Map<A,S> sockets)
protected abstract A
createIncomingAddress(SocketChannel socket)
protected abstract S
createSocketEntry(A address, SocketChannel socketChannel, boolean useClientMode, TransportStateReference tmStateReference)
protected void
doServer(Map<A,S> sockets)
Do the NIO server processing.protected void
fireIncrementCounterSessionClose()
Selector
getSelector()
void
interrupt()
Interrupts this task.void
join()
Waits until this task has been finished.protected abstract SocketChannel
openSocketChannel(ProtocolFamily family)
protected abstract void
processQueues()
protected abstract boolean
readMessage(SelectionKey sk, SocketChannel readChannel, A incomingAddress, S socketEntry)
abstract S
removeSocketEntry(A incomingAddress)
abstract void
run()
void
sendMessage(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.void
terminate()
TheWorkerPool
might call this method to hint the activeWorkTask
instance to complete execution as soon as possible.protected A
writeData(SelectionKey sk, A incomingAddress)
protected void
writeMessage(S entry, SocketChannel sc)
-
-
-
Field Detail
-
transportMapping
protected final AbstractConnectionOrientedTransportMapping<A extends Address,S extends AbstractSocketEntry<A>> transportMapping
-
stop
protected volatile boolean stop
-
selector
protected Selector selector
-
pending
protected final LinkedList<S extends AbstractSocketEntry<A>> pending
-
lastError
protected Throwable lastError
-
ssc
protected ServerSocketChannel ssc
-
-
Constructor Detail
-
AbstractTransportServerThread
public AbstractTransportServerThread(AbstractConnectionOrientedTransportMapping<A,S> transportMapping, A serverAddress) throws IOException
Create a server thread for anAbstractConnectionOrientedTransportMapping
on 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 Detail
-
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 IOException
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.- 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 protocolsMPv3
or higher.sockets
- the client connections available and to be updated.- Throws:
IOException
- if sending the message failed because of IO exceptions.
-
getSelector
public Selector getSelector()
-
openSocketChannel
protected abstract SocketChannel openSocketChannel(ProtocolFamily family) throws IOException
- Throws:
IOException
-
readMessage
protected abstract boolean readMessage(SelectionKey sk, SocketChannel readChannel, A incomingAddress, S socketEntry) throws IOException
- Throws:
IOException
-
doServer
protected void doServer(Map<A,S> sockets)
Do the NIO server processing.- Parameters:
sockets
-
-
fireIncrementCounterSessionClose
protected void fireIncrementCounterSessionClose()
-
processQueues
protected abstract void processQueues()
-
connectChannel
protected void connectChannel(SelectionKey sk, A incomingAddress)
-
writeData
protected A writeData(SelectionKey sk, A incomingAddress)
-
createIncomingAddress
protected abstract A createIncomingAddress(SocketChannel socket) throws IOException
- Throws:
IOException
-
closeChannel
protected void closeChannel(SelectableChannel channel)
-
writeMessage
protected void writeMessage(S entry, SocketChannel sc) throws IOException
- Throws:
IOException
-
close
public void close()
-
terminate
public void terminate()
Description copied from interface:WorkerTask
TheWorkerPool
might call this method to hint the activeWorkTask
instance to complete execution as soon as possible.- Specified by:
terminate
in interfaceWorkerTask
-
join
public void join()
Description copied from interface:WorkerTask
Waits until this task has been finished.- Specified by:
join
in interfaceWorkerTask
-
interrupt
public void interrupt()
Description copied from interface:WorkerTask
Interrupts this task.- Specified by:
interrupt
in interfaceWorkerTask
- See Also:
Thread.interrupt()
-
-