1 20 21 22 23 24 25 package org.snmp4j.transport; 26 27 import java.io.IOException ; 28 import java.net.*; 29 import java.nio.ByteBuffer ; 30 31 import org.snmp4j.log.*; 32 import org.snmp4j.smi.*; 33 import org.snmp4j.SNMP4JSettings; 34 35 43 public class DefaultUdpTransportMapping extends UdpTransportMapping { 44 45 private static final LogAdapter logger = 46 LogFactory.getLogger(DefaultUdpTransportMapping.class); 47 48 protected DatagramSocket socket = null; 49 protected ListenThread listener; 50 private int socketTimeout = 1000; 51 52 private int receiveBufferSize = 0; 54 61 public DefaultUdpTransportMapping() throws IOException { 62 super(new UdpAddress(InetAddress.getLocalHost(), 0)); 63 socket = new DatagramSocket(udpAddress.getPort()); 64 } 65 66 79 public DefaultUdpTransportMapping(UdpAddress udpAddress, 80 boolean reuseAddress) throws IOException { 81 super(udpAddress); 82 socket = new DatagramSocket(null); 83 socket.setReuseAddress(reuseAddress); 84 final SocketAddress addr = 85 new InetSocketAddress(udpAddress.getInetAddress(),udpAddress.getPort()); 86 socket.bind(addr); 87 } 88 89 98 public DefaultUdpTransportMapping(UdpAddress udpAddress) throws IOException { 99 super(udpAddress); 100 socket = new DatagramSocket(udpAddress.getPort(), 101 udpAddress.getInetAddress()); 102 } 103 104 public void sendMessage(Address targetAddress, byte[] message) 105 throws java.io.IOException 106 { 107 InetSocketAddress targetSocketAddress = 108 new InetSocketAddress(((UdpAddress)targetAddress).getInetAddress(), 109 ((UdpAddress)targetAddress).getPort()); 110 if (logger.isDebugEnabled()) { 111 logger.debug("Sending message to "+targetAddress+" with length "+ 112 message.length+": "+ 113 new OctetString(message).toHexString()); 114 } 115 socket.send(new DatagramPacket(message, message.length, 116 targetSocketAddress)); 117 } 118 119 124 public void close() throws IOException { 125 ListenThread l = listener; 126 if (l != null) { 127 l.close(); 128 l.interrupt(); 129 if (socketTimeout > 0) { 130 try { 131 l.join(); 132 } 133 catch (InterruptedException ex) { 134 logger.warn(ex); 135 } 136 } 137 listener = null; 138 } 139 if (!socket.isClosed()) { 140 socket.disconnect(); 141 socket.close(); 142 } 143 } 144 145 153 public synchronized void listen() throws IOException { 154 if (listener != null) { 155 throw new SocketException("Port already listening"); 156 } 157 listener = new ListenThread(); 158 listener.setDaemon(true); 160 listener.start(); 161 } 162 163 173 public void setPriority(int newPriority) { 174 ListenThread lt = listener; 175 if (lt != null) { 176 lt.setPriority(newPriority); 177 } 178 } 179 180 187 public int getPriority() { 188 ListenThread lt = listener; 189 if (lt != null) { 190 return lt.getPriority(); 191 } 192 else { 193 return Thread.NORM_PRIORITY; 194 } 195 } 196 197 206 public void setThreadName(String name) { 207 ListenThread lt = listener; 208 if (lt != null) { 209 lt.setName(name); 210 } 211 } 212 213 219 public String getThreadName() { 220 ListenThread lt = listener; 221 if (lt != null) { 222 return lt.getName(); 223 } 224 else { 225 return null; 226 } 227 } 228 229 public void setMaxInboundMessageSize(int maxInboundMessageSize) { 230 this.maxInboundMessageSize = maxInboundMessageSize; 231 } 232 233 public int getSocketTimeout() { 234 return socketTimeout; 235 } 236 237 245 public int getReceiveBufferSize() { 246 return receiveBufferSize; 247 } 248 249 256 public void setReceiveBufferSize(int receiveBufferSize) { 257 if (receiveBufferSize <= 0) { 258 throw new IllegalArgumentException ("Receive buffer size must be > 0"); 259 } 260 this.receiveBufferSize = receiveBufferSize; 261 } 262 263 public void setSocketTimeout(int socketTimeout) { 264 this.socketTimeout = socketTimeout; 265 } 266 267 public boolean isListening() { 268 return (listener != null); 269 } 270 271 class ListenThread extends Thread { 272 273 private byte[] buf; 274 private volatile boolean stop = false; 275 276 277 public ListenThread() throws SocketException { 278 buf = new byte[getMaxInboundMessageSize()]; 279 setName("DefaultUDPTransportMapping_"+getAddress()); 280 } 281 282 public void run() { 283 try { 284 socket.setSoTimeout(getSocketTimeout()); 285 if (receiveBufferSize > 0) { 286 socket.setReceiveBufferSize(Math.max(receiveBufferSize, 287 maxInboundMessageSize)); 288 } 289 if (logger.isInfoEnabled()) { 290 logger.info("UDP receive buffer size for socket " + 291 getAddress() + " is set to: " + 292 socket.getReceiveBufferSize()); 293 } 294 } 295 catch (SocketException ex) { 296 logger.error(ex); 297 setSocketTimeout(0); 298 } 299 while (!stop) { 300 DatagramPacket packet = new DatagramPacket(buf, buf.length, 301 udpAddress.getInetAddress(), 302 udpAddress.getPort()); 303 try { 304 socket.receive(packet); 305 if (logger.isDebugEnabled()) { 306 logger.debug("Received message from "+packet.getAddress()+"/"+ 307 packet.getPort()+ 308 " with length "+packet.getLength()+": "+ 309 new OctetString(packet.getData(), 0, 310 packet.getLength()).toHexString()); 311 } 312 ByteBuffer bis; 313 if (isAsyncMsgProcessingSupported()) { 316 byte[] bytes = new byte[packet.getLength()]; 317 System.arraycopy(packet.getData(), 0, bytes, 0, bytes.length); 318 bis = ByteBuffer.wrap(bytes); 319 } 320 else { 321 bis = ByteBuffer.wrap(packet.getData()); 322 } 323 fireProcessMessage(new UdpAddress(packet.getAddress(), 324 packet.getPort()), bis); 325 } 326 catch (SocketTimeoutException stex) { 327 } 329 catch (PortUnreachableException purex) { 330 synchronized (DefaultUdpTransportMapping.this) { 331 listener = null; 332 } 333 logger.error(purex); 334 if (logger.isDebugEnabled()) { 335 purex.printStackTrace(); 336 } 337 if (SNMP4JSettings.isFowardRuntimeExceptions()) { 338 throw new RuntimeException (purex); 339 } 340 break; 341 } 342 catch (IOException iox) { 343 logger.warn(iox); 344 if (logger.isDebugEnabled()) { 345 iox.printStackTrace(); 346 } 347 if (SNMP4JSettings.isFowardRuntimeExceptions()) { 348 throw new RuntimeException (iox); 349 } 350 } 351 } 352 synchronized (DefaultUdpTransportMapping.this) { 353 listener = null; 354 stop = true; 355 socket.close(); 356 } 357 } 358 359 public void close() { 360 stop = true; 361 } 362 } 363 } 364 | Popular Tags |