org.snmp4j.util
Class MultiThreadedMessageDispatcher

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

public class MultiThreadedMessageDispatcher
extends java.lang.Object
implements MessageDispatcher

The MultiThreadedMessageDispatcher class is a decorator for any MessageDispatcher instances that processes incoming message with a supplied ThreadPool. The processing is thus parallelized on up to the size of the supplied thread pool threads.

In contrast to a MessageDispatcherImpl a MultiThreadedMessageDispatcher copies the incoming ByteBuffer for processMessage(TransportMapping sourceTransport, Address incomingAddress, ByteBuffer wholeMessage) to allow parallel processing of the buffer.

Since:
1.0.2
Version:
1.10
Author:
Frank Fock

Constructor Summary
MultiThreadedMessageDispatcher(WorkerPool workerPool, MessageDispatcher decoratedDispatcher)
          Creates a multi thread message dispatcher using the provided ThreadPool to concurrently process incoming messages that are forwarded to the supplied decorated MessageDispatcher.
 
Method Summary
 void addCommandResponder(CommandResponder listener)
          Adds a CommandResponder instance to the message dispatcher.
 void addMessageProcessingModel(MessageProcessingModel model)
          Adds a MessageProcessingModel to the dispatcher.
 void addTransportMapping(TransportMapping transport)
          Adds a TransportMapping to the dispatcher.
 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 the Collection of transport mappings in this message dispatcher.
 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 removeCommandResponder(CommandResponder listener)
          Removes a previously added CommandResponder instance from the message dispatcher.
 void removeMessageProcessingModel(MessageProcessingModel model)
          Removes a previously added MessageProcessingModel from the dispatcher.
 TransportMapping removeTransportMapping(TransportMapping transport)
          Removes a previously added TransportMapping from the dispatcher.
 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.
 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 transportMapping, Address transportAddress, int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, boolean expectResponse, PduHandleCallback callback)
          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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiThreadedMessageDispatcher

public MultiThreadedMessageDispatcher(WorkerPool workerPool,
                                      MessageDispatcher decoratedDispatcher)
Creates a multi thread message dispatcher using the provided ThreadPool to concurrently process incoming messages that are forwarded to the supplied decorated MessageDispatcher.

Parameters:
workerPool - a WorkerPool instance (that can be shared). The worker pool has to be stopped externally.
decoratedDispatcher - the decorated MessageDispatcher that must be multi-threading safe.
Method Detail

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.

addMessageProcessingModel

public void addMessageProcessingModel(MessageProcessingModel model)
Description copied from interface: MessageDispatcher
Adds a MessageProcessingModel to the dispatcher. In order to support a specific SNMP protocol version, the message dispatcher needs a message processing model to process messages before they can be dispatched.

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

removeMessageProcessingModel

public void removeMessageProcessingModel(MessageProcessingModel model)
Description copied from interface: MessageDispatcher
Removes a previously added MessageProcessingModel from the dispatcher.

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

getMessageProcessingModel

public MessageProcessingModel getMessageProcessingModel(int messageProcessingModel)
Description copied from interface: MessageDispatcher
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

addTransportMapping

public void addTransportMapping(TransportMapping transport)
Description copied from interface: MessageDispatcher
Adds a TransportMapping to the dispatcher. The transport mapping is used to send and receive messages to/from the network.

Specified by:
addTransportMapping in interface MessageDispatcher
Parameters:
transport - a TransportMapping instance.

removeTransportMapping

public TransportMapping removeTransportMapping(TransportMapping transport)
Description copied from interface: MessageDispatcher
Removes a previously added TransportMapping from the dispatcher.

Specified by:
removeTransportMapping in interface MessageDispatcher
Parameters:
transport - a TransportMapping instance.
Returns:
the TransportMapping instance supplied if it could be successfully removed, null otherwise.

getTransportMappings

public java.util.Collection getTransportMappings()
Description copied from interface: MessageDispatcher
Gets the Collection of transport mappings in this message dispatcher.

Specified by:
getTransportMappings in interface MessageDispatcher
Returns:
a Collection with the registered transport mappings.

addCommandResponder

public void addCommandResponder(CommandResponder listener)
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:
listener - a CommandResponder instance.

removeCommandResponder

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

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

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 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

sendPdu

public PduHandle sendPdu(TransportMapping transportMapping,
                         Address transportAddress,
                         int messageProcessingModel,
                         int securityModel,
                         byte[] securityName,
                         int securityLevel,
                         PDU pdu,
                         boolean expectResponse,
                         PduHandleCallback callback)
                  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:
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).
callback - 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

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

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.

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.

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

getTransport

public TransportMapping getTransport(Address destAddress)
Description copied from interface: MessageDispatcher
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.

Copyright 2005-2010 Frank Fock (SNMP4J.org)

Copyright © 2011 SNMP4J.org. All Rights Reserved.