1 11 12 13 package com.sun.jmx.snmp.daemon; 14 15 16 import java.net.InetAddress ; 19 import java.net.DatagramPacket ; 20 import java.net.DatagramSocket ; 21 import java.net.SocketException ; 22 import java.io.IOException ; 23 24 import com.sun.jmx.trace.Trace; 27 28 29 38 39 final class SnmpSocket implements java.lang.Runnable { 40 41 42 45 private DatagramSocket _socket = null; 46 private SnmpResponseHandler _dgramHdlr = null; 47 private Thread _sockThread = null; 48 private byte[] _buffer = null; 49 private transient boolean isClosing = false; 50 51 int _socketPort = 0; 52 int responseBufSize = 1024; 53 String dbgTag = "SnmpSocket"; 54 55 58 64 public SnmpSocket(SnmpResponseHandler rspHdlr, InetAddress addr, int bufferSize) throws SocketException { 65 super(); 66 67 if (isTraceOn()) { 68 trace("constructor", "Creating new SNMP datagram socket"); 69 } 70 71 _socket = new DatagramSocket (0, addr); 73 _socketPort = _socket.getLocalPort(); 74 responseBufSize = bufferSize; 75 _buffer = new byte[responseBufSize]; 76 _dgramHdlr = rspHdlr; 77 _sockThread = new Thread (this, "SnmpSocket"); 78 _sockThread.start(); 79 } 80 81 84 92 public synchronized void sendPacket(byte[] buff, int length, InetAddress addr, int port) throws IOException { 93 DatagramPacket dgrmpkt; 94 dgrmpkt = new DatagramPacket (buff, length, addr, port); 95 sendPacket(dgrmpkt); 96 } 97 98 103 public synchronized void sendPacket(DatagramPacket dgrmpkt) throws IOException { 104 105 try { 106 if (isValid()) { 107 if (isTraceOn()) { 108 trace("sendPacket", "Sending DatagramPacket. Length = " + dgrmpkt.getLength() + 109 " through socket = " + _socket.toString()); 110 } 111 _socket.send(dgrmpkt); 112 } else 113 throw new IOException ("Invalid state of SNMP datagram socket."); 114 } catch (IOException e) { 115 if (isDebugOn()) { 116 debug("sendPacket", "Io error while sending"); 117 debug("sendPacket", e.getMessage()); 118 } 119 throw e; 120 } 121 } 122 123 128 public synchronized boolean isValid() { 129 return _socket != null && _sockThread != null && _sockThread.isAlive(); 130 } 131 132 135 public synchronized void close() { 136 137 isClosing = true; 138 139 if (isTraceOn()) { 140 trace("close", "Closing and destroying the SNMP datagram socket -> " + toString()); 141 } 142 143 try { 144 DatagramSocket sn = new java.net.DatagramSocket (0); 147 byte[] ob = new byte[1]; 148 DatagramPacket pk = new DatagramPacket (ob , 1, java.net.InetAddress.getLocalHost(), _socketPort); 149 sn.send(pk); 150 sn.close(); 151 } catch (Exception e) {} 152 153 if (_socket != null) { 157 _socket.close() ; 158 _socket = null ; 159 } 160 161 if (_sockThread != null && _sockThread.isAlive()) { 164 _sockThread.interrupt(); 165 try { 166 _sockThread.join(); 169 } catch (InterruptedException e) { 170 } 172 _sockThread = null ; 173 } 174 } 175 176 180 public void run() { 181 Thread.currentThread().setPriority(8); 182 183 while (true) { 184 try { 185 DatagramPacket dgrmpkt = new DatagramPacket (_buffer, _buffer.length); 186 187 if (isTraceOn()) { 188 trace("run", "[" + Thread.currentThread().toString() + "]:" + "Blocking for receiving packet"); 189 } 190 191 _socket.receive(dgrmpkt); 192 193 if (isClosing) 196 break; 197 198 if (isTraceOn()) { 199 trace("run", "[" + Thread.currentThread().toString() + "]:" + "Received a packet"); 200 } 201 202 if (dgrmpkt.getLength() <= 0) 203 continue; 204 205 if (isTraceOn()) { 206 trace("run", "[" + Thread.currentThread().toString() + "]:" + "Received a packet from : " + 207 dgrmpkt.getAddress().toString() + ", Length = " + dgrmpkt.getLength()); 208 } 209 210 handleDatagram(dgrmpkt); 211 212 if (isClosing) 215 break; 216 217 } catch (IOException io) { 218 if (isClosing) { 222 break; 223 } 224 if (isDebugOn()) { 225 debug("run", io.getMessage()); 226 debug("run", io); 227 } 228 } catch (Exception e) { 229 if (isClosing) { 233 break; 234 } 235 if (isDebugOn()) { 236 debug("run", "Exception in socket thread..."); 237 debug("run", e); 238 } 239 } catch (ThreadDeath d) { 240 if (isDebugOn()) { 241 debug("run", "Socket Thread DEAD..." + toString()); 242 debug("run", d); 243 } 244 close(); 245 throw d; } catch (Error err) { 247 if (isDebugOn()) { 248 debug("run", err); 249 } 250 handleJavaError(err); 251 } 252 } 253 } 254 255 261 public synchronized void finalize() { 262 close(); 263 } 264 265 268 271 private synchronized void handleJavaError(Throwable thr) { 272 if (thr instanceof OutOfMemoryError ) { 273 if (isDebugOn()) { 274 debug("handleJavaError", thr); 275 } 276 Thread.currentThread().yield(); 277 return ; 278 } 279 if (_socket != null) { 280 _socket.close(); 281 _socket = null; 282 } 283 284 if (isDebugOn()) { 285 debug("handleJavaError", "Global Internal error"); 286 } 287 Thread.currentThread().yield(); 288 } 289 290 private synchronized void handleDatagram(DatagramPacket dgrmpkt) { 291 _dgramHdlr.processDatagram(dgrmpkt); 292 } 293 294 297 boolean isTraceOn() { 298 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP); 299 } 300 301 void trace(String clz, String func, String info) { 302 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP, clz, func, info); 303 } 304 305 void trace(String func, String info) { 306 trace(dbgTag, func, info); 307 } 308 309 boolean isDebugOn() { 310 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP); 311 } 312 313 void debug(String clz, String func, String info) { 314 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, info); 315 } 316 317 void debug(String clz, String func, Throwable exception) { 318 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, exception); 319 } 320 321 void debug(String func, String info) { 322 debug(dbgTag, func, info); 323 } 324 325 void debug(String func, Throwable exception) { 326 debug(dbgTag, func, exception); 327 } 328 } 329 330 | Popular Tags |