1 11 12 package com.sun.jmx.snmp.daemon; 13 14 import java.util.Enumeration ; 15 import java.util.Vector ; 16 17 import com.sun.jmx.trace.Trace; 20 21 27 28 final class SnmpSendServer extends Thread { 29 30 33 private int intervalRange = 5 * 1000 ; 34 private Vector readyPool ; 35 36 SnmpQManager snmpq = null ; 37 String dbgTag = "SnmpSendServer"; 38 39 boolean isBeingDestroyed = false; 43 44 47 public SnmpSendServer(ThreadGroup grp, SnmpQManager q) { 48 super(grp, "SnmpSendServer") ; 49 snmpq = q ; 50 start() ; 51 } 52 53 public synchronized void stopSendServer() { 54 55 if (isAlive()) { 56 interrupt(); 57 try { 58 join(); 61 } catch (InterruptedException e) { 62 } 64 } 65 } 66 67 public void run () { 68 Thread.currentThread().setPriority(Thread.NORM_PRIORITY); 69 70 if (isTraceOn()) { 71 trace("run", "Thread Started"); 72 } 73 74 while (true) { 75 try { 76 prepareAndSendRequest() ; 77 if (isBeingDestroyed == true) 78 break; 79 } catch (Exception anye) { 80 if (isDebugOn()) { 81 debug("run", "Exception in send server"); 82 debug("run", anye); 83 } 84 } catch (ThreadDeath td) { 85 if (isDebugOn()) { 88 debug("run", "Exiting... Fatal error"); 89 } 90 throw td ; 91 } catch (OutOfMemoryError ome) { 92 if (isDebugOn()) { 93 debug("run", "Out of memory"); 94 } 95 } catch (Error err) { 96 if (isDebugOn()) { 97 debug("run", err); 98 } 99 throw err ; 100 } 101 } 102 } 103 104 private void prepareAndSendRequest() { 105 106 if (readyPool == null || readyPool.isEmpty()) { 107 if (isTraceOn()) { 109 trace("prepareAndSendRequest", "Blocking for inform requests"); 110 } 111 readyPool = snmpq.getAllOutstandingRequest(intervalRange) ; 112 if (isBeingDestroyed == true) 113 return; 114 } else { 115 if (isDebugOn()) { 116 debug("prepareAndSendRequest", "Inform requests from a previous block left unprocessed. Will try again"); 117 } 118 } 119 120 if (isTraceOn()) { 121 trace("prepareAndSendRequest", "List of inform requests to send : " + reqListToString(readyPool)); 122 } 123 124 synchronized(this) { 125 if (readyPool.size() < 2) { 126 fireRequestList(readyPool) ; 128 return ; 129 } 130 131 while (!readyPool.isEmpty()) { 132 SnmpInformRequest req = (SnmpInformRequest) readyPool.lastElement() ; 133 if (req != null && req.inProgress()) { 134 fireRequest(req) ; 135 } 136 readyPool.removeElementAt(readyPool.size() - 1) ; 137 } 138 readyPool.removeAllElements() ; 139 } 140 } 141 142 145 void fireRequest(SnmpInformRequest req) { 146 if (req != null && req.inProgress()) { 147 if (isTraceOn()) { 148 trace("fireRequest", "Firing inform request directly. -> " + req.getRequestId()); 149 } 150 req.action() ; 151 } 152 } 153 154 void fireRequestList(Vector reqList) { 155 while (!reqList.isEmpty()) { 157 SnmpInformRequest req = (SnmpInformRequest) reqList.lastElement() ; 158 if (req != null && req.inProgress()) 159 fireRequest(req) ; 160 reqList.removeElementAt(reqList.size() - 1) ; 161 } 162 } 163 164 final String reqListToString(Vector vec) { 165 StringBuffer s = new StringBuffer (vec.size() * 100) ; 166 167 Enumeration dbge = vec.elements() ; 168 while (dbge.hasMoreElements()) { 169 SnmpInformRequest reqc = (SnmpInformRequest) dbge.nextElement() ; 170 s.append("InformRequestId -> ") ; 171 s.append(reqc.getRequestId()) ; 172 s.append(" / Destination -> ") ; 173 s.append(reqc.getAddress()) ; 174 s.append(". ") ; 175 } 176 String str = s.toString() ; 177 s = null ; 178 return str ; 179 } 180 181 184 boolean isTraceOn() { 185 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP); 186 } 187 188 void trace(String clz, String func, String info) { 189 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP, clz, func, info); 190 } 191 192 void trace(String func, String info) { 193 trace(dbgTag, func, info); 194 } 195 196 boolean isDebugOn() { 197 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP); 198 } 199 200 void debug(String clz, String func, String info) { 201 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, info); 202 } 203 204 void debug(String clz, String func, Throwable exception) { 205 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, exception); 206 } 207 208 void debug(String func, String info) { 209 debug(dbgTag, func, info); 210 } 211 212 void debug(String func, Throwable exception) { 213 debug(dbgTag, func, exception); 214 } 215 } 216 | Popular Tags |