View Javadoc
1   // NAME
2   //      $RCSfile: SNMPRunBean.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 1.9 $
7   // CREATED
8   //      $Date: 2006/02/09 14:20:09 $
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 forms the base of the Runnable SNMP beans.
63   * It extends the SNMPBean class.
64   * </p>
65   *
66   * <p>
67   * This bean is used when SNMP requests have to be sent continuously,
68   * instead of just once.
69   * For that purpose the update interval <em>setUpdateInterval()</em> can 
70   * be set. The default value is <em>2000</em>, i.e. 2 msec.
71   * </p>
72   *
73   * <p>
74   * When implementing an extention of this class, the method
75   * <em>action()</em> should 
76   * be implemented. It should somehow start the run by calling
77   * <em>setRunning(true)</em>.
78   * Stopping the thread might be called by the application or applet.
79   * </p>
80   *
81   * @see OneInterfaceBean
82   * @see InterfaceIndexesBean
83   *
84   * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
85   * @version $Revision: 1.9 $ $Date: 2006/02/09 14:20:09 $
86   */
87  public abstract class SNMPRunBean extends SNMPBean implements Runnable {
88      private static final String version_id = "@(#)$Id: SNMPRunBean.java,v 1.9 2006/02/09 14:20:09 birgit Exp $ Copyright Westhawk Ltd";
89  
90      protected int interval = 2000;
91      protected Thread me = null;
92      protected boolean running = false;
93  
94      /**
95       * Method according to the Runnable interface. This method should
96       * provide the continuous sending of a Pdu, with a sleeping interval set
97       * by setUpdateInterval().
98       *
99       * @see #setUpdateInterval(int)
100      * @see #setRunning
101      */
102     public abstract void run();
103 
104     /**
105      * The default constructor
106      */
107     public SNMPRunBean() {
108     }
109 
110     /**
111      * Returns the update interval. This is the interval that the
112      * bean will sleep between 2 requests.
113      *
114      * @return the update interval in msec
115      * @see #setUpdateInterval(int)
116      * @see #setUpdateInterval(String)
117      */
118     public int getUpdateInterval() {
119         return interval;
120     }
121 
122     /**
123      * Sets the update interval. This is the interval that the
124      * bean will sleep between 2 requests.
125      * The default will be <em>2000</em> (= 2 sec).
126      *
127      * @param i the interval in msec
128      * @see #getUpdateInterval
129      * @see #setUpdateInterval(String)
130      */
131     public void setUpdateInterval(int i) {
132         if (interval != i) {
133             interval = i;
134         }
135     }
136 
137     /**
138      * Sets the update interval as String.
139      *
140      * @param i the interval in msec as String
141      * @see #getUpdateInterval
142      * @see #setUpdateInterval(int)
143      */
144     public void setUpdateInterval(String i) {
145         int iNo;
146         try {
147             iNo = Integer.valueOf(i.trim()).intValue();
148             setUpdateInterval(iNo);
149         } catch (NumberFormatException exp) {
150         }
151     }
152 
153     /**
154      * Returns if the bean is running.
155      * 
156      * @return the running mode
157      * @see #setRunning
158      */
159     public boolean isRunning() {
160         return running;
161     }
162 
163     /**
164      * Starts or stops the thread.
165      * Starting this thread should be a result of calling the action()
166      * method, so should be implemented in the action method of any child
167      * class.
168      *
169      * Stopping this thread may be called by the application of applet.
170      * This method does NOT call Thread.stop() anymore. Every run() method
171      * should check isRunning(), so that when setRunning(false) is called,
172      * the run() method will stop running!!
173      *
174      * @see #isRunning
175      * @see SNMPBean#action()
176      */
177     public synchronized void setRunning(boolean b) {
178         if (running != b) {
179             running = b;
180 
181             if (running) {
182                 if (me == null) {
183                     me = new Thread(this);
184                     me.setPriority(Thread.MIN_PRIORITY);
185                 }
186                 me.start();
187             }
188         }
189     }
190 
191 }