KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > actions > ActionPing


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.actions;
25
26 import net.fenyo.gnetwatch.*;
27 import net.fenyo.gnetwatch.GUI.GUI;
28 import net.fenyo.gnetwatch.actions.Action.InterruptCause;
29 import net.fenyo.gnetwatch.activities.Background;
30 import net.fenyo.gnetwatch.data.*;
31 import net.fenyo.gnetwatch.targets.*;
32
33 import java.io.*;
34 import java.util.regex.Matcher JavaDoc;
35 import java.util.regex.Pattern JavaDoc;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 /**
41  * Instances of this action class can send/receive ICMP echo messages to their target
42  * and create events of type EventReachable.
43  * @author Alexandre Fenyo
44  * @version $Id: ActionPing.java,v 1.32 2007/03/12 05:04:14 fenyo Exp $
45  */

46
47 public class ActionPing extends Action {
48   private static Log log = LogFactory.getLog(ActionPing.class);
49
50   // main, Background & Queue threads
51
// supports any thread
52
private ExternalCommand cmd_ping = null;
53
54   /**
55    * Constructor.
56    * @param target target this action works on.
57    * @param background queue manager by which this action will add events.
58    */

59   // GUI thread
60
// supports any thread
61
public ActionPing(final Target target, final Background background) {
62     super(target, background);
63     setItem("ping");
64   }
65
66   /**
67    * Constructor.
68    * @param none.
69    */

70   // GUI thread
71
// supports any thread
72
public ActionPing() {
73     setItem("ping");
74   }
75
76   /**
77    * Called to inform about the current GUI.
78    * @param gui current GUI instance.
79    * @return void.
80    */

81   protected void initialize(final GUI gui) {
82     super.initialize(gui);
83     if (gui != null) setImageWatch();
84   }
85
86   /**
87    * Returns the associated target.
88    * @param none.
89    * @return Target associated target.
90    */

91   // any thread
92
public String JavaDoc getQueueName() {
93     return "icmp";
94   }
95
96   /**
97    * Returns the timeout associated with this action.
98    * @param none.
99    * @return long timeout.
100    */

101   // any thread
102
public long getMaxDelay() {
103     return 4000;
104   }
105
106   /**
107    * Asks this action to stop rapidely.
108    * @param cause cause.
109    * @return void.
110    * @throws IOException IO exception.
111    */

112   // main & Background threads
113
// supports any thread
114
public void interrupt(final InterruptCause cause) throws IOException {
115     if (cmd_ping != null) cmd_ping.end();
116   }
117
118   /**
119    * Computes the RTT to the target.
120    * @param none.
121    * @return void.
122    * @throws IOException IO exception.
123    * @throws InterruptedException exception.
124    */

125   // Queue thread
126
// supports any thread
127
public void invoke() throws IOException, InterruptedException JavaDoc {
128     if (isDisposed() == true) return;
129
130     try {
131       super.invoke();
132
133       if (TargetIPv4.class.isInstance(getTarget())) {
134         final TargetIPv4 target = (TargetIPv4) getTarget();
135         String JavaDoc address = target.getAddress().toString();
136         // remplacer tous les substrin(1) par cette forme avec indexof
137
address = address.substring(1 + address.indexOf('/'));
138
139         final String JavaDoc [] cmdLine = new String JavaDoc [] {
140             "ping", getGUI().getConfig().getProperty("ping.countparameter"), "1", address
141 // "sleep", "10000"
142
};
143         String JavaDoc cmd_line = "";
144         for (final String JavaDoc part : cmdLine) cmd_line += part + " ";
145         // getGUI().setStatus("forking external command: " + cmd_line);
146
getGUI().setStatus(getGUI().getConfig().getPattern("forking", cmd_line));
147         cmd_ping = new ExternalCommand(cmdLine, true);
148         cmd_ping.fork();
149
150         final String JavaDoc cmd_output = cmd_ping.readStdout();
151         final Matcher JavaDoc match =
152           Pattern.compile(address + getGUI().getConfig().getProperty("ping.regex")).matcher(cmd_output);
153         if (match.find() == true) {
154           final int delay = new Integer JavaDoc(match.group(2)).intValue();
155           getTarget().addEvent(new EventReachable(true, new Integer JavaDoc(match.group(2)).intValue()));
156           getGUI().setStatus(getGUI().getConfig().getPattern("received_icmp", address));
157           setDescription("rtt: " + delay + " ms");
158         } else {
159           getTarget().addEvent(new EventReachable(false));
160           getGUI().setStatus(getGUI().getConfig().getPattern("received_icmp_timeout", address));
161           setDescription(getGUI().getConfig().getString("unreachable"));
162         }
163         cmd_ping.end();
164       }
165
166       if (TargetIPv6.class.isInstance(getTarget())) {
167         final TargetIPv6 target = (TargetIPv6) getTarget();
168         String JavaDoc address = target.getAddress().toString();
169         // remplacer tous les substrin(1) par cette forme avec indexof
170
address = address.substring(1 + address.indexOf('/'));
171
172         final String JavaDoc [] cmdLine = new String JavaDoc [] {
173             "ping6", getGUI().getConfig().getProperty("ping.countparameter"), "1", address
174 // "sleep", "10000"
175
};
176         cmd_ping = new ExternalCommand(cmdLine, true);
177         cmd_ping.fork();
178
179         final String JavaDoc cmd_output = cmd_ping.readStdout();
180         // pattern could by a better matcher...
181
final Matcher JavaDoc match =
182           Pattern.compile("(.|\r|\n)*:.*?([0-9]+)[^0-9]*ms(.|\r|\n)*").matcher(cmd_output);
183         if (match.find() == true) {
184           final int delay = new Integer JavaDoc(match.group(2)).intValue();
185           getTarget().addEvent(new EventReachable(true, new Integer JavaDoc(match.group(2)).intValue()));
186           setDescription("rtt: " + delay + " ms");
187         } else {
188           getTarget().addEvent(new EventReachable(false));
189           setDescription(getGUI().getConfig().getString("unreachable"));
190         }
191         cmd_ping.end();
192       }
193
194     } catch (final InterruptedException JavaDoc ex) {
195       getTarget().addEvent(new EventReachable(false));
196       setDescription(getGUI().getConfig().getString("unreachable"));
197       throw ex;
198     }
199   }
200
201   /**
202    * Called when this element is being removed.
203    * @param none.
204    * @return void.
205    */

206   protected void disposed() {
207     // remove us from the action queue
208
super.disposed();
209
210     // interrupt if currently running
211
try {
212       interrupt(InterruptCause.removed);
213     } catch (final IOException ex) {
214       log.error("Exception", ex);
215     }
216   }
217 }
218
Popular Tags