1. /*
  2. * @(#)file SnmpResponseHandler.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 1.9
  5. * @(#)date 04/09/15
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp.daemon;
  12. // JAVA imports
  13. //
  14. import java.net.DatagramPacket;
  15. // JMX imports
  16. //
  17. import com.sun.jmx.snmp.SnmpMessage;
  18. import com.sun.jmx.snmp.SnmpPduFactory;
  19. import com.sun.jmx.snmp.SnmpPduPacket;
  20. import com.sun.jmx.snmp.SnmpPduRequest;
  21. // SNMP Runtime imports
  22. //
  23. import com.sun.jmx.trace.Trace;
  24. /**
  25. * This class is used to handle received inform request responses.
  26. * This classes parses the SNMP inform response packet to obtain the corresponding inform request.
  27. */
  28. class SnmpResponseHandler {
  29. // VARIABLES
  30. //----------
  31. SnmpAdaptorServer adaptor = null;
  32. SnmpQManager snmpq = null;
  33. // CONSTRUCTORS
  34. //-------------
  35. public SnmpResponseHandler(SnmpAdaptorServer adp, SnmpQManager s) {
  36. adaptor = adp;
  37. snmpq = s;
  38. }
  39. // PUBLIC METHODS
  40. //---------------
  41. public synchronized void processDatagram(DatagramPacket dgrm) {
  42. byte []data = dgrm.getData();
  43. int datalen = dgrm.getLength();
  44. if (isTraceOn()) {
  45. trace("processDatagram", "Received from " + dgrm.getAddress().toString() + " Length = " + datalen +
  46. "\nDump : \n" + SnmpMessage.dumpHexBuffer(data, 0, datalen));
  47. }
  48. try {
  49. SnmpMessage msg = new SnmpMessage();
  50. msg.decodeMessage(data, datalen);
  51. msg.address = dgrm.getAddress();
  52. msg.port = dgrm.getPort();
  53. // Retreive the PDU factory of the SNMP adaptor to decode the received inform response.
  54. //
  55. SnmpPduFactory pduFactory = adaptor.getPduFactory();
  56. if (pduFactory == null) {
  57. if (isDebugOn()) {
  58. debug("processDatagram", "Dropping packet. Unable to find the pdu factory of the SNMP adaptor server");
  59. }
  60. }
  61. else {
  62. SnmpPduPacket snmpProt = (SnmpPduPacket)pduFactory.decodeSnmpPdu(msg);
  63. if (snmpProt == null) {
  64. if (isDebugOn()) {
  65. debug("processDatagram", "Dropping packet. Pdu factory returned a null value");
  66. }
  67. }
  68. else if (snmpProt instanceof SnmpPduRequest) {
  69. SnmpPduRequest pduReq = (SnmpPduRequest)snmpProt;
  70. SnmpInformRequest req = snmpq.removeRequest(pduReq.requestId) ;
  71. if (req != null) {
  72. req.invokeOnResponse(pduReq);
  73. } else {
  74. if (isDebugOn()) {
  75. debug("processDatagram", "Dropping packet. Unable to find corresponding for InformRequestId = " + pduReq.requestId);
  76. }
  77. }
  78. }
  79. else {
  80. if (isDebugOn()) {
  81. debug("processDatagram", "Dropping packet. The packet does not contain an inform response");
  82. }
  83. }
  84. snmpProt = null ;
  85. }
  86. } catch (Exception e) {
  87. if (isDebugOn()) {
  88. debug("processDatagram", "Exception while processsing");
  89. debug("processDatagram", e);
  90. }
  91. }
  92. }
  93. // TRACES & DEBUG
  94. //---------------
  95. boolean isTraceOn() {
  96. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP);
  97. }
  98. void trace(String clz, String func, String info) {
  99. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
  100. }
  101. void trace(String func, String info) {
  102. trace(dbgTag, func, info);
  103. }
  104. boolean isDebugOn() {
  105. return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP);
  106. }
  107. void debug(String clz, String func, String info) {
  108. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
  109. }
  110. void debug(String clz, String func, Throwable exception) {
  111. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, exception);
  112. }
  113. void debug(String func, String info) {
  114. debug(dbgTag, func, info);
  115. }
  116. void debug(String func, Throwable exception) {
  117. debug(dbgTag, func, exception);
  118. }
  119. String dbgTag = "SnmpResponseHandler";
  120. }