1 // NAME 2 // $RCSfile: NTServiceNamesBean.java,v $ 3 // DESCRIPTION 4 // [given below in javadoc format] 5 // DELTA 6 // $Revision: 1.13 $ 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 the names of the network services installed on 63 * a NT server. The NT mib is described in the 64 * 65 * <a href="http://premium.microsoft.com/msdn/library/winresource/dnwinnt/f1d/d25/s86a2.htm">LAN Manager MIB II for Windows NT Objects</a> . 66 * 67 * You will have to register to the MSDN before accessing this page. 68 * </p> 69 * 70 * <p> 71 * The properties in the parent classes should be set, before calling 72 * the action() method. Via a PropertyChangeEvent the application/applet 73 * will be notified. 74 * </p> 75 * 76 * @see SNMPBean#setHost 77 * @see SNMPBean#setPort 78 * @see SNMPBean#setCommunityName 79 * @see SNMPRunBean#setUpdateInterval 80 * @see SNMPBean#addPropertyChangeListener 81 * @see SNMPBean#action 82 * @see GetNextPdu 83 * 84 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a> 85 * @version $Revision: 1.13 $ $Date: 2006/01/25 18:08:56 $ 86 * 87 */ 88 public class NTServiceNamesBean extends SNMPRunBean implements Observer { 89 private static final String version_id = "@(#)$Id: NTServiceNamesBean.java,v 1.13 2006/01/25 18:08:56 birgit Exp $ Copyright Westhawk Ltd"; 90 91 public final static String svSvcName = "1.3.6.1.4.1.77.1.2.3.1.1"; 92 93 private int svSvcName_len; 94 private GetNextPdu pdu; 95 private Hashtable serviceHash; 96 97 private boolean isGetNextInFlight; 98 private Date lastUpdateDate = null; 99 100 /** 101 * The default constructor. 102 */ 103 public NTServiceNamesBean() { 104 serviceHash = new Hashtable(); 105 svSvcName_len = svSvcName.length(); 106 } 107 108 /** 109 * The constructor that will set the host and the port no. 110 * 111 * @param h the hostname 112 * @param p the port no 113 * @see SNMPBean#setHost 114 * @see SNMPBean#setPort 115 */ 116 public NTServiceNamesBean(String h, int p) { 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 NTServiceNamesBean(String h, int p, String b) { 134 this(); 135 setHost(h); 136 setPort(p); 137 setBindAddress(b); 138 } 139 140 /** 141 * Returns the date of the moment when this bean was last updated. 142 * This might be null when the first time the update was not finished. 143 * 144 * @return the last update date 145 */ 146 public Date getLastUpdateDate() { 147 return lastUpdateDate; 148 } 149 150 /** 151 * Returns the indices of the NT network services. 152 * The OID of this network service is a concatenation of the 153 * name (svSvcName) OID and the network service specific index. 154 * The index should be used to get the other properties of this service. 155 * 156 * @see #getIndex(String) 157 * @see #svSvcName 158 */ 159 public Enumeration getIndices() { 160 return serviceHash.elements(); 161 } 162 163 /** 164 * Returns the index of one of the services. 165 * The OID of this network service is a concatenation of the 166 * name (svSvcName) OID and the network service specific index. 167 * The index should be used to get the other properties of this service. 168 * 169 * @param name The name of the service 170 * @return the service index, might be null if no service with such name 171 * exists 172 * @see #getIndices 173 * @see #getNames 174 */ 175 public String getIndex(String name) { 176 String ret = null; 177 if (name != null) { 178 ret = (String) serviceHash.get(name); 179 } 180 return ret; 181 } 182 183 /** 184 * Returns the names of the NT network services (the list 185 * of svSvcName). 186 */ 187 public Enumeration getNames() { 188 return serviceHash.keys(); 189 } 190 191 /** 192 * Returns the number of NT network services. 193 */ 194 public synchronized int getCount() { 195 return serviceHash.size(); 196 } 197 198 /** 199 * This method starts the action of the bean. It will initialises 200 * all variables before starting. 201 */ 202 public void action() { 203 if (isHostPortReachable()) { 204 serviceHash.clear(); 205 lastUpdateDate = new Date(); 206 isGetNextInFlight = false; 207 setRunning(true); 208 } 209 } 210 211 /** 212 * Implements the running of the bean. 213 * 214 * It will send the Pdu, if the previous one is not still in flight. 215 * 216 * @see SNMPRunBean#isRunning() 217 */ 218 public void run() { 219 while (context != null && isRunning()) { 220 if (isGetNextInFlight == false) { 221 // start the GetNext loop again 222 isGetNextInFlight = true; 223 pdu = new GetNextPdu(context); 224 pdu.addObserver(this); 225 pdu.addOid(svSvcName); 226 try { 227 pdu.send(); 228 } catch (PduException exc) { 229 System.out.println("PduException " + exc.getMessage()); 230 } catch (IOException exc) { 231 System.out.println("IOException " + exc.getMessage()); 232 } 233 } 234 235 try { 236 Thread.sleep(interval); 237 } catch (InterruptedException ix) { 238 ; 239 } 240 } 241 } 242 243 /** 244 * This method is called when the Pdu response is received. When all 245 * answers are received it will fire the property change event. 246 * 247 * The answers are stored in a hashtable, this is done because the speed 248 * can only be calculated with the previous answer. 249 * 250 * @see SNMPBean#addPropertyChangeListener 251 */ 252 public void update(Observable obs, Object ov) { 253 varbind var; 254 String hashKey; 255 String oid, index, name; 256 257 pdu = (GetNextPdu) obs; 258 if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { 259 var = (varbind) ov; 260 oid = var.getOid().toString(); 261 if (oid.startsWith(svSvcName)) { 262 // index is the part of the oid AFTER the svSvcName 263 index = oid.substring(svSvcName_len + 1); 264 265 name = ((AsnOctets) var.getValue()).getValue(); 266 267 // update the hashtable with the new answer 268 serviceHash.put(name, index); 269 270 // perform the GetNext on the just received answer 271 pdu = new GetNextPdu(context); 272 pdu.addObserver(this); 273 pdu.addOid(oid); 274 try { 275 pdu.send(); 276 } catch (PduException exc) { 277 System.out.println("PduException " + exc.getMessage()); 278 } catch (IOException exc) { 279 System.out.println("IOException " + exc.getMessage()); 280 } 281 } else { 282 // the GetNext loop has ended 283 lastUpdateDate = new Date(); 284 isGetNextInFlight = false; 285 firePropertyChange("serviceNames", null, null); 286 } 287 } else { 288 // the GetNext loop has ended 289 lastUpdateDate = new Date(); 290 isGetNextInFlight = false; 291 firePropertyChange("serviceNames", null, null); 292 } 293 } 294 295 }