Package org.snmp4j
Class MessageDispatcherImpl
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, org.snmp4j.smi.Address, java.nio.ByteBuffer, org.snmp4j.TransportStateReference) will be called from a
TransportMapping whereas the method sendPdu(org.snmp4j.Target, org.snmp4j.PDU, boolean) will be
called by the application.- Version:
- 2.0
- Author:
- Frank Fock
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor creates a message dispatcher without any associated message processing models. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a listener for authentication failure events caused by unauthenticated incoming messages.voidAdds aCommandResponderinstance to the message dispatcher.voidaddCounterListener(CounterListener counterListener) Adds aCounterListener.voidAdds a message processing model to this message dispatcher.voidaddTransportMapping(TransportMapping transport) Adds a transport mapping.protected voidcheckOutgoingMsg(Address transportAddress, int messageProcessingModel, PDU pdu) Checks outgoing messages for consistency between PDU and target used.protected voidprotected PduHandleprotected voiddispatchMessage(TransportMapping sourceTransport, MessageProcessingModel mp, Address incomingAddress, BERInputStream wholeMessage, TransportStateReference tmStateReference) Actually decodes and dispatches an incoming SNMP message using the supplied message processing model.protected voidFires anAuthenticationFailureEventto all registered listeners.protected voidfireIncrementCounter(CounterEvent event) Fires a counter incrementation event.protected voidFires aCommandResponderEvent.getMessageProcessingModel(int messageProcessingModel) Gets theMessageProcessingModelfor the supplied message processing model ID.intGets the next unique request ID.getTransport(Address destAddress) Returns a transport mapping that can handle the supplied address.Gets a collection of all registered transport mappings.booleanReturns whether consistency checks for outgoing messages are activated.voidprocessMessage(TransportMapping sourceTransport, Address incomingAddress, ByteBuffer wholeMessage, TransportStateReference tmStateReference) Process an incoming SNMP message.voidprocessMessage(TransportMapping sourceTransport, Address incomingAddress, BERInputStream wholeMessage, TransportStateReference tmStateReference) voidreleaseStateReference(int messageProcessingModel, PduHandle pduHandle) Release any state references associated with the suppliedPduHandlein the specified message processing model.voidRemoves anAuthenticationFailureListener.voidRemoves a previously addedCommandResponderinstance from the message dispatcher.voidremoveCounterListener(CounterListener counterListener) Removes aCounterListener.voidRemoves a message processing model from this message dispatcher.removeTransportMapping(TransportMapping transport) Removes a transport mapping.intreturnResponsePdu(int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, int maxSizeResponseScopedPDU, StateReference stateReference, StatusInformation statusInformation) Returns a response PDU to the sender of the corresponding request PDU.protected voidsendMessage(TransportMapping transport, Address destAddress, byte[] message, TransportStateReference tmStateReference) Sends a message using theTransportMappingthat has been assigned for the supplied address type.Sends a PDU to the supplied transport address.sendPdu(TransportMapping transportMapping, Target target, PDU pdu, boolean expectResponse) Sends a PDU to the supplied transport address.sendPdu(TransportMapping transport, Target target, PDU pdu, boolean expectResponse, PduHandleCallback<PDU> pduHandleCallback) Sends a PDU to the supplied transport address and returns thePduHandlethat uniquely identifies the request as response after the request has been sent and optional, if aPduHandleCallbackis given, it returns also thePduHandlejust before the request is sent through the the callback interface.voidsetCheckOutgoingMsg(boolean checkOutgoingMsg) Enables or disables the consistency checks for outgoing messages.
-
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:
addMessageProcessingModelin interfaceMessageDispatcher- Parameters:
model- a MessageProcessingModel instance.
-
removeMessageProcessingModel
Removes a message processing model from this message dispatcher.- Specified by:
removeMessageProcessingModelin 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:
addTransportMappingin interfaceMessageDispatcher- Parameters:
transport- a TransportMapping instance. If there is already another transport mapping registered that supports the same address class, thentransportwill be registered but not used for messages without specific transport mapping.
-
removeTransportMapping
Removes a transport mapping.- Specified by:
removeTransportMappingin interfaceMessageDispatcher- Parameters:
transport- a previously added TransportMapping instance.- Returns:
- the supplied TransportMapping if it has been successfully removed,
nullotherwise.
-
getTransportMappings
Gets a collection of all registered transport mappings.- Specified by:
getTransportMappingsin interfaceMessageDispatcher- Returns:
- a Collection instance.
-
getNextRequestID
public int getNextRequestID()Description copied from interface:MessageDispatcherGets the next unique request ID. The returned ID is unique across the last 2^31-1 IDs generated by this message dispatcher.- Specified by:
getNextRequestIDin 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 void sendMessage(TransportMapping transport, Address destAddress, byte[] message, TransportStateReference tmStateReference) throws IOException Sends a message using theTransportMappingthat has been assigned for the supplied address type.- Parameters:
transport- the transport mapping to be used to send the message.destAddress- the transport address where to send the message. ThedestAddressmust be compatible with the suppliedtransport.message- the SNMP message to send.tmStateReference- the transport state reference that holds transport state information for this message.- 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
Returns a transport mapping that can handle the supplied address.- Specified by:
getTransportin interfaceMessageDispatcher- Parameters:
destAddress- an Address instance.- Returns:
- a
TransportMappinginstance that can be used to send an SNMP message todestAddressornullif such a transport mapping does not exist. - Since:
- 1.6
-
dispatchMessage
protected void dispatchMessage(TransportMapping sourceTransport, MessageProcessingModel mp, Address incomingAddress, BERInputStream wholeMessage, TransportStateReference tmStateReference) throws IOException Actually decodes and dispatches an incoming SNMP message using the supplied message processing model.- Parameters:
sourceTransport- aTransportMappingthat matches the incomingAddress type.mp- aMessageProcessingModelto process the message.incomingAddress- theAddressfrom the entity that sent this message.wholeMessage- theBERInputStreamcontaining the SNMP message.tmStateReference- the transport model state reference as defined by RFC 5590.- Throws:
IOException- if the message cannot be decoded.
-
processMessage
public void processMessage(TransportMapping sourceTransport, Address incomingAddress, ByteBuffer wholeMessage, TransportStateReference tmStateReference) Description copied from interface:MessageDispatcherProcess 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:
processMessagein interfaceMessageDispatcher- Specified by:
processMessagein interfaceTransportListener- Parameters:
sourceTransport- aTransportMappinginstance denoting the transport that received the message and that will be used to send any responses to this message. ThesourceTransporthas to support theincomingAddress's implementation class.incomingAddress- theAddressfrom which the message has been received.wholeMessage- anByteBuffercontaining the received SNMP message.tmStateReference- the transport model state reference as defined by RFC 5590.
-
processMessage
public void processMessage(TransportMapping sourceTransport, Address incomingAddress, BERInputStream wholeMessage, TransportStateReference tmStateReference) -
sendPdu
Description copied from interface:MessageDispatcherSends a PDU to the supplied transport address. This method behaves like a call toMessageDispatcher.sendPdu(TransportMapping transportMapping, Target target, PDU pdu, boolean expectResponse)withtransportMappingset tonull.- Specified by:
sendPduin interfaceMessageDispatcher- Parameters:
target- the target which identifies, transport address, message processing model, security model, security name and level.pdu- the SNMP Protocol Data UnitexpectResponse-trueif a response is expected and a state reference should be saved (if needed for the supplied message processing model).- Returns:
- an
PduHandlethat uniquely identifies this request. - Throws:
MessageException- if sending of the PDU failed.
-
sendPdu
public PduHandle sendPdu(TransportMapping transport, Target target, PDU pdu, boolean expectResponse, PduHandleCallback<PDU> pduHandleCallback) throws MessageException Description copied from interface:MessageDispatcherSends a PDU to the supplied transport address and returns thePduHandlethat uniquely identifies the request as response after the request has been sent and optional, if aPduHandleCallbackis given, it returns also thePduHandlejust before the request is sent through the the callback interface.- Specified by:
sendPduin interfaceMessageDispatcher- Parameters:
transport- theTransportMappingto be used to send the PDU. IftransportMappingisnullthe 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-trueif 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
PduHandlethat 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 int returnResponsePdu(int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, int maxSizeResponseScopedPDU, StateReference stateReference, StatusInformation statusInformation) throws MessageException Description copied from interface:MessageDispatcherReturns a response PDU to the sender of the corresponding request PDU.- Specified by:
returnResponsePduin interfaceMessageDispatcher- Parameters:
messageProcessingModel- intsecurityModel- intsecurityName- byte[]securityLevel- intpdu- PDUmaxSizeResponseScopedPDU- intstateReference- StateReferencestatusInformation- StatusInformation- Returns:
- an MP error status or
SnmpConstants.SNMP_MP_OKif the operation was successful. - Throws:
MessageException- if message processing fails with a fatal error.
-
releaseStateReference
Description copied from interface:MessageDispatcherRelease any state references associated with the suppliedPduHandlein the specified message processing model.- Specified by:
releaseStateReferencein interfaceMessageDispatcher- Parameters:
messageProcessingModel- a message processing model ID.pduHandle- thePduHandlethat identifies a confirmed class message.- See Also:
-
removeCommandResponder
Description copied from interface:MessageDispatcherRemoves a previously addedCommandResponderinstance from the message dispatcher.- Specified by:
removeCommandResponderin interfaceMessageDispatcher- Parameters:
l- aCommandResponderinstance.
-
addCommandResponder
Description copied from interface:MessageDispatcherAdds aCommandResponderinstance 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:
addCommandResponderin interfaceMessageDispatcher- Parameters:
l- aCommandResponderinstance.
-
fireProcessPdu
Fires aCommandResponderEvent. Listeners are called in order of their registration until a listener has processed the PDU successfully.- Parameters:
e- aCommandResponderEventevent.
-
getMessageProcessingModel
Gets theMessageProcessingModelfor the supplied message processing model ID.- Specified by:
getMessageProcessingModelin interfaceMessageDispatcher- Parameters:
messageProcessingModel- a message processing model ID (seeMessageProcessingModel.getID()).- Returns:
- a MessageProcessingModel instance if the ID is known, otherwise
null
-
removeCounterListener
Removes aCounterListener.- Parameters:
counterListener- a previously addedCounterListener.
-
addCounterListener
Adds aCounterListener.- Parameters:
counterListener- aCounterListenerthat will be informed when a counter needs to incremented.
-
fireIncrementCounter
Fires a counter incrementation event.- Parameters:
event- theCounterEventcontaining 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- iftrueoutgoing 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
trueoutgoing 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- theAuthenticationFailureListenerto add.- Since:
- 1.5
-
removeAuthenticationFailureListener
Removes anAuthenticationFailureListener.- Parameters:
l- theAuthenticationFailureListenerto remove.
-
fireAuthenticationFailure
Fires anAuthenticationFailureEventto all registered listeners.- Parameters:
event- the event to fire.
-
sendPdu
public PduHandle sendPdu(TransportMapping transportMapping, Target target, PDU pdu, boolean expectResponse) throws MessageException Description copied from interface:MessageDispatcherSends a PDU to the supplied transport address.- Specified by:
sendPduin interfaceMessageDispatcher- Parameters:
transportMapping- theTransportMappingto be used to send the PDU. IftransportMappingisnullthe 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-trueif a response is expected and a state reference should be saved (if needed for the supplied message processing model).- Returns:
- an
PduHandlethat uniquely identifies this request. - Throws:
MessageException- if sending of the PDU failed.
-