KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)file SnmpResponseHandler.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 1.9
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 // JAVA imports
15
//
16
import java.net.DatagramPacket JavaDoc;
17
18 // JMX imports
19
//
20
import com.sun.jmx.snmp.SnmpMessage;
21 import com.sun.jmx.snmp.SnmpPduFactory;
22 import com.sun.jmx.snmp.SnmpPduPacket;
23 import com.sun.jmx.snmp.SnmpPduRequest;
24
25 // SNMP Runtime imports
26
//
27
import com.sun.jmx.trace.Trace;
28
29 /**
30  * This class is used to handle received inform request responses.
31  * This classes parses the SNMP inform response packet to obtain the corresponding inform request.
32  */

33
34 class SnmpResponseHandler {
35     
36     // VARIABLES
37
//----------
38

39     SnmpAdaptorServer adaptor = null;
40     SnmpQManager snmpq = null;
41
42     // CONSTRUCTORS
43
//-------------
44

45     public SnmpResponseHandler(SnmpAdaptorServer adp, SnmpQManager s) {
46         adaptor = adp;
47         snmpq = s;
48     }
49
50     // PUBLIC METHODS
51
//---------------
52

53     public synchronized void processDatagram(DatagramPacket JavaDoc dgrm) {
54         
55         byte []data = dgrm.getData();
56         int datalen = dgrm.getLength();
57         
58         if (isTraceOn()) {
59             trace("processDatagram", "Received from " + dgrm.getAddress().toString() + " Length = " + datalen +
60                   "\nDump : \n" + SnmpMessage.dumpHexBuffer(data, 0, datalen));
61         }
62     
63         try {
64             SnmpMessage msg = new SnmpMessage();
65             msg.decodeMessage(data, datalen);
66             msg.address = dgrm.getAddress();
67             msg.port = dgrm.getPort();
68             
69             // Retreive the PDU factory of the SNMP adaptor to decode the received inform response.
70
//
71
SnmpPduFactory pduFactory = adaptor.getPduFactory();
72             if (pduFactory == null) {
73                 if (isDebugOn()) {
74                     debug("processDatagram", "Dropping packet. Unable to find the pdu factory of the SNMP adaptor server");
75                 }
76             }
77             else {
78                 SnmpPduPacket snmpProt = (SnmpPduPacket)pduFactory.decodeSnmpPdu(msg);
79                 
80                 if (snmpProt == null) {
81                     if (isDebugOn()) {
82                         debug("processDatagram", "Dropping packet. Pdu factory returned a null value");
83                     }
84                 }
85                 else if (snmpProt instanceof SnmpPduRequest) {
86                     
87                     SnmpPduRequest pduReq = (SnmpPduRequest)snmpProt;
88                     SnmpInformRequest req = snmpq.removeRequest(pduReq.requestId) ;
89                     if (req != null) {
90                         req.invokeOnResponse(pduReq);
91                     } else {
92                         if (isDebugOn()) {
93                             debug("processDatagram", "Dropping packet. Unable to find corresponding for InformRequestId = " + pduReq.requestId);
94                         }
95                     }
96                 }
97                 else {
98                     if (isDebugOn()) {
99                         debug("processDatagram", "Dropping packet. The packet does not contain an inform response");
100                     }
101                 }
102                 snmpProt = null ;
103             }
104         } catch (Exception JavaDoc e) {
105             if (isDebugOn()) {
106                 debug("processDatagram", "Exception while processsing");
107                 debug("processDatagram", e);
108             }
109         }
110     }
111
112     // TRACES & DEBUG
113
//---------------
114

115     boolean isTraceOn() {
116         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP);
117     }
118
119     void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
120         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
121     }
122
123     void trace(String JavaDoc func, String JavaDoc info) {
124         trace(dbgTag, func, info);
125     }
126     
127     boolean isDebugOn() {
128         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP);
129     }
130
131     void debug(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
132         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
133     }
134
135     void debug(String JavaDoc clz, String JavaDoc func, Throwable JavaDoc exception) {
136         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, exception);
137     }
138
139     void debug(String JavaDoc func, String JavaDoc info) {
140         debug(dbgTag, func, info);
141     }
142     
143     void debug(String JavaDoc func, Throwable JavaDoc exception) {
144         debug(dbgTag, func, exception);
145     }
146
147     String JavaDoc dbgTag = "SnmpResponseHandler";
148 }
149
Popular Tags