org.snmp4j
Class MessageDispatcherImpl

java.lang.Object
  extended by org.snmp4j.MessageDispatcherImpl
All Implemented Interfaces:
MessageDispatcher, TransportListener

public class MessageDispatcherImpl
extends java.lang.Object
implements MessageDispatcher

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) will be called from a TransportMapping whereas the method sendPdu(org.snmp4j.smi.Address, int, int, byte[], int, org.snmp4j.PDU, boolean) will be called by the application.

Version:
1.8
Author:
Frank Fock
See Also:
Snmp, TransportMapping, MessageProcessingModel, MPv1, MPv2c, MPv3

Constructor Summary
MessageDispatcherImpl()
          Default constructor creates a message dispatcher without any associated message processing models.
 
Method Summary
 void addAuthenticationFailureListener(AuthenticationFailureListener l)
          Adds a listener for authentication failure events caused by unauthenticated incoming messages.
 void addCommandResponder(CommandResponder l)
          Adds a CommandResponder instance to the message dispatcher.
 void addCounterListener(CounterListener counterListener)
          Adds a CounterListener.
 void addMessageProcessingModel(MessageProcessingModel model)
          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  PduHandle createPduHandle()
           
protected  void dispatchMessage(TransportMapping sourceTransport, MessageProcessingModel mp, Address incomingAddress, BERInputStream wholeMessage)
          Actually decodes and dispatches an incoming SNMP message using the supplied message processing model.
protected  void fireAuthenticationFailure(AuthenticationFailureEvent event)
          Fires an AuthenticationFailureEvent to all registered listeners.
protected  void fireIncrementCounter(CounterEvent event)
          Fires a counter incrementation event.
