View Javadoc
1   // NAME
2   //      $RCSfile: OneIntPdu.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 3.14 $
7   // CREATED
8   //      $Date: 2006/11/29 16:12:50 $
9   // COPYRIGHT
10  //      Westhawk Ltd
11  // TO DO
12  //
13  
14  /*
15   * Copyright (C) 1995, 1996 by West Consulting BV
16   *
17   * Permission to use, copy, modify, and distribute this software
18   * for any purpose and without fee is hereby granted, provided
19   * that the above copyright notices appear in all copies and that
20   * both the copyright notice and this permission notice appear in
21   * supporting documentation.
22   * This software is provided "as is" without express or implied
23   * warranty.
24   * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
25   */
26  
27  /*
28   * Copyright (C) 1996 - 2006 by Westhawk Ltd
29   * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
30   *
31   * Permission to use, copy, modify, and distribute this software
32   * for any purpose and without fee is hereby granted, provided
33   * that the above copyright notices appear in all copies and that
34   * both the copyright notice and this permission notice appear in
35   * supporting documentation.
36   * This software is provided "as is" without express or implied
37   * warranty.
38   * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
39   */
40   
41  
42  package uk.co.westhawk.snmp.pdu;
43  
44  /*-
45   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
46   * SNMP Java Client
47   * ჻჻჻჻჻჻
48   * Copyright 2023 MetricsHub, Westhawk
49   * ჻჻჻჻჻჻
50   * This program is free software: you can redistribute it and/or modify
51   * it under the terms of the GNU Lesser General Public License as
52   * published by the Free Software Foundation, either version 3 of the
53   * License, or (at your option) any later version.
54   *
55   * This program is distributed in the hope that it will be useful,
56   * but WITHOUT ANY WARRANTY; without even the implied warranty of
57   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
58   * GNU General Lesser Public License for more details.
59   *
60   * You should have received a copy of the GNU General Lesser Public
61   * License along with this program.  If not, see
62   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
63   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
64   */
65  import uk.co.westhawk.snmp.stack.*;
66  import java.util.*;
67  
68  /**
69   * <p>
70   * The OneIntPdu class will ask for one (1) object (oid) of the 
71   * AsnInteger type, based on the Get request.
72   * </p>
73   *
74   * <p>
75   * Unless an exception occurred the Object to the update() method of the
76   * Observer will be an Integer.
77   * In the case of an exception, that exception will be passed.
78   * </p>
79   *
80   * <p>
81   * For SNMPv3: The receiver of a request PDU acts as the authoritative engine.
82   * </p>
83   *
84   * @see GetPdu_vec
85   *
86   * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
87   * @version $Revision: 3.14 $ $Date: 2006/11/29 16:12:50 $
88   */
89  public class OneIntPdu extends GetPdu {
90      private static final String version_id = "@(#)$Id: OneIntPdu.java,v 3.14 2006/11/29 16:12:50 birgit Exp $ Copyright Westhawk Ltd";
91  
92      Integer value;
93  
94      /**
95       * Constructor.
96       *
97       * @param con The context of the request
98       */
99      public OneIntPdu(SnmpContextBasisFace con) {
100         super(con);
101     }
102 
103     /**
104      * Constructor that will send the request immediately. No Observer
105      * is set.
106      *
107      * @param con the SnmpContextBasisFace
108      * @param oid the oid
109      */
110     public OneIntPdu(SnmpContextBasisFace con, String oid)
111             throws PduException, java.io.IOException {
112         this(con, oid, null);
113     }
114 
115     /**
116      * Constructor that will send the request immediately.
117      *
118      * @param con the SnmpContextBasisFace
119      * @param oid the oid
120      * @param o   the Observer that will be notified when the answer is received
121      */
122     public OneIntPdu(SnmpContextBasisFace con, String oid, Observer o)
123             throws PduException, java.io.IOException {
124         super(con);
125         if (o != null) {
126             addObserver(o);
127         }
128         addOid(oid);
129         send();
130     }
131 
132     /**
133      * Returns the value (the answer) of this request.
134      *
135      * @return the value
136      */
137     public Integer getValue() {
138         return value;
139     }
140 
141     /**
142      * The value of the request is set. This will be called by
143      * Pdu.fillin().
144      *
145      * @param n   the index of the value
146      * @param res the value
147      * @see Pdu#new_value
148      */
149     protected void new_value(int n, varbind res) {
150         AsnObject val = res.getValue();
151         if (val instanceof AsnInteger) {
152             AsnInteger va = (AsnInteger) res.getValue();
153             if (n == 0) {
154                 value = new Integer(va.getValue());
155             }
156         } else {
157             value = null;
158         }
159     }
160 
161     /**
162      * This method notifies all observers.
163      * This will be called by Pdu.fillin().
164      * 
165      * <p>
166      * Unless an exception occurred the Object to the update() method of the
167      * Observer will be an Integer.
168      * In the case of an exception, that exception will be passed.
169      * </p>
170      */
171     protected void tell_them() {
172         notifyObservers(value);
173     }
174 
175 }