1 20 21 22 23 package org.snmp4j.util; 24 25 import org.snmp4j.MessageDispatcher; 26 import org.snmp4j.CommandResponder; 27 import org.snmp4j.mp.MessageProcessingModel; 28 import org.snmp4j.TransportMapping; 29 import java.util.Collection ; 30 import org.snmp4j.smi.Address; 31 import org.snmp4j.asn1.BERInputStream; 32 import org.snmp4j.mp.PduHandle; 33 import org.snmp4j.PDU; 34 import org.snmp4j.mp.StateReference; 35 import org.snmp4j.mp.StatusInformation; 36 import org.snmp4j.MessageException; 37 import java.nio.ByteBuffer ; 38 import org.snmp4j.mp.PduHandleCallback; 39 40 50 public class MultiThreadedMessageDispatcher implements MessageDispatcher { 51 52 private MessageDispatcher dispatcher; 53 private ThreadPool threadPool; 54 55 68 public MultiThreadedMessageDispatcher(ThreadPool threadPool, 69 MessageDispatcher decoratedDispatcher) { 70 this.threadPool = threadPool; 71 this.dispatcher = decoratedDispatcher; 72 } 73 74 public int getNextRequestID() { 75 return dispatcher.getNextRequestID(); 76 } 77 78 public void addMessageProcessingModel(MessageProcessingModel model) { 79 dispatcher.addMessageProcessingModel(model); 80 } 81 82 public void removeMessageProcessingModel(MessageProcessingModel model) { 83 dispatcher.removeMessageProcessingModel(model); 84 } 85 86 public MessageProcessingModel getMessageProcessingModel(int messageProcessingModel) { 87 return dispatcher.getMessageProcessingModel(messageProcessingModel); 88 } 89 90 public void addTransportMapping(TransportMapping transport) { 91 dispatcher.addTransportMapping(transport); 92 } 93 94 public TransportMapping removeTransportMapping(TransportMapping transport) { 95 return dispatcher.removeTransportMapping(transport); 96 } 97 98 public Collection getTransportMappings() { 99 return dispatcher.getTransportMappings(); 100 } 101 102 public void addCommandResponder(CommandResponder listener) { 103 dispatcher.addCommandResponder(listener); 104 } 105 106 public void removeCommandResponder(CommandResponder listener) { 107 dispatcher.removeCommandResponder(listener); 108 } 109 110 public PduHandle sendPdu(Address transportAddress, 111 int messageProcessingModel, 112 int securityModel, 113 byte[] securityName, 114 int securityLevel, 115 PDU pdu, 116 boolean expectResponse) throws MessageException { 117 return dispatcher.sendPdu(transportAddress, messageProcessingModel, 118 securityModel, securityName, securityLevel, 119 pdu, expectResponse); 120 } 121 122 public PduHandle sendPdu(TransportMapping transportMapping, 123 Address transportAddress, 124 int messageProcessingModel, 125 int securityModel, 126 byte[] securityName, 127 int securityLevel, 128 PDU pdu, 129 boolean expectResponse) throws MessageException { 130 return dispatcher.sendPdu(transportMapping, transportAddress, 131 messageProcessingModel, 132 securityModel, securityName, 133 securityLevel, pdu, expectResponse); 134 } 135 136 public PduHandle sendPdu(TransportMapping transportMapping, 137 Address transportAddress, 138 int messageProcessingModel, 139 int securityModel, byte[] securityName, 140 int securityLevel, PDU pdu, boolean expectResponse, 141 PduHandleCallback callback) throws MessageException { 142 return dispatcher.sendPdu(transportMapping, transportAddress, 143 messageProcessingModel, 144 securityModel, securityName, 145 securityLevel, pdu, expectResponse, callback); 146 } 147 148 public int returnResponsePdu(int messageProcessingModel, 149 int securityModel, 150 byte[] securityName, 151 int securityLevel, 152 PDU pdu, 153 int maxSizeResponseScopedPDU, 154 StateReference stateReference, 155 StatusInformation statusInformation) 156 throws MessageException 157 { 158 return dispatcher.returnResponsePdu(messageProcessingModel, 159 securityModel, securityName, 160 securityLevel, pdu, 161 maxSizeResponseScopedPDU, 162 stateReference, 163 statusInformation); 164 } 165 166 public void processMessage(TransportMapping sourceTransport, 167 Address incomingAddress, 168 BERInputStream wholeMessage) { 169 MessageTask task = new MessageTask(sourceTransport, 171 incomingAddress, 172 wholeMessage); 173 threadPool.execute(task); 174 } 175 176 public void processMessage(TransportMapping sourceTransport, 177 Address incomingAddress, ByteBuffer wholeMessage) { 178 processMessage(sourceTransport, incomingAddress, 179 new BERInputStream(wholeMessage)); 180 } 181 182 public void releaseStateReference(int messageProcessingModel, 183 PduHandle pduHandle) { 184 dispatcher.releaseStateReference(messageProcessingModel, pduHandle); 185 } 186 187 public TransportMapping getTransport(Address destAddress) { 188 return dispatcher.getTransport(destAddress); 189 } 190 191 class MessageTask implements Runnable { 192 private TransportMapping sourceTransport; 193 private Address incomingAddress; 194 private BERInputStream wholeMessage; 195 196 public MessageTask(TransportMapping sourceTransport, 197 Address incomingAddress, 198 BERInputStream wholeMessage) { 199 this.sourceTransport = sourceTransport; 200 this.incomingAddress = incomingAddress; 201 this.wholeMessage = wholeMessage; 202 } 203 204 public void run() { 205 dispatcher.processMessage(sourceTransport, incomingAddress, wholeMessage); 206 } 207 208 } 209 } 210 | Popular Tags |