📄 snmpsession.java
字号:
/* * @(#)file SnmpSession.java * @(#)author Sun Microsystems, Inc. * @(#)version 1.13 * @(#)date 08/07/21 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * */package com.sun.jmx.snmp.daemon;// java imports//import java.util.Vector;import java.util.Enumeration;import java.util.Hashtable;import java.util.Stack;import java.net.InetAddress;import java.net.SocketException;import java.io.InterruptedIOException;// jmx imports//import com.sun.jmx.snmp.SnmpDefinitions;import com.sun.jmx.snmp.SnmpStatusException;import com.sun.jmx.snmp.SnmpVarBindList;import com.sun.jmx.snmp.SnmpScopedPduRequest;// SNMP runtime imports//import com.sun.jmx.trace.Trace;/** * This class is used for sending INFORM REQUESTS from an agent to a manager. * * Creates, controls, and manages one or more inform requests. * * The SnmpSession maintains the list of all active inform requests and inform responses. * Each SnmpSession has a dispatcher that is a thread used to service all the inform requests it creates * and each SnmpSession uses a separate socket for sending/receiving inform requests/responses. * * An SnmpSession object is associated with an SNMP adaptor server. * It is created the first time an inform request is sent by the SNMP adaptor server * and is destroyed (with its associated SnmpSocket) when the SNMP adaptor server is stopped. * */class SnmpSession implements SnmpDefinitions, Runnable { // PRIVATE VARIABLES //------------------ /** * The SNMP adaptor associated with this SnmpSession. */ protected transient SnmpAdaptorServer adaptor; /** * The SnmpSocket to be used to communicate with the manager * by all inform requests created in this session. */ protected transient SnmpSocket informSocket = null; /** * This table maintains the list of inform requests. */ private transient Hashtable informRequestList = new Hashtable(); /** * This table maintains the list of inform responses. * A FIFO queue is needed here. */ private transient Stack informRespq = new Stack(); /** * The dispatcher that will service all inform responses to inform requests generated * using this session object. An SnmpSession object creates one or more inform requests. * Thus it services all inform requests, which are created by this session object, * when an inform response arrives for an inform request generated by the session. */ private transient Thread myThread = null; /** * Request being synchronized from session thread. This happens when * a user does sync operation from a callback. */ private transient SnmpInformRequest syncInformReq ; SnmpQManager snmpQman = null; String dbgTag = "SnmpSession"; private boolean isBeingCancelled = false; // PUBLIC CONSTRUCTORS //-------------------- /** * Constructor for creating a new session. * @param adp The SNMP adaptor associated with this SnmpSession. * @exception SocketException Unable to initialize the SnmpSocket. */ public SnmpSession(SnmpAdaptorServer adp) throws SocketException { adaptor = adp; snmpQman = new SnmpQManager(); SnmpResponseHandler snmpRespHdlr = new SnmpResponseHandler(adp, snmpQman); initialize(adp, snmpRespHdlr); } /** * Constructor for creating a new session. Allows subclassing. */ public SnmpSession() throws SocketException { } // OTHER METHODS //-------------- /** * Initializes the SnmpSession. * @param adp The SNMP adaptor associated with this SnmpSession. * @exception SocketException Unable to initialize the SnmpSocket. */ protected synchronized void initialize(SnmpAdaptorServer adp, SnmpResponseHandler snmpRespHdlr) throws SocketException { informSocket = new SnmpSocket(snmpRespHdlr, adp.getAddress(), adp.getBufferSize().intValue()); myThread = new Thread(this, "SnmpSession"); myThread.start(); } /** * Indicates whether the thread for this session is active and the SNMP adaptor server ONLINE. * @return true if active, false otherwise. */ synchronized boolean isSessionActive() { //return ((myThread != null) && (myThread.isAlive())); return ((adaptor.isActive()) && (myThread != null) && (myThread.isAlive())); } /** * Gets the SnmpSocket which will be used by inform requests created in this session. * @return The socket which will be used in this session. */ SnmpSocket getSocket() { return informSocket; } /** * Gets the SnmpQManager which will be used by inform requests created in this session. * @return The SnmpQManager which will be used in this session. */ SnmpQManager getSnmpQManager() { return snmpQman; } /** * Indicates whether this session is performing synchronous operation for an inform request. * @return <CODE>true</CODE> if the session is performing synchronous operation, <CODE>false</CODE> otherwise. */ private synchronized boolean syncInProgress() { return syncInformReq != null ; } private synchronized void setSyncMode(SnmpInformRequest req) { syncInformReq = req ; } private synchronized void resetSyncMode() { if (syncInformReq == null) return ; syncInformReq = null ; if (thisSessionContext()) return ; this.notifyAll() ; } /** * Returns <CODE>true</CODE> if the current executing thread is this session's dispatcher. * Typically used to detect whether the user is doing a sync operation from * this dispatcher context. For instance, a user gives a sync command * from within a request callback using its associated session. * @return <CODE>true</CODE> if current thread is this session's dispatcher, <CODE>false</CODE> otherwise. */ boolean thisSessionContext() { return (Thread.currentThread() == myThread) ; } /** * Sends an inform request to the specified InetAddress destination using the specified community string. * @param addr The InetAddress destination for this inform request. * @param cs The community string to be used for the inform request. * @param cb The callback that is invoked when a request is complete. * @param vblst A list of SnmpVarBind instances or null. * @exception SnmpStatusException SNMP adaptor is not ONLINE or session * is dead. */ SnmpInformRequest makeAsyncRequest(InetAddress addr, String cs, SnmpInformHandler cb, SnmpVarBindList vblst, int port) throws SnmpStatusException { if (!isSessionActive()) { throw new SnmpStatusException("SNMP adaptor server not ONLINE"); } SnmpInformRequest snmpreq = new SnmpInformRequest(this, adaptor, addr, cs, port, cb); snmpreq.start(vblst); return snmpreq; } /** * Performs sync operations on active requests. Any number of inform requests * can be done in sync mode but only one per thread. * The user can do synchronous operation using the request handle only. */ void waitForResponse(SnmpInformRequest req, long waitTime) { if (! req.inProgress()) return ; setSyncMode(req) ; if (isTraceOn()) { trace("waitForResponse", "Session switching to sync mode for inform request " + req.getRequestId()); } long maxTime ; if (waitTime <= 0) maxTime = System.currentTimeMillis() + 6000 * 1000 ; else maxTime = System.currentTimeMillis() + waitTime ; while (req.inProgress() || syncInProgress()) { waitTime = maxTime - System.currentTimeMillis() ; if (waitTime <= 0) break ; synchronized (this) { if (! informRespq.removeElement(req)) { try { this.wait(waitTime) ; } catch(InterruptedException e) { } continue ; } } try { processResponse(req) ; } catch (Exception e) { if (isDebugOn()) { debug("waitForResponse", e); } } } resetSyncMode() ; return ; } /** * Dispatcher method for this session thread. This is the dispatcher method * which goes in an endless-loop and waits for servicing inform requests * which received a reply from the manager. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -