java.lang.Object
org.snmp4j.MessageDispatcherImpl
- All Implemented Interfaces:
MessageDispatcher
,TransportListener
The
MessageDispatcherImpl
decodes and dispatches incoming
messages using MessageProcessingModel
instances and encodes
and sends outgoing messages using an appropriate TransportMapping
instances.
The method processMessage(org.snmp4j.TransportMapping<? super A>, A, java.nio.ByteBuffer, org.snmp4j.TransportStateReference)
will be called from a
TransportMapping
whereas the method sendPdu(org.snmp4j.Target<A>, org.snmp4j.PDU, boolean)
will be
called by the application.
- Version:
- 3.0.3
- Author:
- Frank Fock
- See Also:
-
Constructor Summary
ConstructorDescriptionDefault constructor creates a message dispatcher without any associated message processing models. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a listener for authentication failure events caused by unauthenticated incoming messages.void
Adds aCommandResponder
instance to the message dispatcher.void
addCounterListener
(CounterListener counterListener) Adds aCounterListener
.void
Adds a message processing model to this message dispatcher.void
addTransportMapping
(TransportMapping<?> transport) Adds a transport mapping.protected void
checkOutgoingMsg
(Address transportAddress, int messageProcessingModel, PDU pdu) Checks outgoing messages for consistency between PDU and target used.protected void
configureAuthoritativeEngineID
(Target<?> target, MessageProcessingModel mp) protected PduHandle
protected <A extends Address>
voiddispatchMessage
(TransportMapping<? super A> sourceTransport, MessageProcessingModel mp, A incomingAddress, BERInputStream wholeMessage, TransportStateReference tmStateReference) Actually decodes and dispatches an incoming SNMP message using the supplied message processing model.protected void
Fires anAuthenticationFailureEvent
to all registered listeners.protected void
fireIncrementCounter
(CounterEvent event) Fires a counter incrementation event.protected void
Fires aCommandResponderEvent
.getMessageProcessingModel
(int messageProcessingModel) Gets theMessageProcessingModel
for the supplied message processing model ID.int
Gets the next unique request ID.<A extends Address>
TransportMapping<? super A> getTransport
(A destAddress) Deprecated.<A extends Address>
TransportMapping<? super A> getTransport
(A destAddress, TransportType transportType) Returns a transport mapping that can handle the supplied address.Collection
<TransportMapping<? extends Address>> Gets a collection of all registered transport mappings.boolean
Returns whether consistency checks for outgoing messages are activated.<A extends Address>
voidprocessMessage
(TransportMapping<? super A> sourceTransport, A incomingAddress, ByteBuffer wholeMessage, TransportStateReference tmStateReference) Process an incoming SNMP message.<A extends Address>
voidprocessMessage
(TransportMapping<? super A> sourceTransport, A incomingAddress, BERInputStream wholeMessage, TransportStateReference tmStateReference) void
releaseStateReference
(int messageProcessingModel, PduHandle pduHandle) Release any state references associated with the suppliedPduHandle
in the specified message processing model.void
Removes anAuthenticationFailureListener
.void
Removes a previously addedCommandResponder
instance from the message dispatcher.removeCounterListener
(CounterListener counterListener) Removes aCounterListener
.void
Removes a message processing model from this message dispatcher.TransportMapping
<? extends Address> removeTransportMapping
(TransportMapping<?> transport) Removes a transport mapping.<A extends Address>
intreturnResponsePdu
(int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, int maxSizeResponseScopedPDU, StateReference<A> stateReference, StatusInformation statusInformation) Returns a response PDU to the sender of the corresponding request PDU.protected <A extends Address>
voidsendMessage
(TransportMapping<? super A> transport, A destAddress, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) Sends a message using theTransportMapping
that has been assigned for the supplied address type.Sends a PDU to the supplied transport address.sendPdu
(TransportMapping<? super A> transportMapping, Target<A> target, PDU pdu, boolean expectResponse) Sends a PDU to the supplied transport address.sendPdu
(TransportMapping<? super A> transport, Target<A> target, PDU pdu, boolean expectResponse, PduHandleCallback<PDU> pduHandleCallback) Sends a PDU to the supplied transport address and returns thePduHandle
that uniquely identifies the request as response after the request has been sent and optional, if aPduHandleCallback
is given, it returns also thePduHandle
just before the request is sent through the the callback interface.void
setCheckOutgoingMsg
(boolean checkOutgoingMsg) Enables or disables the consistency checks for outgoing messages.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.snmp4j.MessageDispatcher
stop
-
Constructor Details
-
MessageDispatcherImpl
public MessageDispatcherImpl()Default constructor creates a message dispatcher without any associated message processing models.
-
-
Method Details
-
addMessageProcessingModel
Adds a message processing model to this message dispatcher. If a message processing model with the same ID as the supplied one already exists it will not be changed. Please callremoveMessageProcessingModel(org.snmp4j.mp.MessageProcessingModel)
before to replace a message processing model.- Specified by:
addMessageProcessingModel
in interfaceMessageDispatcher
- Parameters:
model
- a MessageProcessingModel instance.
-
removeMessageProcessingModel
Removes a message processing model from this message dispatcher.- Specified by:
removeMessageProcessingModel
in interfaceMessageDispatcher
- Parameters:
model
- a previously added MessageProcessingModel instance.
-
addTransportMapping
Adds a transport mapping. When an outgoing message is processed where no specific transport mapping has been specified, then the message dispatcher will use the transport mapping that supports the supplied address class of the target.- Specified by:
addTransportMapping
in interfaceMessageDispatcher
- Parameters:
transport
- a TransportMapping instance. If there is already another transport mapping registered that supports the same address class, thentransport
will be registered but not used for messages without specific transport mapping.
-
removeTransportMapping
Removes a transport mapping.- Specified by:
removeTransportMapping
in interfaceMessageDispatcher
- Parameters:
transport
- a previously added TransportMapping instance.- Returns:
- the supplied TransportMapping if it has been successfully removed,
null
otherwise.
-
getTransportMappings
Gets a collection of all registered transport mappings.- Specified by:
getTransportMappings
in interfaceMessageDispatcher
- Returns:
- a Collection instance.
-
getNextRequestID
public int getNextRequestID()Description copied from interface:MessageDispatcher
Gets the next unique request ID. The returned ID is unique across the last 2^31-1 IDs generated by this message dispatcher.- Specified by:
getNextRequestID
in interfaceMessageDispatcher
- Returns:
- an integer value in the range 1..2^31-1. The returned ID can be used to map responses to requests send through this message dispatcher.
-
createPduHandle
-
sendMessage
protected <A extends Address> void sendMessage(TransportMapping<? super A> transport, A destAddress, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) throws IOException Sends a message using theTransportMapping
that has been assigned for the supplied address type.- Type Parameters:
A
- anAddress
class that is the super class for the addresses supported by this transport mapping.- Parameters:
transport
- the transport mapping to be used to send the message.destAddress
- the transport address where to send the message. ThedestAddress
must be compatible with the suppliedtransport
.message
- the SNMP message to send.tmStateReference
- the transport state reference that holds transport state information for this message.timeoutMillis
- maximum number of milli seconds the connection creation might take (if connection based).maxRetries
- maximum retries during connection creation.- Throws:
IOException
- if an I/O error occurred while sending the message or if there is no transport mapping defined for the supplied address type.
-
getTransport
Deprecated.UsegetTransport(Address, TransportType)
instead to make sure that the returned mapping supports the intended SNMP application type usage (i.e. command/notification generator or command receiver).Returns a transport mapping that can handle the supplied address. ThegetTransportMappings()
known to this message dispatcher are searched for a matching transport mapping using theAddress
class hierarchy from the given address class to the root superclass. Each superclass is tested if it is compatible with the supplied address by callingAddress.isTransportCompatible(Class)
for that supper class. If that call returnstrue
, then the firstTransportMapping
that returnstrue
onTransportMapping.isAddressSupported(Address, boolean)
(exactMatch =true
) on the givendestAddress
is returned. If no such mapping can be found, the list is search again, but withexactMatch
false
. If still no supported mapping can be found, thennull
will be returned.- Specified by:
getTransport
in interfaceMessageDispatcher
- Type Parameters:
A
- theAddress
type to get aTransportMapping
for.- Parameters:
destAddress
- an Address instance.- Returns:
- a
TransportMapping
instance that can be used to sent a SNMP message todestAddress
ornull
if such a transport mapping does not exists. - Since:
- 1.6
-
getTransport
public <A extends Address> TransportMapping<? super A> getTransport(A destAddress, TransportType transportType) Returns a transport mapping that can handle the supplied address. ThegetTransportMappings()
known to this message dispatcher are searched for a matching transport mapping using theAddress
class hierarchy from the given address class to the root superclass. Each superclass is tested if it is compatible with the supplied address by callingAddress.isTransportCompatible(Class)
for that supper class. If that call returnstrue
, then the firstTransportMapping
that returnstrue
onTransportMapping.isAddressSupported(Address, boolean)
(exactMatch =true
) on the givendestAddress
is returned. If no such mapping can be found, the list is search again, but withexactMatch
false
. If still no supported mapping can be found, thennull
will be returned.- Specified by:
getTransport
in interfaceMessageDispatcher
- Type Parameters:
A
- theAddress
type to get aTransportMapping
for.- Parameters:
destAddress
- an Address instance.transportType
- specifies the SNMP application type that will use the returned transport mapping.transportType
must be eitherTransportType.receiver
orTransportType.sender
. IfTransportType.any
is provided anIllegalArgumentException
will be thrown.- Returns:
- a
TransportMapping
instance that can be used to send an SNMP message todestAddress
ornull
if such a transport mapping does not exist. - Since:
- 3.2.0
-
dispatchMessage
protected <A extends Address> void dispatchMessage(TransportMapping<? super A> sourceTransport, MessageProcessingModel mp, A incomingAddress, BERInputStream wholeMessage, TransportStateReference tmStateReference) throws IOException Actually decodes and dispatches an incoming SNMP message using the supplied message processing model.- Type Parameters:
A
- theAddress
type.- Parameters:
sourceTransport
- aTransportMapping
that matches the incomingAddress type.mp
- aMessageProcessingModel
to process the message.incomingAddress
- theAddress
from the entity that sent this message.wholeMessage
- theBERInputStream
containing the SNMP message.tmStateReference
- the transport model state reference as defined by RFC 5590.- Throws:
IOException
- if the message cannot be decoded.
-
processMessage
public <A extends Address> void processMessage(TransportMapping<? super A> sourceTransport, A incomingAddress, ByteBuffer wholeMessage, TransportStateReference tmStateReference) Description copied from interface:MessageDispatcher
Process an incoming SNMP message. The message is processed and dispatched according to the message's content, the message processing models, and the command responder available to the dispatcher.- Specified by:
processMessage
in interfaceMessageDispatcher
- Specified by:
processMessage
in interfaceTransportListener
- Type Parameters:
A
- theAddress
type.- Parameters:
sourceTransport
- aTransportMapping
instance denoting the transport that received the message and that will be used to send any responses to this message. ThesourceTransport
has to support theincomingAddress
's implementation class.incomingAddress
- theAddress
from which the message has been received.wholeMessage
- anByteBuffer
containing the received SNMP message.tmStateReference
- the transport model state reference as defined by RFC 5590.
-
processMessage
public <A extends Address> void processMessage(TransportMapping<? super A> sourceTransport, A incomingAddress, BERInputStream wholeMessage, TransportStateReference tmStateReference) -
sendPdu
public <A extends Address> PduHandle sendPdu(Target<A> target, PDU pdu, boolean expectResponse) throws MessageException Description copied from interface:MessageDispatcher
Sends a PDU to the supplied transport address. This method behaves like a call toMessageDispatcher.sendPdu(TransportMapping transportMapping, Target target, PDU pdu, boolean expectResponse)
withtransportMapping
set tonull
.- Specified by:
sendPdu
in interfaceMessageDispatcher
- Type Parameters:
A
- the targetAddress
type.- Parameters:
target
- the target which identifies, transport address, message processing model, security model, security name and level.pdu
- the SNMP Protocol Data UnitexpectResponse
-true
if a response is expected and a state reference should be saved (if needed for the supplied message processing model).- Returns:
- an
PduHandle
that uniquely identifies this request. - Throws:
MessageException
- if sending of the PDU failed.
-
sendPdu
public <A extends Address> PduHandle sendPdu(TransportMapping<? super A> transport, Target<A> target, PDU pdu, boolean expectResponse, PduHandleCallback<PDU> pduHandleCallback) throws MessageException Description copied from interface:MessageDispatcher
Sends a PDU to the supplied transport address and returns thePduHandle
that uniquely identifies the request as response after the request has been sent and optional, if aPduHandleCallback
is given, it returns also thePduHandle
just before the request is sent through the the callback interface.- Specified by:
sendPdu
in interfaceMessageDispatcher
- Type Parameters:
A
- the targetAddress
type.- Parameters:
transport
- theTransportMapping
to be used to send the PDU. IftransportMapping
isnull
the message dispatcher will determine the appropriate transport mapping for the given transport address.target
- the target which identifies, transport address, message processing model, security model, security name and level.pdu
- the SNMP Protocol Data UnitexpectResponse
-true
if a response is expected and a state reference should be saved (if needed for the supplied message processing model).pduHandleCallback
- an optional callback instance that is informed (if notnull
) about the newly assigned PduHandle just before the message is sent out.- Returns:
- an
PduHandle
that uniquely identifies this request. - Throws:
MessageException
- if sending of the PDU failed.
-
configureAuthoritativeEngineID
-
checkOutgoingMsg
protected void checkOutgoingMsg(Address transportAddress, int messageProcessingModel, PDU pdu) throws MessageException Checks outgoing messages for consistency between PDU and target used.- Parameters:
transportAddress
- the target address.messageProcessingModel
- the message processing model to be used.pdu
- the PDU to be sent.- Throws:
MessageException
- if unrecoverable inconsistencies have been detected.
-
returnResponsePdu
public <A extends Address> int returnResponsePdu(int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, int maxSizeResponseScopedPDU, StateReference<A> stateReference, StatusInformation statusInformation) throws MessageException Description copied from interface:MessageDispatcher
Returns a response PDU to the sender of the corresponding request PDU.- Specified by:
returnResponsePdu
in interfaceMessageDispatcher
- Type Parameters:
A
- theAddress
type.- Parameters:
messageProcessingModel
- the message processing model, seeMessageProcessingModel
securityModel
- the security model to use, seeSecurityModel
securityName
- the security namesecurityLevel
- the security level for the message to be returned, seeSecurityLevel
pdu
- the protocol data unit (PDU
) to send.maxSizeResponseScopedPDU
- the maximum size agreed for the response scoped PDU.stateReference
- the state reference to be used.statusInformation
- the status information to be used.- Returns:
- an MP error status or
SnmpConstants.SNMP_MP_OK
if the operation was successful. - Throws:
MessageException
- if message processing fails with a fatal error.
-
releaseStateReference
Description copied from interface:MessageDispatcher
Release any state references associated with the suppliedPduHandle
in the specified message processing model.- Specified by:
releaseStateReference
in interfaceMessageDispatcher
- Parameters:
messageProcessingModel
- a message processing model ID.pduHandle
- thePduHandle
that identifies a confirmed class message.- See Also:
-
removeCommandResponder
Description copied from interface:MessageDispatcher
Removes a previously addedCommandResponder
instance from the message dispatcher.- Specified by:
removeCommandResponder
in interfaceMessageDispatcher
- Parameters:
l
- aCommandResponder
instance.
-
addCommandResponder
Description copied from interface:MessageDispatcher
Adds aCommandResponder
instance to the message dispatcher. Successfully processed SNMP messages will be presented to all command responder (in the order in which they have been added) until a responder uses theCommandResponderEvent.setProcessed(boolean processed)
to set the processed status of the event totrue
.- Specified by:
addCommandResponder
in interfaceMessageDispatcher
- Parameters:
l
- aCommandResponder
instance.
-
fireProcessPdu
Fires aCommandResponderEvent
. Listeners are called in order of their registration until a listener has processed the PDU successfully.- Parameters:
e
- aCommandResponderEvent
event.
-
getMessageProcessingModel
Gets theMessageProcessingModel
for the supplied message processing model ID.- Specified by:
getMessageProcessingModel
in interfaceMessageDispatcher
- Parameters:
messageProcessingModel
- a message processing model ID (seeMessageProcessingModel.getID()
).- Returns:
- a MessageProcessingModel instance if the ID is known, otherwise
null
-
removeCounterListener
Removes aCounterListener
.- Specified by:
removeCounterListener
in interfaceMessageDispatcher
- Parameters:
counterListener
- a previously addedCounterListener
.- Returns:
- the
CounterListener
instance if it could be successfully removed,null
otherwise.
-
addCounterListener
Adds aCounterListener
.- Specified by:
addCounterListener
in interfaceMessageDispatcher
- Parameters:
counterListener
- aCounterListener
that will be informed when a counter needs to incremented.
-
fireIncrementCounter
Fires a counter incrementation event.- Parameters:
event
- theCounterEvent
containing the OID of the counter that needs to be incremented.
-
setCheckOutgoingMsg
public void setCheckOutgoingMsg(boolean checkOutgoingMsg) Enables or disables the consistency checks for outgoing messages. If the checks are enabled, then GETBULK messages sent to SNMPv1 targets will be converted to GETNEXT messages.In general, if an automatically conversion is not possible, an error is thrown when such a message is to be sent.
The default is consistency checks enabled.
- Parameters:
checkOutgoingMsg
- iftrue
outgoing messages are checked for consistency. Currently, only the PDU type will be checked against the used SNMP version. Iffalse
, no checks will be performed.
-
isCheckOutgoingMsg
public boolean isCheckOutgoingMsg()Returns whether consistency checks for outgoing messages are activated.- Returns:
- if
true
outgoing messages are checked for consistency. Iffalse
, no checks are performed.
-
addAuthenticationFailureListener
Adds a listener for authentication failure events caused by unauthenticated incoming messages.- Parameters:
l
- theAuthenticationFailureListener
to add.- Since:
- 1.5
-
removeAuthenticationFailureListener
Removes anAuthenticationFailureListener
.- Parameters:
l
- theAuthenticationFailureListener
to remove.
-
fireAuthenticationFailure
Fires anAuthenticationFailureEvent
to all registered listeners.- Parameters:
event
- the event to fire.
-
sendPdu
public <A extends Address> PduHandle sendPdu(TransportMapping<? super A> transportMapping, Target<A> target, PDU pdu, boolean expectResponse) throws MessageException Description copied from interface:MessageDispatcher
Sends a PDU to the supplied transport address.- Specified by:
sendPdu
in interfaceMessageDispatcher
- Type Parameters:
A
- theAddress
type.- Parameters:
transportMapping
- theTransportMapping
to be used to send the PDU. IftransportMapping
isnull
the message dispatcher will determine the appropriate transport mapping for the given transport address.target
- the target which identifies, transport address, message processing model, security model, security name and level.pdu
- the SNMP Protocol Data UnitexpectResponse
-true
if a response is expected and a state reference should be saved (if needed for the supplied message processing model).- Returns:
- an
PduHandle
that uniquely identifies this request. - Throws:
MessageException
- if sending of the PDU failed.
-
getTransport(Address, TransportType)
instead to make sure that the returned mapping supports the intended SNMP application type usage (i.e.