1 11 13 package com.sun.jmx.snmp ; 14 15 import java.net.InetAddress ; 18 import java.net.UnknownHostException ; 19 import java.io.Serializable ; 20 21 import com.sun.jmx.snmp.SnmpPduFactory ; 24 25 import com.sun.jmx.snmp.SnmpPduFactoryBER ; 28 29 71 72 public class SnmpPeer implements Serializable { 73 76 80 81 public static int defaultSnmpRequestPktSize = 2 * 1024 ; 82 83 87 public static int defaultSnmpResponsePktSize = 8 * 1024 ; 88 89 90 93 97 private int maxVarBindLimit = 25 ; 98 99 103 private int portNum = 161 ; 104 105 109 private int maxTries = 3 ; 110 111 115 private int timeout = 3000; 116 117 120 private SnmpPduFactory pduFactory = new SnmpPduFactoryBER() ; 121 122 125 private long _maxrtt ; 126 129 private long _minrtt ; 130 133 private long _avgrtt ; 134 135 139 private SnmpParams _snmpParameter = new SnmpParameters() ; 140 141 144 private InetAddress _devAddr = null ; 145 146 152 private int maxSnmpPacketSize = defaultSnmpRequestPktSize ; 153 154 157 InetAddress _devAddrList[] = null ; 158 159 162 int _addrIndex = 0 ; 163 164 165 private boolean customPduFactory = false; 166 167 170 175 public SnmpPeer(String host) throws UnknownHostException { 176 this(host, (int)161) ; 177 } 178 179 184 public SnmpPeer(InetAddress netaddr, int port) { 185 _devAddr = netaddr ; 186 portNum = port; 187 } 188 189 193 public SnmpPeer(InetAddress netaddr) { 194 _devAddr = netaddr ; 195 } 196 197 203 public SnmpPeer(String host, int port) throws UnknownHostException { 204 useIPAddress(host) ; 205 portNum = port; 206 } 207 208 209 212 220 final public synchronized void useIPAddress(String ipaddr) throws UnknownHostException { 221 _devAddr = InetAddress.getByName(ipaddr) ; 222 } 223 224 230 final public synchronized String ipAddressInUse() { 231 byte [] adr = _devAddr.getAddress() ; 232 return 233 (adr[0]&0xFF) + "." + (adr[1]&0xFF) + "." + 234 (adr[2]&0xFF) + "." + (adr[3]&0xFF); 235 } 236 237 242 final public synchronized void useAddressList(InetAddress adrList[]) { 243 _devAddrList = adrList ; 244 _addrIndex = 0 ; 245 useNextAddress() ; 246 } 247 248 253 final public synchronized void useNextAddress() { 254 if (_devAddrList == null) 255 return ; 256 257 258 if (_addrIndex > _devAddrList.length-1) 259 260 _addrIndex = 0 ; 261 _devAddr = _devAddrList[_addrIndex++] ; 262 } 263 264 270 public boolean allowSnmpSets() { 271 return _snmpParameter.allowSnmpSets() ; 272 } 273 274 281 public boolean equals(Object obj) { 282 if (this == obj) 283 return true ; 284 292 return false ; 293 } 294 295 299 final public InetAddress [] getDestAddrList() { 300 return _devAddrList; 301 } 302 303 307 final public InetAddress getDestAddr() { 308 return _devAddr ; 309 } 310 311 315 final public int getDestPort() { 316 return portNum ; 317 } 318 319 323 final public synchronized void setDestPort(int newPort) { 324 portNum = newPort ; 325 } 326 327 331 final public int getTimeout() { 332 return timeout; 333 } 334 335 339 final public synchronized void setTimeout(int newTimeout) { 340 if (newTimeout < 0) 341 throw new IllegalArgumentException (); 342 timeout= newTimeout; 343 } 344 345 349 final public int getMaxTries() { 350 return maxTries; 351 } 352 353 357 final public synchronized void setMaxTries(int newMaxTries) { 358 if (newMaxTries < 0) 359 throw new IllegalArgumentException (); 360 maxTries= newMaxTries; 361 } 362 363 367 final public String getDevName() { 368 return getDestAddr().getHostName() ; 369 } 370 371 375 public String toString() { 376 return "Peer/Port : " + getDevName() + "/" + getDestPort() ; 377 } 378 379 383 final public synchronized int getVarBindLimit() { 384 return maxVarBindLimit ; 385 } 386 387 391 final public synchronized void setVarBindLimit(int limit) { 392 maxVarBindLimit = limit ; 393 } 394 395 399 public void setParams(SnmpParams params) { 400 _snmpParameter = params; 401 } 402 406 public SnmpParams getParams() { 407 return _snmpParameter; 408 } 409 410 414 final public int getMaxSnmpPktSize() { 415 return maxSnmpPacketSize ; 416 } 417 418 422 final public synchronized void setMaxSnmpPktSize(int newsize) { 423 maxSnmpPacketSize = newsize ; 424 } 425 426 boolean isCustomPduFactory() { 427 return customPduFactory; 428 } 429 430 436 public void finalize() { 437 _devAddr = null ; 438 _devAddrList = null ; 439 _snmpParameter = null ; 440 } 441 442 446 public long getMinRtt() { 447 return _minrtt ; 448 } 449 450 454 public long getMaxRtt() { 455 return _maxrtt ; 456 } 457 458 462 public long getAvgRtt() { 463 return _avgrtt ; 464 } 465 466 467 470 private void updateRttStats(long tm) { 471 if (_minrtt > tm) 472 _minrtt = tm ; 473 else if (_maxrtt < tm) 474 _maxrtt = tm ; 475 else 476 _avgrtt = tm ; } 478 } 479 | Popular Tags |