📄 snmpcontextv3pool.java
字号:
byte [] res = null; if (context != null) { res = context.encodeDiscoveryPacket(msg_type, rId, errstat, errind, ve, obj); } return res;}public byte [] encodePacket(byte msg_type, int rId, int errstat, int errind, Enumeration ve, Object obj) throws java.io.IOException, EncodingException{ byte [] res = null; if (context != null) { res = context.encodePacket(msg_type, rId, errstat, errind, ve, obj); } return res;}public void sendPacket(byte[] packet){ if (context != null) { context.sendPacket(packet); }}/** * Releases the resources held by this context. This method will * decrement the reference counter. When the reference counter reaches * zero the actual context is removed from the pool and destroyed. */public void destroy(){ try { if (hasChanged == true) { context = getMatchingContext(); } } catch (java.io.IOException exc) { if (AsnObject.debug > 0) { System.out.println(getClass().getName() + ".destroy(): " + exc.getMessage()); } } if (context != null) { String hashKey = getHashKey(); synchronized(contextPool) { int count = 0; SnmpContextPoolItem item = (SnmpContextPoolItem) contextPool.get(hashKey); if (item != null) { count = item.getCounter(); count--; item.setCounter(count); } if (count <= 0) { contextPool.remove(hashKey); context.destroy(); context = null; } } }}/** * Destroys all the contexts in the pool and empties the pool. * * @see #destroy() * @since 4_14 */public void destroyPool(){ Hashtable copyOfPool = null; synchronized(contextPool) { synchronized(contextPool) { copyOfPool = (Hashtable) contextPool.clone(); } contextPool.clear(); } context = null; hasChanged = true; Enumeration keys = copyOfPool.keys(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); SnmpContextPoolItem item = (SnmpContextPoolItem) copyOfPool.get(key); if (item != null) { SnmpContextBasisFace cntxt = (SnmpContextBasisFace) item.getContext(); cntxt.destroy(); } } copyOfPool.clear();}public boolean isDestroyed(){ boolean isDestroyed = true; if (context != null) { isDestroyed = context.isDestroyed(); } return isDestroyed;}/** * Returns a context from the pool. * This methods checks for an existing context that matches all our * properties. If such a context does not exist, a new one is created and * added to the pool. * * @return A context from the pool * @see #getHashKey */protected SnmpContextv3 getMatchingContext()throws java.io.IOException, IllegalArgumentException{ SnmpContextPoolItem item = null; SnmpContextv3 newContext = null; String hashKey = getHashKey(); synchronized(contextPool) { int count=0; if (contextPool.containsKey(hashKey)) { item = (SnmpContextPoolItem) contextPool.get(hashKey); newContext = (SnmpContextv3) item.getContext(); count = item.getCounter(); } else { newContext = new SnmpContextv3(hostname, hostPort, bindAddr, socketType); newContext.setContextEngineId(contextEngineId); newContext.setContextName(contextName); newContext.setUserName(userName); newContext.setUseAuthentication(useAuthentication); newContext.setUserAuthenticationPassword(userAuthenticationPassword); newContext.setAuthenticationProtocol(authenticationProtocol); newContext.setUsePrivacy(usePrivacy); newContext.setUserPrivacyPassword(userPrivacyPassword); newContext.setUsmAgent(usmAgent); item = new SnmpContextPoolItem(newContext); contextPool.put(hashKey, item); } hasChanged = false; count++; item.setCounter(count); } return newContext;}/** * Dumps the pool of contexts. This is for debug purposes. * @param title The title of the dump */public void dumpContexts(String title){ try { if (hasChanged == true) { context = getMatchingContext(); } } catch (java.io.IOException exc) { if (AsnObject.debug > 0) { System.out.println(getClass().getName() + ".dumpContexts(): " + exc.getMessage()); } } System.out.println(title + " " + contextPool.size() + " context(s)"); Enumeration keys = contextPool.keys(); int i=0; while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); SnmpContextPoolItem item = (SnmpContextPoolItem) contextPool.get(key); if (item != null) { int count = item.getCounter(); SnmpContextv3 cntxt = (SnmpContextv3) item.getContext(); if (cntxt == context) { System.out.println("\tcurrent context: "); } System.out.println("\tcontext " + i + ": " + key + ", count: " + count + ", " + cntxt.toString() + "\n" + ", " + cntxt.getDebugString()); i++; } } System.out.println("\thasChanged: " + hasChanged);}/** * Returns the hash key. This key is built out of all properties. It * serves as key for the hashtable of (v3) contexts. * * @return The hash key */public String getHashKey(){ StringBuffer buffer = new StringBuffer(); buffer.append(hostname); buffer.append("_").append(hostPort); buffer.append("_").append(bindAddr); buffer.append("_").append(socketType); buffer.append("_").append(useAuthentication); buffer.append("_").append(authenticationProtocol); buffer.append("_").append(userAuthenticationPassword); buffer.append("_").append(userName); buffer.append("_").append(usePrivacy); buffer.append("_").append(userPrivacyPassword); buffer.append("_").append(SnmpUtilities.toHexString(contextEngineId)); buffer.append("_").append(contextName); buffer.append("_v").append(getVersion()); return buffer.toString();}public void addTrapListener(TrapListener l) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.addTrapListener(l);}public void removeTrapListener(TrapListener l) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.removeTrapListener(l);}public void addTrapListener(TrapListener l, int port) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.addTrapListener(l, port);}public void removeTrapListener(TrapListener l, int port) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.removeTrapListener(l, port);}public void addTrapListener(TrapListener l, ListeningContextPool lcontext) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.addTrapListener(l, lcontext);}public void removeTrapListener(TrapListener l, ListeningContextPool lcontext) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.removeTrapListener(l, lcontext);}public void addRequestPduListener(RequestPduListener l) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.addRequestPduListener(l);}public void removeRequestPduListener(RequestPduListener l) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.removeRequestPduListener(l);}public void addRequestPduListener(RequestPduListener l, int port) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.addRequestPduListener(l, port);}public void removeRequestPduListener(RequestPduListener l, int port) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.removeRequestPduListener(l, port);}public void addRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.addRequestPduListener(l, lcontext);}public void removeRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } context.removeRequestPduListener(l, lcontext);}public Pdu processIncomingPdu(byte [] message)throws DecodingException, java.io.IOException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } Pdu pdu = null; pdu = context.processIncomingPdu(message); return pdu;}/** * Returns a string representation of the object. * @return The string */public String toString(){ String res = ""; try { if (hasChanged == true || context == null) { context = getMatchingContext(); } res = context.toString(); } catch (java.io.IOException exc) { if (AsnObject.debug > 0) { System.out.println(getClass().getName() + ".toString(): " + exc.getMessage()); } } return res;}/** * This method is not supported. It will throw a CloneNotSupportedException. * * @since 4_14 */public Object clone() throws CloneNotSupportedException{ throw new CloneNotSupportedException();}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -