Class AbstractTransportMapping<A extends Address>
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>
The
AbstractTransportMapping provides an abstract
implementation for the message dispatcher list and the maximum inbound
message size.- Version:
- 3.12.0
- Author:
- Frank Fock
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected longprotected WorkerTaskprotected intprotected CommonTimerprotected List<TransportAuditListener> protected List<TransportListener> protected List<TransportStateListener> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds aTransportAuditListenerthat is able to control (allow/deny) and audit the communication of this transport mapping.voidAdds a transport listener to the transport.voidAdd aTransportStateListenerto getTransportStateEvents if the state of this transport mapping changes.abstract voidclose()Closes this resource and releases any associated system resources.protected voidprotected voidfireProcessMessage(A address, ByteBuffer buf, TransportStateReference tmStateReference) Processes an incoming message received from the specified address and notifies the registered transport listeners.protected booleanNotifies all registeredTransportAuditListeners about the suppliedTransportAuditEvent.protected AReturns the listen address of this transport mapping for use inTransportAuditEvents without propagating exceptions.abstract AReturns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.Gets theWorkerTaskthat is responsible for receiving new messages.intGets the maximum length of an incoming message that can be successfully processed by this transport mapping implementation.intReturns the priority of the internal listen thread.Gets theCommonTimerthat controls socket cleanup operations.Gets the primaryAddressclass that is supported by this transport mapping.Gets an unmodifiable set of the suspended addresses of thisReturns the name of the listen thread.protected voidhandleDroppedMessageToSend(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.booleanReturnstrueif asynchronous (multi-threaded) message processing may be implemented.booleanReturnstrueif the transport mapping is listening for incoming messages.booleanChecks whether at least oneTransportAuditListeneris registered with this transport mapping.abstract voidlisten()Starts listening for incoming connections or events.voidRemoves all registeredTransportAuditListeners.voidvoidRemoves a previously addedTransportAuditListener.voidRemoves a transport listener.voidRemove aTransportStateListenerfrom this transport mapping.booleanresumeAddress(A addressToResumeSending) Resume sending of messages to the specified address.abstract voidsendMessage(A address, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) Sends a message to the supplied address using this transport.voidsetAsyncMsgProcessingSupported(boolean asyncMsgProcessingSupported) Specifies whether this transport mapping has to support asynchronous messages processing or not.voidsetPriority(int newPriority) Changes the priority of the listen thread for this UDP transport mapping.voidsetThreadName(String name) Sets the name of the listen thread for this UDP transport mapping.voidsuspendAddress(A addressToSuspendSending) Suspend sending of messages to the specified address, regardless if a connection is already established or not.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface TransportMapping
getMaxOutboundMessageSize, getSupportedAddressClasses, getSupportedTransportType, isAddressSupported, isAddressSupported
-
Field Details
-
transportListener
-
transportAuditListeners
-
maxInboundMessageSize
protected int maxInboundMessageSize -
asyncMsgProcessingSupported
protected boolean asyncMsgProcessingSupported -
suspendedAddresses
-
listenWorkerTask
-
transportStateListeners
-
connectionTimeout
protected long connectionTimeout -
socketCleaner
-
-
Constructor Details
-
AbstractTransportMapping
protected AbstractTransportMapping()
-
-
Method Details
-
getSupportedAddressClass
Description copied from interface:TransportMappingGets the primaryAddressclass that is supported by this transport mapping.- Specified by:
getSupportedAddressClassin interfaceTransportMapping<A extends Address>- Returns:
- a subclass of
Address.
-
isListening
public boolean isListening()Description copied from interface:TransportMappingReturnstrueif 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:
isListeningin interfaceTransportMapping<A extends Address>- Returns:
trueif 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:
sendMessagein interfaceTransportMapping<A extends Address>- Parameters:
address- anAddressinstance 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
Description copied from interface:TransportMappingAdds a transport listener to the transport. Normally, at least one transport listener needs to be added to process incoming messages.- Specified by:
addTransportListenerin interfaceTransportMapping<A extends Address>- Parameters:
l- aTransportListenerinstance.
-
removeTransportListener
Description copied from interface:TransportMappingRemoves a transport listener. Incoming messages will no longer be propagated to the suppliedTransportListener.- Specified by:
removeTransportListenerin interfaceTransportMapping<A extends Address>- Parameters:
l- aTransportListenerinstance.
-
removeAllTransportListeners
public void removeAllTransportListeners() -
addTransportAuditListener
Adds aTransportAuditListenerthat is able to control (allow/deny) and audit the communication of this transport mapping. If the same listener is already registered, it is not added again.- Parameters:
l- theTransportAuditListenerto add.- Since:
- 3.12.0
-
removeTransportAuditListener
Removes a previously addedTransportAuditListener.- Parameters:
l- theTransportAuditListenerto remove.- Since:
- 3.12.0
-
removeAllTransportAuditListeners
public void removeAllTransportAuditListeners()Removes all registeredTransportAuditListeners.- Since:
- 3.12.0
-
isTransportAudit
public boolean isTransportAudit()Checks whether at least oneTransportAuditListeneris registered with this transport mapping. This method can be used to quickly check whether building and firing aTransportAuditEventis necessary at all, in order to avoid the associated overhead when no audit listener is registered.- Returns:
trueif at least oneTransportAuditListeneris registered,falseotherwise.- Since:
- 3.12.0
-
getAuditListenAddress
Returns the listen address of this transport mapping for use inTransportAuditEvents without propagating exceptions. Some transport mappings throw an exception fromgetListenAddress()when they are not listening yet; this helper returnsnullin that case so that audit event creation never fails.- Returns:
- the listen address or
nullif it cannot be determined. - Since:
- 3.12.0
-
fireTransportAuditEvent
Notifies all registeredTransportAuditListeners about the suppliedTransportAuditEvent. All listeners are always notified (so that they can audit the event), even if one of them denies the operation.- Parameters:
event- theTransportAuditEventto fire.- Returns:
trueif the operation described byeventis permitted (i.e. no listener denied it or no listener is registered at all),falseif at least one listener denied the operation. ForTransportAuditEvent.TransportOperation.CLOSEevents the result should be ignored by the caller.- Since:
- 3.12.0
-
fireProcessMessage
protected void fireProcessMessage(A address, ByteBuffer buf, TransportStateReference tmStateReference) Processes an incoming message received from the specified address and notifies the registered transport listeners. If transport auditing is enabled, an audit event will be fired before processing the message.- Parameters:
address- the address of the sender from which the message is receivedbuf- theByteBuffercontaining the received messagetmStateReference- a reference to the transport state associated with this message
-
close
Closes this resource and releases any associated system resources. If the resource is already closed, invoking this method has no effect.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceTransportMapping<A extends Address>- Throws:
IOException- if an I/O error occurs while closing the resource
-
suspendAddress
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
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:
trueif the specified address was previously suspended and is now resumed to allow sending messages,falseotherwise.- 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- anAddressinstance 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
Starts listening for incoming connections or events. This method is intended to be implemented by subclasses to define the specific behavior required for listening operations in various contexts (e.g., network connections, event streams).- Specified by:
listenin interfaceTransportMapping<A extends Address>- Throws:
IOException- if an I/O error occurs during the listening process.
-
getSocketCleaner
Gets theCommonTimerthat controls socket cleanup operations.- Returns:
- a socket cleaner timer.
- Since:
- 3.0
-
getMaxInboundMessageSize
public int getMaxInboundMessageSize()Description copied from interface:TransportMappingGets the maximum length of an incoming message that can be successfully processed by this transport mapping implementation.- Specified by:
getMaxInboundMessageSizein interfaceTransportMapping<A extends Address>- Returns:
- an integer > 484.
-
isAsyncMsgProcessingSupported
public boolean isAsyncMsgProcessingSupported()Returnstrueif asynchronous (multi-threaded) message processing may be implemented. The default istrue.- Returns:
- if
falseis 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- iffalsetheMessageDispatcher.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. Iftruethe 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:
-
getPriority
public int getPriority()Returns the priority of the internal listen thread.- Returns:
- a value between
Thread.MIN_PRIORITYandThread.MAX_PRIORITY. - Since:
- 3.6.0
-
setThreadName
-
getThreadName
Returns the name of the listen thread.- Returns:
- the thread name if in listening mode, otherwise
null. - Since:
- 3.6.0
-
addTransportStateListener
Add aTransportStateListenerto getTransportStateEvents if the state of this transport mapping changes.- Parameters:
l- the listener callback interface.- Since:
- 3.7.0
-
removeTransportStateListener
Remove aTransportStateListenerfrom this transport mapping.- Parameters:
l- the listener callback interface to be removed.- Since:
- 3.7.0
-
getListenAddress
Description copied from interface:TransportMappingReturns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.- Specified by:
getListenAddressin interfaceTransportMapping<A extends Address>- Returns:
- the address for incoming packets or
nullthis transport mapping is not configured to listen for incoming packets.
-
fireConnectionStateChanged
-
getListenWorkerTask
Gets theWorkerTaskthat is responsible for receiving new messages.- Returns:
- a
WorkerTaskinstance which is most likely aDefaultThreadFactory.WorkerThread. - Since:
- 3.7.0
-
getSuspendedAddresses
-