KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > daemon > SnmpSendServer


1 /*
2  * @(#)file SnmpSendServer.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 1.6
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12 package com.sun.jmx.snmp.daemon;
13
14 import java.util.Enumeration JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 // import debug stuff
18
//
19
import com.sun.jmx.trace.Trace;
20
21 /**
22  * This class starts a thread which picks up a session from the queue
23  * and prepares the inform request protocol data unit (PDU) packet and sends
24  * it to the manager. The request is then added to the wait queue and
25  * marked as one that is waiting for a response.
26  */

27
28 final class SnmpSendServer extends Thread JavaDoc {
29     
30     // VARIABLES
31
//----------
32

33     private int intervalRange = 5 * 1000 ;
34     private Vector JavaDoc readyPool ;
35
36     SnmpQManager snmpq = null ;
37     String JavaDoc dbgTag = "SnmpSendServer";
38     
39     // This boolean is used to stop handling requests while the corresponding SnmpQManager
40
// is being destroyed.
41
//
42
boolean isBeingDestroyed = false;
43     
44     // CONSTRUCTORS
45
//-------------
46

47     public SnmpSendServer(ThreadGroup JavaDoc 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                 // Wait until the thread die.
59
//
60
join();
61             } catch (InterruptedException JavaDoc e) {
62                 // Ignore...
63
}
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 JavaDoc anye) {
80                 if (isDebugOn()) {
81                     debug("run", "Exception in send server");
82                     debug("run", anye);
83                 }
84             } catch (ThreadDeath JavaDoc td) {
85                 // This is not good but Netscape does kill all threads when
86
// the pagecontext changes.
87
if (isDebugOn()) {
88                     debug("run", "Exiting... Fatal error");
89                 }
90                 throw td ;
91             } catch (OutOfMemoryError JavaDoc ome) {
92                 if (isDebugOn()) {
93                     debug("run", "Out of memory");
94                 }
95             } catch (Error JavaDoc 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             // wait to be signaled by the an active request.
108
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                 // Fire all requests as independent requests.
127
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     /**
143      * This will fire the specified request.
144      */

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 JavaDoc reqList) {
155         // Fire all requests as independent requests.
156
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 JavaDoc reqListToString(Vector JavaDoc vec) {
165         StringBuffer JavaDoc s = new StringBuffer JavaDoc(vec.size() * 100) ;
166
167         Enumeration JavaDoc 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 JavaDoc str = s.toString() ;
177         s = null ;
178         return str ;
179     }
180
181     // TRACES & DEBUG
182
//---------------
183

184     boolean isTraceOn() {
185         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP);
186     }
187
188     void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
189         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
190     }
191
192     void trace(String JavaDoc func, String JavaDoc 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 JavaDoc clz, String JavaDoc func, String JavaDoc info) {
201         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
202     }
203
204     void debug(String JavaDoc clz, String JavaDoc func, Throwable JavaDoc exception) {
205         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, exception);
206     }
207
208     void debug(String JavaDoc func, String JavaDoc info) {
209         debug(dbgTag, func, info);
210     }
211     
212     void debug(String JavaDoc func, Throwable JavaDoc exception) {
213         debug(dbgTag, func, exception);
214     }
215 }
216
Popular Tags