- java.lang.Object
-
- org.snmp4j.transport.AbstractTransportMapping<A>
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,TransportMapping<A>
- Direct Known Subclasses:
AbstractConnectionOrientedTransportMapping
,DummyTransport
,DummyTransport.DummyTransportResponder
,UdpTransportMapping
public abstract class AbstractTransportMapping<A extends Address> extends Object implements TransportMapping<A>
TheAbstractTransportMapping
provides an abstract implementation for the message dispatcher list and the maximum inbound message size.- Version:
- 3.6.0
- Author:
- Frank Fock
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
asyncMsgProcessingSupported
protected long
connectionTimeout
protected WorkerTask
listenWorkerTask
protected int
maxInboundMessageSize
protected CommonTimer
socketCleaner
protected Set<A>
suspendedAddresses
protected List<TransportListener>
transportListener
protected List<TransportStateListener>
transportStateListeners
-
Constructor Summary
Constructors Constructor Description AbstractTransportMapping()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addTransportListener(TransportListener l)
Adds a transport listener to the transport.void
addTransportStateListener(TransportStateListener l)
Add aTransportStateListener
to getTransportStateEvent
s if the state of this transport mapping changes.abstract void
close()
Closes the transport an releases all bound resources synchronously.protected void
fireConnectionStateChanged(TransportStateEvent change)
protected void
fireProcessMessage(A address, ByteBuffer buf, TransportStateReference tmStateReference)
abstract A
getListenAddress()
Returns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.WorkerTask
getListenWorkerTask()
Gets theWorkerTask
that is responsible for receiving new messages.int
getMaxInboundMessageSize()
Gets the maximum length of an incoming message that can be successfully processed by this transport mapping implementation.int
getPriority()
Returns the priority of the internal listen thread.CommonTimer
getSocketCleaner()
Gets theCommonTimer
that controls socket cleanup operations.abstract Class<? extends Address>
getSupportedAddressClass()
Gets the primaryAddress
class that is supported by this transport mapping.Set<A>
getSuspendedAddresses()
Gets a unmodifiable set of the suspended addresses of thisString
getThreadName()
Returns the name of the listen thread.protected void
handleDroppedMessageToSend(A address, byte[] message, TransportStateReference transportStateReference, long timeoutMillis, int maxRetries)
Handle a message that could not be send to the specified address, because there is no server socket for receiving responses.boolean
isAsyncMsgProcessingSupported()
Returnstrue
if asynchronous (multi-threaded) message processing may be implemented.boolean
isListening()
Returnstrue
if the transport mapping is listening for incoming messages.abstract void
listen()
Listen for incoming messages.void
removeAllTransportListeners()
void
removeTransportListener(TransportListener l)
Removes a transport listener.void
removeTransportStateListener(TransportStateListener l)
Remove aTransportStateListener
from this transport mapping.boolean
resumeAddress(A addressToResumeSending)
Resume sending of messages to the specified address.abstract void
sendMessage(A address, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries)
Sends a message to the supplied address using this transport.void
setAsyncMsgProcessingSupported(boolean asyncMsgProcessingSupported)
Specifies whether this transport mapping has to support asynchronous messages processing or not.void
setPriority(int newPriority)
Changes the priority of the listen thread for this UDP transport mapping.void
setThreadName(String name)
Sets the name of the listen thread for this UDP transport mapping.void
suspendAddress(A addressToSuspendSending)
Suspend sending of messages to the specified address, regardless if a connection is already established or not.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.snmp4j.TransportMapping
getSupportedAddressClasses, getSupportedTransportType, isAddressSupported, isAddressSupported
-
-
-
-
Field Detail
-
transportListener
protected List<TransportListener> transportListener
-
maxInboundMessageSize
protected int maxInboundMessageSize
-
asyncMsgProcessingSupported
protected boolean asyncMsgProcessingSupported
-
listenWorkerTask
protected WorkerTask listenWorkerTask
-
transportStateListeners
protected transient List<TransportStateListener> transportStateListeners
-
connectionTimeout
protected long connectionTimeout
-
socketCleaner
protected CommonTimer socketCleaner
-
-
Method Detail
-
getSupportedAddressClass
public abstract Class<? extends Address> getSupportedAddressClass()
Description copied from interface:TransportMapping
Gets the primaryAddress
class that is supported by this transport mapping.- Specified by:
getSupportedAddressClass
in interfaceTransportMapping<A extends Address>
- Returns:
- a subclass of
Address
.
-
isListening
public boolean isListening()
Description copied from interface:TransportMapping
Returnstrue
if the transport mapping is listening for incoming messages. For connection oriented transport mappings this is a prerequisite to be able to send SNMP messages. For connectionless transport mappings it is a prerequisite to be able to receive responses.- Specified by:
isListening
in interfaceTransportMapping<A extends Address>
- Returns:
true
if this transport mapping is listening for messages.
-
sendMessage
public abstract void sendMessage(A address, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) throws IOException
Sends a message to the supplied address using this transport. If the target address has been suspended, then instead actually sending the message on the wire, the methodhandleDroppedMessageToSend(Address, byte[], TransportStateReference, long, int)
will be called. To stop suspending of a target address, callresumeAddress(Address)
for that address.- Specified by:
sendMessage
in interfaceTransportMapping<A extends Address>
- Parameters:
address
- anAddress
instance denoting the target address.message
- the whole message as an array of bytes.tmStateReference
- the (optional) transport model state reference as defined by RFC 5590 section 6.1.timeoutMillis
- maximum number of milliseconds the connection creation might take (if connection based).maxRetries
- maximum retries during connection creation.- Throws:
IOException
- if any underlying IO operation fails.
-
addTransportListener
public void addTransportListener(TransportListener l)
Description copied from interface:TransportMapping
Adds a transport listener to the transport. Normally, at least one transport listener needs to be added to process incoming messages.- Specified by:
addTransportListener
in interfaceTransportMapping<A extends Address>
- Parameters:
l
- aTransportListener
instance.
-
removeTransportListener
public void removeTransportListener(TransportListener l)
Description copied from interface:TransportMapping
Removes a transport listener. Incoming messages will no longer be propagated to the suppliedTransportListener
.- Specified by:
removeTransportListener
in interfaceTransportMapping<A extends Address>
- Parameters:
l
- aTransportListener
instance.
-
removeAllTransportListeners
public void removeAllTransportListeners()
-
fireProcessMessage
protected void fireProcessMessage(A address, ByteBuffer buf, TransportStateReference tmStateReference)
-
close
public abstract void close() throws IOException
Description copied from interface:TransportMapping
Closes the transport an releases all bound resources synchronously.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceTransportMapping<A extends Address>
- Throws:
IOException
- if any IO operation for the close fails.
-
suspendAddress
public void suspendAddress(A addressToSuspendSending)
Suspend sending of messages to the specified address, regardless if a connection is already established or not. To be able to send messages again to the specified address usingsendMessage(Address, byte[], TransportStateReference, long, int)
, callresumeAddress(Address)
.- Parameters:
addressToSuspendSending
- an arbitrary remote address for which any messages send bysendMessage(Address, byte[], TransportStateReference, long, int)
should be dropped before sending and reopening a connection to that address.- Since:
- 3.4.4
-
resumeAddress
public boolean resumeAddress(A addressToResumeSending)
Resume sending of messages to the specified address.- Parameters:
addressToResumeSending
- an arbitrary remote address for which any messages send bysendMessage(Address, byte[], TransportStateReference, long, int)
should be dropped before sending and reopening a connection to that address.- Returns:
true
if the specified address was previously suspended and is now resumed to allow sending messages,false
otherwise.- Since:
- 3.4.4
-
handleDroppedMessageToSend
protected void handleDroppedMessageToSend(A address, byte[] message, TransportStateReference transportStateReference, long timeoutMillis, int maxRetries)
Handle a message that could not be send to the specified address, because there is no server socket for receiving responses.- Parameters:
address
- anAddress
instance denoting the target address.message
- the whole message as an array of bytes.transportStateReference
- the (optional) transport model state reference as defined by RFC 5590 section 6.1.timeoutMillis
- maximum number of milliseconds the connection creation might take (if connection based).maxRetries
- maximum retries during connection creation.- Since:
- 3.4.4
-
listen
public abstract void listen() throws IOException
Description copied from interface:TransportMapping
Listen for incoming messages. For connection oriented transports, this method needs to be called beforeTransportMapping.sendMessage(A, byte[], org.snmp4j.TransportStateReference, long, int)
is called for the first time.- Specified by:
listen
in interfaceTransportMapping<A extends Address>
- Throws:
IOException
- if an IO operation exception occurs while starting the listener.
-
getSocketCleaner
public CommonTimer getSocketCleaner()
Gets theCommonTimer
that controls socket cleanup operations.- Returns:
- a socket cleaner timer.
- Since:
- 3.0
-
getMaxInboundMessageSize
public int getMaxInboundMessageSize()
Description copied from interface:TransportMapping
Gets the maximum length of an incoming message that can be successfully processed by this transport mapping implementation.- Specified by:
getMaxInboundMessageSize
in interfaceTransportMapping<A extends Address>
- Returns:
- an integer > 484.
-
isAsyncMsgProcessingSupported
public boolean isAsyncMsgProcessingSupported()
Returnstrue
if asynchronous (multi-threaded) message processing may be implemented. The default istrue
.- Returns:
- if
false
is returned theMessageDispatcher.processMessage(org.snmp4j.TransportMapping, org.snmp4j.smi.Address, java.nio.ByteBuffer, org.snmp4j.TransportStateReference)
method must not return before the message has been entirely processed.
-
setAsyncMsgProcessingSupported
public void setAsyncMsgProcessingSupported(boolean asyncMsgProcessingSupported)
Specifies whether this transport mapping has to support asynchronous messages processing or not.- Parameters:
asyncMsgProcessingSupported
- iffalse
theMessageDispatcher.processMessage(org.snmp4j.TransportMapping, org.snmp4j.smi.Address, java.nio.ByteBuffer, org.snmp4j.TransportStateReference)
method must not return before the message has been entirely processed, because the incoming message buffer is not copied before the message is being processed. Iftrue
the message buffer is copied for each call, so that the message processing can be implemented asynchronously.
-
setPriority
public void setPriority(int newPriority)
Changes the priority of the listen thread for this UDP transport mapping. This method has no effect, if called beforelisten()
has been called for this transport mapping.- Parameters:
newPriority
- the new priority.- Since:
- 3.6.0
- See Also:
Thread.setPriority(int)
-
getPriority
public int getPriority()
Returns the priority of the internal listen thread.- Returns:
- a value between
Thread.MIN_PRIORITY
andThread.MAX_PRIORITY
. - Since:
- 3.6.0
-
setThreadName
public void setThreadName(String name)
Sets the name of the listen thread for this UDP transport mapping. This method has no effect, if called beforelisten()
has been called for this transport mapping.- Parameters:
name
- the new thread name.- Since:
- 3.6.0
-
getThreadName
public String getThreadName()
Returns the name of the listen thread.- Returns:
- the thread name if in listening mode, otherwise
null
. - Since:
- 3.6.0
-
addTransportStateListener
public void addTransportStateListener(TransportStateListener l)
Add aTransportStateListener
to getTransportStateEvent
s if the state of this transport mapping changes.- Parameters:
l
- the listener callback interface.- Since:
- 3.7.0
-
removeTransportStateListener
public void removeTransportStateListener(TransportStateListener l)
Remove aTransportStateListener
from this transport mapping.- Parameters:
l
- the listener callback interface to be removed.- Since:
- 3.7.0
-
getListenAddress
public abstract A getListenAddress()
Description copied from interface:TransportMapping
Returns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.- Specified by:
getListenAddress
in interfaceTransportMapping<A extends Address>
- Returns:
- the address for incoming packets or
null
this transport mapping is not configured to listen for incoming packets.
-
fireConnectionStateChanged
protected void fireConnectionStateChanged(TransportStateEvent change)
-
getListenWorkerTask
public WorkerTask getListenWorkerTask()
Gets theWorkerTask
that is responsible for receiving new messages.- Returns:
- a
WorkerTask
instance which is most likely aDefaultThreadFactory.WorkerThread
. - Since:
- 3.7.0
-
-