KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > AbstractTarget


1 /*_############################################################################
2   _##
3   _## SNMP4J - AbstractTarget.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j;
22
23 import org.snmp4j.smi.Address;
24 import org.snmp4j.mp.SnmpConstants;
25
26 /**
27  * A <code>AbstratTarget</code> class is an abstract representation of a remote
28  * SNMP entity. It represents a target with an Address object, as well protocol
29  * parameters such as retransmission and timeout policy. Implementors of the
30  * <code>Target</code> interface can subclass <code>AbstratTarget</code> to
31  * take advantage of the implementation of common <code>Target</code>
32  * properties.
33  *
34  * @author Frank Fock
35  * @version 1.6
36  * @since 1.2
37  */

38 public abstract class AbstractTarget implements Target {
39
40   private Address address;
41   private int version = SnmpConstants.version3;
42   private int retries = 0;
43   private long timeout = 1000;
44   private int maxSizeRequestPDU = 65535;
45
46   /**
47    * Default constructor
48    */

49   protected AbstractTarget() {
50   }
51
52   /**
53    * Creates a SNMPv3 target with no retries and a timeout of one second.
54    * @param address
55    * an <code>Address</code> instance.
56    */

57   protected AbstractTarget(Address address) {
58     this.address = address;
59   }
60
61   /**
62    * Gets the address of this target.
63    * @return
64    * an Address instance.
65    */

66   public Address getAddress() {
67     return address;
68   }
69
70   /**
71    * Sets the address of the target.
72    * @param address
73    * an Address instance.
74    */

75   public void setAddress(Address address) {
76     this.address = address;
77   }
78
79   /**
80    * Sets the SNMP version (thus the SNMP messagen processing model) of the
81    * target.
82    * @param version
83    * the message processing model ID.
84    * @see org.snmp4j.mp.SnmpConstants#version1
85    * @see org.snmp4j.mp.SnmpConstants#version2c
86    * @see org.snmp4j.mp.SnmpConstants#version3
87    */

88   public void setVersion(int version) {
89     this.version = version;
90   }
91
92   /**
93    * Gets the SNMP version (NMP messagen processing model) of the target.
94    * @return
95    * the message processing model ID.
96    * @see org.snmp4j.mp.SnmpConstants#version1
97    * @see org.snmp4j.mp.SnmpConstants#version2c
98    * @see org.snmp4j.mp.SnmpConstants#version3
99    */

100   public int getVersion() {
101     return version;
102   }
103
104   /**
105    * Sets the number of retries to be performed before a request is timed out.
106    * @param retries
107    * the number of retries. <em>Note: If the number of retries is set to
108    * 0, then the request will be sent out exactly once.</em>
109    */

110   public void setRetries(int retries) {
111     if (retries < 0) {
112       throw new IllegalArgumentException JavaDoc("Number of retries < 0");
113     }
114     this.retries = retries;
115   }
116
117   /**
118    * Gets the number of retries.
119    * @return
120    * an integer >= 0.
121    */

122   public int getRetries() {
123     return retries;
124   }
125
126   /**
127    * Sets the timeout for a target.
128    * @param timeout
129    * timeout in milliseconds before a confirmed request is resent or
130    * timed out.
131    */

132   public void setTimeout(long timeout) {
133     this.timeout = timeout;
134   }
135
136   /**
137    * Gets the timeout for a target.
138    * @return
139    * the timeout in milliseconds.
140    */

141   public long getTimeout() {
142     return timeout;
143   }
144
145   /**
146    * Gets the maxmim size of request PDUs that this target is able to respond
147    * to. The default is 65535.
148    * @return
149    * the maximum PDU size of request PDUs for this target. Which is always
150    * greater than 484.
151    */

152   public int getMaxSizeRequestPDU() {
153     return maxSizeRequestPDU;
154   }
155
156   /**
157    * Sets the maximum size of request PDUs that this target is able to receive.
158    * @param maxSizeRequestPDU
159    * the maximum PDU (SNMP message) size this session will be able to
160    * process.
161    */

162   public void setMaxSizeRequestPDU(int maxSizeRequestPDU) {
163     if (maxSizeRequestPDU < SnmpConstants.MIN_PDU_LENGTH) {
164       throw new IllegalArgumentException JavaDoc("The minimum PDU length is: "+
165                                          SnmpConstants.MIN_PDU_LENGTH);
166     }
167     this.maxSizeRequestPDU = maxSizeRequestPDU;
168   }
169
170   protected String JavaDoc toStringAbstractTarget() {
171     return "address="+getAddress()+", version="+version+
172         ", timeout="+timeout+", retries="+retries;
173   }
174
175   public String JavaDoc toString() {
176     return getClass().getName()+"["+toStringAbstractTarget()+"]";
177   }
178
179   public Object JavaDoc clone() {
180     try {
181       return super.clone();
182     }
183     catch (CloneNotSupportedException JavaDoc ex) {
184       return null;
185     }
186   }
187 }
188
189
Popular Tags