protected  void fireProcessPdu(CommandResponderEvent e)
          Fires a CommandResponderEvent.
 MessageProcessingModel getMessageProcessingModel(int messageProcessingModel)
          Gets the MessageProcessingModel for the supplied message processing model ID.
 int getNextRequestID()
          Gets the next unique request ID.
 TransportMapping getTransport(Address destAddress)
          Returns a transport mapping that can handle the supplied address.
 java.util.Collection getTransportMappings()
          Gets a collection of all registered transport mappings.
 boolean isCheckOutgoingMsg()
          Returns whether consistency checks for outgoing messages are activated.
 void processMessage(TransportMapping sourceTransport, Address incomingAddress, BERInputStream wholeMessage)
          Process an incoming SNMP message.
 void processMessage(TransportMapping sourceTransport, Address incomingAddress, java.nio.ByteBuffer wholeMessage)
          Process an incoming SNMP message.
 void releaseStateReference(int messageProcessingModel, PduHandle pduHandle)
          Release any state references associated with the supplied PduHandle in the specified message processing model.
 void removeAuthenticationFailureListener(AuthenticationFailureListener l)
          Removes an AuthenticationFailureListener.
 void removeCommandResponder(CommandResponder l)
          Removes a previously added CommandResponder instance from the message dispatcher.
 void removeCounterListener(CounterListener counterListener)
          Removes a CounterListener.
 void removeMessageProcessingModel(MessageProcessingModel model)
          Removes a message processing model from this message dispatcher.
 TransportMapping removeTransportMapping(TransportMapping transport)
          Removes a transport mapping.
 int returnResponsePdu(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  void sendMessage(TransportMapping transport, Address destAddress, byte[] message)
          Sends a message using the TransportMapping that has been assigned for the supplied address type.
 PduHandle sendPdu(Address transportAddress, int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, boolean expectResponse)
          Sends a PDU to the supplied transport address.
 PduHandle sendPdu(TransportMapping transportMapping, Address transportAddress, int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, boolean expectResponse)
          Sends a PDU to the supplied transport address.
 PduHandle sendPdu(TransportMapping transport, Address transportAddress, int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, boolean expectResponse, PduHandleCallback pduHandleCallback)
          Sends a PDU to the supplied transport address and returns the PduHandle that uniquely identifies the request as response after the request has been sent and otional, if a PduHandleCallback is given, it returns also the PduHandle 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
 

Constructor Detail

MessageDispatcherImpl

public MessageDispatcherImpl()
Default constructor creates a message dispatcher without any associated message processing models.

Method Detail

addMessageProcessingModel

public void addMessageProcessingModel(MessageProcessingModel model)
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 call removeMessageProcessingModel(org.snmp4j.mp.MessageProcessingModel) before to replace a message processing model.

Specified by:
addMessageProcessingModel in interface MessageDispatcher
Parameters:
model - a MessageProcessingModel instance.

removeMessageProcessingModel

public void removeMessageProcessingModel(MessageProcessingModel model)
Removes a message processing model from this message dispatcher.

Specified by:
removeMessageProcessingModel in interface MessageDispatcher
Parameters:
model - a previously added MessageProcessingModel instance.

addTransportMapping

public void addTransportMapping(TransportMapping transport)
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 interface MessageDispatcher
Parameters:
transport - a TransportMapping instance. If there is already another transport mapping registered that supports the same address class, then transport will be registered but not used for messages without specific transport mapping.

removeTransportMapping

public TransportMapping removeTransportMapping(TransportMapping transport)
Removes a transport mapping.

Specified by:
removeTransportMapping in interface MessageDispatcher
Parameters:
transport - a previously added TransportMapping instance.
Returns:
the supplied TransportMapping if it has been successfully removed, nullotherwise.

getTransportMappings

public java.util.Collection getTransportMappings()
Gets a collection of all registered transport mappings.

Specified by:
getTransportMappings in interface MessageDispatcher
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 interface MessageDispatcher
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

protected PduHandle createPduHandle()

sendMessage

protected void sendMessage(TransportMapping transport,
                           Address destAddress,
                           byte[] message)
                    throws java.io.IOException
Sends a message using the TransportMapping that 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. The destAddress must be compatible with the supplied transport.
message - the SNMP message to send.
Throws:
java.io.IOException - if an I/O error occured while sending the message or if there is no transport mapping defined for the supplied address type.

getTransport

public TransportMapping getTransport(Address destAddress)
Returns a transport mapping that can handle the supplied address.

Specified by:
getTransport in interface MessageDispatcher
Parameters:
destAddress - an Address instance.
Returns:
a TransportMapping instance that can be used to sent a SNMP message to destAddress or null if such a transport mapping does not exists.
Since:
1.6

dispatchMessage

protected void dispatchMessage(TransportMapping sourceTransport,
                               MessageProcessingModel mp,
                               Address incomingAddress,
                               BERInputStream wholeMessage)
                        throws java.io.IOException
Actually decodes and dispatches an incoming SNMP message using the supplied message processing model.

Parameters:
sourceTransport - a TransportMapping that matches the incomingAddress type.
mp - a MessageProcessingModel to process the message.
incomingAddress - the Address from the entity that sent this message.
wholeMessage - the BERInputStream containing the SNMP message.
Throws:
java.io.IOException - if the message cannot be decoded.

processMessage

public void processMessage(TransportMapping sourceTransport,
                           Address incomingAddress,
                           java.nio.ByteBuffer wholeMessage)
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 interface MessageDispatcher
Specified by:
processMessage in interface TransportListener
Parameters:
sourceTransport - a TransportMapping instance denoting the transport that received the message and that will be used to send any responses to this message. The sourceTransport has to support the incomingAddress's implementation class.
incomingAddress - the Address from which the message has been received.
wholeMessage - an ByteBuffer containing the received SNMP message.

processMessage

public void processMessage(TransportMapping sourceTransport,
                           Address incomingAddress,
                           BERInputStream wholeMessage)
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 interface MessageDispatcher
Parameters:
sourceTransport - a TransportMapping instance denoting the transport that received the message and that will be used to send any responses to this message. The sourceTransport has to support the incomingAddress's implementation class.
incomingAddress - the Address from which the message has been received.
wholeMessage - an BERInputStream containing the received SNMP message. The supplied input stream must support marks, otherwise an IllegalArgumentException is thrown.

sendPdu

public PduHandle sendPdu(Address transportAddress,
                         int messageProcessingModel,
                         int securityModel,
                         byte[] securityName,
                         int securityLevel,
                         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 to MessageDispatcher.sendPdu(TransportMapping transportMapping, Address transportAddress, int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, boolean expectResponse) with transportMapping set to null.

Specified by:
sendPdu in interface MessageDispatcher
Parameters:
transportAddress - the target transport address.
messageProcessingModel - typically the SNMP version.
securityModel - Security Model to use.
securityName - on behalf of this principal.
securityLevel - Level of Security requested.
pdu - the SNMP Protocol Data Unit
expectResponse - 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

sendPdu

public PduHandle sendPdu(TransportMapping transport,
                         Address transportAddress,
                         int messageProcessingModel,
                         int securityModel,
                         byte[] securityName,
                         int securityLevel,
                         PDU pdu,
                         boolean expectResponse,
                         PduHandleCallback pduHandleCallback)
                  throws MessageException
Description copied from interface: MessageDispatcher
Sends a PDU to the supplied transport address and returns the PduHandle that uniquely identifies the request as response after the request has been sent and otional, if a PduHandleCallback is given, it returns also the PduHandle just before the request is sent through the the callback interface.

Specified by:
sendPdu in interface MessageDispatcher
Parameters:
transport - the TransportMapping to be used to send the PDU. If transportMapping is null the message dispatcher will determine the appropriate transport mapping for the given transport address.
transportAddress - the target transport address.
messageProcessingModel - typically the SNMP version.
securityModel - Security Model to use.
securityName - on behalf of this principal.
securityLevel - Level of Security requested.
pdu - the SNMP Protocol Data Unit
expectResponse - 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 not null) about the newly assigned PduHandle just before the message is sent out.
Returns:
an PduHandle that uniquely identifies this request.
Throws:
MessageException

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: MessageDispatcher
Returns a response PDU to the sender of the corresponding request PDU.

Specified by:
returnResponsePdu in interface MessageDispatcher
Parameters:
messageProcessingModel - int
securityModel - int
securityName - byte[]
securityLevel - int
pdu - PDU
maxSizeResponseScopedPDU - int
stateReference - StateReference
statusInformation - StatusInformation
Returns:
int
Throws:
MessageException

releaseStateReference

public void releaseStateReference(int messageProcessingModel,
                                  PduHandle pduHandle)
Description copied from interface: MessageDispatcher
Release any state references associated with the supplied PduHandle in the specified message processing model.

Specified by:
releaseStateReference in interface MessageDispatcher
Parameters:
messageProcessingModel - a message processing model ID.
pduHandle - the PduHandle that identifies a confirmed class message.
See Also:
MessageProcessingModel

removeCommandResponder

public void removeCommandResponder(CommandResponder l)
Description copied from interface: MessageDispatcher
Removes a previously added CommandResponder instance from the message dispatcher.

Specified by:
removeCommandResponder in interface MessageDispatcher
Parameters:
l - a CommandResponder instance.

addCommandResponder

public void addCommandResponder(CommandResponder l)
Description copied from interface: MessageDispatcher
Adds a CommandResponder 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 the CommandResponderEvent.setProcessed(boolean processed) to set the processed status of the event to true.

Specified by:
addCommandResponder in interface MessageDispatcher
Parameters:
l - a CommandResponder instance.

fireProcessPdu

protected void fireProcessPdu(CommandResponderEvent e)
Fires a CommandResponderEvent. Listeners are called in order of their registration until a listener has processed the PDU successfully.

Parameters:
e - a CommandResponderEvent event.

getMessageProcessingModel

public MessageProcessingModel getMessageProcessingModel(int messageProcessingModel)
Gets the MessageProcessingModel for the supplied message processing model ID.

Specified by:
getMessageProcessingModel in interface MessageDispatcher
Parameters:
messageProcessingModel - a message processing model ID (see MessageProcessingModel.getID()).
Returns:
a MessageProcessingModel instance if the ID is known, otherwise null

removeCounterListener

public void removeCounterListener(CounterListener counterListener)
Removes a CounterListener.

Parameters:
counterListener - a previously added CounterListener.

addCounterListener

public void addCounterListener(CounterListener counterListener)
Adds a CounterListener.

Parameters:
counterListener - a CounterListener that will be informed when a counter needs to incremented.

fireIncrementCounter

protected void fireIncrementCounter(CounterEvent event)
Fires a counter incrementation event.

Parameters:
event - the CounterEvent 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 - if true outgoing messages are checked for consistency. Currently, only the PDU type will be checked against the used SNMP version. If false, 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. If false, no checks are performed.

addAuthenticationFailureListener

public void addAuthenticationFailureListener(AuthenticationFailureListener l)
Adds a listener for authentication failure events caused by unauthenticated incoming messages.

Parameters:
l - the AuthenticationFailureListener to add.
Since:
1.5

removeAuthenticationFailureListener

public void removeAuthenticationFailureListener(AuthenticationFailureListener l)
Removes an AuthenticationFailureListener.

Parameters:
l - the AuthenticationFailureListener to remove.

fireAuthenticationFailure

protected void fireAuthenticationFailure(AuthenticationFailureEvent event)
Fires an AuthenticationFailureEvent to all registered listeners.

Parameters:
event - the event to fire.

sendPdu

public PduHandle sendPdu(TransportMapping transportMapping,
                         Address transportAddress,
                         int messageProcessingModel,
                         int securityModel,
                         byte[] securityName,
                         int securityLevel,
                         PDU pdu,
                         boolean expectResponse)
                  throws MessageException
Description copied from interface: MessageDispatcher
Sends a PDU to the supplied transport address.

Specified by:
sendPdu in interface MessageDispatcher
Parameters:
transportMapping - the TransportMapping to be used to send the PDU. If transportMapping is null the message dispatcher will determine the appropriate transport mapping for the given transport address.
transportAddress - the target transport address.
messageProcessingModel - typically the SNMP version.
securityModel - Security Model to use.
securityName - on behalf of this principal.
securityLevel - Level of Security requested.
pdu - the SNMP Protocol Data Unit
expectResponse - 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

Copyright 2005-2010 Frank Fock (SNMP4J.org)

Copyright © 2011 SNMP4J.org. All Rights Reserved.