1 // NAME
2 // $RCSfile: OneInterfaceBean.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 1.14 $
7 // CREATED
8 // $Date: 2006/01/25 18:08:56 $
9 // COPYRIGHT
10 // Westhawk Ltd
11 // TO DO
12 //
13
14 /*
15 * Copyright (C) 1998 - 2006 by Westhawk Ltd
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 package uk.co.westhawk.snmp.beans;
28
29 /*-
30 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
31 * SNMP Java Client
32 * ჻჻჻჻჻჻
33 * Copyright 2023 MetricsHub, Westhawk
34 * ჻჻჻჻჻჻
35 * This program is free software: you can redistribute it and/or modify
36 * it under the terms of the GNU Lesser General Public License as
37 * published by the Free Software Foundation, either version 3 of the
38 * License, or (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Lesser Public License for more details.
44 *
45 * You should have received a copy of the GNU General Lesser Public
46 * License along with this program. If not, see
47 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
48 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
49 */
50
51 import uk.co.westhawk.snmp.stack.*;
52 import uk.co.westhawk.snmp.pdu.*;
53 import java.awt.*;
54 import java.util.*;
55 import java.text.*;
56 import java.lang.*;
57 import java.io.*;
58 import java.beans.*;
59
60 /**
61 * <p>
62 * This bean collects information about one interface.
63 * </p>
64 *
65 * <p>
66 * The properties in the parent classes should be set, before calling
67 * the action() method. Via a PropertyChangeEvent the application/applet
68 * will be notified.
69 * </p>
70 *
71 * @see SNMPBean#setHost
72 * @see SNMPBean#setPort
73 * @see SNMPBean#setCommunityName
74 * @see SNMPRunBean#setUpdateInterval
75 * @see #setIndex(int)
76 * @see SNMPBean#addPropertyChangeListener
77 * @see SNMPBean#action
78 * @see InterfaceGetNextPdu
79 * @see InterfaceIndexesBean
80 *
81 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
82 * @version $Revision: 1.14 $ $Date: 2006/01/25 18:08:56 $
83 */
84 public class OneInterfaceBean extends SNMPRunBean implements Observer {
85 private static final String version_id = "@(#)$Id: OneInterfaceBean.java,v 1.14 2006/01/25 18:08:56 birgit Exp $ Copyright Westhawk Ltd";
86
87 private InterfaceGetNextPdu pdu, prev;
88
89 private boolean isPduInFlight;
90 private Date lastUpdateDate = null;
91
92 private int index = 1;
93 private String descr = "";
94 private String operState = "";
95 private long speed = -1;
96
97 /**
98 * The default constructor.
99 */
100 public OneInterfaceBean() {
101 }
102
103 /**
104 * The constructor that will set the host and the port no.
105 *
106 * @param h the hostname
107 * @param p the port no
108 * @see SNMPBean#setHost
109 * @see SNMPBean#setPort
110 */
111 public OneInterfaceBean(String h, int p) {
112 this(h, p, null);
113 }
114
115 /**
116 * The constructor that will set the host, the port no and the local
117 * bind address.
118 *
119 * @param h the hostname
120 * @param p the port no
121 * @param b the local bind address
122 * @see SNMPBean#setHost
123 * @see SNMPBean#setPort
124 * @see SNMPBean#setBindAddress
125 *
126 * @since 4_14
127 */
128 public OneInterfaceBean(String h, int p, String b) {
129 this();
130 setHost(h);
131 setPort(p);
132 setBindAddress(b);
133 }
134
135 /**
136 * Sets the index of the interface that will be requested.
137 *
138 * @param i the index
139 * @see #getIndex()
140 */
141 public void setIndex(int i) {
142 if (index != i) {
143 index = i;
144 }
145 }
146
147 /**
148 * Returns the index of the interface.
149 *
150 * @return the index
151 * @see #setIndex(int)
152 */
153 public int getIndex() {
154 return index;
155 }
156
157 /**
158 * Returns the description of the interface.
159 *
160 * @return the description
161 */
162 public String getDescription() {
163 return descr;
164 }
165
166 /**
167 * Returns the operation state of the interface.
168 *
169 * @return the operation state
170 */
171 public String getOperStatusString() {
172 return operState;
173 }
174
175 /**
176 * Returns the speed (bits per second) of the interface.
177 *
178 * @return the speed
179 */
180 public long getSpeed() {
181 return speed;
182 }
183
184 /**
185 * Returns the date of the moment when this bean was last updated.
186 * This might be null when the first time the update was not finished.
187 *
188 * @return the last update date
189 */
190 public Date getLastUpdateDate() {
191 return lastUpdateDate;
192 }
193
194 /**
195 * This method starts sending the SNMP request. All properties should be
196 * set before this method is called.
197 *
198 * The actual sending will take place in the run method.
199 * It makes a new snmp context and initialises all variables before
200 * starting.
201 */
202 public void action() {
203 if (isHostPortReachable()) {
204 lastUpdateDate = new Date();
205 isPduInFlight = false;
206 setRunning(true);
207 }
208 }
209
210 /**
211 * The run method according to the Runnable interface.
212 * This method will send the Pdu request, if the previous one is not
213 * still in flight.
214 *
215 * @see SNMPRunBean#isRunning()
216 */
217 public void run() {
218 while (context != null && isRunning()) {
219 if (isPduInFlight == false) {
220 isPduInFlight = true;
221 prev = pdu;
222 try {
223 pdu = new InterfaceGetNextPdu(context);
224 pdu.addObserver(this);
225 pdu.addOids(index - 1);
226 pdu.send();
227 } catch (PduException exc) {
228 System.out.println("PduException " + exc.getMessage());
229 } catch (IOException exc) {
230 System.out.println("IOException " + exc.getMessage());
231 }
232 }
233
234 try {
235 Thread.sleep(interval);
236 } catch (InterruptedException ix) {
237 ;
238 }
239 }
240 }
241
242 /**
243 * The update method according to the Observer interface, it will be
244 * called when the Pdu response is received.
245 * The speed is calculated with the previous answer, after that the
246 * property change event is fired.
247 *
248 * @see SNMPBean#addPropertyChangeListener
249 */
250 public void update(Observable obs, Object ov) {
251 if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) {
252 if (prev != null) {
253 speed = pdu.getSpeed(prev);
254 }
255
256 descr = pdu.getIfDescr();
257 operState = pdu.getIfOperStatusStr();
258
259 lastUpdateDate = new Date();
260 isPduInFlight = false;
261 firePropertyChange("Interface", null, null);
262 }
263 }
264
265 }