001: /*
002: * GNetWatch
003: * Copyright 2006, 2007 Alexandre Fenyo
004: * gnetwatch@fenyo.net
005: *
006: * This file is part of GNetWatch.
007: *
008: * GNetWatch is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNetWatch is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with GNetWatch; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package net.fenyo.gnetwatch.targets;
024:
025: import net.fenyo.gnetwatch.*;
026: import net.fenyo.gnetwatch.GUI.GUI;
027: import net.fenyo.gnetwatch.GUI.VisualElement;
028: import net.fenyo.gnetwatch.actions.*;
029: import net.fenyo.gnetwatch.data.EventReachable;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.snmp4j.event.ResponseEvent;
034:
035: import java.net.*;
036:
037: /**
038: * TargetIPv4 implements an IPv6 capable target.
039: * @author Alexandre Fenyo
040: * @version $Id: TargetIPv6.java,v 1.14 2007/03/09 22:44:20 fenyo Exp $
041: */
042:
043: public class TargetIPv6 extends Target {
044: private static Log log = LogFactory.getLog(TargetIPv4.class);
045:
046: private Inet6Address address; // not null
047: private SNMPQuerier snmp_querier;
048: private IPQuerier ip_querier;
049:
050: /**
051: * Constructor.
052: * @param name target name.
053: * @param address IPv6 address.
054: * @param SNMPManager snmp manager.
055: * @throws AlgorithmException exception.
056: */
057: // GUI thread
058: public TargetIPv6(final String name, final Inet6Address address,
059: final SNMPManager snmp_manager) throws AlgorithmException {
060: super (name);
061: if (address == null)
062: throw new AlgorithmException("name is null");
063: this .address = address;
064: snmp_querier = snmp_manager != null ? snmp_manager
065: .getQuerier(address) : null;
066: setItem(address.getHostAddress());
067: ip_querier = new IPQuerier(address);
068: // may last a long time (DNS resolver)
069: // setDescription(address.getCanonicalHostName());
070: }
071:
072: /**
073: * Checks that this host is SNMP capable.
074: * @param none.
075: * @return void.
076: */
077: public void checkSNMPAwareness() {
078: snmp_querier.getSysDescr(new SNMPQuerier.QuerierListener() {
079: public void onResponse(ResponseEvent event) {
080: getGUI().setStatus(
081: getGUI().getConfig().getPattern(
082: "discovered_snmp",
083: getAddress().toString().substring(1)));
084: setImageHostSNMP();
085: }
086:
087: public void onTimeout(ResponseEvent event) {
088: getGUI().setStatus(
089: getGUI().getConfig().getPattern("snmp_timeout",
090: getAddress().toString().substring(1)));
091: }
092: });
093: }
094:
095: /**
096: * Returns the SNMP querier.
097: * @param none.
098: * @return SNMPQuerier querier instance.
099: */
100: public SNMPQuerier getSNMPQuerier() {
101: return snmp_querier;
102: }
103:
104: /**
105: * Returns the IP querier.
106: * @param none.
107: * @return IPQuerier querier instance.
108: */
109: public IPQuerier getIPQuerier() {
110: return ip_querier;
111: }
112:
113: /**
114: * Returns the IP address.
115: * @param none.
116: * @return Inet4Address IP address.
117: */
118: // any thread
119: public Inet6Address getAddress() {
120: return address;
121: }
122:
123: /**
124: * Checks that the parameter can be attached to this target.
125: * @param visual_element parameter to check.
126: * @return true if the parameter can be attached to this target.
127: */
128: public boolean canManageThisChild(final VisualElement visual_element) {
129: if (ActionPing.class.isInstance(visual_element))
130: return true;
131: if (ActionFlood.class.isInstance(visual_element))
132: return true;
133: if (ActionSNMP.class.isInstance(visual_element))
134: return true;
135: if (EventReachable.class.isInstance(visual_element))
136: return true;
137: if (TargetInterface.class.isInstance(visual_element))
138: return true;
139: if (ActionHTTP.class.isInstance(visual_element))
140: return true;
141: if (ActionNmap.class.isInstance(visual_element))
142: return true;
143: return false;
144: }
145:
146: /**
147: * Initializes this target.
148: * @param gui current GUI instance.
149: * @return void.
150: */
151: // final because called by constructor (by means of setItem())
152: protected final void initialize(final GUI gui) {
153: super .initialize(gui);
154: if (gui != null)
155: setImageHost6();
156: }
157:
158: /**
159: * Attaches this target to a specific parent.
160: * @param gui current GUI instance.
161: * @param parent parent.
162: * @return true if this target has been succesfully attached.
163: */
164: public boolean addTarget(final GUI gui, final VisualElement parent) {
165: initialize(gui);
166:
167: if (!canAddTarget(parent))
168: return false;
169: if (parent != null && !parent.canManageThisChild(this ))
170: return false;
171:
172: final boolean is_new = !getGUI()
173: .containsCanonicalInstance(this );
174: final TargetIPv6 target_ipv6 = (TargetIPv6) getGUI()
175: .getCanonicalInstance(this );
176:
177: if (!getGUI().getVisualTransientAll().contains(target_ipv6))
178: target_ipv6.setParent(getGUI(), getGUI()
179: .getVisualTransientAll());
180:
181: if (parent != null)
182: target_ipv6.setParent(getGUI(), parent);
183:
184: return is_new;
185: }
186:
187: /**
188: * Attaches this target to a specific parent defined by its address.
189: * @param gui current GUI instance.
190: * @param addr_str parent address.
191: * @return true if this target has been succesfully attached.
192: */
193: public static void addTargetIPv6(final GUI gui,
194: final String addr_str) {
195: try {
196: synchronized (gui.sync_tree) {
197: final TargetIPv6 foo = new TargetIPv6(addr_str,
198: (Inet6Address) InetAddress.getByName(addr_str),
199: gui.getSNMPManager());
200: if (gui.containsCanonicalInstance(foo))
201: return;
202: gui.asyncExecIfNeeded(new Runnable() {
203: public void run() {
204: synchronized (gui.sync_tree) {
205: if (gui.containsCanonicalInstance(foo))
206: return;
207: if (foo
208: .addTarget(gui,
209: (VisualElement) null) == true)
210: foo.checkSNMPAwareness();
211: }
212: }
213: });
214: }
215: } catch (final AlgorithmException ex) {
216: log.error("Exception", ex);
217: } catch (final UnknownHostException ex) {
218: log.error("Exception", ex);
219: }
220: }
221:
222: /**
223: * Compares two targets.
224: * @param o target to compare to.
225: * @return true if the targets are equal.
226: */
227: // any thread
228: public boolean equals(final Object o) {
229: if (this == o)
230: return true;
231: if ((o == null) || (o.getClass() != getClass()))
232: return false;
233: final TargetIPv6 target = (TargetIPv6) o;
234: return getAddress().equals(target.getAddress());
235: }
236:
237: /**
238: * Returns the hashcode for this target.
239: * @param none.
240: * @return int hashcode.
241: */
242: // any thread
243: public int hashCode() {
244: return getAddress().hashCode();
245: }
246: }
|