View Javadoc
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 Sentry Software, 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  {
86      private static final String     version_id =
87          "@(#)$Id: OneInterfaceBean.java,v 1.14 2006/01/25 18:08:56 birgit Exp $ Copyright Westhawk Ltd";
88  
89      private InterfaceGetNextPdu pdu, prev;
90  
91      private boolean isPduInFlight;
92      private Date lastUpdateDate = null;
93  
94      private int index = 1;
95      private String descr = "";
96      private String operState = "";
97      private long speed = -1;
98  
99  
100 /**
101  * The default constructor.
102  */
103 public OneInterfaceBean() 
104 {
105 }
106 
107 /**
108  * The constructor that will set the host and the port no.
109  *
110  * @param h the hostname
111  * @param p the port no
112  * @see SNMPBean#setHost
113  * @see SNMPBean#setPort
114  */
115 public OneInterfaceBean(String h, int p) 
116 {
117     this(h, p, null);
118 }
119 
120 /**
121  * The constructor that will set the host, the port no and the local
122  * bind address.
123  *
124  * @param h the hostname
125  * @param p the port no
126  * @param b the local bind address
127  * @see SNMPBean#setHost
128  * @see SNMPBean#setPort
129  * @see SNMPBean#setBindAddress
130  *
131  * @since 4_14
132  */
133 public OneInterfaceBean(String h, int p, String b) 
134 {
135     this();
136     setHost(h);
137     setPort(p);
138     setBindAddress(b);
139 }
140 
141 /**
142  * Sets the index of the interface that will be requested.
143  * @param i the index
144  * @see #getIndex()
145  */
146 public void setIndex(int i)
147 {
148     if (index != i)
149     {
150         index = i;
151     }
152 }
153 
154 /**
155  * Returns the index of the interface.
156  * @return the index
157  * @see #setIndex(int)
158  */
159 public int getIndex()
160 {
161     return index;
162 }
163 
164 /**
165  * Returns the description of the interface.
166  * @return the description
167  */
168 public String getDescription()
169 {
170     return descr;
171 }
172 
173 /**
174  * Returns the operation state of the interface.
175  * @return the operation state
176  */
177 public String getOperStatusString()
178 {
179     return operState;
180 }
181 
182 /**
183  * Returns the speed (bits per second) of the interface.
184  * @return the speed
185  */
186 public long getSpeed()
187 {
188     return speed;
189 }
190 
191 /**
192  * Returns the date of the moment when this bean was last updated.
193  * This might be null when the first time the update was not finished.
194  *
195  * @return the last update date
196  */
197 public Date getLastUpdateDate()
198 {
199     return lastUpdateDate;
200 }
201 
202 /**
203  * This method starts sending the SNMP request. All properties should be
204  * set before this method is called.
205  *
206  * The actual sending will take place in the run method.
207  * It makes a new snmp context and initialises all variables before
208  * starting.
209  */
210 public void action() 
211 {
212     if (isHostPortReachable())
213     {
214         lastUpdateDate = new Date();
215         isPduInFlight = false;
216         setRunning(true);
217     }
218 }
219 
220 
221 /**
222  * The run method according to the Runnable interface.
223  * This method will send the Pdu request, if the previous one is not
224  * still in flight.
225  * @see SNMPRunBean#isRunning()
226  */
227 public void run()
228 {
229     while (context != null && isRunning())
230     {
231         if (isPduInFlight == false)
232         {
233             isPduInFlight = true;
234             prev = pdu;
235             try
236             {
237                 pdu = new InterfaceGetNextPdu(context);
238                 pdu.addObserver(this);
239                 pdu.addOids(index-1);
240                 pdu.send();
241             }
242             catch (PduException exc)
243             {
244                 System.out.println("PduException " + exc.getMessage());
245             }
246             catch (IOException exc)
247             {
248                 System.out.println("IOException " + exc.getMessage());
249             }
250         }
251 
252         try
253         {
254             Thread.sleep(interval);
255         } 
256         catch (InterruptedException ix)
257         {
258             ;
259         }
260     }
261 }
262 
263 /**
264  * The update method according to the Observer interface, it will be
265  * called when the Pdu response is received.
266  * The speed is calculated with the previous answer, after that the
267  * property change event is fired.
268  *
269  * @see SNMPBean#addPropertyChangeListener
270  */
271 public void update(Observable obs, Object ov)
272 {
273     if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR)
274     {
275         if (prev != null)
276         {
277             speed = pdu.getSpeed(prev);
278         }
279         
280         descr = pdu.getIfDescr();
281         operState = pdu.getIfOperStatusStr();
282 
283         lastUpdateDate = new Date();
284         isPduInFlight = false;
285         firePropertyChange("Interface", null, null);
286     }
287 }
288 
289 
290 }