View Javadoc
1   // NAME
2   //      $RCSfile: InterfaceGetNextPdu.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 3.17 $
7   // CREATED
8   //      $Date: 2008/05/06 10:17:06 $
9   // COPYRIGHT
10  //      Westhawk Ltd
11  // TO DO
12  //
13  
14  /*
15   * Copyright (C) 2005 - 2006 by Westhawk Ltd
16   * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
17   *
18   * Permission to use, copy, modify, and distribute this software
19   * for any purpose and without fee is hereby granted, provided
20   * that the above copyright notices appear in all copies and that
21   * both the copyright notice and this permission notice appear in
22   * supporting documentation.
23   * This software is provided "as is" without express or implied
24   * warranty.
25   * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
26   */
27   
28  package uk.co.westhawk.snmp.pdu;
29  
30  /*-
31   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
32   * SNMP Java Client
33   * ჻჻჻჻჻჻
34   * Copyright 2023 MetricsHub, Westhawk
35   * ჻჻჻჻჻჻
36   * This program is free software: you can redistribute it and/or modify
37   * it under the terms of the GNU Lesser General Public License as
38   * published by the Free Software Foundation, either version 3 of the
39   * License, or (at your option) any later version.
40   *
41   * This program is distributed in the hope that it will be useful,
42   * but WITHOUT ANY WARRANTY; without even the implied warranty of
43   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44   * GNU General Lesser Public License for more details.
45   *
46   * You should have received a copy of the GNU General Lesser Public
47   * License along with this program.  If not, see
48   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
49   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
50   */
51  
52  import uk.co.westhawk.snmp.stack.*;
53  import uk.co.westhawk.snmp.pdu.*;
54  import java.util.*;
55  
56  
57  /**
58   * The class InterfaceGetNextPdu.
59   *
60   * This file is auto generated by the StubBrowser utility, using Mibble.
61   * See the uk/co/westhawk/stub/ directory.
62   *
63   * Added speed parameter and methods by hand.
64  
65   * Make sure that you replace the package name and classname placeholders. 
66   * Also, move this file to the correct package directory.
67   * If these things are not done, this class will not compile correctly!!
68   *
69   * @version $Revision: 3.17 $ $Date: 2008/05/06 10:17:06 $
70   */
71  public class InterfaceGetNextPdu extends InterfaceGetNextPduStub {
72      private static final String version_id = "@(#)$Id: InterfaceGetNextPdu.java,v 3.17 2008/05/06 10:17:06 birgita Exp $ Copyright Westhawk Ltd";
73  
74      protected long _speed;
75  
76      /**
77       * Constructor.
78       *
79       * @param con The context of the request
80       */
81      public InterfaceGetNextPdu(SnmpContextBasisFace con) {
82          super(con);
83      }
84  
85      /**
86       * Returns the last calculates speed.
87       *
88       * @see #getSpeed(InterfaceGetNextPdu)
89       */
90      public long getSpeed() {
91          return _speed;
92      }
93  
94      /**
95       * Calculates the speed of the interface. This is done by providing the
96       * method with <i>the previous value of this interface</i>. An interface
97       * is marked by its index. Do <i>not</i> confuse it
98       * with <i>the previous interface ifInOctets the MIB</i>.
99       * Total number of octets (received and transmitted) per second.
100      *
101      * @param old The previous value of this interface
102      */
103     public long getSpeed(InterfaceGetNextPdu old) {
104         _speed = -1;
105         if (this._ifOperStatus > 0
106                 &&
107                 old._ifOperStatus > 0
108                 &&
109                 this._valid
110                 &&
111                 old._valid) {
112             long tdiff = (this._sysUpTime - old._sysUpTime);
113             if (tdiff != 0) {
114                 long inO = this._ifInOctets - old._ifInOctets;
115                 long outO = this._ifOutOctets - old._ifOutOctets;
116 
117                 _speed = 100 * (inO + outO) / tdiff;
118             }
119         } else {
120             _speed = -1;
121         }
122         return _speed;
123     }
124 
125     /**
126      * Returns how many interfaces are present.
127      *
128      * @return the number of interfaces
129      */
130     public static int getIfNumber(SnmpContextBasisFace con)
131             throws PduException, java.io.IOException {
132         int ifNumber = 0;
133 
134         if (con != null) {
135             OneIntPdu ifNumberPdu = new OneIntPdu(con, ifNumber_OID + ".0");
136             boolean answered = ifNumberPdu.waitForSelf();
137             boolean timedOut = ifNumberPdu.isTimedOut();
138             if (timedOut == false) {
139                 Integer intValue = ifNumberPdu.getValue();
140                 if (intValue != null) {
141                     ifNumber = intValue.intValue();
142                 }
143             }
144         }
145         return ifNumber;
146     }
147 
148     public String toString() {
149         StringBuffer buffer = new StringBuffer(getClass().getName());
150         buffer.append("[");
151         buffer.append(super.toString());
152         buffer.append(", speed=").append(_speed);
153         buffer.append("]");
154         return buffer.toString();
155     }
156 
157 }