📄 snmpcontextv3pool.java
字号:
// NAME// $RCSfile: SnmpContextv3Pool.java,v $// DESCRIPTION// [given below in javadoc format]// DELTA// $Revision: 3.25 $// CREATED// $Date: 2006/11/30 17:37:06 $// COPYRIGHT// Westhawk Ltd// TO DO///* * Copyright (C) 2000 - 2006 by Westhawk Ltd * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a> * * Permission to use, copy, modify, and distribute this software * for any purpose and without fee is hereby granted, provided * that the above copyright notices appear in all copies and that * both the copyright notice and this permission notice appear in * supporting documentation. * This software is provided "as is" without express or implied * warranty. */package uk.co.westhawk.snmp.stack;import uk.co.westhawk.snmp.pdu.*;import uk.co.westhawk.snmp.util.*;import uk.co.westhawk.snmp.event.*;import java.util.*;/** * This class contains the pool of SNMP v3 contexts. * This class reuses the existings contexts instead of creating a new * one every time. * <p> * Every time a property changes the pool is checked for a SnmpContextv3 * context that matches all the new properties of this class. If no such * context exists, a new one is made. * The PDUs associated with the old context remain associated with the * old context. * </p> * * <p> * A counter indicates the number of times the context is referenced. * The counter is decreased when <code>destroy()</code> is called. * When the counter * reaches zero, the context is released. * </p> * * <p> * Note that because the underlying context can change when a property * is changed and the PDUs remain associated with the old context, all * properties have to be set BEFORE a PDU is sent. * </p> * * @see SnmpContextv3 * @see SnmpContextPool * @see SnmpContextv2cPool * * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a> * @version $Revision: 3.25 $ $Date: 2006/11/30 17:37:06 $ */public class SnmpContextv3Pool implements SnmpContextv3Face{ private static final String version_id = "@(#)$Id: SnmpContextv3Pool.java,v 3.25 2006/11/30 17:37:06 birgit Exp $ Copyright Westhawk Ltd"; protected static Hashtable contextPool; protected String hostname, socketType, bindAddr; protected int hostPort; protected SnmpContextv3 context = null; protected String userName = ""; protected boolean useAuthentication = false; protected String userAuthenticationPassword = ""; protected boolean usePrivacy = false; protected String userPrivacyPassword = ""; protected int authenticationProtocol = MD5_PROTOCOL; protected byte [] contextEngineId = new byte[0]; protected String contextName = Default_ContextName; protected UsmAgent usmAgent = null; protected boolean hasChanged = false;/** * Constructor, using the Standard socket. * * @param host The host to which the PDU will be sent * @param port The port where the SNMP server will be * @see SnmpContextv3#SnmpContextv3(String, int) */public SnmpContextv3Pool(String host, int port) throws java.io.IOException{ this(host, port, null, STANDARD_SOCKET);}/** * Constructor. * * @param host The host to which the PDU will be sent * @param port The port where the SNMP server will be * @param typeSocket The type of socket to use. * * @see SnmpContextv3#SnmpContextv3(String, int, String) * @see SnmpContextBasisFace#STANDARD_SOCKET * @see SnmpContextBasisFace#TCP_SOCKET */public SnmpContextv3Pool(String host, int port, String typeSocket)throws java.io.IOException{ this(host, port, null, typeSocket);}/** * Constructor. * * @param host The host to which the PDU will be sent * @param port The port where the SNMP server will be * @param bindAddress The local address the server will bind to * @param typeSocket The type of socket to use. * * @see SnmpContextv3#SnmpContextv3(String, int, String) * @see SnmpContextBasisFace#STANDARD_SOCKET * @see SnmpContextBasisFace#TCP_SOCKET * * @since 4_14 */public SnmpContextv3Pool(String host, int port, String bindAddress, String typeSocket)throws java.io.IOException{ initPools(); hostname = host; hostPort = port; bindAddr = bindAddress; socketType = typeSocket; // No point in creating a context, a lot of the parameters // are probably going to be set. //context = getMatchingContext();}private static synchronized void initPools(){ if (contextPool == null) { contextPool = new Hashtable(5); }}/** * Returns the SNMP version of the context. * * @return The version */public int getVersion(){ return SnmpConstants.SNMP_VERSION_3;}/** * Returns the host. * * @return The host */public String getHost(){ return hostname;}/** * Returns the port number. * * @return The port no */public int getPort(){ return hostPort;}public String getBindAddress(){ return bindAddr;}/** * Returns the type of socket. * * @return The type of socket */public String getTypeSocket(){ return socketType;}public String getSendToHostAddress(){ String res = null; if (context != null) { res = context.getSendToHostAddress(); } return res;}public String getReceivedFromHostAddress(){ String res = null; if (context != null) { res = context.getReceivedFromHostAddress(); } return res;}public String getUserName(){ return userName;}public void setUserName(String newUserName){ if (newUserName != null && newUserName.equals(userName) == false) { userName = newUserName; hasChanged = true; }}public boolean isUseAuthentication(){ return useAuthentication;}public void setUseAuthentication(boolean newUseAuthentication){ if (newUseAuthentication != useAuthentication) { useAuthentication = newUseAuthentication; hasChanged = true; }}public String getUserAuthenticationPassword(){ return userAuthenticationPassword;}public void setUserAuthenticationPassword(String newUserAuthenticationPd){ if (newUserAuthenticationPd != null && newUserAuthenticationPd.equals(userAuthenticationPassword) == false) { userAuthenticationPassword = newUserAuthenticationPd; hasChanged = true; }}public void setAuthenticationProtocol(int protocol)throws IllegalArgumentException{ if (protocol == MD5_PROTOCOL || protocol == SHA1_PROTOCOL) { if (protocol != authenticationProtocol) { authenticationProtocol = protocol; hasChanged = true; } } else { hasChanged = false; throw new IllegalArgumentException("Authentication Protocol " + "should be MD5 or SHA1"); }}public int getAuthenticationProtocol(){ return authenticationProtocol;}public boolean isUsePrivacy(){ return usePrivacy;}public void setUsePrivacy(boolean newUsePrivacy){ if (newUsePrivacy != usePrivacy) { usePrivacy = newUsePrivacy; hasChanged = true; }}public String getUserPrivacyPassword(){ return userPrivacyPassword;}public void setUserPrivacyPassword(String newUserPrivacyPd){ if (newUserPrivacyPd != null && newUserPrivacyPd.equals(userPrivacyPassword) == false) { userPrivacyPassword = newUserPrivacyPd; hasChanged = true; }}public void setContextEngineId(byte [] newContextEngineId)throws IllegalArgumentException{ if (newContextEngineId != null) { if (newContextEngineId.equals(contextEngineId) == false) { contextEngineId = newContextEngineId; hasChanged = true; } } else { hasChanged = false; throw new IllegalArgumentException("contextEngineId is null"); }}public byte [] getContextEngineId(){ return contextEngineId;}public void setContextName(String newContextName){ if (newContextName != null && newContextName.equals(contextName) == false) { contextName = newContextName; hasChanged = true; }}public String getContextName(){ return contextName;}public void setUsmAgent(UsmAgent newAgent){ if (newAgent != null && newAgent != usmAgent) { usmAgent = newAgent; hasChanged = true; }}public UsmAgent getUsmAgent(){ return usmAgent;}public boolean addDiscoveryPdu(DiscoveryPdu pdu)throws java.io.IOException, PduException, IllegalArgumentException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } return context.addDiscoveryPdu(pdu);}public boolean addPdu(Pdu pdu)throws java.io.IOException, PduException, IllegalArgumentException{ if (hasChanged == true || context == null) { context = getMatchingContext(); } return context.addPdu(pdu);}public boolean removePdu(int requestId){ boolean res = false; if (context != null) { res = context.removePdu(requestId); } return res;}public byte [] encodeDiscoveryPacket(byte msg_type, int rId, int errstat, int errind, Enumeration ve, Object obj) throws java.io.IOException, EncodingException{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -