KickJava   Java API By Example, From Geeks To Geeks.

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


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.activities.Background;
28 import net.fenyo.gnetwatch.data.*;
29 import net.fenyo.gnetwatch.targets.*;
30
31 import java.io.*;
32 import java.net.*;
33 import java.util.Arrays JavaDoc;
34 import java.util.Date JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 /**
40  * Instances of this action class can flood their target with UDP
41  * and create events of type EventFlood to log the throughput.
42  * @author Alexandre Fenyo
43  * @version $Id: ActionFlood.java,v 1.17 2007/03/12 05:04:15 fenyo Exp $
44  */

45
46 public class ActionFlood extends Action {
47   private static Log log = LogFactory.getLog(ActionPing.class);
48
49   private boolean interrupted = false;
50
51   /**
52    * Constructor.
53    * @param target target this action works on.
54    * @param background queue manager by which this action will add events.
55    */

56   // GUI thread
57
// supports any thread
58
public ActionFlood(final Target target, final Background background) {
59     super(target, background);
60     setItem("flood");
61   }
62
63   /**
64    * Constructor.
65    * @param none.
66    */

67   // GUI thread
68
// supports any thread
69
public ActionFlood() {
70     setItem("flood");
71   }
72
73   /**
74    * Returns the associated target.
75    * @param none.
76    * @return Target associated target.
77    */

78   // any thread
79
public String JavaDoc getQueueName() {
80     return "flood";
81   }
82
83   /**
84    * Returns the timeout associated with this action.
85    * @param none.
86    * @return long timeout.
87    */

88   // any thread
89
// bug : au bout de ce tps en ms ca s'arrete
90
public long getMaxDelay() {
91     return 30000000;
92   }
93
94   /**
95    * Asks this action to stop rapidely.
96    * @param cause cause.
97    * @return void.
98    * @throws IOException IO exception.
99    */

100   // main & Background threads
101
// supports any thread
102
public void interrupt(final InterruptCause cause) {
103     interrupted = true;
104   }
105
106   /**
107    * Floods the target.
108    * @param none.
109    * @return void.
110    * @throws IOException IO exception.
111    * @throws InterruptedException exception.
112    */

113   // Queue thread
114
// supports any thread
115
public void invoke() throws IOException, InterruptedException JavaDoc {
116     if (isDisposed() == true) return;
117
118     try {
119       super.invoke();
120
121       // il faudrait copier les queriers à la création de l'action
122
final IPQuerier querier;
123       if (TargetIPv4.class.isInstance(getTarget())) {
124         querier = ((TargetIPv4) getTarget()).getIPQuerier();
125       } else if (TargetIPv6.class.isInstance(getTarget())) {
126         querier = ((TargetIPv6) getTarget()).getIPQuerier();
127       } else return;
128
129       DatagramSocket socket = new DatagramSocket(querier.getPortSrc());
130       socket.setTrafficClass(querier.getTOS() << 2);
131       socket.setBroadcast(true);
132
133       final byte [] buf = new byte [querier.getPDUMaxSize()];
134       Arrays.fill(buf, (byte) 0);
135       final DatagramPacket packet = new DatagramPacket(buf, buf.length,
136           new InetSocketAddress(querier.getAddress(), querier.getPortDst()));
137
138       long last_time = System.currentTimeMillis();
139       int bytes_sent = 0;
140       while (true) {
141         socket.send(packet);
142         bytes_sent += buf.length;
143
144         if (System.currentTimeMillis() - last_time > 1000) {
145           getTarget().addEvent(new EventFlood(bytes_sent));
146           setDescription("" + new Double JavaDoc(((double) 8 * 1000 * bytes_sent) / (System.currentTimeMillis() - last_time)).intValue() + " bit/s");
147
148           // getGUI().setStatus(bytes_sent + " bytes flooded to " + querier.getAddress().toString().substring(1) + " for the last second");
149
getGUI().setStatus(getGUI().getConfig().getPattern("bytes_flooded", bytes_sent, querier.getAddress().toString().substring(1)));
150
151           last_time = System.currentTimeMillis();
152           bytes_sent = 0;
153         }
154
155         if (interrupted == true) {
156           socket.close();
157           return;
158         }
159       }
160     } catch (final InterruptedException JavaDoc ex) {}
161   }
162
163   /**
164    * Called when this element is being removed.
165    * @param none.
166    * @return void.
167    */

168   protected void disposed() {
169     // remove us from the flood queue
170
super.disposed();
171
172     // interrupt if currently running
173
interrupt(InterruptCause.removed);
174   }
175 }
176
Popular Tags